Skip to content

Commit

Permalink
Added shortcut to delete last word (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
aashirbadb authored Oct 5, 2023
1 parent 024af7d commit 423e867
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ mod runner;
use anyhow::{Context, Result};
use clap::Parser;
use crossterm::{
event::{KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
terminal::{
self, disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
},
};
use std::io;
use tui::{backend::CrosstermBackend, Terminal};
Expand Down Expand Up @@ -126,6 +129,17 @@ fn main() -> anyhow::Result<()> {
fn configure_terminal() -> Result<Terminal<CrosstermBackend<io::Stdout>>, anyhow::Error> {
enable_raw_mode().context("Unable to enable raw mode")?;
let mut stdout = io::stdout();
if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
execute!(
stdout,
PushKeyboardEnhancementFlags(
KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
| KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
)
)
.context("Unable to push keyboard enhancement flags")?;
}

execute!(stdout, EnterAlternateScreen).context("Unable to enter alternate screen")?;
let backend = CrosstermBackend::new(stdout);
let terminal = Terminal::new(backend).context("Unable to create terminal")?;
Expand All @@ -138,6 +152,11 @@ fn restore_terminal(
mut terminal: Terminal<CrosstermBackend<io::Stdout>>,
) -> Result<(), anyhow::Error> {
disable_raw_mode().context("Unable to disable raw mode")?;
if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
execute!(terminal.backend_mut(), PopKeyboardEnhancementFlags)
.context("Unable to pop keyboard enhancement flags")?;
}

execute!(terminal.backend_mut(), LeaveAlternateScreen)
.context("Unable to leave alternate screen")?;
terminal.show_cursor().context("Unable to show cursor")?;
Expand Down
27 changes: 26 additions & 1 deletion src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! And test statistics are returned from the runner.

use anyhow::{Context, Result};
use crossterm::event::{self, Event, KeyCode};
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use mockall::automock;
use std::time::{Duration, Instant};
use tui::{
Expand Down Expand Up @@ -71,6 +71,18 @@ impl Runner {
}
}

/// Removes the last word from user input
fn remove_last_word(&mut self) {
let mut words = self.input.split_whitespace().collect::<Vec<&str>>();
words.pop();

self.input = words.join(" ");

if !self.input.is_empty() {
self.input.push(' ');
}
}

/// Method that runs the test.
///
/// It renders the application using the `tui` crate and reacts to user input.
Expand Down Expand Up @@ -129,6 +141,13 @@ impl Runner {
_ => {}
},
InputMode::Editing => match key.code {
// Crossterm returns `ctrl+w` or ``ctrl+h` when `ctrl+backspace` is pressed
// see: https://github.com/crossterm-rs/crossterm/issues/504
KeyCode::Char('h') | KeyCode::Char('w')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.remove_last_word();
}
KeyCode::Char(c) => {
self.input.push(c);

Expand All @@ -147,6 +166,12 @@ impl Runner {
self.raw_valid_characters_count += 1;
}
}
KeyCode::Backspace
if key.modifiers.contains(KeyModifiers::ALT)
| key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.remove_last_word();
}
KeyCode::Backspace => {
self.input.pop();
}
Expand Down

0 comments on commit 423e867

Please sign in to comment.