@@ -16,6 +16,7 @@ use crate::{
1616 commands:: {
1717 ls:: { NodeLs , Summary } ,
1818 tui:: {
19+ TuiResult ,
1920 restore:: Restore ,
2021 widgets:: {
2122 Draw , PopUpPrompt , PopUpText , ProcessEvent , PromptResult , SelectTable ,
@@ -30,10 +31,11 @@ use super::{summary::SummaryMap, widgets::PopUpInput};
3031
3132// the states this screen can be in
3233enum CurrentScreen < ' a , P , S > {
33- Snapshot ,
34+ Ls ,
3435 ShowHelp ( PopUpText ) ,
3536 Restore ( Box < Restore < ' a , P , S > > ) ,
3637 PromptExit ( PopUpPrompt ) ,
38+ PromptLeave ( PopUpPrompt ) ,
3739 ShowFile ( Box < PopUpInput > ) ,
3840}
3941
@@ -57,7 +59,7 @@ General Commands:
5759
5860 " ;
5961
60- pub ( crate ) struct Snapshot < ' a , P , S > {
62+ pub struct Ls < ' a , P , S > {
6163 current_screen : CurrentScreen < ' a , P , S > ,
6264 numeric : bool ,
6365 table : WithBlock < SelectTable > ,
@@ -70,32 +72,49 @@ pub(crate) struct Snapshot<'a, P, S> {
7072 summary_map : SummaryMap ,
7173}
7274
73- pub enum SnapshotResult {
75+ pub enum LsResult {
7476 Exit ,
7577 Return ( SummaryMap ) ,
7678 None ,
7779}
7880
79- impl < ' a , P : ProgressBars , S : IndexedFull > Snapshot < ' a , P , S > {
81+ impl TuiResult for LsResult {
82+ fn exit ( & self ) -> bool {
83+ !matches ! ( self , Self :: None )
84+ }
85+ }
86+
87+ impl < ' a , P : ProgressBars , S : IndexedFull > Ls < ' a , P , S > {
8088 pub fn new (
8189 repo : & ' a Repository < P , S > ,
8290 snapshot : SnapshotFile ,
91+ path : & str ,
8392 summary_map : SummaryMap ,
8493 ) -> Result < Self > {
8594 let header = [ "Name" , "Size" , "Mode" , "User" , "Group" , "Time" ]
8695 . into_iter ( )
8796 . map ( Text :: from)
8897 . collect ( ) ;
8998
90- let tree_id = snapshot. tree ;
91- let tree = repo. get_tree ( & tree_id) ?;
99+ let node = repo. node_from_snapshot_and_path ( & snapshot, path) ?;
100+ let ( tree_id, tree) = node. subtree . map_or_else (
101+ || -> Result < _ > {
102+ Ok ( (
103+ TreeId :: default ( ) ,
104+ Tree {
105+ nodes : vec ! [ node. clone( ) ] ,
106+ } ,
107+ ) )
108+ } ,
109+ |id| Ok ( ( id, repo. get_tree ( & id) ?) ) ,
110+ ) ?;
92111 let mut app = Self {
93- current_screen : CurrentScreen :: Snapshot ,
112+ current_screen : CurrentScreen :: Ls ,
94113 numeric : false ,
95114 table : WithBlock :: new ( SelectTable :: new ( header) , Block :: new ( ) ) ,
96115 repo,
97116 snapshot,
98- path : PathBuf :: new ( ) ,
117+ path : PathBuf :: from ( path ) ,
99118 trees : Vec :: new ( ) ,
100119 tree,
101120 tree_id,
@@ -200,19 +219,20 @@ impl<'a, P: ProgressBars, S: IndexedFull> Snapshot<'a, P, S> {
200219 Ok ( ( ) )
201220 }
202221
203- pub fn goback ( & mut self ) -> bool {
222+ pub fn goback ( & mut self ) {
204223 _ = self . path . pop ( ) ;
205224 if let Some ( ( tree, tree_id, idx) ) = self . trees . pop ( ) {
206225 self . tree = tree;
207226 self . tree_id = tree_id;
208227 self . table . widget . set_to ( idx) ;
209228 self . update_table ( ) ;
210- false
211- } else {
212- true
213229 }
214230 }
215231
232+ pub fn in_root ( & self ) -> bool {
233+ self . trees . is_empty ( )
234+ }
235+
216236 pub fn toggle_numeric ( & mut self ) {
217237 self . numeric = !self . numeric ;
218238 self . update_table ( ) ;
@@ -226,18 +246,24 @@ impl<'a, P: ProgressBars, S: IndexedFull> Snapshot<'a, P, S> {
226246 self . update_table ( ) ;
227247 Ok ( ( ) )
228248 }
249+ }
229250
230- pub fn input ( & mut self , event : Event ) -> Result < SnapshotResult > {
251+ impl < ' a , P : ProgressBars , S : IndexedFull > ProcessEvent for Ls < ' a , P , S > {
252+ type Result = Result < LsResult > ;
253+ fn input ( & mut self , event : Event ) -> Result < LsResult > {
231254 use KeyCode :: { Backspace , Char , Enter , Esc , Left , Right } ;
232255 match & mut self . current_screen {
233- CurrentScreen :: Snapshot => match event {
256+ CurrentScreen :: Ls => match event {
234257 Event :: Key ( key) if key. kind == KeyEventKind :: Press => match key. code {
235258 Enter | Right => self . enter ( ) ?,
236259 Backspace | Left => {
237- if self . goback ( ) {
238- return Ok ( SnapshotResult :: Return ( std:: mem:: take (
239- & mut self . summary_map ,
240- ) ) ) ;
260+ if self . in_root ( ) {
261+ self . current_screen = CurrentScreen :: PromptLeave ( popup_prompt (
262+ "leave ls" ,
263+ "do you want to leave the ls view? (y/n)" . into ( ) ,
264+ ) ) ;
265+ } else {
266+ self . goback ( ) ;
241267 }
242268 }
243269 Esc | Char ( 'q' ) => {
@@ -309,33 +335,42 @@ impl<'a, P: ProgressBars, S: IndexedFull> Snapshot<'a, P, S> {
309335 } ,
310336 CurrentScreen :: ShowFile ( prompt) => match prompt. input ( event) {
311337 TextInputResult :: Cancel | TextInputResult :: Input ( _) => {
312- self . current_screen = CurrentScreen :: Snapshot ;
338+ self . current_screen = CurrentScreen :: Ls ;
313339 }
314340 TextInputResult :: None => { }
315341 } ,
316342 CurrentScreen :: ShowHelp ( _) => match event {
317343 Event :: Key ( key) if key. kind == KeyEventKind :: Press => {
318344 if matches ! ( key. code, Char ( 'q' | ' ' | '?' ) | Esc | Enter ) {
319- self . current_screen = CurrentScreen :: Snapshot ;
345+ self . current_screen = CurrentScreen :: Ls ;
320346 }
321347 }
322348 _ => { }
323349 } ,
324350 CurrentScreen :: Restore ( restore) => {
325351 if restore. input ( event) ? {
326- self . current_screen = CurrentScreen :: Snapshot ;
352+ self . current_screen = CurrentScreen :: Ls ;
327353 }
328354 }
329355 CurrentScreen :: PromptExit ( prompt) => match prompt. input ( event) {
330- PromptResult :: Ok => return Ok ( SnapshotResult :: Exit ) ,
331- PromptResult :: Cancel => self . current_screen = CurrentScreen :: Snapshot ,
356+ PromptResult :: Ok => return Ok ( LsResult :: Exit ) ,
357+ PromptResult :: Cancel => self . current_screen = CurrentScreen :: Ls ,
358+ PromptResult :: None => { }
359+ } ,
360+ CurrentScreen :: PromptLeave ( prompt) => match prompt. input ( event) {
361+ PromptResult :: Ok => {
362+ return Ok ( LsResult :: Return ( std:: mem:: take ( & mut self . summary_map ) ) ) ;
363+ }
364+ PromptResult :: Cancel => self . current_screen = CurrentScreen :: Ls ,
332365 PromptResult :: None => { }
333366 } ,
334367 }
335- Ok ( SnapshotResult :: None )
368+ Ok ( LsResult :: None )
336369 }
370+ }
337371
338- pub fn draw ( & mut self , area : Rect , f : & mut Frame < ' _ > ) {
372+ impl < ' a , P : ProgressBars , S : IndexedFull > Draw for Ls < ' a , P , S > {
373+ fn draw ( & mut self , area : Rect , f : & mut Frame < ' _ > ) {
339374 let rects = Layout :: vertical ( [ Constraint :: Min ( 0 ) , Constraint :: Length ( 1 ) ] ) . split ( area) ;
340375
341376 if let CurrentScreen :: Restore ( restore) = & mut self . current_screen {
@@ -355,9 +390,11 @@ impl<'a, P: ProgressBars, S: IndexedFull> Snapshot<'a, P, S> {
355390
356391 // draw popups
357392 match & mut self . current_screen {
358- CurrentScreen :: Snapshot | CurrentScreen :: Restore ( _) => { }
393+ CurrentScreen :: Ls | CurrentScreen :: Restore ( _) => { }
359394 CurrentScreen :: ShowHelp ( popup) => popup. draw ( area, f) ,
360- CurrentScreen :: PromptExit ( popup) => popup. draw ( area, f) ,
395+ CurrentScreen :: PromptExit ( popup) | CurrentScreen :: PromptLeave ( popup) => {
396+ popup. draw ( area, f) ;
397+ }
361398 CurrentScreen :: ShowFile ( popup) => popup. draw ( area, f) ,
362399 }
363400 }
0 commit comments