Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added shortcut to delete last word #17

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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