use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Widget};
use unicode_width::UnicodeWidthStr;
use crate::action::Action;
use crate::app::{App, Severity, DIRTY_MARKER};
const HINTS: &[(Action, Option<Action>, &str)] = &[
(Action::FocusNext, None, "file tree"),
(Action::HistoryBack, Some(Action::HistoryForward), "history"),
(Action::HintMode, None, "links"),
(Action::ToggleSidebar, None, "hide sidebar"),
(Action::OpenQuickSwitcher, None, "switch"),
(Action::OpenFindInDocument, None, "find"),
(Action::ToggleHelp, None, "help"),
];
pub struct StatusBar<'a> {
app: &'a App,
collapsed: bool,
}
impl<'a> StatusBar<'a> {
pub fn new(app: &'a App, collapsed: bool) -> Self {
StatusBar { app, collapsed }
}
pub fn line(&self, width: u16) -> Line<'static> {
let theme = &self.app.theme;
let mut spans = vec![
Span::styled(
format!(" {} ", self.app.tab().mode.label()),
theme.ui.status_mode,
),
Span::styled(" ", theme.ui.status_bar),
];
if let Some(editor) = &self.app.tab().editor {
let (line, column) = editor.cursor();
if self.app.tab().dirty {
spans.push(Span::styled(format!("{DIRTY_MARKER} "), theme.tabs.dirty));
}
spans.push(Span::styled(
format!("{}:{} ", line + 1, column + 1),
theme.ui.status_bar,
));
}
if let Some((text, severity)) = &self.app.status.message {
let style = match severity {
Severity::Info => theme.ui.status_bar,
Severity::Warning => theme.ui.status_warning,
Severity::Error => theme.ui.status_error,
};
spans.push(Span::styled(text.clone(), style));
} else if let Some(pending) = &self.app.status.pending {
spans.push(Span::styled(pending.clone(), theme.ui.status_warning));
} else if !self.collapsed {
let mut used: usize = spans.iter().map(|s| s.content.width()).sum();
for (action, paired, what) in HINTS {
let Some(mut key) = self.app.keymap.binding_for(action) else {
continue;
};
if let Some(second) = paired.as_ref().and_then(|a| self.app.keymap.binding_for(a)) {
key = format!("{key}/{second}");
}
let label = format!(" {what} ");
let cost = key.width() + label.width();
if used + cost > width as usize {
break;
}
used += cost;
spans.push(Span::styled(key, theme.ui.title));
spans.push(Span::styled(label, theme.ui.status_bar));
}
}
Line::from(spans)
}
}
impl Widget for StatusBar<'_> {
fn render(self, area: Rect, buf: &mut ratatui::buffer::Buffer) {
let style = self.app.theme.ui.status_bar;
Paragraph::new(self.line(area.width))
.style(style)
.render(area, buf);
}
}