mod input;
mod list;
pub use input::{password_input, text_input};
pub use list::{checklist, confirm_card, select_list, summary_select};
use crate::render::{cursor_glyph, dot};
use crate::theme::Theme;
pub enum Flow<T> {
Value(T),
Back,
Abort,
}
pub struct Choice {
pub id: String,
pub label: String,
pub hint: Option<String>,
}
impl Choice {
pub fn new(id: &str, label: &str, hint: Option<&str>) -> Choice {
Choice {
id: id.to_string(),
label: label.to_string(),
hint: hint.map(str::to_string),
}
}
}
#[derive(Clone)]
pub struct CheckItem {
pub id: String,
pub label: String,
pub benefit: String,
pub checked: bool,
pub locked: bool,
}
pub fn nav_up(pos: usize) -> usize {
pos.saturating_sub(1)
}
pub fn nav_down(pos: usize, len: usize) -> usize {
if len == 0 {
0
} else {
(pos + 1).min(len - 1)
}
}
pub fn toggle_check(items: &mut [CheckItem], idx: usize) -> bool {
match items.get_mut(idx) {
Some(item) if !item.locked => {
item.checked = !item.checked;
true
}
_ => false,
}
}
pub fn backspace(raw: &mut String) {
raw.pop();
}
pub fn hint(theme: &Theme, parts: &[&str]) -> String {
parts.join(&format!(" {} ", dot(theme)))
}
pub fn arrows_move(theme: &Theme) -> String {
if theme.ascii {
"Up/Down move".to_string()
} else {
"↑ ↓ move".to_string()
}
}
pub fn left_right(theme: &Theme) -> String {
if theme.ascii {
"Left/Right choose".to_string()
} else {
"← → choose".to_string()
}
}
pub fn choice_row(theme: &Theme, label: &str, selected: bool, inner: usize) -> String {
if selected {
theme.selection_bar(&format!(" {} {label}", theme.dot_filled()), inner)
} else {
format!(" {} {label}", theme.dim(theme.dot_empty()))
}
}
pub fn gutter(theme: &Theme, selected: bool) -> String {
if selected {
theme.accent(cursor_glyph(theme))
} else {
" ".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::theme::ColorTier;
#[test]
fn cursor_nav_clamps_at_both_ends() {
assert_eq!(nav_up(0), 0);
assert_eq!(nav_up(3), 2);
assert_eq!(nav_down(0, 3), 1);
assert_eq!(nav_down(2, 3), 2); assert_eq!(nav_down(0, 0), 0); }
fn items() -> Vec<CheckItem> {
vec![
CheckItem {
id: "radio".into(),
label: "Radio".into(),
benefit: "far".into(),
checked: true,
locked: false,
},
CheckItem {
id: "cam".into(),
label: "Camera".into(),
benefit: "see".into(),
checked: false,
locked: true,
},
]
}
#[test]
fn toggle_flips_unlocked_and_refuses_locked() {
let mut it = items();
assert!(toggle_check(&mut it, 0));
assert!(!it[0].checked);
assert!(!toggle_check(&mut it, 1));
assert!(!it[1].checked);
assert!(!toggle_check(&mut it, 9));
}
#[test]
fn backspace_trims_the_tail() {
let mut raw = "abc".to_string();
backspace(&mut raw);
assert_eq!(raw, "ab");
backspace(&mut raw);
backspace(&mut raw);
backspace(&mut raw); assert_eq!(raw, "");
}
#[test]
fn choice_row_selected_is_a_full_width_amber_bar() {
let theme = Theme {
ascii: false,
tier: ColorTier::Truecolor,
};
let bar = choice_row(&theme, "This flies (Drone)", true, 40);
assert!(
bar.contains("\x1b[48;2;235;193;87m"),
"selected row not an amber bar"
);
assert!(bar.contains('●'), "selected row missing the filled dot");
let plain_row = choice_row(&theme, "This flies (Drone)", false, 40);
assert!(
!plain_row.contains("\x1b[48;2;235;193;87m"),
"unselected row barred"
);
assert!(
plain_row.contains('○'),
"unselected row missing the empty dot"
);
}
#[test]
fn hint_joins_with_the_tier_separator() {
let ascii = Theme {
ascii: true,
tier: ColorTier::None,
};
assert_eq!(hint(&ascii, &["a", "b"]), "a - b");
assert_eq!(arrows_move(&ascii), "Up/Down move");
}
}