use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::style::Color;
use crate::style::{Row, Style, pad, wrap};
use crate::theme::theme;
pub const LABEL_W: usize = 16;
pub fn frame(w: usize, h: usize, title: Row, tabs: Option<(&[&str], usize)>, body: Vec<Row>, hint: &str) -> Vec<Row> {
let mut rows: Vec<Row> = vec![accent_rule(w), title, vec![]];
if let Some((names, tab)) = tabs {
rows.push(tabs_row(names, tab));
rows.push(vec![]);
}
let footer = [vec![], vec![(Style::fg(theme().muted), format!(" {hint}"))]];
let body_h = h.saturating_sub(rows.len() + footer.len()).max(1);
let mut body = body;
body.truncate(body_h);
body.resize(body_h, vec![]);
rows.extend(body);
rows.extend(footer);
rows.truncate(h);
rows
}
pub fn body_height(h: usize, tabs: bool) -> usize {
h.saturating_sub(if tabs { 7 } else { 5 }).max(1)
}
pub fn accent_rule(w: usize) -> Row {
vec![(Style::fg(theme().accent), "─".repeat(w.saturating_sub(1)))]
}
pub fn title(text: &str) -> Row {
vec![(Style::fg(theme().accent).bold(), format!(" {text}"))]
}
pub fn tabs_row(names: &[&str], tab: usize) -> Row {
let mut row: Row = vec![(Style::default(), " ".into())];
for (i, name) in names.iter().enumerate() {
row.push((Style::default(), " ".into()));
row.push((tab_style(i == tab), format!(" {name} ")));
}
row
}
pub fn title_with_tabs(title: &str, names: &[&str], tab: usize) -> Row {
let mut row: Row = vec![(Style::fg(theme().accent).bold(), format!(" {title}"))];
for (i, name) in names.iter().enumerate() {
row.push((Style::default(), " ".into()));
row.push((tab_style(i == tab), format!(" {name} ")));
}
row
}
fn tab_style(on: bool) -> Style {
if on {
let t = theme();
Style::fg(t.on_bright).bg(t.bright)
} else {
Style::default()
}
}
pub fn heading(text: &str, color: Option<Color>) -> Row {
let style = color.map_or(Style::default(), Style::fg).bold();
vec![(style, format!(" {text}"))]
}
pub fn para(text: &str, ind: usize, w: usize, style: Style) -> Vec<Row> {
wrap(text, w.saturating_sub(ind + 2).max(10))
.into_iter()
.map(|p| vec![(style, format!("{}{p}", " ".repeat(ind)))])
.collect()
}
pub fn rule_line(w: usize) -> Row {
vec![(
Style::fg(theme().muted),
format!(" {}", "─".repeat(w.saturating_sub(8).clamp(10, 60))),
)]
}
pub fn field(label: &str, text: &str, w: usize, style: Style) -> Vec<Row> {
let muted = Style::fg(theme().muted);
wrap(text, w.saturating_sub(4 + LABEL_W + 3).max(10))
.into_iter()
.enumerate()
.map(|(i, p)| {
vec![
(muted, format!(" {}", pad(if i == 0 { label } else { "" }, LABEL_W))),
(style, p),
]
})
.collect()
}
pub fn steps(label: &str, items: &[String], w: usize) -> Vec<Row> {
let t = theme();
let mut out = Vec::new();
for (n, text) in items.iter().enumerate() {
for (i, p) in wrap(text, w.saturating_sub(4 + LABEL_W + 6).max(10))
.into_iter()
.enumerate()
{
out.push(vec![
(
Style::fg(t.muted),
format!(" {}", pad(if n == 0 && i == 0 { label } else { "" }, LABEL_W)),
),
(
Style::fg(t.good),
if i == 0 { format!("{}. ", n + 1) } else { " ".into() },
),
(Style::default(), p),
]);
}
}
out
}
pub fn bullet(mark: &str, color: Color, text: &str, w: usize) -> Vec<Row> {
wrap(text, w.saturating_sub(10).max(10))
.into_iter()
.enumerate()
.map(|(i, p)| {
vec![
(
Style::fg(color).bold(),
if i == 0 {
format!(" {mark} ")
} else {
" ".into()
},
),
(Style::default(), p),
]
})
.collect()
}
pub struct Item {
pub text: String,
pub tail: Option<(String, Color)>,
}
impl Item {
pub fn new(text: impl Into<String>) -> Self {
Item {
text: text.into(),
tail: None,
}
}
pub fn tail(mut self, text: impl Into<String>, color: Color) -> Self {
self.tail = Some((text.into(), color));
self
}
}
pub fn list_rows(items: Vec<Item>, sel: usize, h: usize) -> Vec<Row> {
let accent = theme().accent;
let n = items.len();
let first = sel.saturating_sub(h.saturating_sub(1)).min(n.saturating_sub(h));
items
.into_iter()
.enumerate()
.skip(first)
.take(h)
.map(|(i, it)| {
let on = i == sel;
let mut row: Row = vec![(
if on { Style::fg(accent) } else { Style::default() },
format!("{}{}", if on { " ❯ " } else { " " }, it.text),
)];
if let Some((t, c)) = it.tail {
row.push((Style::fg(if on { accent } else { c }), t));
}
row
})
.collect()
}
pub fn scrolled(scroll: &mut usize, lines: Vec<Row>, h: usize) -> Vec<Row> {
let max = lines.len().saturating_sub(h);
*scroll = (*scroll).min(max);
let mut rows: Vec<Row> = lines.into_iter().skip(*scroll).take(h).collect();
if *scroll < max
&& let Some(last) = rows.last_mut()
{
*last = vec![(Style::fg(theme().muted), format!(" ↓ {} more", max - *scroll))];
}
rows
}
pub fn text_field(buf: &[char], cur: usize, base: Style) -> Row {
let t = theme();
let mut row: Row = Vec::new();
if cur > 0 {
row.push((base, buf[..cur].iter().collect()));
}
row.push((
Style::fg(t.on_bright).bg(t.bright),
buf.get(cur).map_or(" ".into(), |c| c.to_string()),
));
if cur + 1 < buf.len() {
row.push((base, buf[cur + 1..].iter().collect()));
}
row
}
pub fn is_close(key: &KeyEvent) -> bool {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
matches!(key.code, KeyCode::Esc)
|| (key.code == KeyCode::Char('q') && !ctrl)
|| (ctrl && key.code == KeyCode::Char('c'))
}
pub fn tab_key(tab: &mut usize, key: &KeyEvent, n: usize) -> bool {
if n == 0 {
return false;
}
match key.code {
KeyCode::Left | KeyCode::BackTab => *tab = (*tab % n + n - 1) % n,
KeyCode::Right | KeyCode::Tab => *tab = (*tab + 1) % n,
KeyCode::Char(c) if c.is_ascii_digit() && !key.modifiers.contains(KeyModifiers::CONTROL) => {
match (c as usize).checked_sub('1' as usize) {
Some(i) if i < n => *tab = i,
_ => return false,
}
}
_ => return false,
}
true
}
pub fn step_of(key: &KeyEvent) -> isize {
match key.code {
KeyCode::Up => -1,
KeyCode::Down => 1,
KeyCode::PageUp => -10,
KeyCode::PageDown => 10,
KeyCode::Home => -10_000,
KeyCode::End => 10_000,
_ => 0,
}
}
pub fn step(v: &mut usize, by: isize, n: usize) {
*v = (*v as isize + by).clamp(0, n.saturating_sub(1) as isize) as usize;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::style::row_text;
#[test]
fn number_keys_pick_tabs_and_zero_picks_none() {
let key = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);
let mut tab = 1;
assert!(!tab_key(&mut tab, &key('0'), 2));
assert!(!tab_key(&mut tab, &key('3'), 2));
assert!(tab_key(&mut tab, &key('1'), 2));
assert_eq!(tab, 0);
assert!(!tab_key(&mut tab, &KeyEvent::new(KeyCode::Left, KeyModifiers::NONE), 0));
}
#[test]
fn frame_is_exactly_as_tall_as_asked() {
for h in [3, 8, 20] {
let rows = frame(60, h, title("T"), Some((&["A", "B"], 1)), vec![vec![]; 40], "hint");
assert_eq!(rows.len(), h);
}
}
#[test]
fn field_puts_the_cursor_on_a_character() {
let buf: Vec<char> = "C:\\x".chars().collect();
assert_eq!(row_text(&text_field(&buf, 4, Style::default())), "C:\\x ");
assert_eq!(row_text(&text_field(&buf, 0, Style::default())), "C:\\x");
assert_eq!(text_field(&buf, 1, Style::default())[1].1, ":");
}
#[test]
fn lists_keep_the_selection_in_view() {
let items = (0..20).map(|i| Item::new(format!("item {i}"))).collect();
let rows = list_rows(items, 15, 5);
assert_eq!(rows.len(), 5);
assert!(row_text(&rows[4]).starts_with(" ❯ item 15"));
}
}