|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use crossterm::event::{Event, KeyCode, KeyEventKind}; |
| 5 | +use ratatui::{prelude::*, widgets::*}; |
| 6 | +use rustic_core::{ |
| 7 | + repofile::{Node, SnapshotFile, Tree}, |
| 8 | + IndexedFull, Repository, |
| 9 | +}; |
| 10 | +use style::palette::tailwind; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + commands::{ |
| 14 | + ls::{NodeLs, Summary}, |
| 15 | + tui::widgets::{popup_text, Draw, PopUpText, ProcessEvent, SelectTable, WithBlock}, |
| 16 | + }, |
| 17 | + config::progress_options::ProgressOptions, |
| 18 | +}; |
| 19 | + |
| 20 | +// the states this screen can be in |
| 21 | +enum CurrentScreen { |
| 22 | + Snapshot, |
| 23 | + ShowHelp(PopUpText), |
| 24 | +} |
| 25 | + |
| 26 | +const INFO_TEXT: &str = |
| 27 | + "(Esc) quit | (Enter) enter dir | (Backspace) return to parent | (?) show all commands"; |
| 28 | + |
| 29 | +const HELP_TEXT: &str = r#" |
| 30 | +General Commands: |
| 31 | +
|
| 32 | + q,Esc : exit |
| 33 | + Enter : enter dir |
| 34 | + Backspace : return to parent dir |
| 35 | + n : toggle numeric IDs |
| 36 | + ? : show this help page |
| 37 | +
|
| 38 | + "#; |
| 39 | + |
| 40 | +pub(crate) struct Snapshot<'a, S> { |
| 41 | + current_screen: CurrentScreen, |
| 42 | + numeric: bool, |
| 43 | + table: WithBlock<SelectTable>, |
| 44 | + repo: &'a Repository<ProgressOptions, S>, |
| 45 | + snapshot: SnapshotFile, |
| 46 | + path: PathBuf, |
| 47 | + trees: Vec<Tree>, |
| 48 | +} |
| 49 | + |
| 50 | +impl<'a, S: IndexedFull> Snapshot<'a, S> { |
| 51 | + pub fn new(repo: &'a Repository<ProgressOptions, S>, snapshot: SnapshotFile) -> Result<Self> { |
| 52 | + let header = ["Name", "Size", "Mode", "User", "Group", "Time"] |
| 53 | + .into_iter() |
| 54 | + .map(Text::from) |
| 55 | + .collect(); |
| 56 | + |
| 57 | + let tree = repo.get_tree(&snapshot.tree)?; |
| 58 | + let mut app = Self { |
| 59 | + current_screen: CurrentScreen::Snapshot, |
| 60 | + numeric: false, |
| 61 | + table: WithBlock::new(SelectTable::new(header), Block::new()), |
| 62 | + repo, |
| 63 | + snapshot, |
| 64 | + path: PathBuf::new(), |
| 65 | + trees: vec![tree], |
| 66 | + }; |
| 67 | + app.update_table(); |
| 68 | + Ok(app) |
| 69 | + } |
| 70 | + |
| 71 | + fn ls_row(&self, node: &Node) -> Vec<Text<'static>> { |
| 72 | + let (user, group) = if self.numeric { |
| 73 | + ( |
| 74 | + node.meta |
| 75 | + .uid |
| 76 | + .map_or_else(|| "?".to_string(), |id| id.to_string()), |
| 77 | + node.meta |
| 78 | + .gid |
| 79 | + .map_or_else(|| "?".to_string(), |id| id.to_string()), |
| 80 | + ) |
| 81 | + } else { |
| 82 | + ( |
| 83 | + node.meta.user.clone().unwrap_or_else(|| "?".to_string()), |
| 84 | + node.meta.group.clone().unwrap_or_else(|| "?".to_string()), |
| 85 | + ) |
| 86 | + }; |
| 87 | + let name = node.name().to_string_lossy().to_string(); |
| 88 | + let size = node.meta.size.to_string(); |
| 89 | + let mtime = node |
| 90 | + .meta |
| 91 | + .mtime |
| 92 | + .map(|t| format!("{}", t.format("%Y-%m-%d %H:%M:%S"))) |
| 93 | + .unwrap_or_else(|| "?".to_string()); |
| 94 | + [name, size, node.mode_str(), user, group, mtime] |
| 95 | + .into_iter() |
| 96 | + .map(Text::from) |
| 97 | + .collect() |
| 98 | + } |
| 99 | + |
| 100 | + pub fn update_table(&mut self) { |
| 101 | + let old_selection = self.table.widget.selected(); |
| 102 | + let tree = self.trees.last().unwrap(); |
| 103 | + let mut rows = Vec::new(); |
| 104 | + let mut summary = Summary::default(); |
| 105 | + for node in &tree.nodes { |
| 106 | + summary.update(node); |
| 107 | + let row = self.ls_row(node); |
| 108 | + rows.push(row); |
| 109 | + } |
| 110 | + |
| 111 | + self.table.widget.set_content(rows, 1); |
| 112 | + |
| 113 | + self.table.block = Block::new() |
| 114 | + .borders(Borders::BOTTOM | Borders::TOP) |
| 115 | + .title(format!("{}:{}", self.snapshot.id, self.path.display())) |
| 116 | + .title_bottom(format!( |
| 117 | + "total: {}, files: {}, dirs: {}, size: {} - {}", |
| 118 | + tree.nodes.len(), |
| 119 | + summary.files, |
| 120 | + summary.dirs, |
| 121 | + summary.size, |
| 122 | + if self.numeric { |
| 123 | + "numeric IDs" |
| 124 | + } else { |
| 125 | + " Id names" |
| 126 | + } |
| 127 | + )) |
| 128 | + .title_alignment(Alignment::Center); |
| 129 | + self.table.widget.set_to(old_selection.unwrap_or_default()); |
| 130 | + } |
| 131 | + |
| 132 | + pub fn enter(&mut self) -> Result<()> { |
| 133 | + if let Some(idx) = self.table.widget.selected() { |
| 134 | + let node = &self.trees.last().unwrap().nodes[idx]; |
| 135 | + if node.is_dir() { |
| 136 | + self.path.push(node.name()); |
| 137 | + self.trees.push(self.repo.get_tree(&node.subtree.unwrap())?); |
| 138 | + } |
| 139 | + } |
| 140 | + self.update_table(); |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + |
| 144 | + pub fn goback(&mut self) -> bool { |
| 145 | + _ = self.path.pop(); |
| 146 | + _ = self.trees.pop(); |
| 147 | + if !self.trees.is_empty() { |
| 148 | + self.update_table(); |
| 149 | + } |
| 150 | + self.trees.is_empty() |
| 151 | + } |
| 152 | + |
| 153 | + pub fn toggle_numeric(&mut self) { |
| 154 | + self.numeric = !self.numeric; |
| 155 | + self.update_table(); |
| 156 | + } |
| 157 | + |
| 158 | + pub fn input(&mut self, event: Event) -> Result<bool> { |
| 159 | + use KeyCode::*; |
| 160 | + match &mut self.current_screen { |
| 161 | + CurrentScreen::Snapshot => match event { |
| 162 | + Event::Key(key) if key.kind == KeyEventKind::Press => match key.code { |
| 163 | + Enter | Right => self.enter()?, |
| 164 | + Backspace | Left => { |
| 165 | + if self.goback() { |
| 166 | + return Ok(true); |
| 167 | + } |
| 168 | + } |
| 169 | + Char('?') => { |
| 170 | + self.current_screen = |
| 171 | + CurrentScreen::ShowHelp(popup_text("help", HELP_TEXT.into())); |
| 172 | + } |
| 173 | + Char('n') => self.toggle_numeric(), |
| 174 | + _ => self.table.input(event), |
| 175 | + }, |
| 176 | + _ => {} |
| 177 | + }, |
| 178 | + CurrentScreen::ShowHelp(_) => match event { |
| 179 | + Event::Key(key) if key.kind == KeyEventKind::Press => { |
| 180 | + if matches!(key.code, Char('q') | Esc | Enter | Char(' ') | Char('?')) { |
| 181 | + self.current_screen = CurrentScreen::Snapshot; |
| 182 | + } |
| 183 | + } |
| 184 | + _ => {} |
| 185 | + }, |
| 186 | + } |
| 187 | + Ok(false) |
| 188 | + } |
| 189 | + |
| 190 | + pub fn draw(&mut self, area: Rect, f: &mut Frame<'_>) { |
| 191 | + let rects = Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).split(area); |
| 192 | + |
| 193 | + // draw the table |
| 194 | + self.table.draw(rects[0], f); |
| 195 | + |
| 196 | + // draw the footer |
| 197 | + let buffer_bg = tailwind::SLATE.c950; |
| 198 | + let row_fg = tailwind::SLATE.c200; |
| 199 | + let info_footer = Paragraph::new(Line::from(INFO_TEXT)) |
| 200 | + .style(Style::new().fg(row_fg).bg(buffer_bg)) |
| 201 | + .centered(); |
| 202 | + f.render_widget(info_footer, rects[1]); |
| 203 | + |
| 204 | + // draw popups |
| 205 | + match &mut self.current_screen { |
| 206 | + CurrentScreen::Snapshot => {} |
| 207 | + CurrentScreen::ShowHelp(popup) => popup.draw(area, f), |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments