-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathmain.rs
More file actions
169 lines (159 loc) · 5.96 KB
/
Copy pathmain.rs
File metadata and controls
169 lines (159 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ANCHOR: all
use std::{error::Error, io};
use ratatui::{
backend::{Backend, CrosstermBackend},
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
Terminal,
};
mod app;
mod ui;
use crate::{
app::{App, CurrentScreen, EditFocus},
ui::ui,
};
// ANCHOR: main_all
// ANCHOR: setup_boilerplate
fn main() -> Result<(), Box<dyn Error>> {
// setup terminal
enable_raw_mode()?;
let mut stderr = io::stderr(); // This is a special case. Normally using stdout is fine
execute!(stderr, EnterAlternateScreen, EnableMouseCapture)?;
// ANCHOR_END: setup_boilerplate
// ANCHOR: application_startup
let backend = CrosstermBackend::new(stderr);
let mut terminal = Terminal::new(backend)?;
// create app and run it
let mut app = App::new();
let res = run_app(&mut terminal, &mut app);
// ANCHOR_END: application_startup
// ANCHOR: ending_boilerplate
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
// ANCHOR_END: ending_boilerplate
// ANCHOR: final_print
if let Ok(do_print) = res {
if do_print {
app.print_json()?;
}
} else if let Err(err) = res {
println!("{err:?}");
}
Ok(())
}
// ANCHOR_END: final_print
// ANCHOR_END: main_all
// ANCHOR: run_app_all
// ANCHOR: run_method_signature
fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<bool> {
// ANCHOR_END: run_method_signature
// ANCHOR: ui_loop
loop {
terminal.draw(|f| ui(f, app))?;
// ANCHOR_END: ui_loop
// ANCHOR: event_poll
// ANCHOR: main_screen
if let Event::Key(key) = event::read()? {
if key.kind == event::KeyEventKind::Release {
// Skip events that are not KeyEventKind::Press
continue;
}
match app.current_screen {
CurrentScreen::Main => match key.code {
KeyCode::Char('e') => {
app.current_screen = CurrentScreen::Editing;
app.start_editing();
}
KeyCode::Char('q') => {
app.current_screen = CurrentScreen::Exiting;
}
_ => {}
},
// ANCHOR_END: main_screen
// ANCHOR: exiting_screen
CurrentScreen::Exiting => match key.code {
KeyCode::Char('y') => {
return Ok(true);
}
KeyCode::Char('n') | KeyCode::Char('q') => {
return Ok(false);
}
_ => {}
},
// ANCHOR_END: exiting_screen
// ANCHOR: editing_enter
CurrentScreen::Editing if key.kind == KeyEventKind::Press => {
match key.code {
KeyCode::Enter => {
if let Some(editing_pair) = &mut app.editing_pair {
match editing_pair.focus {
EditFocus::Key => {
editing_pair.focus = EditFocus::Value;
}
EditFocus::Value => {
app.save_key_value();
app.current_screen = CurrentScreen::Main;
}
}
}
}
// ANCHOR_END: editing_enter
// ANCHOR: backspace_editing
KeyCode::Backspace => {
if let Some(editing_pair) = &mut app.editing_pair {
match editing_pair.focus {
EditFocus::Key => {
editing_pair.key.pop();
}
EditFocus::Value => {
editing_pair.value.pop();
}
}
}
}
// ANCHOR_END: backspace_editing
// ANCHOR: escape_editing
KeyCode::Esc => {
app.current_screen = CurrentScreen::Main;
app.editing_pair = None;
}
// ANCHOR_END: escape_editing
// ANCHOR: tab_editing
KeyCode::Tab => {
app.toggle_editing();
}
// ANCHOR_END: tab_editing
// ANCHOR: character_editing
KeyCode::Char(value) => {
if let Some(editing_pair) = &mut app.editing_pair {
match editing_pair.focus {
EditFocus::Key => {
editing_pair.key.push(value);
}
EditFocus::Value => {
editing_pair.value.push(value);
}
}
}
}
// ANCHOR_END: character_editing
_ => {}
}
}
_ => {}
}
}
// ANCHOR_END: event_poll
}
}
// ANCHOR: run_app_all
// ANCHOR_END: all