data_beans/interactive/mod.rs
1//! Full-screen terminal views (feature `tui`), and line prompts for when
2//! there is no terminal.
3//!
4//! - [`ui`]: the shared pieces for building a view, legume-tui's: the
5//! look ([`ui::LOOK`], terminal foreground plus one accent), [`ui::panel`], [`ui::header`],
6//! [`ui::help_line`], [`ui::input_line`], the [`ui::Screen`] trait driven by
7//! [`ui::run_screen`], and histograms: [`ui::Scale`], [`ui::Binning`],
8//! [`ui::Binned`], [`ui::HistPlot`], and [`ui::MirrorPlot`] for two series
9//! around one zero line.
10//! - [`qc_tui`]: `qc`'s view: cell QC rules moved on their histograms.
11//! - [`list_tui`]: a list to browse, flat (names) or a tree (a file's
12//! hierarchy): filter, mark, write, copy.
13//! - [`plan_tui`]: what a command is about to write, with its settings on
14//! keys: Enter writes, `q` does not.
15//! - `sim_tui` (feature `sim`): a reference-mode simulation against its
16//! reference, from its fidelity report.
17//! - [`stat_tui`]: table-and-histogram explorer over plain name and value
18//! columns, optionally marking entries and handing them back.
19//!
20//! Open a view only when [`tui_available`]; otherwise fall back to text.
21//!
22//! The views run in a legume-tui session ([`TOOL`]): a command opens it
23//! first thing and hands it to [`ui::Tui`] or [`ui::run_screen_in`]; where
24//! the session is hosted, the command keeps running when the connection
25//! drops. [`ui::run_screen`] runs a view in a plain session of its own
26//! (`legume_tui::Session::plain`).
27
28pub mod list_tui;
29pub mod plan_tui;
30pub mod qc_tui;
31#[cfg(feature = "sim")]
32pub mod sim_tui;
33pub mod stat_tui;
34pub mod ui;
35
36/// Whether a full-screen view can run: stdin and stdout are a terminal, and
37/// one that can draw it (`TERM` is not `dumb`).
38pub use legume_tui::view::terminal_available as tui_available;
39
40/// Set, to anything but empty or `0`, to run every command straight through
41/// as `--batch-process` does: for a script or pipeline started from a
42/// terminal, which its commands would otherwise take for the user's.
43pub const BATCH_ENV: &str = "DATA_BEANS_BATCH";
44
45/// Why a command runs without its view.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47enum NoView {
48 /// `--batch-process`, or [`BATCH_ENV`].
49 Batch,
50 /// No terminal to draw it on.
51 NoTerminal,
52}
53
54/// The one rule every command's view follows: batch when asked (`flag`, or
55/// `env`, the value of [`BATCH_ENV`]), else a view where there is a
56/// terminal (`terminal`).
57fn decide(flag: bool, env: Option<&str>, terminal: bool) -> Result<(), NoView> {
58 let asked = flag || env.is_some_and(|v| !v.is_empty() && v != "0");
59 match (asked, terminal) {
60 (true, _) => Err(NoView::Batch),
61 (false, false) => Err(NoView::NoTerminal),
62 (false, true) => Ok(()),
63 }
64}
65
66fn decide_here(flag: bool) -> Result<(), NoView> {
67 decide(
68 flag,
69 std::env::var(BATCH_ENV).ok().as_deref(),
70 tui_available(),
71 )
72}
73
74/// The line said when `command`'s view is skipped for want of a terminal.
75fn no_terminal_line(command: &str) -> String {
76 format!(
77 "{command}: no terminal to draw a view on (stdin or stdout is not one, or \
78 TERM=dumb): running straight through; --batch-process or {BATCH_ENV}=1 \
79 does this without this line"
80 )
81}
82
83/// Whether `command` opens its view, for a command that runs without one
84/// too (`qc`, `stat`): not when batch is asked (`batch`, its
85/// `--batch-process`, or [`BATCH_ENV`]), and only on a terminal. A view
86/// skipped for want of a terminal (output redirected, say) is said in one
87/// line on stderr, so the switch is never silent; asked for, it is quiet.
88pub fn opens_view(command: &str, batch: bool) -> bool {
89 match decide_here(batch) {
90 Ok(()) => true,
91 Err(NoView::Batch) => false,
92 Err(NoView::NoTerminal) => {
93 eprintln!("{}", no_terminal_line(command));
94 false
95 }
96 }
97}
98
99/// Whether `command` picks its rows or columns in the explorer, for a
100/// command that takes them on the command line too (`columns`, `subset-rows`,
101/// ...): only when the command line `selected` none. Then batch asked
102/// (`batch`, or [`BATCH_ENV`]) or no terminal is an error naming
103/// `options`, what selects them instead.
104pub fn picks(command: &str, batch: bool, selected: bool, options: &str) -> anyhow::Result<bool> {
105 if selected {
106 return Ok(false);
107 }
108 match decide_here(batch) {
109 Ok(()) => Ok(true),
110 Err(why) => anyhow::bail!("{}", pick_refused(command, options, why)),
111 }
112}
113
114fn pick_refused(command: &str, options: &str, why: NoView) -> String {
115 match why {
116 NoView::Batch => {
117 format!("{command}: give {options}; run in batch, it has no view to pick them in")
118 }
119 NoView::NoTerminal => {
120 format!("{command}: give {options}, or run it on a terminal to pick them in a view")
121 }
122 }
123}
124
125/// The tool data-beans' sessions belong to: they are named
126/// `data-beans-qc-1`, `data-beans-stat-2`, and `data-beans attach`
127/// finds them.
128pub const TOOL: legume_tui::Tool = legume_tui::Tool::new("data-beans");
129
130/// The detach and switch keys for a title bar ([`ui::title_bar`]), each
131/// with what it does in a word (`detach`, `switch`); none when the views
132/// run plain.
133pub use legume_tui::view::session_keys;
134
135/// The paragraph a command's long help ends with about its session:
136/// `lead` first, for a command whose view is optional (when it runs as a
137/// session), then how it runs, the detach and switch keys, how to return,
138/// and `more` at the end. Its views are always opened on something
139/// named, so a new start never offers the live sessions; `data-beans
140/// attach` does.
141pub fn session_help(lead: Option<&str>, more: &str) -> String {
142 let help = legume_tui::cli::SessionHelp::new()
143 .what_goes_on("the command goes on")
144 .resume_hint(Some(
145 "Starting it again starts a new one (naming the detached ones); \
146 `data-beans attach` with no name",
147 ))
148 .more(more);
149 let help = match lead {
150 Some(lead) => help.lead(lead),
151 None => help,
152 };
153 legume_tui::cli::session_help(&TOOL, help)
154}
155
156#[cfg(test)]
157#[path = "tests/session.rs"]
158mod tests;