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
use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyModifiers},
execute,
style::{Color, Stylize},
terminal::{self, ClearType},
};
use std::io::{stdout, Write};
fn main() {
let mut stdout = stdout();
let mut username = String::new();
let mut threads: i32 = 8;
let mut enable_cache = true;
let mut focus: usize = 0; // 0=username,1=threads,2=toggle,3=submit
terminal::enable_raw_mode().expect("raw");
execute!(stdout, terminal::Clear(ClearType::All)).ok();
loop {
execute!(stdout, cursor::MoveTo(0, 0)).ok();
println!("Raw Form UI (Tab to move, Enter to submit field, Esc to quit)\n");
// Username
if focus == 0 {
println!("{} {}", "▶ Username:".bold(), username.clone());
} else {
println!(" Username: {username}");
}
// Threads
if focus == 1 {
println!("{} {}", "▶ Threads:".bold(), threads);
} else {
println!(" Threads: {threads}");
}
// Toggle
let toggle_label = if enable_cache {
"ON".with(Color::Green)
} else {
"OFF".with(Color::Red)
};
if focus == 2 {
println!("{} {}", "▶ Enable cache:".bold(), toggle_label);
} else {
println!(" Enable cache: {toggle_label}");
}
// Submit button
let submit = "[ Submit ]";
if focus == 3 {
println!("\n{}", submit.with(Color::Black).on(Color::Cyan).bold());
} else {
println!("\n{submit}");
}
println!("\nKeys: Tab/Shift+Tab move • Enter confirm • Backspace edit • +/- for threads • Esc quit");
stdout.flush().ok();
if let Ok(Event::Key(k)) = event::read() {
match k.code {
KeyCode::Esc => {
break;
}
KeyCode::Tab => {
focus = (focus + 1) % 4;
}
KeyCode::BackTab => {
focus = if focus == 0 { 3 } else { focus - 1 };
}
KeyCode::Enter => {
if focus == 3 {
break;
}
// noop per-field (we edit live)
}
KeyCode::Char(c) => {
match focus {
0 => {
if k.modifiers.contains(KeyModifiers::CONTROL) { /* ignore */
} else if c != '+' && c != '-' {
username.push(c);
}
}
1 => {
if c == '+' {
threads = (threads + 1).min(512);
} else if c == '-' {
threads = (threads - 1).max(1);
}
}
2 => {
if c == ' ' {
enable_cache = !enable_cache;
}
}
_ => {}
}
}
KeyCode::Backspace => {
if focus == 0 {
username.pop();
}
}
KeyCode::Left => {
if focus == 1 {
threads = (threads - 1).max(1);
}
}
KeyCode::Right => {
if focus == 1 {
threads = (threads + 1).min(512);
}
}
// '+' and '-' handled in KeyCode::Char above
_ => {}
}
}
}
terminal::disable_raw_mode().ok();
println!(
"\nResult:\n Username = {username}\n Threads = {threads}\n Enable cache = {enable_cache}"
);
}