346 lines
12 KiB
Rust
346 lines
12 KiB
Rust
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
|
||
use ratatui::style::{Color, Modifier, Style};
|
||
use ratatui::text::{Line, Span};
|
||
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap};
|
||
use ratatui::Frame;
|
||
|
||
use crate::app::{App, FormKind, PendingActionKind, View};
|
||
use crate::model::{schedule_label, Task};
|
||
|
||
pub fn render(frame: &mut Frame, app: &App) {
|
||
let rows = Layout::vertical([
|
||
Constraint::Length(3),
|
||
Constraint::Min(5),
|
||
Constraint::Length(3),
|
||
])
|
||
.split(frame.area());
|
||
|
||
let active = Style::default()
|
||
.fg(Color::Black)
|
||
.bg(Color::Cyan)
|
||
.add_modifier(Modifier::BOLD);
|
||
let tasks_style = if app.view == View::Tasks {
|
||
active
|
||
} else {
|
||
Style::default()
|
||
};
|
||
let routines_style = if app.view == View::Routines {
|
||
active
|
||
} else {
|
||
Style::default()
|
||
};
|
||
let archive_style = if app.view == View::Archive {
|
||
active
|
||
} else {
|
||
Style::default()
|
||
};
|
||
let devices_style = if app.view == View::Devices {
|
||
active
|
||
} else {
|
||
Style::default()
|
||
};
|
||
let date = if app.view == View::Tasks {
|
||
format!(" {} ", app.date)
|
||
} else {
|
||
String::new()
|
||
};
|
||
let header = Paragraph::new(Line::from(vec![
|
||
Span::styled(" Tasks ", tasks_style),
|
||
Span::raw(" "),
|
||
Span::styled(" Routines ", routines_style),
|
||
Span::raw(" "),
|
||
Span::styled(" Archive ", archive_style),
|
||
Span::raw(" "),
|
||
Span::styled(" Devices ", devices_style),
|
||
Span::raw(date),
|
||
]))
|
||
.block(Block::default().borders(Borders::ALL).title("Routine"));
|
||
frame.render_widget(header, rows[0]);
|
||
|
||
let items = match app.view {
|
||
View::Tasks => app
|
||
.overdue_tasks
|
||
.iter()
|
||
.map(|task| task_list_item(task, true))
|
||
.chain(app.tasks.iter().map(|task| task_list_item(task, false)))
|
||
.collect::<Vec<_>>(),
|
||
View::Routines => app
|
||
.routines
|
||
.iter()
|
||
.map(|routine| {
|
||
let enabled = if routine.enabled { "●" } else { "○" };
|
||
let reminder = routine
|
||
.reminder_time
|
||
.as_deref()
|
||
.map(|time| format!(" @{time}"))
|
||
.unwrap_or_default();
|
||
let lock = if routine.decryption_failed {
|
||
" 🔒"
|
||
} else {
|
||
""
|
||
};
|
||
let mut lines = vec![Line::from(vec![
|
||
Span::raw(format!("{enabled} {:<8} ", routine.section)),
|
||
priority_span(&routine.priority),
|
||
Span::raw(format!(
|
||
" {} — {}{reminder}{lock}",
|
||
routine.visible_title(),
|
||
schedule_label(&routine.schedule)
|
||
)),
|
||
])];
|
||
if let Some(notes) = routine.visible_notes() {
|
||
lines.push(Line::styled(
|
||
format!(" {notes}"),
|
||
Style::default().fg(Color::DarkGray),
|
||
));
|
||
}
|
||
ListItem::new(lines)
|
||
})
|
||
.collect::<Vec<_>>(),
|
||
View::Archive => app
|
||
.archived_tasks
|
||
.iter()
|
||
.map(|task| {
|
||
let reminder = task
|
||
.reminder_time
|
||
.as_deref()
|
||
.map(|time| format!(" @{time}"))
|
||
.unwrap_or_default();
|
||
let source = task.routine_id.map(|_| " ↻").unwrap_or_default();
|
||
let lock = if task.decryption_failed { " 🔒" } else { "" };
|
||
let mut lines = vec![Line::from(vec![
|
||
Span::raw(format!("▣ {} {:<8} ", task.scheduled_date, task.section)),
|
||
priority_span(&task.priority),
|
||
Span::raw(format!(" {}{reminder}{source}{lock}", task.visible_title())),
|
||
])];
|
||
if let Some(notes) = task.visible_notes() {
|
||
lines.push(Line::styled(
|
||
format!(" {notes}"),
|
||
Style::default().fg(Color::DarkGray),
|
||
));
|
||
}
|
||
ListItem::new(lines)
|
||
})
|
||
.collect::<Vec<_>>(),
|
||
View::Devices => app
|
||
.devices
|
||
.iter()
|
||
.map(|device| {
|
||
let mark = if device.status == "active" {
|
||
"●"
|
||
} else {
|
||
"○"
|
||
};
|
||
let revoked = device
|
||
.revoked_at
|
||
.map(|timestamp| format!(" revoked:{timestamp}"))
|
||
.unwrap_or_default();
|
||
ListItem::new(Line::from(format!(
|
||
"{mark} {:<10} {} {} created:{}{revoked}",
|
||
device.status,
|
||
device.name,
|
||
short_recipient(&device.age_recipient),
|
||
device.created_at,
|
||
)))
|
||
})
|
||
.collect::<Vec<_>>(),
|
||
};
|
||
let title = match app.view {
|
||
View::Tasks if !app.overdue_tasks.is_empty() => {
|
||
format!(
|
||
"Overdue ({}) • Tasks for selected day",
|
||
app.overdue_tasks.len()
|
||
)
|
||
}
|
||
View::Tasks => "Tasks for selected day".to_owned(),
|
||
View::Routines => "Daily, weekly, and monthly routines".to_owned(),
|
||
View::Archive => "Archived tasks".to_owned(),
|
||
View::Devices => "Encryption devices (public recipients only)".to_owned(),
|
||
};
|
||
let list = List::new(items)
|
||
.block(Block::default().borders(Borders::ALL).title(title))
|
||
.highlight_style(Style::default().bg(Color::DarkGray).fg(Color::White))
|
||
.highlight_symbol("› ");
|
||
let mut state =
|
||
ListState::default().with_selected((app.item_count() > 0).then_some(app.selected));
|
||
frame.render_stateful_widget(list, rows[1], &mut state);
|
||
|
||
let help = match app.view {
|
||
View::Tasks => {
|
||
"q quit Tab routines h/l day t today j/k move a add e edit Space complete s move x archive r refresh"
|
||
}
|
||
View::Routines => "q quit Tab tasks j/k move a add routine r refresh",
|
||
View::Archive => "q quit Tab tasks j/k move u restore x delete permanently r refresh",
|
||
View::Devices => "q quit Tab tasks j/k move a register device x revoke r refresh",
|
||
};
|
||
let footer = Paragraph::new(vec![
|
||
Line::from(help),
|
||
Line::styled(&app.status, Style::default().fg(Color::Yellow)),
|
||
])
|
||
.alignment(Alignment::Left)
|
||
.block(Block::default().borders(Borders::TOP));
|
||
frame.render_widget(footer, rows[2]);
|
||
|
||
if let Some(form) = &app.form {
|
||
let height = (form.fields.len() as u16 * 2 + 5).min(frame.area().height);
|
||
let area = centered_rect(72, height, frame.area());
|
||
frame.render_widget(Clear, area);
|
||
let title = match form.kind {
|
||
FormKind::Task => format!("New task for {}", app.date),
|
||
FormKind::EditTask { .. } => "Edit task".to_owned(),
|
||
FormKind::Routine => "New routine".to_owned(),
|
||
FormKind::Reschedule { .. } => "Reschedule task".to_owned(),
|
||
FormKind::Device => "Register encryption device".to_owned(),
|
||
};
|
||
let lines = form
|
||
.fields
|
||
.iter()
|
||
.enumerate()
|
||
.flat_map(|(index, field)| {
|
||
let style = if index == form.active {
|
||
Style::default()
|
||
.fg(Color::Cyan)
|
||
.add_modifier(Modifier::BOLD)
|
||
} else {
|
||
Style::default()
|
||
};
|
||
[
|
||
Line::styled(format!("{} ({})", field.label, field.hint), style),
|
||
Line::from(format!(" {}", field.display())),
|
||
]
|
||
})
|
||
.chain([
|
||
Line::from(""),
|
||
Line::from("Ctrl+S save • Esc cancel • Tab field"),
|
||
])
|
||
.collect::<Vec<_>>();
|
||
let dialog = Paragraph::new(lines)
|
||
.wrap(Wrap { trim: false })
|
||
.block(Block::default().borders(Borders::ALL).title(title));
|
||
frame.render_widget(dialog, area);
|
||
}
|
||
|
||
if let Some(pending) = &app.pending_action {
|
||
let area = centered_rect(60, 7, frame.area());
|
||
frame.render_widget(Clear, area);
|
||
let (subject, verb, title) = match pending.kind {
|
||
PendingActionKind::Archive => ("task", "Archive", "Confirm archive"),
|
||
PendingActionKind::Delete => {
|
||
("task", "Permanently delete", "Confirm permanent deletion")
|
||
}
|
||
PendingActionKind::RevokeDevice => ("device", "Revoke", "Confirm device revocation"),
|
||
};
|
||
let dialog = Paragraph::new(vec![
|
||
Line::from(format!("{verb} {subject} “{}”?", pending.title)),
|
||
Line::from(""),
|
||
Line::styled(
|
||
"y: confirm • n/Esc: cancel",
|
||
Style::default().fg(Color::Yellow),
|
||
),
|
||
])
|
||
.alignment(Alignment::Center)
|
||
.wrap(Wrap { trim: true })
|
||
.block(Block::default().borders(Borders::ALL).title(title));
|
||
frame.render_widget(dialog, area);
|
||
}
|
||
}
|
||
|
||
fn task_list_item(task: &Task, overdue: bool) -> ListItem<'static> {
|
||
let mark = if task.status == "completed" {
|
||
"✓"
|
||
} else {
|
||
"○"
|
||
};
|
||
let reminder = task
|
||
.reminder_time
|
||
.as_deref()
|
||
.map(|time| format!(" @{time}"))
|
||
.unwrap_or_default();
|
||
let source = task.routine_id.map(|_| " ↻").unwrap_or_default();
|
||
let completed = task.completed_at.map(|_| " done").unwrap_or_default();
|
||
let lock = if task.decryption_failed { " 🔒" } else { "" };
|
||
let prefix = if overdue {
|
||
Span::styled(
|
||
format!("⚠ {} {:<8} ", task.scheduled_date, task.section),
|
||
Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
|
||
)
|
||
} else {
|
||
Span::raw(format!("{mark} {:<8} ", task.section))
|
||
};
|
||
let mut lines = vec![Line::from(vec![
|
||
prefix,
|
||
priority_span(&task.priority),
|
||
Span::raw(format!(
|
||
" {}{reminder}{source}{completed}{lock}",
|
||
task.visible_title()
|
||
)),
|
||
])];
|
||
if let Some(notes) = task.visible_notes() {
|
||
lines.push(Line::styled(
|
||
format!(" {notes}"),
|
||
Style::default().fg(Color::DarkGray),
|
||
));
|
||
}
|
||
ListItem::new(lines)
|
||
}
|
||
|
||
fn priority_span(priority: &str) -> Span<'static> {
|
||
Span::styled(format!("{priority:<8}"), priority_style(priority))
|
||
}
|
||
|
||
fn priority_style(priority: &str) -> Style {
|
||
let color = match priority {
|
||
"must" => Color::Red,
|
||
"should" => Color::Yellow,
|
||
"optional" => Color::Green,
|
||
_ => Color::Reset,
|
||
};
|
||
Style::default().fg(color).add_modifier(Modifier::BOLD)
|
||
}
|
||
|
||
fn short_recipient(recipient: &str) -> String {
|
||
const EDGE: usize = 10;
|
||
if recipient.chars().count() <= EDGE * 2 + 1 {
|
||
return recipient.to_owned();
|
||
}
|
||
let start: String = recipient.chars().take(EDGE).collect();
|
||
let end: String = recipient
|
||
.chars()
|
||
.rev()
|
||
.take(EDGE)
|
||
.collect::<String>()
|
||
.chars()
|
||
.rev()
|
||
.collect();
|
||
format!("{start}…{end}")
|
||
}
|
||
|
||
fn centered_rect(width_percent: u16, height: u16, area: Rect) -> Rect {
|
||
let vertical_margin = area.height.saturating_sub(height) / 2;
|
||
let vertical = Layout::vertical([
|
||
Constraint::Length(vertical_margin),
|
||
Constraint::Length(height),
|
||
Constraint::Min(0),
|
||
])
|
||
.split(area);
|
||
let horizontal_margin = (100 - width_percent) / 2;
|
||
Layout::horizontal([
|
||
Constraint::Percentage(horizontal_margin),
|
||
Constraint::Percentage(width_percent),
|
||
Constraint::Percentage(horizontal_margin),
|
||
])
|
||
.split(vertical[1])[1]
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn priorities_have_distinct_colors() {
|
||
assert_eq!(priority_style("must").fg, Some(Color::Red));
|
||
assert_eq!(priority_style("should").fg, Some(Color::Yellow));
|
||
assert_eq!(priority_style("optional").fg, Some(Color::Green));
|
||
}
|
||
}
|