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
//! Example demonstrating println with Element support
//!
//! This example shows how to use rnk::println() to print both text and
//! complex UI elements above the running application.
//!
//! Controls:
//! - Enter: Print a styled banner
//! - Space: Print a simple text message
//! - q: Quit
use rnk::prelude::*;
fn main() -> std::io::Result<()> {
// Run in inline mode (default) to see println output
render(app).run()
}
fn app() -> Element {
let counter = use_signal(|| 0u32);
let app = use_app();
let count = counter.clone();
let app_clone = app.clone();
use_input(move |input, key| {
match input {
"q" => app_clone.exit(),
" " => {
// Print simple text
let current = count.get();
count.update(|c| *c += 1);
rnk::println(format!("Message #{}: Hello from rnk!", current + 1));
}
_ if key.return_key => {
// Print a styled element
let banner = create_banner(count.get() + 1);
rnk::println(banner);
count.update(|c| *c += 1);
}
_ => {}
}
});
Box::new()
.flex_direction(FlexDirection::Column)
.padding(1)
.border_style(BorderStyle::Round)
.border_color(Color::Cyan)
.child(
Text::new("println() with Element Support")
.color(Color::Cyan)
.bold()
.into_element(),
)
.child(
Box::new()
.margin_top(1.0)
.child(Text::new(format!("Messages printed: {}", counter.get())).into_element())
.into_element(),
)
.child(
Box::new()
.margin_top(1.0)
.flex_direction(FlexDirection::Column)
.child(Text::new("Controls:").dim().into_element())
.child(
Text::new(" Space: Print simple text message")
.dim()
.into_element(),
)
.child(
Text::new(" Enter: Print styled banner element")
.dim()
.into_element(),
)
.child(Text::new(" q: Quit").dim().into_element())
.into_element(),
)
.child(
Box::new()
.margin_top(1.0)
.child(
Text::new("Note: Messages persist above this UI in terminal history")
.italic()
.color(Color::Yellow)
.into_element(),
)
.into_element(),
)
.into_element()
}
/// Create a styled banner element
fn create_banner(number: u32) -> Element {
Box::new()
.border_style(BorderStyle::Double)
.border_color(Color::Magenta)
.padding(1)
.child(
Box::new()
.flex_direction(FlexDirection::Column)
.child(
Text::new(format!("🎉 Banner #{}", number))
.color(Color::Magenta)
.bold()
.into_element(),
)
.child(
Box::new()
.margin_top(1.0)
.child(
Text::new("This is a complex UI element printed via rnk::println()")
.color(Color::White)
.into_element(),
)
.into_element(),
)
.child(
Box::new()
.margin_top(1.0)
.child(
Text::new("✓ Supports borders, colors, padding, and layout")
.color(Color::Green)
.into_element(),
)
.into_element(),
)
.into_element(),
)
.into_element()
}