tui_tools/lib.rs
1//
2// Copyright 2023, [object Object]
3// Licensed under MIT
4//
5
6use std::io::{self, Write};
7/// Colorize text in a terminal, automatically detects if the terminal supports colors
8///
9/// example:
10/// ```
11/// use tui_tools::Colors;
12///
13/// println!("{}", "Hello World!".red());
14///
15/// ```
16pub mod colors;
17pub use colors::Colors;
18
19
20// How do I get keyboard input without the user pressing the Enter key? - https://stackoverflow.com/a/73765863
21extern {
22 fn _getch() -> core::ffi::c_char;
23}
24
25
26/// Gets a single utf-8 character from the keyboard
27///
28/// example:
29/// ```
30/// use tui_tools::getch;
31///
32/// let c = getch(); //
33///
34/// println!("{}", c);
35///
36/// ```
37pub fn getch() -> u8 {
38 unsafe {
39 _getch() as u8
40 }
41}
42
43/// Gets a single utf-8 character from the keyboard as a char
44pub fn getch_as_char() -> char {
45 getch() as char
46}
47
48/// Prints a message and creates a input on the same line
49pub fn same_line_input(msg: &str) -> String {
50 print!("{}", msg);
51
52 io::stdout().flush().unwrap();
53
54 let mut input = String::new();
55
56 io::stdin().read_line(&mut input).unwrap();
57
58 input.trim().to_string()
59}
60
61pub mod cls {
62 /// Clears the screen and moves the cursor to the top left
63 ///
64 /// example:
65 /// ```
66 /// use tui_tools::cls;
67 ///
68 /// cls();
69 ///
70 /// ```
71 pub fn cls() {
72 print!("{}[2J{}[1;1H", 27 as char, 27 as char);
73 }
74}
75
76pub use cls::cls;