@@ -115,98 +115,11 @@ fn main() -> anyhow::Result<()> {
115115
116116 sync_snapshots ( & restic, & mut cache, args. parallelism ) ?;
117117
118- let entries = cache. get_entries ( None ) ?;
119- if entries. is_empty ( ) {
120- eprintln ! ( "The repository has no snapshots!" ) ;
121- return Ok ( ( ) ) ;
122- }
123-
124- // UI
125- stderr ( ) . execute ( EnterAlternateScreen ) ?;
126- defer ! {
127- stderr( ) . execute( LeaveAlternateScreen ) . unwrap( ) ;
128- }
129- enable_raw_mode ( ) ?;
130- defer ! {
131- disable_raw_mode( ) . unwrap( ) ;
132- }
133- let mut terminal = Terminal :: new ( CrosstermBackend :: new ( stderr ( ) ) ) ?;
134- terminal. clear ( ) ?;
135-
136- let mut app = {
137- let rect = terminal. size ( ) ?;
138- App :: new (
139- rect,
140- None ,
141- Utf8PathBuf :: new ( ) ,
142- entries,
143- cache. get_marks ( ) . unwrap ( ) ,
144- vec ! [
145- "Enter" . bold( ) ,
146- ":Details " . into( ) ,
147- "m" . bold( ) ,
148- ":Mark " . into( ) ,
149- "u" . bold( ) ,
150- ":Unmark " . into( ) ,
151- "c" . bold( ) ,
152- ":ClearAllMarks " . into( ) ,
153- "g" . bold( ) ,
154- ":Generate " . into( ) ,
155- "q" . bold( ) ,
156- ":Quit" . into( ) ,
157- ] ,
158- )
159- } ;
160-
161- let mut output_paths = vec ! [ ] ;
162-
163- render ( & mut terminal, & app) ?;
164- ' outer: loop {
165- let mut o_event = convert_event ( crossterm:: event:: read ( ) ?) ;
166- while let Some ( event) = o_event {
167- o_event = match app. update ( event) {
168- Action :: Nothing => None ,
169- Action :: Render => {
170- render ( & mut terminal, & app) ?;
171- None
172- }
173- Action :: Quit => break ' outer,
174- Action :: Generate ( paths) => {
175- output_paths = paths;
176- break ' outer;
177- }
178- Action :: GetParentEntries ( path_id) => {
179- let parent_id = cache. get_parent_id ( path_id) ?
180- . expect ( "The UI requested a GetParentEntries with a path_id that does not exist" ) ;
181- let entries = cache. get_entries ( parent_id) ?;
182- Some ( Event :: Entries { path_id : parent_id, entries } )
183- }
184- Action :: GetEntries ( path_id) => {
185- let entries = cache. get_entries ( path_id) ?;
186- Some ( Event :: Entries { path_id, entries } )
187- }
188- Action :: GetEntryDetails ( path_id) =>
189- Some ( Event :: EntryDetails ( cache. get_entry_details ( path_id) ?
190- . expect ( "The UI requested a GetEntryDetails with a path_id that does not exist" ) ) ) ,
191- Action :: UpsertMark ( path) => {
192- cache. upsert_mark ( & path) ?;
193- Some ( Event :: Marks ( cache. get_marks ( ) ?) )
194- }
195- Action :: DeleteMark ( loc) => {
196- cache. delete_mark ( & loc) . unwrap ( ) ;
197- Some ( Event :: Marks ( cache. get_marks ( ) ?) )
198- }
199- Action :: DeleteAllMarks => {
200- cache. delete_all_marks ( ) ?;
201- Some ( Event :: Marks ( Vec :: new ( ) ) )
202- }
203- }
204- }
205- }
206-
207- for line in output_paths {
118+ let paths = ui ( cache) ?;
119+ for line in paths {
208120 println ! ( "{}" , escape_for_exclude( line. as_str( ) ) ) ;
209121 }
122+
210123 Ok ( ( ) )
211124}
212125
@@ -497,6 +410,91 @@ fn convert_event(event: crossterm::event::Event) -> Option<Event> {
497410 }
498411}
499412
413+ fn ui ( mut cache : Cache ) -> anyhow:: Result < Vec < Utf8PathBuf > > {
414+ let entries = cache. get_entries ( None ) ?;
415+ if entries. is_empty ( ) {
416+ eprintln ! ( "The repository is empty!" ) ;
417+ return Ok ( vec ! [ ] ) ;
418+ }
419+
420+ stderr ( ) . execute ( EnterAlternateScreen ) ?;
421+ defer ! {
422+ stderr( ) . execute( LeaveAlternateScreen ) . unwrap( ) ;
423+ }
424+ enable_raw_mode ( ) ?;
425+ defer ! {
426+ disable_raw_mode( ) . unwrap( ) ;
427+ }
428+ let mut terminal = Terminal :: new ( CrosstermBackend :: new ( stderr ( ) ) ) ?;
429+ terminal. clear ( ) ?;
430+
431+ let mut app = {
432+ let rect = terminal. size ( ) ?;
433+ App :: new (
434+ rect,
435+ None ,
436+ Utf8PathBuf :: new ( ) ,
437+ entries,
438+ cache. get_marks ( ) ?,
439+ vec ! [
440+ "Enter" . bold( ) ,
441+ ":Details " . into( ) ,
442+ "m" . bold( ) ,
443+ ":Mark " . into( ) ,
444+ "u" . bold( ) ,
445+ ":Unmark " . into( ) ,
446+ "c" . bold( ) ,
447+ ":ClearAllMarks " . into( ) ,
448+ "g" . bold( ) ,
449+ ":Generate " . into( ) ,
450+ "q" . bold( ) ,
451+ ":Quit" . into( ) ,
452+ ] ,
453+ )
454+ } ;
455+
456+ render ( & mut terminal, & app) ?;
457+ loop {
458+ let mut o_event = convert_event ( crossterm:: event:: read ( ) ?) ;
459+ while let Some ( event) = o_event {
460+ o_event = match app. update ( event) {
461+ Action :: Nothing => None ,
462+ Action :: Render => {
463+ render ( & mut terminal, & app) ?;
464+ None
465+ }
466+ Action :: Quit => return Ok ( vec ! [ ] ) ,
467+ Action :: Generate ( paths) => return Ok ( paths) ,
468+ Action :: GetParentEntries ( path_id) => {
469+ let parent_id = cache. get_parent_id ( path_id) ?
470+ . expect ( "The UI requested a GetParentEntries with a path_id that does not exist" ) ;
471+ let entries = cache. get_entries ( parent_id) ?;
472+ Some ( Event :: Entries { path_id : parent_id, entries } )
473+ }
474+ Action :: GetEntries ( path_id) => {
475+ let entries = cache. get_entries ( path_id) ?;
476+ Some ( Event :: Entries { path_id, entries } )
477+ }
478+ Action :: GetEntryDetails ( path_id) =>
479+ Some ( Event :: EntryDetails ( cache. get_entry_details ( path_id) ?
480+ . expect ( "The UI requested a GetEntryDetails with a path_id that does not exist" ) ) ) ,
481+ Action :: UpsertMark ( path) => {
482+ cache. upsert_mark ( & path) ?;
483+ Some ( Event :: Marks ( cache. get_marks ( ) ?) )
484+ }
485+ Action :: DeleteMark ( loc) => {
486+ cache. delete_mark ( & loc) . unwrap ( ) ;
487+ Some ( Event :: Marks ( cache. get_marks ( ) ?) )
488+ }
489+ Action :: DeleteAllMarks => {
490+ cache. delete_all_marks ( ) ?;
491+ Some ( Event :: Marks ( Vec :: new ( ) ) )
492+ }
493+ }
494+ }
495+ }
496+ }
497+
500498fn render < ' a > (
501499 terminal : & ' a mut Terminal < impl Backend > ,
502500 app : & App ,
0 commit comments