@@ -26,6 +26,11 @@ pub struct TerminalApp {
2626 pub state : DisplayState ,
2727}
2828
29+ enum CursorDirection {
30+ Down ,
31+ Up ,
32+ }
33+
2934impl TerminalApp {
3035 fn draw < B > ( & self , terminal : & mut Terminal < B > ) -> Result < ( ) , Error >
3136 where
@@ -63,24 +68,10 @@ impl TerminalApp {
6368 self . draw ( terminal) ?;
6469 for key in keys. filter_map ( Result :: ok) {
6570 match key {
66- Char ( 'j' ) => {
67- let entries =
68- sorted_entries ( & self . traversal . tree , self . state . root , self . state . sorting ) ;
69- let next_selected_pos = match self . state . selected {
70- Some ( ref selected) => entries
71- . iter ( )
72- . find_position ( |( idx, _) | * idx == * selected)
73- . map ( |( idx, _) | idx + 1 )
74- . unwrap_or ( 0 ) ,
75- None => 0 ,
76- } ;
77- self . state . selected = match entries. get ( next_selected_pos) {
78- Some ( ( idx, _) ) => Some ( * idx) ,
79- None => self . state . selected ,
80- } ;
81- }
71+ Char ( 'k' ) => self . change_vertical_index ( CursorDirection :: Up ) ,
72+ Char ( 'j' ) => self . change_vertical_index ( CursorDirection :: Down ) ,
8273 Char ( 's' ) => self . state . sorting . toggle_size ( ) ,
83- Ctrl ( 'c' ) | Char ( '\n' ) | Char ( ' q') => break ,
74+ Ctrl ( 'c' ) | Char ( 'q' ) => break ,
8475 _ => { }
8576 } ;
8677 self . draw ( terminal) ?;
@@ -90,6 +81,25 @@ impl TerminalApp {
9081 } )
9182 }
9283
84+ fn change_vertical_index ( & mut self , direction : CursorDirection ) -> ( ) {
85+ let entries = sorted_entries ( & self . traversal . tree , self . state . root , self . state . sorting ) ;
86+ let next_selected_pos = match self . state . selected {
87+ Some ( ref selected) => entries
88+ . iter ( )
89+ . find_position ( |( idx, _) | * idx == * selected)
90+ . map ( |( idx, _) | match direction {
91+ CursorDirection :: Down => idx. saturating_add ( 1 ) ,
92+ CursorDirection :: Up => idx. saturating_sub ( 1 ) ,
93+ } )
94+ . unwrap_or ( 0 ) ,
95+ None => 0 ,
96+ } ;
97+ self . state . selected = match entries. get ( next_selected_pos) {
98+ Some ( ( idx, _) ) => Some ( * idx) ,
99+ None => self . state . selected ,
100+ } ;
101+ }
102+
93103 pub fn initialize < B > (
94104 terminal : & mut Terminal < B > ,
95105 options : WalkOptions ,
0 commit comments