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
//! Terminal resize example - responding to terminal size changes
//!
//! Equivalent to ink's examples/terminal-resize
//!
//! Run with: cargo run --example terminal_resize
use rnk::prelude::*;
fn main() -> std::io::Result<()> {
render(app).run()
}
fn app() -> Element {
let app = use_app();
let term_size = use_signal(|| crossterm::terminal::size().unwrap_or((80, 24)));
let term_size_clone = term_size.clone();
use_input(move |ch, _key| {
if ch == "q" {
app.exit();
}
// Update terminal size on any key press (simple approach)
if let Ok(size) = crossterm::terminal::size() {
term_size_clone.set(size);
}
});
let (width, height) = term_size.get();
let width = width as u32;
let height = height as u32;
Box::new()
.flex_direction(FlexDirection::Column)
.padding(1)
// Title
.child(
Box::new()
.border_style(BorderStyle::Double)
.border_color(Color::Cyan)
.padding(1)
.child(
Text::new("Terminal Resize Demo")
.color(Color::Cyan)
.bold()
.into_element(),
)
.into_element(),
)
.child(Newline::new().into_element())
// Current dimensions
.child(
Box::new()
.border_style(BorderStyle::Round)
.border_color(Color::Yellow)
.padding(1)
.flex_direction(FlexDirection::Column)
.child(
Text::new("Current Terminal Size:")
.color(Color::Yellow)
.bold()
.into_element(),
)
.child(Newline::new().into_element())
.child(
Box::new()
.flex_direction(FlexDirection::Row)
.child(Text::new(" Width: ").into_element())
.child(
Text::new(format!("{} columns", width))
.color(Color::Green)
.bold()
.into_element(),
)
.into_element(),
)
.child(
Box::new()
.flex_direction(FlexDirection::Row)
.child(Text::new(" Height: ").into_element())
.child(
Text::new(format!("{} rows", height))
.color(Color::Green)
.bold()
.into_element(),
)
.into_element(),
)
.into_element(),
)
.child(Newline::new().into_element())
// Size indicator
.child(
Box::new()
.flex_direction(FlexDirection::Column)
.child(
Text::new(format!(
"Size category: {}",
if width < 40 {
"Small"
} else if width < 80 {
"Medium"
} else if width < 120 {
"Large"
} else {
"Extra Large"
}
))
.color(Color::Magenta)
.into_element(),
)
.into_element(),
)
.child(Newline::new().into_element())
// Visual size bar
.child(
Box::new()
.flex_direction(FlexDirection::Column)
.child(Text::new("Width visualization:").dim().into_element())
.child(
Text::new("█".repeat((width as usize).min(60)))
.color(Color::Blue)
.into_element(),
)
.into_element(),
)
.child(Newline::new().into_element())
// Instructions
.child(
Text::new("Try resizing your terminal window!")
.color(Color::Ansi256(245))
.italic()
.into_element(),
)
.child(Text::new("Press 'q' to quit").dim().into_element())
.into_element()
}