Skip to content

Commit 556c79f

Browse files
author
cht
committed
tomorrow will finish
1 parent 68f9369 commit 556c79f

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.rs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};
2626
use unicode_width::UnicodeWidthStr;
2727
mod spider;
2828
enum 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
}
4346
impl 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

src/spider.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,3 @@ pub fn get_the_key(paths: Vec<String>) -> Result<Vec<Vec<String>>> {
3838
}
3939
Ok(output)
4040
}
41-

0 commit comments

Comments
 (0)