@@ -27,6 +27,13 @@ const FORBIDDEN_REGEX_CHARS: &[char] = &[
2727 '*' , '+' , '?' , '|' , '{' , '}' , '(' , ')' , '[' , ']' , '\\' , '^' , '$' ,
2828] ;
2929
30+ enum QueryInputAction {
31+ None ,
32+ Cancel ,
33+ Submit ( String ) ,
34+ Invalid ( String ) ,
35+ }
36+
3037pub fn is_quit ( event : KeyEvent ) -> bool {
3138 event. code == KeyCode :: F ( 10 )
3239 || ( event. code == KeyCode :: Char ( 'c' ) && event. modifiers . contains ( KeyModifiers :: CONTROL ) )
@@ -104,10 +111,10 @@ fn handle_enter(
104111 let absolute = event. modifiers . contains ( KeyModifiers :: SHIFT ) ;
105112 let _ = file_ops:: copy_to_clipboard ( clipboard, panel, absolute) ;
106113 } else if !navigate:: enter_selected ( fs, panel)
107- && let Some ( path ) = panel. get_selected_path ( )
108- && fs . is_file ( & path )
114+ && let Some ( entry ) = panel. entries . get ( panel . cursor )
115+ && entry . is_file ( )
109116 {
110- open. open ( & path) ;
117+ open. open ( & entry . path ) ;
111118 }
112119}
113120
@@ -175,19 +182,12 @@ pub fn handle_find_input(
175182 state : & mut AppState ,
176183) {
177184 if let Some ( filter) = find_filter {
178- match event. code {
179- KeyCode :: Char ( c ) => filter . push ( c ) ,
180- KeyCode :: Backspace => {
181- filter . pop ( ) ;
185+ match handle_query_input_event ( event, filter ) {
186+ QueryInputAction :: None => { }
187+ QueryInputAction :: Invalid ( message ) => {
188+ state . active_panel_mut ( ) . set_notification ( message )
182189 }
183- KeyCode :: Enter => {
184- let query = match validate_query ( filter) {
185- Ok ( query) => query,
186- Err ( message) => {
187- state. active_panel_mut ( ) . set_notification ( message) ;
188- return ;
189- }
190- } ;
190+ QueryInputAction :: Submit ( query) => {
191191 let sender = sender. clone ( ) ;
192192 let pane = state. active_pane ;
193193 let pwd = state. active_panel ( ) . current_path . clone ( ) ;
@@ -204,11 +204,10 @@ pub fn handle_find_input(
204204 } ) ;
205205 * find_filter = None ;
206206 }
207- KeyCode :: Esc => {
207+ QueryInputAction :: Cancel => {
208208 * find_filter = None ;
209209 navigate:: refresh_entries ( fs, state. active_panel_mut ( ) ) ;
210210 }
211- _ => { }
212211 }
213212 }
214213}
@@ -220,35 +219,46 @@ pub fn handle_ripgrep_input(
220219 state : & mut AppState ,
221220) {
222221 if let Some ( f) = filter {
223- match event. code {
224- KeyCode :: Char ( c ) => f . push ( c ) ,
225- KeyCode :: Backspace => {
226- f . pop ( ) ;
222+ match handle_query_input_event ( event, f ) {
223+ QueryInputAction :: None => { }
224+ QueryInputAction :: Invalid ( message ) => {
225+ state . active_panel_mut ( ) . set_notification ( message )
227226 }
228- KeyCode :: Enter => {
229- let query = match validate_query ( f) {
230- Ok ( query) => query,
231- Err ( message) => {
232- state. active_panel_mut ( ) . set_notification ( message) ;
233- return ;
234- }
235- } ;
227+ QueryInputAction :: Submit ( query) => {
236228 let results =
237229 RipGrepAdapter :: new ( ) . find ( & query, & state. active_panel ( ) . current_path ) ;
238230 let panel = state. active_panel_mut ( ) ;
239231 let base = panel. current_path . clone ( ) ;
240232 navigate:: replace_entries_from_search ( panel, results, & base) ;
241233 * filter = None ;
242234 }
243- KeyCode :: Esc => {
235+ QueryInputAction :: Cancel => {
244236 * filter = None ;
245237 navigate:: refresh_entries ( fs, state. active_panel_mut ( ) ) ;
246238 }
247- _ => { }
248239 }
249240 }
250241}
251242
243+ fn handle_query_input_event ( event : KeyEvent , input : & mut String ) -> QueryInputAction {
244+ match event. code {
245+ KeyCode :: Char ( c) => {
246+ input. push ( c) ;
247+ QueryInputAction :: None
248+ }
249+ KeyCode :: Backspace => {
250+ input. pop ( ) ;
251+ QueryInputAction :: None
252+ }
253+ KeyCode :: Enter => match validate_query ( input) {
254+ Ok ( query) => QueryInputAction :: Submit ( query) ,
255+ Err ( message) => QueryInputAction :: Invalid ( message) ,
256+ } ,
257+ KeyCode :: Esc => QueryInputAction :: Cancel ,
258+ _ => QueryInputAction :: None ,
259+ }
260+ }
261+
252262fn validate_query ( raw : & str ) -> Result < String , String > {
253263 let query = raw. trim ( ) ;
254264 if query. is_empty ( ) {
0 commit comments