1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::;
use ;
use crossterm as ct;
/// Reads a line of input from the user after displaying a prompt.
///
/// # Parameters
/// - `prompt`: A `&str` containing the message to display before user input.
///
/// # Returns
/// - `Ok(String)`: The user’s input as a `String`, including the newline character.
/// - `Err(FtuiError)`: Returns an error.
///
/// # Notes
/// - The returned `String` includes the newline (`\n`). Use `.trim()` if necessary.
///
/// # Example
/// ```rust
/// // Get the user input and print it out if error occure print the error
/// match line("Input Something") {
/// Ok(e) => println!("User Input {}", e),
/// Err(e) => eprintln!("Error: {}", e),
/// };
/// ```
/// Reads a key press event as `KeyCode` from the terminal without blocking.
///
/// # Returns
/// - `Ok(Some(KeyCode))`: If a key event is detected.
/// - `Ok(None)`: If no key event is detected.
/// - `Err(FtuiError)`: Returns an error.
///
/// # Notes
/// - This function does not block waiting for input.
///
/// # Example
/// ```rust
/// fn main() -> FtuiResult<()> {
/// // Get the user key input as `KeyCode` and print it out
/// match key()? {
/// Some(key) => println!("Key pressed: {:?}", key),
/// None => println!("No key press detected"),
/// };
///
/// Ok(())
/// }
/// ```
/// Converts a `KeyCode` into its corresponding character, if applicable.
///
/// # Parameters
/// - `code`: The `KeyCode` to convert.
///
/// # Returns
/// - `Some(char)`: If the `KeyCode` represents a printable character.
/// - `None`: If the `KeyCode` is not a character (e.g., arrow keys, function keys).
///
/// # Example
/// ```rust
/// fn main() -> FtuiResult<()> {
/// // Capture user keyboard input as a KeyCode.
/// // If reading fails, terminate with an error.
/// let key_code = key()?;
///
/// // If a key was pressed, attempt to convert it to a character.
/// match key_code {
/// Some(code) => match keycode_to_char(code) {
/// // Print the character if it's a printable key.
/// Some(c) => println!("Key pressed: {}", c),
/// None => println!("Unprintable KeyCode"),
/// },
/// // No key was pressed, exit the function.
/// None => return,
/// }
///
/// Ok(())
/// }
/// ```
/// Reads a key press event as a `char` from the terminal without blocking.
///
/// # Returns
/// - `Ok(Some(char))`: if a printable key was pressed.
/// - `Ok(None)`: if a non-printable key was pressed or no input was detected.
/// - `Err(FtuiError)`: Returns an error.
///
/// # Example
/// ```rust
/// fn main() -> FtuiResult<()> {
/// // Capture user keyboard input as a character and print it out if
/// // possible.
/// match key_char()? {
/// Some(c) => println!("Key pressed: {}", c),
/// None => println!("No key pressed or no printable key pressed"),
/// }
/// }
/// ```