use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{
Block, Borders, Cell, Clear, Paragraph, Row as TableRow, Table, TableState, Tabs, Wrap,
};
use super::{App, Filter, InputPurpose, MessageKind, RowStatus};
use crate::report::describe_age;
pub fn draw(frame: &mut Frame, app: &App) {
if let Some(gauge) = app.build_progress() {
let [header, list, details, build, footer] = Layout::new(
Direction::Vertical,
[
Constraint::Length(3),
Constraint::Min(5),
Constraint::Length(resting_details_height(app)),
Constraint::Length(3),
Constraint::Length(3),
],
)
.areas(frame.area());
draw_tabs(frame, app, header);
draw_list(frame, app, list);
draw_details(frame, app, details);
draw_gauge(frame, &gauge, app.transient_panel_title(), build);
draw_footer(frame, app, footer);
if app.show_help {
draw_help(frame, app, frame.area());
}
return;
}
let details_h = if app.build_report.is_some() {
frame.area().height.saturating_sub(11).clamp(9, 16)
} else {
resting_details_height(app)
};
let [header, list, details, footer] = Layout::new(
Direction::Vertical,
[
Constraint::Length(3),
Constraint::Min(5),
Constraint::Length(details_h),
Constraint::Length(3),
],
)
.areas(frame.area());
draw_tabs(frame, app, header);
draw_list(frame, app, list);
draw_details(frame, app, details);
draw_footer(frame, app, footer);
if app.show_help {
let area = frame.area();
draw_help(frame, app, area);
}
}
fn draw_gauge(frame: &mut Frame, gauge: &str, title: &str, area: Rect) {
let block = Block::default()
.borders(Borders::ALL)
.title(title.to_owned());
let inner = block.inner(area);
frame.render_widget(block, area);
frame.render_widget(
Paragraph::new(Span::styled(
format!(" {gauge}"),
Style::default().fg(Color::Yellow),
)),
inner,
);
}
fn draw_tabs(frame: &mut Frame, app: &App, area: Rect) {
let titles = Filter::ALL.iter().map(|f| match f {
Filter::All => format!(" Packages ({}) ", app.total()),
Filter::Updates => format!(" Updates ({}) ", app.updates_available()),
Filter::Pinned => format!(" Pinned ({}) ", app.pinned_count()),
});
let tabs = Tabs::new(titles)
.select(app.filter.index())
.highlight_style(
Style::default()
.fg(Color::Black)
.bg(Color::Cyan)
.add_modifier(Modifier::BOLD),
)
.block(
Block::default()
.borders(Borders::ALL)
.title(format!(" cargo-lbin — {} ", app.prefix().display())),
);
frame.render_widget(tabs, area);
}
fn status_cell(status: &RowStatus) -> Cell<'static> {
let (text, color) = match status {
RowStatus::UpToDate => ("✓ up to date".to_owned(), Color::Green),
RowStatus::Outdated(latest) => (format!("↑ {latest}"), Color::Yellow),
RowStatus::Unknown => ("? not checked".to_owned(), Color::Gray),
};
Cell::from(Span::styled(text, Style::default().fg(color)))
}
fn draw_list(frame: &mut Frame, app: &App, area: Rect) {
let rows: Vec<TableRow> = app
.visible()
.into_iter()
.map(|row| {
let mut name = row.name.clone();
if row.locked {
name.push_str(" [locked]");
}
if row.pinned {
name.push_str(" [pinned]");
}
if !row.also.is_empty() {
name.push_str(&crate::prefixes::describe(&row.also));
}
TableRow::new(vec![
Cell::from(name),
Cell::from(row.version.clone()),
status_cell(&row.status),
])
})
.collect();
let empty_note = match app.filter {
Filter::All => "nothing installed under this prefix — press i to install",
Filter::Updates => "no unpinned updates known — press r to check crates.io",
Filter::Pinned => "no pinned crates — p in Packages pins the selected crate",
};
let block = Block::default().borders(Borders::ALL);
if rows.is_empty() {
let note = Paragraph::new(Span::styled(
format!(" {empty_note}"),
Style::default().fg(Color::DarkGray),
))
.block(block);
frame.render_widget(note, area);
return;
}
let header = TableRow::new(["NAME", "VERSION", "STATUS"])
.style(Style::default().add_modifier(Modifier::BOLD));
let table = Table::new(
rows,
[
Constraint::Min(20),
Constraint::Length(16),
Constraint::Length(24),
],
)
.header(header)
.block(block)
.row_highlight_style(
Style::default()
.fg(Color::White)
.bg(Color::DarkGray)
.add_modifier(Modifier::BOLD),
)
.highlight_symbol("> ");
let mut state = TableState::default().with_selected(Some(app.selected));
frame.render_stateful_widget(table, area, &mut state);
}
fn draw_report(
frame: &mut Frame,
app: &App,
report: &crate::tui::BuildReport,
scroll: u16,
area: Rect,
) {
let color = if report.failed {
Color::Red
} else {
Color::Yellow
};
let mut lines: Vec<Line> = vec![Line::from(Span::styled(
report.title.clone(),
Style::default().fg(color).add_modifier(Modifier::BOLD),
))];
lines.extend(
report
.lines
.iter()
.map(|l| Line::from(Span::raw(l.clone()))),
);
let inner_w = area.width.saturating_sub(2).max(1);
let inner_h = area.height.saturating_sub(2).max(1);
let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false });
let total_rows = u16::try_from(paragraph.line_count(inner_w)).unwrap_or(u16::MAX);
let max_scroll = total_rows.saturating_sub(inner_h);
app.report_scroll_max.set(max_scroll);
let effective = scroll.min(max_scroll);
let block = Block::default()
.borders(Borders::ALL)
.title(if max_scroll > 0 {
" Esc/Enter dismisses · Up/Down scrolls "
} else {
" Esc/Enter dismisses "
});
frame.render_widget(paragraph.block(block).scroll((effective, 0)), area);
}
fn resting_details_height(app: &App) -> u16 {
if let Some(choice) = &app.downgrade_choice {
let lines =
u16::try_from(choice.versions.len()).unwrap_or(9).min(9) + u16::from(choice.older > 0);
return lines + 2;
}
let also = app.selected_row().map_or(0, |row| row.also.len());
8 + u16::try_from(also).unwrap_or(2).min(2)
}
fn draw_details(frame: &mut Frame, app: &App, area: Rect) {
if let Some(report) = &app.build_report {
draw_report(frame, app, report, app.report_scroll, area);
return;
}
if let Some(choice) = &app.downgrade_choice {
let mut lines: Vec<Line> = choice
.versions
.iter()
.enumerate()
.map(|(i, v)| {
Line::from(vec![
Span::styled(
format!("[{}] ", i + 1),
Style::default().add_modifier(Modifier::BOLD),
),
Span::raw(v.to_string()),
])
})
.collect();
if choice.older > 0 {
lines.push(Line::from(Span::styled(
format!(
"and {} older — install {}@VERSION for one of those",
choice.older, choice.name
),
Style::default().fg(Color::Gray),
)));
}
let panel =
Paragraph::new(lines).block(Block::default().borders(Borders::ALL).title(format!(
" Downgrade {} from {} (digit installs, Esc dismisses) ",
choice.name, choice.current
)));
frame.render_widget(panel, area);
return;
}
if let Some(search) = &app.search_result {
let name_w = search.hits.iter().map(|h| h.name.len()).max().unwrap_or(0);
let lines: Vec<Line> = search
.hits
.iter()
.enumerate()
.map(|(i, hit)| {
let mut spans = vec![
Span::styled(
format!("[{}] ", i + 1),
Style::default().add_modifier(Modifier::BOLD),
),
Span::raw(format!("{:<name_w$} {} ", hit.name, hit.version)),
];
if let Some(have) = search.installed.get(&hit.name) {
spans.push(Span::styled(
format!("[installed {have}] "),
Style::default().fg(Color::Green),
));
}
spans.push(Span::styled(
hit.description.clone(),
Style::default().fg(Color::Gray),
));
Line::from(spans)
})
.collect();
let panel = Paragraph::new(lines).block(Block::default().borders(Borders::ALL).title(
format!(" Search: {} (digit installs, Esc dismisses) ", search.query),
));
frame.render_widget(panel, area);
return;
}
let lines = match app.selected_row() {
Some(row) => detail_lines(row),
None => vec![Line::from(Span::styled(
"nothing selected",
Style::default().fg(Color::DarkGray),
))],
};
let panel =
Paragraph::new(lines).block(Block::default().borders(Borders::ALL).title(" Selected "));
frame.render_widget(panel, area);
}
fn detail_lines(row: &super::Row) -> Vec<Line<'static>> {
let mut lines: Vec<Line> = Vec::new();
lines.push(kv("Crate", &row.name, Style::default()));
lines.push(kv("Installed", &row.version, Style::default()));
match &row.status {
RowStatus::UpToDate => {
lines.push(kv(
"Latest",
&row.version,
Style::default().fg(Color::Green),
));
}
RowStatus::Outdated(latest) => lines.push(kv(
"Latest",
&latest.to_string(),
Style::default().fg(Color::Yellow),
)),
RowStatus::Unknown => lines.push(kv(
"Latest",
"not checked (press r)",
Style::default().fg(Color::Gray),
)),
}
lines.push(kv("Binaries", &row.bins.join(", "), Style::default()));
if row.locked {
lines.push(kv(
"Locked",
"yes (--locked, reused on update)",
Style::default(),
));
} else {
lines.push(kv("Locked", "no", Style::default()));
}
if row.pinned {
lines.push(kv(
"Pinned",
"yes — held at this version; p to unpin",
Style::default().fg(Color::Yellow),
));
} else {
lines.push(kv("Pinned", "no", Style::default()));
}
for other in &row.also {
lines.push(kv(
"Also in",
&crate::text::sanitize(&format!("{} @{}", other.prefix.display(), other.version)),
Style::default().fg(Color::Gray),
));
}
lines
}
fn key_bar(app: &App) -> &'static str {
if app.confirm.is_some() {
" y confirm · any other key cancel"
} else if app.input.is_some() {
" Enter run · Esc cancel"
} else if app.manifest_error.is_some() {
if app.oneshot_running() {
" manifest unavailable · c cancel · ? help · q quit"
} else {
" manifest unavailable · v verify · r retry load · B other prefix · ? help · q quit"
}
} else if app.build_running() {
if app.build_report.is_some() {
" ↑/↓ scroll warnings · Esc/Enter dismiss · c cancel/escalate · Ctrl-C cancel/quit"
} else {
" ↑/↓ select · c cancel/escalate · Ctrl-C cancel/quit"
}
} else if app.oneshot_running() {
" ↑/↓ select · c cancel · ? help · q quit"
} else {
" ↑/↓ select · Tab filter · Enter/u update · U update all · i install · x remove · \
m migrate · M migrate all · B other prefix · p pin · D downgrade · v verify · r check · s search · ? help · q quit"
}
}
fn draw_footer(frame: &mut Frame, app: &App, area: Rect) {
use std::fmt::Write as _;
let [keys_area, status_area, line_area] = Layout::new(
Direction::Vertical,
[
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(1),
],
)
.areas(area);
let keys = key_bar(app);
frame.render_widget(
Paragraph::new(Span::styled(keys, Style::default().fg(Color::DarkGray))),
keys_area,
);
let checked = match app.report_age {
Some(age) => format!("checked {}", describe_age(age)),
None => "never checked".to_owned(),
};
let (status, status_color) = if app.manifest_error.is_some() {
(
" managed crate count unavailable — the manifest did not load".to_owned(),
Color::Red,
)
} else {
let mut status = format!(
" {} packages · {} updates",
app.total(),
app.updates_available()
);
match (app.pinned_count(), app.pinned_outdated()) {
(0, _) => {}
(p, 0) => {
let _ = write!(status, " · {p} pinned");
}
(p, held) => {
let _ = write!(status, " · {p} pinned ({held} behind)");
}
}
if app.not_checked() > 0 {
let _ = write!(status, " · {} not checked", app.not_checked());
}
let _ = write!(status, " · {checked}");
(status, Color::DarkGray)
};
frame.render_widget(
Paragraph::new(Span::styled(status, Style::default().fg(status_color))),
status_area,
);
if let Some(confirm) = &app.confirm {
let line = Span::styled(
format!(" {}", confirm.prompt),
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::BOLD),
);
frame.render_widget(Paragraph::new(line), line_area);
} else if let Some(input) = &app.input {
let label = match input.purpose {
InputPurpose::Install => "install: ",
InputPurpose::Search => "search: ",
};
let text = format!(" {label}{}", input.buffer);
let cursor_x = line_area
.x
.saturating_add(u16::try_from(text.chars().count()).unwrap_or(u16::MAX));
frame.render_widget(Paragraph::new(text), line_area);
frame.set_cursor_position((
cursor_x.min(line_area.right().saturating_sub(1)),
line_area.y,
));
} else if app.build_progress().is_none()
&& let Some(label) = app.busy()
{
let line = Span::styled(format!(" {label}"), Style::default().fg(Color::Cyan));
frame.render_widget(Paragraph::new(line), line_area);
} else if let Some(message) = &app.message {
let color = match message.kind {
MessageKind::Info => Color::Green,
MessageKind::Warning => Color::Yellow,
MessageKind::Error => Color::Red,
};
let line = Span::styled(format!(" {}", message.text), Style::default().fg(color));
frame.render_widget(Paragraph::new(line), line_area);
}
}
fn draw_help(frame: &mut Frame, app: &App, area: Rect) {
if app.manifest_error.is_some() {
let lines = [
"v verify: audit the broken managed state",
"c cancel the running verify (its result is discarded)",
"r retry loading the manifest",
"B switch to the other known prefix",
"q / Esc quit (from the list)",
"",
"The manifest did not load. Mutating actions, update",
"checks and search are disabled until it is repaired",
"by hand — v names every finding.",
"",
"any key closes this help",
];
draw_help_box(frame, &lines, area);
return;
}
let lines = [
"↑/↓ j/k select Tab Packages / Updates / Pinned",
"g/G Home/End first / last ? this help",
"",
"Enter, u update selected crate (confirmed in the terminal)",
"U run update --all: fresh plan from crates.io, not the cache",
"i install: NAME[@VERSION]... [--locked] (@VERSION pins);",
" a single crate builds in place inside the Build panel,",
" a batch hands the terminal over as before",
"x remove selected crate (asks first; in place unless",
" removal needs sudo — then the terminal, as before)",
"m migrate selected crate to the other prefix (asks",
" first; pinned rebuilds its exact version there,",
" unpinned installs the latest; retired here;",
" /usr/local <-> ~/.local only, custom via CLI --to)",
"M migrate every crate (asks first; a queue of single",
" migrations, one summary; c cancels the batch)",
"c cancel the running build (again, or automatically",
" after ~2s of no effect: SIGKILL); past the placement",
" door a cancel is too late: an install finishes placing,",
" a migration proceeds through its retirement attempt;",
" for r/v/s: request cancellation — the update check stops",
" between requests, a verify/search result is discarded",
"B jump to the other prefix (/usr/local <-> ~/.local);",
" the selection follows the crate when it is visible there",
"p pin / unpin selected crate; a pin declares the version:",
" update --all holds it back, m migrates exactly it",
" (in place unless pinning needs sudo)",
"D downgrade: the panel offers older versions, a digit installs one",
"v verify: the manifest's claims checked against the disk,",
" read-only; violations and warnings land in a panel,",
" naming the repair where one is unambiguous",
"r check crates.io for updates (writes the report)",
"s search crates.io by keyword; a digit then picks a hit",
" and opens the install line with its name",
"",
"Nothing runs on its own: no refresh or network access on start.",
"Single-crate installs build inside the TUI: the build runs first",
"and sudo is asked for when placement begins, on the real terminal",
"(the first lock in a prefix is the exception — that one is asked",
"before the build); a failed build",
"keeps its last lines and the log path in the details panel.",
"update and batch installs hand the terminal to cargo",
"and sudo as before, and return when you press Enter.",
"",
"q / Esc quit (from the list); during a build, Ctrl-C",
" cancels it and quits once the worker stops",
"",
"any key closes this help",
];
draw_help_box(frame, &lines, area);
}
fn draw_help_box(frame: &mut Frame, lines: &[&str], area: Rect) {
let width = lines.iter().map(|l| l.chars().count()).max().unwrap_or(0);
let popup = centered(
area,
u16::try_from(width + 4).unwrap_or(u16::MAX),
u16::try_from(lines.len() + 2).unwrap_or(u16::MAX),
);
frame.render_widget(Clear, popup);
let text: Vec<Line> = lines.iter().map(|l| Line::from(format!(" {l}"))).collect();
let help = Paragraph::new(text).block(Block::default().borders(Borders::ALL).title(" Keys "));
frame.render_widget(help, popup);
}
fn centered(area: Rect, width: u16, height: u16) -> Rect {
let width = width.min(area.width);
let height = height.min(area.height);
Rect {
x: area.x + (area.width - width) / 2,
y: area.y + (area.height - height) / 2,
width,
height,
}
}
fn kv(key: &str, value: &str, value_style: Style) -> Line<'static> {
Line::from(vec![
Span::styled(
format!("{key:<10}"),
Style::default().add_modifier(Modifier::BOLD),
),
Span::styled(value.to_owned(), value_style),
])
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tui::{Row, RowStatus};
fn line_text(line: &Line) -> String {
line.spans.iter().map(|s| s.content.as_ref()).collect()
}
fn row(name: &str) -> Row {
Row {
name: name.to_owned(),
version: "1.2.3".to_owned(),
bins: vec![name.to_owned()],
locked: false,
pinned: false,
also: Vec::new(),
status: RowStatus::Unknown,
}
}
#[test]
fn details_tell_the_whole_entry_including_the_negatives() {
let plain = detail_lines(&row("foo"));
let texts: Vec<String> = plain.iter().map(line_text).collect();
assert!(
texts
.iter()
.any(|l| l.contains("Locked") && l.contains("no")),
"{texts:?}"
);
assert!(
texts
.iter()
.any(|l| l.contains("Pinned") && l.contains("no")),
"{texts:?}"
);
assert!(
!texts.iter().any(|l| l.contains("Also in")),
"not elsewhere is the ordinary state, not information: {texts:?}"
);
let mut full = row("foo");
full.locked = true;
full.pinned = true;
full.bins = vec!["foo".to_owned(), "fooctl".to_owned()];
full.also = vec![crate::prefixes::AlsoIn {
prefix: std::path::PathBuf::from("/usr/local"),
version: "1.1.0".to_owned(),
}];
let texts: Vec<String> = detail_lines(&full).iter().map(line_text).collect();
assert!(
texts
.iter()
.any(|l| l.contains("Locked") && l.contains("yes")),
"{texts:?}"
);
assert!(
texts
.iter()
.any(|l| l.contains("Pinned") && l.contains("yes")),
"{texts:?}"
);
assert!(
texts.iter().any(|l| l.contains("foo, fooctl")),
"every binary, in one line: {texts:?}"
);
assert!(
texts
.iter()
.any(|l| l.contains("Also in") && l.contains("/usr/local @1.1.0")),
"the foreign copy is a native line with its version: {texts:?}"
);
}
#[test]
fn details_show_every_foreign_copy() {
let mut r = row("foo");
r.also = vec![
crate::prefixes::AlsoIn {
prefix: std::path::PathBuf::from("/usr/local"),
version: "1.0.0".to_owned(),
},
crate::prefixes::AlsoIn {
prefix: std::path::PathBuf::from("/home/u/.local"),
version: "1.1.0".to_owned(),
},
];
let texts: Vec<String> = detail_lines(&r).iter().map(line_text).collect();
assert_eq!(
texts.len(),
8,
"six fixed lines plus one per copy: {texts:?}"
);
assert!(texts[6].contains("/usr/local @1.0.0"), "{texts:?}");
assert!(texts[7].contains("/home/u/.local @1.1.0"), "{texts:?}");
}
#[test]
fn resting_height_makes_room_for_every_line() {
let prefix = std::env::temp_dir().join("cargo-lbin-test-tui-details-height");
let _ = std::fs::remove_dir_all(&prefix);
std::fs::create_dir_all(&prefix).unwrap();
let mut app = crate::tui::App::new(&prefix).unwrap();
app.rows = vec![row("foo")];
assert_eq!(
resting_details_height(&app),
8,
"no foreign copy: six lines + borders"
);
let content = |app: &super::super::App| {
u16::try_from(detail_lines(app.selected_row().unwrap()).len()).unwrap()
};
assert_eq!(resting_details_height(&app), content(&app) + 2);
app.rows[0].also = vec![crate::prefixes::AlsoIn {
prefix: std::path::PathBuf::from("/usr/local"),
version: "1.0.0".to_owned(),
}];
assert_eq!(resting_details_height(&app), content(&app) + 2);
app.rows[0].also.push(crate::prefixes::AlsoIn {
prefix: std::path::PathBuf::from("/home/u/.local"),
version: "1.1.0".to_owned(),
});
assert_eq!(
resting_details_height(&app),
content(&app) + 2,
"two foreign copies still fit inside the borders"
);
let _ = std::fs::remove_dir_all(&prefix);
}
#[test]
fn an_offer_gets_a_panel_tall_enough_for_every_version() {
let prefix = std::env::temp_dir().join("cargo-lbin-test-tui-offer-height");
let _ = std::fs::remove_dir_all(&prefix);
std::fs::create_dir_all(&prefix).unwrap();
let mut app = crate::tui::App::new(&prefix).unwrap();
app.rows = vec![row("foo")];
let offer = |versions: usize, older: usize| crate::tui::DowngradeChoice {
name: "foo".to_owned(),
current: "1.2.0".to_owned(),
versions: (0..versions)
.map(|i| semver::Version::parse(&format!("1.0.{i}")).unwrap())
.collect(),
older,
};
app.downgrade_choice = Some(offer(2, 0));
assert_eq!(resting_details_height(&app), 4, "two lines and the borders");
app.downgrade_choice = Some(offer(9, 0));
assert_eq!(resting_details_height(&app), 11, "nine still fit");
app.downgrade_choice = Some(offer(9, 3));
assert_eq!(
resting_details_height(&app),
12,
"the overflow note is a line of its own"
);
app.downgrade_choice = None;
assert_eq!(
resting_details_height(&app),
8,
"with no offer the entry's own height is back"
);
let _ = std::fs::remove_dir_all(&prefix);
}
#[test]
fn details_sanitize_the_foreign_prefix() {
let mut r = row("foo");
r.also = vec![crate::prefixes::AlsoIn {
prefix: std::path::PathBuf::from("/usr/\x1b[31mlocal"),
version: "1.1.0".to_owned(),
}];
let texts: Vec<String> = detail_lines(&r).iter().map(line_text).collect();
assert!(texts.iter().all(|l| !l.contains('\x1b')), "{texts:?}");
}
}