Skip to content

Commit 02cc4e6

Browse files
committed
refactor: minor refactors here and there
1 parent dfa2de2 commit 02cc4e6

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

src/core/ev_handler.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ pub fn handle_event(
8282
p.line_numbers = l;
8383
command_queue.push_back(Command::FormatRedrawDisplay);
8484
}
85+
Command::UserInput(InputEvent::Number(n)) => {
86+
p.prefix_num.push(n);
87+
command_queue.push_back(Command::FormatRedrawPrompt);
88+
}
8589
#[cfg(feature = "search")]
8690
Command::UserInput(InputEvent::Search(m)) => {
8791
p.search_mode = m;
@@ -95,7 +99,7 @@ pub fn handle_event(
9599
let mut active = lock.lock();
96100
*active = false;
97101
drop(active);
98-
// let string = search::fetch_input(&mut out, p.search_mode, p.rows)?;
102+
cvar.notify_one();
99103
let search_result = search::fetch_input(&mut out, p)?;
100104
let mut active = lock.lock();
101105
*active = true;

src/core/init.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ fn event_reader(
330330
#[cfg(feature = "search")]
331331
{
332332
let (lock, cvar) = (&user_input_active.0, &user_input_active.1);
333-
let mut active = lock.lock();
334-
if !*active {
335-
cvar.wait(&mut active);
336-
}
333+
let _guard = cvar.wait_while(&mut lock.lock(), |pending| !*pending);
337334
}
338335

339336
if event::poll(std::time::Duration::from_millis(100))
@@ -344,17 +341,14 @@ fn event_reader(
344341
// Get the events
345342
let input = guard.input_classifier.classify_input(ev, &guard);
346343
if let Some(iev) = input {
347-
if let InputEvent::Number(n) = iev {
348-
guard.prefix_num.push(n);
349-
guard.format_prompt();
350-
} else if !guard.prefix_num.is_empty() {
344+
if !matches!(iev, InputEvent::Number(_)) {
351345
guard.prefix_num.clear();
352346
guard.format_prompt();
353347
}
354348
if let Err(TrySendError::Disconnected(_)) = evtx.try_send(Command::UserInput(iev)) {
355349
break;
356350
}
357-
} else if !guard.prefix_num.is_empty() {
351+
} else {
358352
guard.prefix_num.clear();
359353
guard.format_prompt();
360354
}

src/search.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ where
406406
)?;
407407
Ok(())
408408
};
409-
410409
match so.ev.as_ref().unwrap() {
411410
Event::Key(KeyEvent { kind, .. }) if *kind != KeyEventKind::Press => (),
412411
// If Esc is pressed, cancel the search and also make sure that the search query is
@@ -533,20 +532,20 @@ where
533532
so.cursor_position = so.string.len().saturating_add(1).try_into().unwrap();
534533
term::move_cursor(out, so.cursor_position, so.rows, true)?;
535534
}
536-
537-
Event::Key(event) => {
535+
Event::Key(KeyEvent {
536+
code: KeyCode::Char(c),
537+
modifiers: KeyModifiers::NONE,
538+
..
539+
}) => {
538540
// For any character key, without a modifier, insert it into so.string before
539541
// current cursor position and update the line
540-
if let KeyCode::Char(c) = event.code {
541-
so.string
542-
.insert(so.cursor_position.saturating_sub(1).into(), c);
543-
544-
populate_word_index(so);
545-
refresh_display(out, so)?;
546-
so.cursor_position = so.cursor_position.saturating_add(1);
547-
term::move_cursor(out, so.cursor_position, so.rows, false)?;
548-
out.flush()?;
549-
}
542+
so.string
543+
.insert(so.cursor_position.saturating_sub(1).into(), *c);
544+
populate_word_index(so);
545+
refresh_display(out, so)?;
546+
so.cursor_position = so.cursor_position.saturating_add(1);
547+
term::move_cursor(out, so.cursor_position, so.rows, false)?;
548+
out.flush()?;
550549
}
551550
_ => return Ok(()),
552551
}

0 commit comments

Comments
 (0)