forked from DragonOS-Community/Held
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode.rs
700 lines (593 loc) · 22.9 KB
/
mode.rs
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
use std::io::Read;
use std::sync::atomic::Ordering;
use std::sync::{Mutex, MutexGuard};
use std::{fmt::Debug, io};
use crate::config::lastline_cmd::LastLineCommand;
use crate::utils::buffer::LineState;
#[cfg(feature = "dragonos")]
use crate::utils::input::KeyEventType;
use crate::utils::terminal::TermManager;
use crate::utils::ui::uicore::{UiCore, APP_INFO, TAB_SIZE};
use crate::utils::ui::{
event::KeyEventCallback,
uicore::{CONTENT_WINSIZE, DEF_STYLE},
};
use crate::utils::ui::event::WarpUiCallBackType;
pub trait InputMode: KeyEventCallback + Debug {
fn mode_type(&self) -> ModeType;
#[cfg(not(feature = "dragonos"))]
fn event_route(
&self,
ui: &mut MutexGuard<UiCore>,
event: crossterm::event::Event,
) -> io::Result<WarpUiCallBackType> {
match event {
crossterm::event::Event::FocusGained => todo!(),
crossterm::event::Event::FocusLost => todo!(),
crossterm::event::Event::Key(key) => self.key_event_route(ui, key),
crossterm::event::Event::Mouse(_) => todo!(),
crossterm::event::Event::Paste(_) => todo!(),
crossterm::event::Event::Resize(_, _) => todo!(),
}
}
#[cfg(not(feature = "dragonos"))]
fn key_event_route(
&self,
ui: &mut MutexGuard<UiCore>,
keyev: crossterm::event::KeyEvent,
) -> io::Result<WarpUiCallBackType> {
let callback = match keyev.code {
crossterm::event::KeyCode::Backspace => self.backspace(ui)?,
crossterm::event::KeyCode::Enter => self.enter(ui)?,
crossterm::event::KeyCode::Left => self.left(ui)?,
crossterm::event::KeyCode::Right => self.right(ui)?,
crossterm::event::KeyCode::Up => self.up(ui)?,
crossterm::event::KeyCode::Down => self.down(ui)?,
crossterm::event::KeyCode::Home => todo!(),
crossterm::event::KeyCode::End => todo!(),
crossterm::event::KeyCode::PageUp => todo!(),
crossterm::event::KeyCode::PageDown => todo!(),
crossterm::event::KeyCode::Tab => self.tab(ui)?,
crossterm::event::KeyCode::BackTab => todo!(),
crossterm::event::KeyCode::Delete => todo!(),
crossterm::event::KeyCode::Insert => todo!(),
crossterm::event::KeyCode::F(_) => todo!(),
crossterm::event::KeyCode::Char(c) => self.input_data(ui, &[c as u8])?,
crossterm::event::KeyCode::Null => todo!(),
crossterm::event::KeyCode::Esc => self.esc(ui)?,
crossterm::event::KeyCode::CapsLock => todo!(),
crossterm::event::KeyCode::ScrollLock => todo!(),
crossterm::event::KeyCode::NumLock => todo!(),
crossterm::event::KeyCode::PrintScreen => todo!(),
crossterm::event::KeyCode::Pause => todo!(),
crossterm::event::KeyCode::Menu => todo!(),
crossterm::event::KeyCode::KeypadBegin => todo!(),
crossterm::event::KeyCode::Media(_) => todo!(),
crossterm::event::KeyCode::Modifier(_) => todo!(),
};
Ok(callback)
}
#[cfg(feature = "dragonos")]
fn key_event_route(
&self,
ui: &mut MutexGuard<UiCore>,
key: KeyEventType,
) -> io::Result<WarpUiCallBackType> {
match key {
KeyEventType::Common(c) => self.input_data(ui, &[c]),
KeyEventType::Up => self.up(ui),
KeyEventType::Down => self.down(ui),
KeyEventType::Right => self.right(ui),
KeyEventType::Left => self.left(ui),
KeyEventType::Enter => self.enter(ui),
KeyEventType::Tab => self.tab(ui),
KeyEventType::Backspace => self.backspace(ui),
KeyEventType::Esc => self.esc(ui),
KeyEventType::Unknown(_) => {
ui.update_bottom_state_bar()?;
Ok(WarpUiCallBackType::None)
}
}
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ModeType {
Command,
LastLine,
Insert,
}
impl InputMode for Command {
fn mode_type(&self) -> ModeType {
ModeType::Command
}
}
impl InputMode for LastLine {
fn mode_type(&self) -> ModeType {
ModeType::LastLine
}
}
impl InputMode for Insert {
fn mode_type(&self) -> ModeType {
ModeType::Insert
}
}
#[derive(Debug)]
pub struct Command;
impl Command {
pub fn jump_to_next_flag(
&self,
ui: &mut MutexGuard<UiCore>,
flags: LineState,
) -> io::Result<()> {
let offset = ui.buffer.offset();
let y = ui.cursor.y() as usize;
let start_line_number = offset + y + 1;
if start_line_number >= ui.buffer.line_count() {
return Ok(());
}
let content = &ui.buffer.all_buffer()[start_line_number..];
// 下一个flaged位置
let idx = content.iter().position(|x| x.flags.contains(flags));
if idx.is_some() {
// y + idx
let line_number = start_line_number + idx.unwrap();
let new_y = ui.buffer.goto_line(line_number);
ui.render_content(0, CONTENT_WINSIZE.read().unwrap().rows as usize)?;
ui.cursor.move_to_row(new_y)?;
ui.cursor.highlight(Some(y as u16))?;
}
Ok(())
}
pub fn jump_to_previous_flag(
&self,
ui: &mut MutexGuard<UiCore>,
flags: LineState,
) -> io::Result<()> {
let offset = ui.buffer.offset();
let y = ui.cursor.y() as usize;
if offset == 0 && y == 0 {
return Ok(());
}
let end_linenumber = offset + y - 1;
let content = &ui.buffer.all_buffer()[0..end_linenumber];
// 下一个flaged位置
let idx = content.iter().rposition(|x| x.flags.contains(flags));
if idx.is_some() {
// y + idx
let new_y = ui.buffer.goto_line(idx.unwrap());
ui.render_content(0, CONTENT_WINSIZE.read().unwrap().rows as usize)?;
ui.cursor.move_to_row(new_y)?;
ui.cursor.highlight(Some(y as u16))?;
}
Ok(())
}
fn jump_to_first_char(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
// 移动到行第一个单词的首字母
let first_char = {
let line = ui.buffer.get_line(ui.cursor.y()).data;
let mut idx = 0;
for char in line {
if char == b" "[0] {
idx += 1;
} else if char == b"\t"[0] {
idx += 4;
}
}
idx
};
ui.cursor.move_to_columu(first_char)?;
return Ok(WarpUiCallBackType::None);
}
fn do_delete_on_d_clicked(
&self,
ui: &mut MutexGuard<UiCore>,
) -> io::Result<WarpUiCallBackType> {
let buf: &mut [u8] = &mut [0; 8];
let _ = io::stdin().read(buf)?;
match buf[0] {
b'd' => {
TermManager::clear_under_cursor()?;
let y = ui.cursor.y() as usize;
ui.buffer.delete_line(y);
let count = ui.buffer.line_count() - y as usize - 1;
ui.render_content(y as u16, count)?;
}
b'0' => {
let x = ui.cursor.x() as usize;
let y = ui.cursor.y() as usize;
match ui.buffer.delete_until_line_beg(x, y) {
Some(..) => {
// 文本变动重新渲染
ui.cursor.move_to_columu(0)?;
ui.render_content(y as u16, 1)?;
}
None => {}
};
}
b'$' => {
let x = ui.cursor.x() as usize;
let y = ui.cursor.y() as usize;
match ui.buffer.delete_until_endl(x, y) {
Some(..) => {
ui.cursor.move_left(1)?;
ui.render_content(y as u16, 1)?;
}
None => {}
}
}
b'w' | b'e' => {
let x = ui.cursor.x();
let y = ui.cursor.y();
let next_word_pos = ui.buffer.search_nextw_beg(x, y);
let linesize = ui.buffer.get_linesize(y);
// 如果下一个单词在当前行,则删除当前单词
if next_word_pos < linesize.into() {
ui.buffer.remove_str(x, y, next_word_pos - x as usize);
} else {
// 如果下一个单词在下一行,则删除当前行剩余部分
self.left(ui)?;
ui.buffer.delete_until_endl(x.into(), y.into());
}
ui.render_content(y, 1)?;
}
b'b' => {
let old_x = ui.cursor.x();
let old_y = ui.cursor.y();
self.jump_to_prevw_beg(ui)?;
let x = ui.cursor.x();
let y = ui.cursor.y();
if old_y == y {
ui.buffer.remove_str(x, y, old_x as usize - x as usize);
ui.render_content(y, 1)?;
} else {
ui.buffer.delete_until_endl(x as usize, y as usize);
ui.buffer.delete_until_line_beg(old_x as usize, old_y as usize);
ui.buffer.merge_line(old_y);
let linecount = ui.buffer.line_count();
TermManager::clear_under_cursor()?;
ui.render_content(y, linecount - y as usize - 1)?;
}
}
_ => {}
}
return Ok(WarpUiCallBackType::None);
}
fn jump_to_next_word(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
let x = ui.cursor.x();
let y = ui.cursor.y();
let pos = ui.buffer.search_nextw_beg(x, y);
let linesize = ui.buffer.get_linesize(y);
if pos < linesize as usize {
// 如果下一个单词在当前行,则移动光标到该单词的起始位置
ui.cursor.move_to_columu(pos as u16)?;
} else if y + 1 < ui.buffer.line_count() as u16 {
// 如果当前行不是最后一行,则移动到下一行的开头
self.down(ui)?;
ui.cursor.move_to_columu(0)?;
} else {
// 如果当前行是最后一行,则移动到当前行的末尾
ui.cursor.move_to_columu(linesize as u16 - 1)?;
}
return Ok(WarpUiCallBackType::None);
}
fn jump_to_nextw_ending(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
let x = ui.cursor.x();
let y = ui.cursor.y();
let linesize = ui.buffer.get_linesize(y) as usize;
// 如果光标已经在当前行的末尾,则尝试移动到下一行的末尾或单词末尾
if x as usize >= linesize - 2 {
if y < ui.buffer.line_count() as u16 - 1 {
let next_end_pos = ui.buffer.search_nextw_end(0, y + 1) as u16;
ui.cursor.move_to(next_end_pos, y + 1)?;
ui.cursor.highlight(Some(y))?;
} else {
// 如果已经是最后一行,则保持光标在当前行的末尾
ui.cursor.move_to_columu(linesize as u16 - 1)?;
}
return Ok(WarpUiCallBackType::None);
}
let next_end_pos = ui.buffer.search_nextw_end(x, y) as u16;
// 如果下一个单词的末尾在当前行,则移动光标到该单词的末尾
ui.cursor.move_to_columu(next_end_pos.min(linesize as u16 - 2))?;
return Ok(WarpUiCallBackType::None);
}
fn jump_to_prevw_beg(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
let x = ui.cursor.x();
let y = ui.cursor.y();
// 如果光标已在行首,则尝试移动到上一行的单词首字母
if x == 0 {
if y > 0 {
let end_of_prev_line = ui.buffer.get_linesize(y - 1) - 1;
let prev_word_pos = match ui.buffer.search_prevw_beg(end_of_prev_line, y - 1) {
Some(pos) => pos,
None => 0
};
ui.cursor.move_to(prev_word_pos as u16, y - 1)?;
ui.cursor.highlight(Some(y))?;
} else {
// 如果已经是第一行,则保持光标在当前行的起始位置
ui.cursor.move_to_columu(0)?;
}
return Ok(WarpUiCallBackType::None);
}
let prev_word_pos = match ui.buffer.search_prevw_beg(x, y) {
Some(pos) => pos,
None => 0
};
ui.cursor.move_to(prev_word_pos as u16, y)?;
return Ok(WarpUiCallBackType::None);
}
}
impl KeyEventCallback for Command {
fn backspace(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::None)
}
fn enter(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::None)
}
fn tab(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::None)
}
fn esc(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::None)
}
fn input_data(
&self,
ui: &mut MutexGuard<UiCore>,
data: &[u8],
) -> io::Result<WarpUiCallBackType> {
match data {
b":" => {
// 保存位置
ui.cursor.store_pos();
return Ok(WarpUiCallBackType::ChangMode(ModeType::LastLine));
}
b"i" | b"I" => {
// 切换Insert模式
return Ok(WarpUiCallBackType::ChangMode(ModeType::Insert));
}
// hjkl 与 Vim 的效果一致
b"h" => self.left(ui),
// 向下
b"j" => self.down(ui),
// 向上
b"k" => self.up(ui),
// 向右
b"l" => self.right(ui),
b"L" => {
// 设置当前行lock
let flag = ui.buffer.line_flags(ui.cursor.y());
let offset = ui.buffer.offset();
if flag.contains(LineState::LOCKED) {
ui.buffer
.remove_line_flags(offset + ui.cursor.y() as usize, LineState::LOCKED);
} else {
ui.buffer
.add_line_flags(offset + ui.cursor.y() as usize, LineState::LOCKED);
}
let y = ui.cursor.y();
ui.render_content(y, 1)?;
return Ok(WarpUiCallBackType::None);
}
b"f" | b"F" => {
// 设置当前行flag
let flag = ui.buffer.line_flags(ui.cursor.y());
let offset = ui.buffer.offset();
if flag.contains(LineState::FLAGED) {
ui.buffer
.remove_line_flags(offset + ui.cursor.y() as usize, LineState::FLAGED);
} else {
ui.buffer
.add_line_flags(offset + ui.cursor.y() as usize, LineState::FLAGED);
}
let y = ui.cursor.y();
ui.render_content(y, 1)?;
return Ok(WarpUiCallBackType::None);
}
b"q" | b"Q" => {
// 跳转到上一个flag行
self.jump_to_previous_flag(ui, LineState::FLAGED)?;
return Ok(WarpUiCallBackType::None);
}
b"w" => self.jump_to_next_word(ui),
b"e" => self.jump_to_nextw_ending(ui),
b"b" => self.jump_to_prevw_beg(ui),
b"W" => {
// 跳转到下一个flag行
self.jump_to_next_flag(ui, LineState::FLAGED)?;
return Ok(WarpUiCallBackType::None);
}
b"a" | b"A" => {
self.jump_to_previous_flag(ui, LineState::LOCKED)?;
return Ok(WarpUiCallBackType::None);
}
b"s" | b"S" => {
self.jump_to_next_flag(ui, LineState::LOCKED)?;
return Ok(WarpUiCallBackType::None);
}
b"0" => {
// 移动到行首
ui.cursor.move_to_columu(0)?;
return Ok(WarpUiCallBackType::None);
}
b"^" => self.jump_to_first_char(ui),
b"$" => {
// 移动到行末
let line_end = ui.buffer.get_linesize(ui.cursor.y()) - 1;
ui.cursor.move_to_columu(line_end)?;
return Ok(WarpUiCallBackType::None);
}
b"d" => self.do_delete_on_d_clicked(ui),
b"x" => {
let y = ui.cursor.y();
let x = ui.cursor.x();
if x < ui.buffer.get_linesize(y) - 1 {
ui.buffer.remove_char(x, y);
ui.render_content(y, 1)?;
}
return Ok(WarpUiCallBackType::None);
}
b"G" => {
// 移动到最后一行
let line_count = ui.buffer.line_count() as u16;
let y = ui.cursor.y();
ui.scroll_down(line_count - y)?;
ui.cursor.move_to_row(line_count - 1)?;
ui.cursor.highlight(Some(y))?;
return Ok(WarpUiCallBackType::None);
}
_ => {
return Ok(WarpUiCallBackType::None);
}
}
}
}
#[derive(Debug)]
pub struct Insert;
impl KeyEventCallback for Insert {
fn enter(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
let line_idx = ui.cursor.y();
let col = ui.cursor.x();
let line = ui.buffer.get_line(line_idx);
if line.flags.contains(LineState::LOCKED) {
APP_INFO.lock().unwrap().info = "Row is locked".to_string();
return Ok(WarpUiCallBackType::None);
}
ui.buffer.input_enter(col, line_idx);
DEF_STYLE.read().unwrap().set_content_style()?;
// 清空改行光标后的内容
TermManager::clear_until_new_line()?;
// 执行渲染后续文本
ui.cursor.move_to_nextline(1)?;
ui.cursor.clear_current_line()?;
let ret = ui.render_content(
line_idx + 1,
(CONTENT_WINSIZE.read().unwrap().rows - line_idx) as usize,
)?;
if ret == 0 {
ui.scroll_up(1)?;
ui.render_content(
line_idx + 1,
(CONTENT_WINSIZE.read().unwrap().rows - line_idx) as usize,
)?;
ui.cursor.move_up(1)?;
}
let last = ui.cursor.y() - 1;
ui.cursor.highlight(Some(last))?;
ui.set_edited();
Ok(WarpUiCallBackType::None)
}
fn tab(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
ui.set_edited();
let x = ui.cursor.x();
let tab_size = TAB_SIZE.load(Ordering::SeqCst);
let space_size = tab_size - (x % tab_size);
for _ in 0..space_size {
ui.buffer
.insert_char(' ' as u8, ui.cursor.x(), ui.cursor.y());
}
let y = ui.cursor.y();
ui.render_content(y, 1)?;
ui.cursor.move_right(space_size)?;
Ok(WarpUiCallBackType::None)
}
fn esc(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::ChangMode(ModeType::Command))
}
fn input_data(
&self,
ui: &mut MutexGuard<UiCore>,
data: &[u8],
) -> io::Result<WarpUiCallBackType> {
let x = ui.cursor.x();
let y = ui.cursor.y();
let line = ui.buffer.get_line(y);
if line.flags.contains(LineState::LOCKED) {
APP_INFO.lock().unwrap().info = "Row is locked".to_string();
return Ok(WarpUiCallBackType::None);
}
for (idx, ch) in data.iter().enumerate() {
ui.buffer.insert_char(*ch, x + idx as u16, y);
}
let line_data = ui.buffer.get_line(y);
// 考虑长度包含\n,所以要减1
ui.cursor.write(String::from_utf8_lossy(
&line_data.data[x as usize..(line_data.size() - 1)],
))?;
ui.cursor.move_to_columu(x + data.len() as u16)?;
ui.set_edited();
ui.cursor.highlight(None)?;
Ok(WarpUiCallBackType::None)
}
}
#[derive(Debug)]
pub struct LastLine {
buf: Mutex<Vec<u8>>,
}
impl LastLine {
pub fn new() -> Self {
Self {
buf: Mutex::new(vec![':' as u8]),
}
}
pub fn reset(&self) {
self.buf.lock().unwrap().resize(1, ':' as u8);
}
}
impl KeyEventCallback for LastLine {
fn enter(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
let mut buf = self.buf.lock().unwrap();
let cmd = String::from_utf8_lossy(&buf).to_string();
let ret = LastLineCommand::process(ui, cmd);
ui.cursor.move_to(1, u16::MAX - 1)?;
// ui.cursor.move_to_columu(1)?;
TermManager::clear_until_new_line()?;
ui.cursor.move_to(1, u16::MAX - 1)?;
buf.resize(1, 0);
if ret == WarpUiCallBackType::None {
ui.cursor.restore_pos()?;
return Ok(WarpUiCallBackType::ChangMode(ModeType::Command));
}
Ok(ret)
}
fn tab(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::None)
}
fn backspace(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
if ui.cursor.x() == 1 {
return Ok(WarpUiCallBackType::None);
}
self.left(ui)?;
self.buf.lock().unwrap().remove(ui.cursor.x() as usize);
ui.cursor.write(' ')?;
self.left(ui)?;
Ok(WarpUiCallBackType::None)
}
fn up(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::None)
}
fn down(&self, _ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
Ok(WarpUiCallBackType::None)
}
fn esc(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> {
ui.cursor.restore_pos()?;
Ok(WarpUiCallBackType::ChangMode(ModeType::Command))
}
fn input_data(
&self,
ui: &mut MutexGuard<UiCore>,
data: &[u8],
) -> io::Result<WarpUiCallBackType> {
let mut buf = self.buf.lock().unwrap();
if ui.cursor.x() == buf.len() as u16 {
buf.extend(data);
} else {
let index = ui.cursor.x() as usize;
for (i, &item) in data.iter().enumerate() {
buf.insert(index + i, item);
}
}
ui.cursor.write(String::from_utf8_lossy(&data))?;
Ok(WarpUiCallBackType::None)
}
}