Skip to content

Commit c5b8b2c

Browse files
committed
feat(utils): add keyboard interaction utilities
1 parent 7f8d2ad commit c5b8b2c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/utils/keypress.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::i18n::t;
2+
use std::io::{self, Read, Write};
3+
use std::process;
4+
5+
/// Waits for user to press any key
6+
///
7+
/// # Arguments
8+
///
9+
/// * `prompt` - Optional message to display. If None, a default message will be shown
10+
/// * `is_en` - Whether to display messages in English
11+
pub fn wait_for_key(prompt: Option<&str>) {
12+
if let Some(msg) = prompt {
13+
print!("{} ", msg);
14+
} else {
15+
print!("{}", t("wait_for_key"));
16+
}
17+
io::stdout().flush().unwrap();
18+
let mut buffer = [0u8; 1];
19+
io::stdin().read_exact(&mut buffer).unwrap();
20+
}
21+
22+
/// Waits for user input and then exits the program
23+
///
24+
/// # Arguments
25+
///
26+
/// * `msg` - Optional message to display before exiting. If None, a default message will be shown
27+
pub fn exit_on_keypress(msg: Option<&str>) {
28+
wait_for_key(msg);
29+
process::exit(0);
30+
}

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod keypress;

0 commit comments

Comments
 (0)