@@ -20,14 +20,15 @@ use tui::{
2020 layout:: { Constraint , Direction , Layout } ,
2121 style:: { Color , Modifier , Style } ,
2222 text:: { Span , Spans , Text } ,
23- widgets:: { Block , Borders , List , ListItem , Paragraph , ListState } ,
23+ widgets:: { Block , Borders , List , ListItem , ListState , Paragraph } ,
2424 Frame , Terminal ,
2525} ;
2626use unicode_width:: UnicodeWidthStr ;
2727mod spider;
2828enum InputMode {
2929 Normal ,
3030 Editing ,
31+ Select ,
3132}
3233
3334/// App holds the state of the application
@@ -39,6 +40,8 @@ struct App {
3940 /// History of recorded messages
4041 messages : Vec < String > ,
4142 state : ListState ,
43+ index : Option < usize > ,
44+ stateoflist : bool ,
4245}
4346impl App {
4447 fn next ( & mut self ) {
@@ -53,6 +56,7 @@ impl App {
5356 None => 0 ,
5457 } ;
5558 self . state . select ( Some ( i) ) ;
59+ self . index = Some ( i) ;
5660 }
5761
5862 fn previous ( & mut self ) {
@@ -67,6 +71,7 @@ impl App {
6771 None => 0 ,
6872 } ;
6973 self . state . select ( Some ( i) ) ;
74+ self . index = Some ( i) ;
7075 }
7176
7277 fn unselect ( & mut self ) {
@@ -80,6 +85,8 @@ impl Default for App {
8085 input_mode : InputMode :: Normal ,
8186 messages : Vec :: new ( ) ,
8287 state : ListState :: default ( ) ,
88+ index : None ,
89+ stateoflist : false ,
8390 }
8491 }
8592}
@@ -122,16 +129,25 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
122129 KeyCode :: Char ( 'e' ) => {
123130 app. input_mode = InputMode :: Editing ;
124131 }
132+ KeyCode :: Char ( 's' ) => {
133+ app. input_mode = InputMode :: Select ;
134+ }
125135 KeyCode :: Char ( 'q' ) => {
126136 return Ok ( ( ) ) ;
127137 }
128138 _ => { }
129139 } ,
130140 InputMode :: Editing => match key. code {
131141 KeyCode :: Enter => {
132- let input = vec ! [ app. input. clone( ) ] ;
133- app. messages = spider:: get_the_key ( input. clone ( ) ) . unwrap ( ) [ 0 ] . clone ( ) ;
134- app. state . select ( Some ( 0 ) ) ;
142+ let input = vec ! [ app. input. clone( ) ] ;
143+ let get_list = spider:: get_the_key ( input. clone ( ) ) ;
144+ if let Ok ( list) = get_list {
145+ if !list. is_empty ( ) {
146+ app. messages = list[ 0 ] . clone ( ) ;
147+ app. stateoflist = true ;
148+ app. state . select ( Some ( 0 ) ) ;
149+ }
150+ }
135151 //app.messages.push(app.input.drain(..).collect());
136152 }
137153 KeyCode :: Char ( c) => {
@@ -143,11 +159,26 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
143159 KeyCode :: Esc => {
144160 app. input_mode = InputMode :: Normal ;
145161 }
146- KeyCode :: Left => app. unselect ( ) ,
147- KeyCode :: Down => app. next ( ) ,
148- KeyCode :: Up => app. previous ( ) ,
149162 _ => { }
150163 } ,
164+ InputMode :: Select => {
165+ if app. stateoflist {
166+ match key. code {
167+ KeyCode :: Left => app. unselect ( ) ,
168+ KeyCode :: Down => app. next ( ) ,
169+ KeyCode :: Up => app. previous ( ) ,
170+ KeyCode :: Esc => {
171+ app. input_mode = InputMode :: Normal ;
172+ }
173+ KeyCode :: Enter => {
174+ app. input = app. index . as_ref ( ) . unwrap ( ) . to_string ( ) ;
175+ }
176+ _ => { }
177+ }
178+ } else {
179+ app. input_mode = InputMode :: Normal ;
180+ }
181+ }
151182 }
152183 }
153184 }
@@ -175,9 +206,21 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
175206 Span :: raw( " to exit, " ) ,
176207 Span :: styled( "e" , Style :: default ( ) . add_modifier( Modifier :: BOLD ) ) ,
177208 Span :: raw( " to start editing." ) ,
209+ Span :: styled( "s" , Style :: default ( ) . add_modifier( Modifier :: BOLD ) ) ,
210+ Span :: raw( " to select trees" ) ,
178211 ] ,
179212 Style :: default ( ) . add_modifier ( Modifier :: RAPID_BLINK ) ,
180213 ) ,
214+ InputMode :: Select => (
215+ vec ! [
216+ Span :: raw( "Press " ) ,
217+ Span :: styled( "Esc" , Style :: default ( ) . add_modifier( Modifier :: BOLD ) ) ,
218+ Span :: raw( " to stop editing, " ) ,
219+ Span :: styled( "Enter" , Style :: default ( ) . add_modifier( Modifier :: BOLD ) ) ,
220+ Span :: raw( " to record the message" ) ,
221+ ] ,
222+ Style :: default ( ) ,
223+ ) ,
181224 InputMode :: Editing => (
182225 vec ! [
183226 Span :: raw( "Press " ) ,
@@ -196,13 +239,13 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
196239
197240 let input = Paragraph :: new ( app. input . as_ref ( ) )
198241 . style ( match app. input_mode {
199- InputMode :: Normal => Style :: default ( ) ,
242+ InputMode :: Normal | InputMode :: Select => Style :: default ( ) ,
200243 InputMode :: Editing => Style :: default ( ) . fg ( Color :: Yellow ) ,
201244 } )
202245 . block ( Block :: default ( ) . borders ( Borders :: ALL ) . title ( "Input" ) ) ;
203246 f. render_widget ( input, chunks[ 1 ] ) ;
204247 match app. input_mode {
205- InputMode :: Normal =>
248+ InputMode :: Normal | InputMode :: Select =>
206249 // Hide the cursor. `Frame` does this by default, so we don't need to do anything here
207250 { }
208251
0 commit comments