#linux-terminal #single-line #terminal #linux #text-input #text-editing

bin+lib term-basics-linux

A simple crate with basic functionality for terminal applications

24 releases (2 stable)

1.0.1 May 24, 2024
1.0.0 Nov 28, 2023
0.5.8 Nov 18, 2022
0.5.7 Sep 12, 2021
0.2.0 Jul 12, 2019

#196 in Command-line interface

Download history 2/week @ 2024-09-22 3/week @ 2024-09-29

1,579 downloads per month

MIT license

24KB
465 lines

term-basics-linux

Rust crate library that provides basic terminal input functionality for Linux. The main feature is the single line input field. It supports editing the text with backspace and delete, as well as navigating using home, end and the arrow keys.

print!("your input: ");
let input = tbl::input_field_simple(true);

It supports a list of predefined inputs for the user to scroll between (up/down arrow):

let mut his = tbl::InputList::new(2);
his.add("one");
his.add("two");
println!("{}", tbl::input_field_scrollable(&mut his, true));

You can also hide/substitute the typed characters. This is handy for password input:

// like doas/sudo
let pass = tbl::input_field(&mut tbl::InputList::new(0), tbl::PrintChar::None, true);
// like websites
let pass = tbl::input_field(&mut tbl::InputList::new(0), tbl::PrintChar::Substitute('*'), true);

Another feature is getch() which returns the characters from stdin without the user having to press enter. It can be useful and is not available by default in rust. All other input fields are using this function.

design

This crate is very minimalistic by design, It does just a few things. It is for Linux only. This keeps it simple and lightweight.

keycodes

Sometimes different terminal emulators use different codes for certain keys like delete or end. For example, backspace is 127 on both suckless simple terminal (ST) and the build in terminal emulator in vscode. But delete is 27-91-80 on ST and 27-91-51-126 on vscode. End is 27-91-52-126 on ST and 27-91-70 on vscode. The test_chars function can help look up what key code it is on your platform.

License

Copyright (C) 2024 Cody Bloemhard

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

Dependencies

~0.4–1MB
~21K SLoC