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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! Drilldown example — master+detail pattern with selection preservation.
//!
//! Demonstrates the in-state-enum approach to navigation (Screen enum)
//! as the lightweight alternative to Router for cases where you don't
//! need a history stack. The user lands on the Roster screen (a Table
//! of operations), presses Enter on a selected row to drill into the
//! PerOp detail screen, and presses Esc to return to the Roster with
//! the original selection preserved.
//!
//! Per-view KeyHints + state-aware event handling: the bottom row of
//! each screen renders a `KeyHints` bar listing only the keys active
//! on that screen, and `handle_event_with_state` gates Up/Down to the
//! Roster (no roster-selection ticks while drilled in) and Esc to the
//! PerOp (no drill-out attempts from the Roster). `q` quits from any
//! screen.
//!
//! Surface exercised:
//! - TableState<Operation> for the Roster
//! - PaneLayout::view_with for the PerOp split (header + body)
//! - styled_line + StyledInline for emphasized metrics
//! - PaneConfig::with_title_style for the PerOp header pane
//! - KeyHints for per-view affordance hints
//! - App::handle_event_with_state for screen-gated key bindings
//!
//! Compare with examples/router.rs (history-stack navigation via
//! RouterState). See src/component/router/mod.rs module docs for
//! "When to use Router vs an in-state enum".
//!
//! Run with: cargo run --example drilldown --all-features
use envision::prelude::*;
/// One operation row in the Roster.
#[derive(Clone, Debug, PartialEq)]
struct Operation {
id: String,
duration_ms: f64,
status: String,
}
impl TableRow for Operation {
fn cells(&self) -> Vec<Cell> {
vec![
Cell::from(self.id.clone()),
Cell::from(format!("{:.1} ms", self.duration_ms)),
Cell::from(self.status.clone()),
]
}
}
/// Application screen state.
#[derive(Clone, Debug)]
enum Screen {
Roster,
PerOp { selected: usize },
}
struct DrillApp;
#[derive(Clone)]
struct State {
screen: Screen,
roster: TableState<Operation>,
operations: Vec<Operation>,
roster_hints: KeyHintsState,
perop_hints: KeyHintsState,
}
#[derive(Clone, Debug)]
enum Msg {
DrillIn,
DrillOut,
SelectNext,
SelectPrev,
Quit,
}
/// Carve out the bottom row for KeyHints; return (main_area, hints_area).
fn split_hints_row(area: Rect) -> (Rect, Rect) {
let chunks = Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).split(area);
(chunks[0], chunks[1])
}
impl App for DrillApp {
type State = State;
type Message = Msg;
type Args = ();
fn init(_args: ()) -> (State, Command<Msg>) {
let operations = vec![
Operation {
id: "op-001".into(),
duration_ms: 12.4,
status: "ok".into(),
},
Operation {
id: "op-002".into(),
duration_ms: 837.2,
status: "slow".into(),
},
Operation {
id: "op-003".into(),
duration_ms: 4.1,
status: "ok".into(),
},
];
let columns = vec![
Column::new("ID", Constraint::Length(12)),
Column::new("Duration", Constraint::Length(14)),
Column::new("Status", Constraint::Min(10)),
];
let mut roster = TableState::new(operations.clone(), columns);
roster.set_selected_index(Some(0));
let roster_hints = KeyHintsState::new()
.hint("↑/↓", "select")
.hint("Enter", "open")
.hint("q", "quit");
let perop_hints = KeyHintsState::new().hint("Esc", "back").hint("q", "quit");
(
State {
screen: Screen::Roster,
roster,
operations,
roster_hints,
perop_hints,
},
Command::none(),
)
}
fn update(state: &mut State, msg: Msg) -> Command<Msg> {
match msg {
Msg::DrillIn => {
if let Some(idx) = state.roster.selected_index() {
state.screen = Screen::PerOp { selected: idx };
}
}
Msg::DrillOut => {
if let Screen::PerOp { selected } = state.screen {
state.roster.set_selected_index(Some(selected));
state.screen = Screen::Roster;
}
}
Msg::SelectNext => {
let next = state
.roster
.selected_index()
.map(|i| i + 1)
.unwrap_or(0)
.min(state.operations.len().saturating_sub(1));
state.roster.set_selected_index(Some(next));
}
Msg::SelectPrev => {
let prev = state.roster.selected_index().unwrap_or(0).saturating_sub(1);
state.roster.set_selected_index(Some(prev));
}
Msg::Quit => return Command::quit(),
}
Command::none()
}
fn view(state: &State, frame: &mut Frame) {
use envision::component::pane_layout::{
PaneConfig, PaneDirection, PaneLayout, PaneLayoutState,
};
let area = frame.area();
let theme = Theme::default();
let (main_area, hints_area) = split_hints_row(area);
match &state.screen {
Screen::Roster => {
let mut ctx = RenderContext::new(frame, main_area, &theme).focused(true);
<Table<Operation> as Component>::view(&state.roster, &mut ctx);
let mut hints_ctx = RenderContext::new(frame, hints_area, &theme).focused(true);
<KeyHints as Component>::view(&state.roster_hints, &mut hints_ctx);
}
Screen::PerOp { selected } => {
use envision::component::styled_text::StyledInline;
use envision::render::styled_line;
let op = &state.operations[*selected];
let layout = PaneLayoutState::new(
PaneDirection::Vertical,
vec![
PaneConfig::new("header")
.with_title(format!(" {} ", op.id))
.with_title_style(
Style::default()
.add_modifier(Modifier::BOLD)
.fg(Color::Cyan),
)
.with_proportion(0.25),
PaneConfig::new("body")
.with_title(" Details ")
.with_proportion(0.75),
],
);
PaneLayout::view_with(
&layout,
&mut RenderContext::new(frame, main_area, &theme).focused(true),
|pane_id, child_ctx| match pane_id {
"header" => {
let inlines = vec![
StyledInline::Plain("duration: ".to_string()),
StyledInline::bold(format!("{:.1} ms", op.duration_ms)),
];
styled_line(child_ctx.frame, child_ctx.area, &inlines, child_ctx.theme);
}
"body" => {
let inlines = vec![
StyledInline::Plain("status: ".to_string()),
StyledInline::colored(op.status.clone(), Color::Green),
];
styled_line(child_ctx.frame, child_ctx.area, &inlines, child_ctx.theme);
}
_ => {}
},
);
let mut hints_ctx = RenderContext::new(frame, hints_area, &theme).focused(true);
<KeyHints as Component>::view(&state.perop_hints, &mut hints_ctx);
}
}
}
/// State-aware key handling: each screen owns only the keys it
/// advertises. Up/Down moves the Roster selection only when the
/// Roster is the active screen; Esc returns to the Roster only
/// from PerOp. `q` quits from any screen.
fn handle_event_with_state(state: &State, event: &Event) -> Option<Msg> {
let key = event.as_key()?;
if matches!(key.code, Key::Char('q')) {
return Some(Msg::Quit);
}
match state.screen {
Screen::Roster => match key.code {
Key::Down => Some(Msg::SelectNext),
Key::Up => Some(Msg::SelectPrev),
Key::Enter => Some(Msg::DrillIn),
_ => None,
},
Screen::PerOp { .. } => match key.code {
Key::Esc => Some(Msg::DrillOut),
_ => None,
},
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 16 rows: ~13 for the main content + 1 for hints + table chrome.
let mut vt = Runtime::<DrillApp, _>::virtual_builder(60, 16).build()?;
println!("=== Drilldown Example ===\n");
vt.tick()?;
println!("Roster (initial):");
println!("{}\n", vt.display());
vt.dispatch(Msg::SelectNext);
vt.tick()?;
println!("After selecting row 1:");
println!("{}\n", vt.display());
vt.dispatch(Msg::DrillIn);
vt.tick()?;
println!("After Enter (PerOp detail for op-002, PerOp hints active):");
println!("{}\n", vt.display());
vt.dispatch(Msg::DrillOut);
vt.tick()?;
println!("After Esc (back to Roster, selection preserved, Roster hints restored):");
println!("{}\n", vt.display());
Ok(())
}