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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use crate::{app::App, util::parse_offset, widgets::MessageType};
use ratatui::{
Frame,
widgets::{Clear, Paragraph},
};
use crate::{editor::UIState, widgets::Message};
use crate::app::Dz6Error;
use clap::{Parser, Subcommand};
use ratatui::crossterm::event::{Event, KeyCode};
use std::io::Result;
use tui_input::backend::crossterm::EventHandler;
pub struct Commands;
#[derive(Subcommand, Debug)]
enum Command {
Q,
W,
Wq,
X,
Cmt {
offset: String,
comment: String,
},
Set {
option: String,
value: Option<String>,
},
Sel {
start: String,
length: String,
},
}
#[derive(Parser, Debug)]
struct CommandLine {
#[clap(subcommand)]
command: Option<Command>,
}
#[derive(Debug, PartialEq)]
enum OffsetType {
Backward,
Absolute,
Forward,
}
fn try_goto(app: &mut App, offset: &str) {
let offset_direction;
let mut new_offset = offset;
if let Some(offset_stripped) = offset.strip_prefix('-') {
offset_direction = OffsetType::Backward;
new_offset = offset_stripped;
} else if let Some(offset_stripped) = offset.strip_prefix('+') {
offset_direction = OffsetType::Forward;
new_offset = offset_stripped;
} else {
offset_direction = OffsetType::Absolute;
}
if let Ok(mut ofs) = parse_offset(new_offset) {
if offset_direction == OffsetType::Forward {
ofs = app.hex_view.offset.saturating_add(ofs);
} else if offset_direction == OffsetType::Backward {
ofs = app.hex_view.offset.saturating_sub(ofs);
}
if ofs < app.file_info.size {
app.dialog_renderer = None;
app.state = UIState::Normal;
app.goto(ofs);
} else {
app.last_error = Dz6Error {
message: format!(
"Invalid range: {}; maximum offset for this file is {}",
offset,
app.file_info.size.saturating_sub(1)
),
};
app.dialog_renderer = Some(command_error_draw);
}
} else {
app.last_error = Dz6Error {
message: format!("Not an editor command: {}", offset),
};
app.dialog_renderer = Some(command_error_draw);
}
}
pub fn parse_command(app: &mut App, cmdline: &str) {
if cmdline.is_empty() {
app.state = UIState::Normal;
app.dialog_renderer = None;
return;
}
let args = shell_words::split(cmdline).unwrap_or_default();
let mut argv: Vec<&str> = Vec::with_capacity(args.len() + 1);
argv.push("dz6");
for s in args.iter() {
argv.push(s.as_str());
}
match CommandLine::try_parse_from(argv) {
Ok(cli) => match cli.command {
// quit
Some(Command::Q) => app.running = false,
// write to file
Some(Command::W) => {
let _ = app.write_to_file();
if app.config.database {
let _ = app.save_database();
}
app.dialog_renderer = None;
app.state = UIState::Normal;
}
// write and quit
Some(Command::Wq) | Some(Command::X) => {
let _ = app.write_to_file();
if app.config.database {
let _ = app.save_database();
}
app.dialog_renderer = None;
app.running = false;
}
// comment <offset> <comment>
Some(Command::Cmt { offset, comment }) => {
if let Ok(mut ofs) = parse_offset(&offset) {
if offset.starts_with('+') {
ofs = ofs.saturating_add(app.hex_view.offset);
}
if ofs < app.file_info.size {
Commands::comment(app, ofs, comment);
app.dialog_renderer = None;
} else {
app.last_error = Dz6Error {
message: format!(
"Invalid range: {}; maximum offset for this file is {}",
cmdline,
app.file_info.size.saturating_sub(1)
),
};
app.dialog_renderer = Some(command_error_draw);
}
} else {
app.last_error = Dz6Error {
message: format!("Invalid argument: {}", offset),
};
app.dialog_renderer = Some(command_error_draw);
}
app.state = UIState::Normal;
}
// set
Some(Command::Set { option, value }) => {
match option.as_str() {
// bytes per line
"byteline" => {
if let Some(val) = value {
if let Ok(bpl) = val.parse::<usize>() {
// Bound user typed value by `max` - 1
if app.screen.width > 0 {
let max = ((app.screen.width - 9) / 4) as usize;
app.config.hex_mode_bytes_per_line = bpl.min(max - 1);
} else {
app.config.hex_mode_bytes_per_line = bpl.min(64);
}
app.config.hex_mode_bytes_per_line_auto = false;
} else if let Ok(bpl) = val.parse::<String>()
&& bpl == "auto"
{
app.config.hex_mode_bytes_per_line_auto = true;
if app.screen.width > 0 {
let max = ((app.screen.width - 9) / 4) as usize;
app.config.hex_mode_bytes_per_line = max - 1;
}
}
}
app.dialog_renderer = None;
}
// control / non-graphic bytes
"ctrlchar" => {
if let Some(val) = value
&& val.len() == 1
{
let c = val.chars().next().unwrap_or('.');
app.config.hex_mode_non_graphic_char = c;
}
app.dialog_renderer = None;
}
// save database files <filename>.dz6
"db" => {
app.config.database = true;
app.dialog_renderer = None;
}
"nodb" => {
app.config.database = false;
app.dialog_renderer = None;
}
// dim (gray out) control bytes
"dimctrl" => {
app.config.dim_control_chars = true;
app.dialog_renderer = None;
}
// dim null bytes only
"dimzero" => {
app.config.dim_control_chars = false;
app.config.dim_zeroes = true;
app.dialog_renderer = None;
}
"nodim" => {
app.config.dim_control_chars = false;
app.config.dim_zeroes = false;
app.dialog_renderer = None;
}
// theme
"theme" => {
if let Some(val) = value {
match val.as_str() {
"dark" => {
app.config.theme = crate::themes::DARK;
app.dialog_renderer = None;
}
"light" => {
app.config.theme = crate::themes::LIGHT;
app.dialog_renderer = None;
}
_ => {
app.last_error = Dz6Error {
message: format!("Invalid theme: {}", val),
};
app.dialog_renderer = Some(command_error_draw);
}
}
}
}
// search wrap
"wrapscan" | "ws" => {
app.config.search_wrapscan = true;
app.dialog_renderer = None;
}
"nowrapscan" | "nows" => {
app.config.search_wrapscan = false;
app.dialog_renderer = None;
}
// search ignorecase
"ignorecase" | "ic" => {
app.config.search_ignorecase = true;
app.dialog_renderer = None;
}
"noignorecase" | "noic" => {
app.config.search_ignorecase = false;
app.dialog_renderer = None;
}
// search smartcase
// if ignorecase == true and needle contains a capital letter,
// then ignorecase will be set to false
"smartcase" | "scs" => {
app.config.search_smartcase = true;
app.dialog_renderer = None;
}
"nosmartcase" | "noscs" => {
app.config.search_smartcase = false;
app.dialog_renderer = None;
}
// view
"view" => {
if let Some(val) = value {
match val.as_str() {
"text" => {
app.editor_view = crate::editor::AppView::Text;
app.dialog_renderer = None;
}
"hex" => {
app.editor_view = crate::editor::AppView::Hex;
app.dialog_renderer = None;
}
"header" => {
app.editor_view = crate::editor::AppView::Header;
app.dialog_renderer = None;
}
_ => {}
}
}
}
_ => {
app.dialog_renderer = None;
}
}
app.state = UIState::Normal;
}
Some(Command::Sel { start, length }) => {
app.state = UIState::HexSelection;
app.dialog_renderer = None;
if let Ok(st) = parse_offset(&start)
&& let Ok(len) = parse_offset(&length)
{
app.hex_view.selection.start = st;
app.hex_view.selection.end = st.saturating_add(len);
app.goto(st);
}
}
None => {
try_goto(app, cmdline);
}
},
Err(_) => {
// goto as :offset
try_goto(app, cmdline);
}
}
}
// command bar
pub fn command_draw(app: &mut App, frame: &mut Frame) {
let para = Paragraph::new(format!(":{}", app.command_input.input.value()));
frame.render_widget(Clear, app.command_area);
frame.render_widget(para, app.command_area);
let x = app.command_input.input.visual_cursor();
frame.set_cursor_position((app.command_area.x + 1 + x as u16, app.command_area.y));
}
pub fn command_events(app: &mut App, event: &Event) -> Result<bool> {
if let Event::Key(key) = event {
match key.code {
KeyCode::Esc => {
app.dialog_renderer = None;
app.state = UIState::Normal;
}
KeyCode::Enter => {
let v = app.command_input.input.value_and_reset();
parse_command(app, &v);
app.command_input.push(v);
}
KeyCode::Up => {
app.command_input.up();
}
KeyCode::Down => {
app.command_input.down();
}
_ => {
app.command_input.input.handle_event(event);
}
}
}
Ok(false)
}
pub fn command_error_draw(app: &mut App, frame: &mut Frame) {
let mut dialog = Message::from(&app.last_error.message);
dialog.kind = MessageType::Error;
dialog.render(app, frame);
app.state = UIState::Error;
}