-
Notifications
You must be signed in to change notification settings - Fork 103
/
ex_7.rs
85 lines (70 loc) · 1.98 KB
/
ex_7.rs
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
/*
Copyright © 2013 Free Software Foundation, Inc
See licensing in LICENSE file
File: examples/ex_7.rs
Author: Jesse 'Jeaye' Wilkerson
Description:
Basic input and attribute example, using the Unicode-aware get_wch functions.
*/
extern crate ncurses;
use std::char;
use ncurses::*;
#[cfg(feature = "wide")]
fn main()
{
let locale_conf = LcCategory::all;
setlocale(locale_conf, "en_US.UTF-8");
/* Setup ncurses. */
initscr();
raw();
/* Require input within 2 seconds. */
halfdelay(20);
/* Enable mouse events. */
mousemask(ALL_MOUSE_EVENTS as mmask_t, None);
/* Allow for extended keyboard (like F1). */
keypad(stdscr(), true);
noecho();
/* Prompt for a character. */
addstr("Enter a character within 2 seconds: ").unwrap();
/* Wait for input. */
let ch = wget_wch(stdscr());
match ch {
Some(WchResult::KeyCode(KEY_MOUSE)) => {
/* Enable attributes and output message. */
attron(A_BOLD | A_BLINK);
addstr("\nMouse").unwrap();
attroff(A_BOLD | A_BLINK);
addstr(" pressed").unwrap();
}
Some(WchResult::KeyCode(_)) => {
/* Enable attributes and output message. */
attron(A_BOLD | A_BLINK);
addstr("\nKeycode").unwrap();
attroff(A_BOLD | A_BLINK);
addstr(" pressed").unwrap();
}
Some(WchResult::Char(c)) => {
/* Enable attributes and output message. */
addstr("\nKey pressed: ");
attron(A_BOLD | A_BLINK);
addstr(format!("{}\n", char::from_u32(c as u32).expect("Invalid char")).as_ref()).unwrap();
attroff(A_BOLD | A_BLINK);
}
None => {
addstr("\nYou didn't enter a character in time!").unwrap();
}
}
/* Refresh, showing the previous message. */
refresh();
/* Wait for one more character before exiting. Disable the input timeout. */
nocbreak();
getch();
endwin();
}
#[cfg(not(feature = "wide"))]
fn main() {
initscr();
addstr("This example requires wide character support.").unwrap();
getch();
endwin();
}