Skip to main content

data_beans/interactive/
ui.rs

1//! Shared pieces of the full-screen views, legume-tui's: the screen loop
2//! and the chrome (`legume_tui::view`), histogram scales, binning and plots
3//! (`legume_tui::hist`), all drawn in data-beans' [`LOOK`].
4
5use ratatui::crossterm::event::KeyEvent;
6use ratatui::style::{Color, Modifier, Style};
7
8pub use legume_tui::hist::{
9    compact, median, BarValue, Binned, Binning, HistPlot, MirrorPlot, MirrorSide, Scale,
10};
11pub use legume_tui::view::{
12    header, help_line, input_line, modes_of, notify, panel, run_screen_in, run_screen_in_with,
13    title_bar, Screen, Tui, TICK,
14};
15pub use legume_tui::Look;
16
17/// The one accent, for what needs the eye: what a cutoff drops, the
18/// selection, and key hints.
19const ACCENT: Color = Color::Rgb(217, 119, 87);
20
21/// The views' styles: the terminal's own foreground (so light and dark
22/// backgrounds both work) for nearly everything, dim for what is
23/// secondary, and [`ACCENT`].
24pub const LOOK: Look = Look::new(
25    Style::new(),
26    Style::new().add_modifier(Modifier::BOLD),
27    Style::new().add_modifier(Modifier::DIM),
28    Style::new().add_modifier(Modifier::REVERSED),
29    Style::new().add_modifier(Modifier::DIM),
30    Style::new().fg(ACCENT),
31);
32
33/// Ask for `text` on the clipboard (`pbcopy`, else the terminal's OSC 52).
34/// Whether a terminal took it cannot be known.
35pub fn copy(text: &str) {
36    // Nothing to do about a clipboard that is not there.
37    let _ = legume_tui::keys::copy(text, true);
38}
39
40/// Run `screen` full screen until it is done, in a plain session of
41/// data-beans' own. The terminal is restored on return and on panic.
42pub fn run_screen<S: Screen>(screen: &mut S) -> anyhow::Result<()> {
43    Ok(legume_tui::view::run_screen(&super::TOOL, screen)?)
44}
45
46/// [`run_screen`], with each key (but Ctrl-C) handed to `on_key` rather
47/// than [`Screen::handle_key`].
48pub fn run_screen_with<S: Screen>(
49    screen: &mut S,
50    on_key: impl FnMut(&mut S, KeyEvent),
51) -> anyhow::Result<()> {
52    Ok(legume_tui::view::run_screen_with(
53        &super::TOOL,
54        screen,
55        on_key,
56    )?)
57}