rat-widget 3.2.1

ratatui widgets extended edition
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#![allow(dead_code)]

use crate::mini_salsa::endless_scroll::{EndlessScroll, EndlessScrollState};
use crate::mini_salsa::{MiniSalsaState, mock_init, run_ui, setup_logging};
use rat_event::{HandleEvent, Regular, ct_event, try_flow};
use rat_focus::{Focus, FocusBuilder};
use rat_menu::event::MenuOutcome;
use rat_menu::menuline::{MenuLine, MenuLineState};
use rat_scrolled::Scroll;
use rat_theme4::{StyleName, WidgetStyle};
use rat_widget::event::Outcome;
use rat_widget::list::selection::RowSelection;
use rat_widget::list::{List, ListState};
use rat_widget::statusline::StatusLineState;
use rat_widget::tabbed::{TabPlacement, TabType, Tabbed, TabbedState};
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::{Constraint, Layout, Rect};
use ratatui_core::style::{Style, Stylize};
use ratatui_core::symbols::border;
use ratatui_core::text::Line;
use ratatui_core::widgets::{StatefulWidget, Widget};
use ratatui_crossterm::crossterm::event::Event;
use ratatui_widgets::block::Block;
use ratatui_widgets::borders::BorderType;

mod mini_salsa;

fn main() -> Result<(), anyhow::Error> {
    setup_logging()?;

    let mut state = State {
        border_type: None,
        placement: TabPlacement::default(),
        style: TabType::default(),
        close: false,
        tabbed: TabbedState::default(),
        tabs_0: Default::default(),
        tabs_1: Default::default(),
        tabs_2: Default::default(),
        menu: MenuLineState::default(),
        status: StatusLineState::default(),
    };
    state.menu.focus.set(true);

    run_ui("tabbed1", mock_init, event, render, &mut state)
}

struct Data {}

struct State {
    border_type: Option<(BorderType, border::Set<'static>)>,
    placement: TabPlacement,
    style: TabType,
    close: bool,

    tabbed: TabbedState,

    tabs_0: ListState<RowSelection>,
    tabs_1: EndlessScrollState,
    tabs_2: EndlessScrollState,

    menu: MenuLineState,
    status: StatusLineState,
}

fn render(
    buf: &mut Buffer,
    area: Rect,
    ctx: &mut MiniSalsaState,
    state: &mut State,
) -> Result<(), anyhow::Error> {
    let l1 = Layout::vertical([
        Constraint::Length(1),
        Constraint::Fill(1),
        Constraint::Length(1),
        Constraint::Length(1),
    ])
    .split(area);

    let l2 = Layout::horizontal([
        Constraint::Length(25),
        Constraint::Fill(1),
        Constraint::Length(15),
    ])
    .split(l1[1]);

    let mut tab = Tabbed::new()
        .styles(ctx.theme.style(WidgetStyle::TABBED))
        .tab_type(state.style)
        .placement(state.placement);
    if state.close {
        tab = tab.closeable(true);
    }
    if let Some(border_type) = state.border_type {
        tab = tab.block(
            Block::bordered()
                .border_type(border_type.0)
                .border_set(border_type.1),
        );
    }
    tab = tab.tabs(["Issues", "Numbers", "More numbers"]);
    tab.render(l2[1], buf, &mut state.tabbed);

    match state.tabbed.selected().expect("tab") {
        0 => List::<RowSelection>::new(LIST)
            .scroll(Scroll::new())
            .styles(ctx.theme.style(WidgetStyle::LIST))
            .render(state.tabbed.widget_area, buf, &mut state.tabs_0),
        1 => EndlessScroll::new()
            .max(2024)
            .style(ctx.theme.p.bluegreen(0))
            .focus_style(ctx.theme.style_style(Style::FOCUS))
            .v_scroll(Scroll::new().styles(ctx.theme.style(WidgetStyle::SCROLL)))
            .render(state.tabbed.widget_area, buf, &mut state.tabs_1),
        2 => EndlessScroll::new()
            .max(2024)
            .style(ctx.theme.p.cyan(0))
            .focus_style(ctx.theme.style_style(Style::FOCUS))
            .v_scroll(Scroll::new().styles(ctx.theme.style(WidgetStyle::SCROLL)))
            .render(state.tabbed.widget_area, buf, &mut state.tabs_2),
        _ => {}
    }

    let mut area = Rect::new(l2[0].x, l2[0].y, l2[0].width, 1);

    Line::from("F1: close").yellow().render(area, buf);
    area.y += 1;
    Line::from("F2: type").yellow().render(area, buf);
    area.y += 1;
    Line::from("F3: alignment").yellow().render(area, buf);
    area.y += 1;
    Line::from("F5: border").yellow().render(area, buf);
    area.y += 1;
    Line::from("F12: key-nav").yellow().render(area, buf);

    MenuLine::new()
        .title("||||")
        .item_parsed("_Quit")
        .title_style(Style::default().black().on_yellow())
        .style(Style::default().black().on_dark_gray())
        .render(l1[3], buf, &mut state.menu);

    Ok(())
}

fn focus(state: &State) -> Focus {
    let mut fb = FocusBuilder::default();
    fb.widget(&state.tabbed);
    if let Some(sel) = state.tabbed.selected {
        match sel {
            0 => _ = fb.widget(&state.tabs_0),
            1 => _ = fb.widget(&state.tabs_1),
            2 => _ = fb.widget(&state.tabs_2),
            _ => {}
        }
    }
    fb.widget(&state.menu);
    let f = fb.build();
    f.enable_log();
    f
}

fn event(
    event: &Event,
    ctx: &mut MiniSalsaState,
    state: &mut State,
) -> Result<Outcome, anyhow::Error> {
    ctx.focus_outcome = focus(state).handle(event, Regular);

    try_flow!(match event {
        ct_event!(keycode press F(1)) => {
            state.close = !state.close;
            Outcome::Changed
        }
        ct_event!(keycode press F(2)) => {
            state.style = match state.style {
                TabType::Glued => TabType::Attached,
                TabType::Attached => TabType::Glued,
                _ => TabType::Glued,
            };
            Outcome::Changed
        }
        ct_event!(keycode press SHIFT-F(2)) => {
            state.style = match state.style {
                TabType::Glued => TabType::Attached,
                TabType::Attached => TabType::Glued,
                _ => TabType::Glued,
            };
            Outcome::Changed
        }
        ct_event!(keycode press F(3)) => {
            state.placement = match state.placement {
                TabPlacement::Top => TabPlacement::Right,
                TabPlacement::Right => TabPlacement::Bottom,
                TabPlacement::Bottom => TabPlacement::Left,
                TabPlacement::Left => TabPlacement::Top,
            };
            Outcome::Changed
        }
        ct_event!(keycode press SHIFT-F(3)) => {
            state.placement = match state.placement {
                TabPlacement::Top => TabPlacement::Left,
                TabPlacement::Right => TabPlacement::Top,
                TabPlacement::Bottom => TabPlacement::Right,
                TabPlacement::Left => TabPlacement::Bottom,
            };
            Outcome::Changed
        }
        ct_event!(keycode press F(5)) => {
            state.border_type = match state.border_type {
                None => Some((BorderType::Plain, border::PLAIN)),
                Some((BorderType::Plain, border::PLAIN)) => {
                    Some((BorderType::Plain, border::ONE_EIGHTH_TALL))
                }
                Some((BorderType::Plain, border::ONE_EIGHTH_TALL)) => {
                    Some((BorderType::Plain, border::ONE_EIGHTH_WIDE))
                }
                Some((BorderType::Plain, border::ONE_EIGHTH_WIDE)) => {
                    Some((BorderType::Plain, border::PROPORTIONAL_WIDE))
                }
                Some((BorderType::Plain, border::PROPORTIONAL_WIDE)) => {
                    Some((BorderType::Plain, border::PROPORTIONAL_TALL))
                }
                Some((BorderType::Plain, border::PROPORTIONAL_TALL)) => {
                    Some((BorderType::Double, border::DOUBLE))
                }
                Some((BorderType::Double, border::DOUBLE)) => {
                    Some((BorderType::Rounded, border::ROUNDED))
                }
                Some((BorderType::Rounded, border::ROUNDED)) => {
                    Some((BorderType::Thick, border::THICK))
                }
                Some((BorderType::Thick, border::THICK)) => {
                    Some((BorderType::QuadrantInside, border::QUADRANT_INSIDE))
                }
                Some((BorderType::QuadrantInside, border::QUADRANT_INSIDE)) => {
                    Some((BorderType::QuadrantOutside, border::QUADRANT_OUTSIDE))
                }
                Some((BorderType::QuadrantOutside, border::QUADRANT_OUTSIDE)) => None,
                _ => Some((BorderType::Plain, border::PLAIN)),
            };
            Outcome::Changed
        }
        ct_event!(keycode press SHIFT-F(5)) => {
            state.border_type = match state.border_type {
                None => Some((BorderType::QuadrantOutside, border::QUADRANT_OUTSIDE)),
                Some((BorderType::Plain, border::PLAIN)) => None,
                Some((BorderType::Plain, border::ONE_EIGHTH_TALL)) => {
                    Some((BorderType::Plain, border::PLAIN))
                }
                Some((BorderType::Plain, border::ONE_EIGHTH_WIDE)) => {
                    Some((BorderType::Plain, border::ONE_EIGHTH_TALL))
                }
                Some((BorderType::Plain, border::PROPORTIONAL_WIDE)) => {
                    Some((BorderType::Plain, border::ONE_EIGHTH_WIDE))
                }
                Some((BorderType::Plain, border::PROPORTIONAL_TALL)) => {
                    Some((BorderType::Plain, border::PROPORTIONAL_WIDE))
                }
                Some((BorderType::Double, border::DOUBLE)) => {
                    Some((BorderType::Plain, border::PROPORTIONAL_TALL))
                }
                Some((BorderType::Rounded, border::ROUNDED)) => {
                    Some((BorderType::Double, border::DOUBLE))
                }
                Some((BorderType::Thick, border::THICK)) => {
                    Some((BorderType::Rounded, border::ROUNDED))
                }
                Some((BorderType::QuadrantInside, border::QUADRANT_INSIDE)) => {
                    Some((BorderType::Thick, border::THICK))
                }
                Some((BorderType::QuadrantOutside, border::QUADRANT_OUTSIDE)) => {
                    Some((BorderType::QuadrantInside, border::QUADRANT_INSIDE))
                }
                _ => Some((BorderType::Plain, border::PLAIN)),
            };
            Outcome::Changed
        }
        ct_event!(keycode press F(12)) => {
            focus(state).focus(&state.tabbed);
            Outcome::Changed
        }
        _ => Outcome::Continue,
    });

    try_flow!(HandleEvent::handle(&mut state.tabbed, event, Regular));

    try_flow!({
        if let Some(sel) = state.tabbed.selected() {
            match sel {
                0 => state.tabs_0.handle(event, Regular),
                1 => state.tabs_1.handle(event, Regular),
                2 => state.tabs_2.handle(event, Regular),
                _ => Outcome::Continue,
            }
        } else {
            Outcome::Continue
        }
    });

    try_flow!(match state.menu.handle(event, Regular) {
        MenuOutcome::Activated(0) => {
            ctx.quit = true;
            Outcome::Changed
        }
        _ => Outcome::Continue,
    });

    Ok(Outcome::Continue)
}

static LIST: [&str; 28] = [
    "Advisory: Ratatui / Crossterm Version incompatibility (0.27 / 0.28 and beyond)
#1298 · joshka opened on Aug 6, 2024",
    "Examples may not be compatible with the latest release
#779 · joshka opened on Jan 10, 2024",
    "Ratatui's Vision
#1321 · orhun opened on Aug 13, 2024",
    "Convert the buffer type into a trait with the current buffer as one of it's implementations
Type: Enhancement
Status: Open.
#1450 In ratatui/ratatui;· giocri opened on Oct 25, 2024",
    "Canvas rendering issue when area is huge
Type: Bug
Status: Open.
#1449 In ratatui/ratatui;· JeromeSchmied opened on Oct 22, 2024",
    "Checkbox and Radio button widgets
Status: Design Needed
Type: Enhancement
Status: Open.
#1446 In ratatui/ratatui;· fujiapple852 opened on Oct 21, 2024",
    "Stylize the highlight_symbol in List Widget
    Effort: Good First Issue
Type: Enhancement
Status: Open.
#1443 In ratatui/ratatui;· aktaboot opened on Oct 21, 2024",
    "Implement Rect::resize similar to Rect::offset
Type: Enhancement
Status: Open.
#1440 In ratatui/ratatui;· kardwen opened on Oct 20, 2024",
    "Inline viewport should support an Terminal::insert_lines_before method.
Type: Enhancement
Status: Open.
#1426 In ratatui/ratatui;· nfachan opened on Oct 17, 2024",
    "Rendering Caching needs to be one level deeper
Priority: Low
Type: Bug
Status: Open.
#1405 In ratatui/ratatui;· joshka opened on Oct 6, 2024",
    "Support asserting TestBackend buffer with color
Effort: Good First Issue
Type: Enhancement
Status: Open.
#1402 In ratatui/ratatui;· orhun opened on Oct 5, 2024",
    "Introduce new way to compare visually output of layout tests
Type: Enhancement
Status: Open.
#1400 In ratatui/ratatui;· kdheepak opened on Oct 3, 2024",
    "Korean characters are not rendered correctly
Type: Bug
Status: Open.
#1396 In ratatui/ratatui;· sxyazi opened on Oct 2, 2024",
    "GraphType::Bar does not work when y-axis minimum bound is > 0
Type: Bug
Status: Open.
#1391 In ratatui/ratatui;· Yomguithereal opened on Sep 30, 2024",
    "Modularize Ratatui crate
Type: Enhancement
Type: RFC
Status: Open.
#1388 In ratatui/ratatui;· joshka opened on Sep 27, 2024",
    "Most examples don't work or look bad in macOS's Terminal.app
Type: Bug
Status: Open.
#1387 In ratatui/ratatui;· ccbrown opened on Sep 25, 2024",
    "Scrolling the list widget by page
Effort: Difficult
Status: Design Needed
Type: Enhancement
Status: Open.
#1370 In ratatui/ratatui;· francisdb opened on Sep 12, 2024",
    "insert_before dynamic height based on content
Type: Enhancement
Status: Open.
#1365 In ratatui/ratatui;· staehle opened on Sep 11, 2024",
    "Add support for Dash borders
Effort: Easy
Effort: Good First Issue
Type: Enhancement
Status: Open.
#1355 In ratatui/ratatui;· 0xfalafel opened on Sep 6, 2024",
    "dweatherstone
crossterm::style::Stylize is imported instead of ratatui::style::Stylize
Type: Bug
Status: Open.
#1347 In ratatui/ratatui;· orhun opened on Aug 28, 2024",
    "underline_color() on an input field makes the screen flicker from that line to the bottom
Status: Pending
Type: Bug
Status: Open.
#1346 In ratatui/ratatui;· mazzi opened on Aug 27, 2024",
    "Extremely high CPU usage of the terminal.draw method
Type: Bug
Status: Open.
#1338 In ratatui/ratatui;· gtema opened on Aug 23, 2024",
    "insert_before and emoji width empty space issue
Type: Bug
Status: Open.
#1332 In ratatui/ratatui;· Kongga666 opened on Aug 20, 2024",
    "Ratatui's Vision
Type: Enhancement
Status: Open.
#1321 In ratatui/ratatui;· orhun opened on Aug 13, 2024",
    "Feature request: Add feature flags for backends
Type: Enhancement
Status: Open.
#1319 In ratatui/ratatui;· nick42d opened on Aug 11, 2024",
    "Advisory: Ratatui / Crossterm Version incompatibility (0.27 / 0.28 and beyond)
Type: Documentation
Status: Open.
#1298 In ratatui/ratatui;· joshka opened on Aug 6, 2024",
    "Allow reversing Chart Legend direction
Type: Enhancement
Status: Open.
#1290 In ratatui/ratatui;· nullstalgia opened on Aug 5, 2024",
    "WidgetRef / StatefulWidgetRef tracking issue
Type: Documentation
Status: Open.
#1287 In ratatui/ratatui;· joshka opened on Aug 5, 2024",
];