1- use std:: time:: { Duration , Instant } ;
1+ use std:: {
2+ path:: PathBuf ,
3+ time:: { Duration , Instant } ,
4+ } ;
25
36use crossterm:: event:: { self , Event , KeyCode , KeyEventKind , KeyModifiers , MouseEventKind } ;
47use ratatui:: {
@@ -10,6 +13,7 @@ use ratatui::{
1013
1114use crate :: {
1215 exit:: ExitHandler ,
16+ model:: { recent:: RecentFiles , table_path:: resolve_table_path} ,
1317 screen:: { EventResult , FrameState , Screen , ScreenCommand , home:: MenuScreen } ,
1418 theme:: Theme ,
1519 widget:: footer:: Footer ,
@@ -44,25 +48,33 @@ impl BlinkState {
4448
4549pub struct App {
4650 theme : Theme ,
47- cwd : String ,
51+ cwd : PathBuf ,
52+ recent : RecentFiles ,
4853 active_screen : Box < dyn Screen > ,
4954 exit_handler : ExitHandler ,
5055 blink : BlinkState ,
5156 footer : Footer ,
5257}
5358
5459impl App {
55- pub fn new ( ) -> Self {
56- let full_cwd = std:: env:: current_dir ( )
57- . map ( |p| p. display ( ) . to_string ( ) )
58- . unwrap_or_else ( |_| String :: from ( "." ) ) ;
59- let display_cwd = replace_homedir:: replace_homedir ( & full_cwd, "~" ) ;
60+ pub fn new ( initial_file : Option < String > ) -> Self {
61+ let cwd = std:: env:: current_dir ( ) . unwrap_or_else ( |_| PathBuf :: from ( "." ) ) ;
62+ let display_cwd = replace_homedir:: replace_homedir ( & cwd. display ( ) . to_string ( ) , "~" ) ;
6063 let theme = Theme :: dark ( ) ;
64+ let mut recent = RecentFiles :: load ( ) ;
65+ let active_screen: Box < dyn Screen > = if let Some ( file) = initial_file {
66+ let path = resolve_table_path ( & file, & cwd) ;
67+ recent. add ( & path) ;
68+ Box :: new ( crate :: screen:: editor:: TableScreen :: new ( theme, path) )
69+ } else {
70+ Box :: new ( MenuScreen :: new ( theme, cwd. clone ( ) , recent. items ( ) . to_vec ( ) ) )
71+ } ;
6172
6273 Self {
6374 theme,
64- cwd : full_cwd. clone ( ) ,
65- active_screen : Box :: new ( MenuScreen :: new ( theme, full_cwd) ) ,
75+ cwd,
76+ recent,
77+ active_screen,
6678 exit_handler : ExitHandler :: new ( Duration :: from_secs ( 1 ) ) ,
6779 blink : BlinkState :: new ( ) ,
6880 footer : Footer :: new ( display_cwd, APP_VERSION . to_string ( ) , theme) ,
@@ -151,11 +163,24 @@ impl App {
151163 fn process_cmd ( & mut self , cmd : ScreenCommand ) {
152164 match cmd {
153165 ScreenCommand :: OpenEditor { path } => {
166+ self . recent . add ( & path) ;
154167 self . active_screen =
155168 Box :: new ( super :: screen:: editor:: TableScreen :: new ( self . theme , path) ) ;
156169 }
170+ ScreenCommand :: RemoveRecent { path } => {
171+ self . recent . remove ( & path) ;
172+ self . active_screen = Box :: new ( MenuScreen :: new (
173+ self . theme ,
174+ self . cwd . clone ( ) ,
175+ self . recent . items ( ) . to_vec ( ) ,
176+ ) ) ;
177+ }
157178 ScreenCommand :: GoHome => {
158- self . active_screen = Box :: new ( MenuScreen :: new ( self . theme , self . cwd . clone ( ) ) ) ;
179+ self . active_screen = Box :: new ( MenuScreen :: new (
180+ self . theme ,
181+ self . cwd . clone ( ) ,
182+ self . recent . items ( ) . to_vec ( ) ,
183+ ) ) ;
159184 }
160185 }
161186 }
0 commit comments