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

feature: search paste support #881

Merged
merged 7 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feature: add pasting to search
Supports pasting events to the search bar (e.g. shift-insert, ctrl-shift-v).
  • Loading branch information
ClementTsang committed Nov 9, 2022
commit e7135d6b91818344074c7835f45ec17c72033b54
59 changes: 58 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
time::Instant,
};

use concat_string::concat_string;
use unicode_segmentation::GraphemeCursor;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

Expand Down Expand Up @@ -35,7 +36,7 @@ pub mod widgets;

use frozen_state::FrozenState;

const MAX_SEARCH_LENGTH: usize = 200;
const MAX_SEARCH_LENGTH: usize = 200; // FIXME: Remove this limit, it's unnecessary.

#[derive(Debug, Clone)]
pub enum AxisScaling {
Expand Down Expand Up @@ -2714,4 +2715,60 @@ impl App {
1 + self.app_config_fields.table_gap
}
}

/// A quick and dirty way to handle paste events.
pub fn handle_paste(&mut self, paste: String) {
// Partially copy-pasted from the single-char variant; should probably optimize this process in the future.
let is_in_search_widget = self.is_in_search_widget();
if let Some(proc_widget_state) = self
.proc_state
.widget_states
.get_mut(&(self.current_widget.widget_id - 1))
{
let curr_width = UnicodeWidthStr::width(
proc_widget_state
.proc_search
.search_state
.current_search_query
.as_str(),
);
let paste_width = UnicodeWidthStr::width(paste.as_str());

if is_in_search_widget
&& proc_widget_state.is_search_enabled()
&& curr_width + paste_width <= MAX_SEARCH_LENGTH
{
let left_bound = proc_widget_state.get_search_cursor_position();
let new_left_bound = (left_bound + paste_width).saturating_sub(1);
let curr_query = &mut proc_widget_state
.proc_search
.search_state
.current_search_query;
let (left, right) = curr_query.split_at(left_bound);
*curr_query = concat_string!(left, paste, right);

proc_widget_state.proc_search.search_state.grapheme_cursor = GraphemeCursor::new(
new_left_bound,
proc_widget_state
.proc_search
.search_state
.current_search_query
.len(),
true,
);
proc_widget_state.search_walk_forward(new_left_bound);

proc_widget_state
.proc_search
.search_state
.char_cursor_position += paste_width;

proc_widget_state.update_query();
proc_widget_state.proc_search.search_state.cursor_direction =
CursorDirection::Right;

return;
}
}
}
}
13 changes: 11 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::{

use anyhow::{Context, Result};
use crossterm::{
event::EnableMouseCapture,
event::{EnableBracketedPaste, EnableMouseCapture},
execute,
terminal::{enable_raw_mode, EnterAlternateScreen},
};
Expand Down Expand Up @@ -120,7 +120,12 @@ fn main() -> Result<()> {

// Set up up tui and crossterm
let mut stdout_val = stdout();
execute!(stdout_val, EnterAlternateScreen, EnableMouseCapture)?;
execute!(
stdout_val,
EnterAlternateScreen,
EnableMouseCapture,
EnableBracketedPaste
)?;
enable_raw_mode()?;

let mut terminal = Terminal::new(CrosstermBackend::new(stdout_val))?;
Expand Down Expand Up @@ -151,6 +156,10 @@ fn main() -> Result<()> {
handle_mouse_event(event, &mut app);
update_data(&mut app);
}
BottomEvent::PasteEvent(paste) => {
app.handle_paste(paste);
update_data(&mut app);
}
BottomEvent::Update(data) => {
app.data_collection.eat_data(data);

Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use std::{

use crossterm::{
event::{
poll, read, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent,
MouseEventKind,
poll, read, DisableBracketedPaste, DisableMouseCapture, Event, KeyCode, KeyEvent,
KeyModifiers, MouseEvent, MouseEventKind,
},
execute,
style::Print,
Expand Down Expand Up @@ -71,6 +71,7 @@ pub type Pid = libc::pid_t;
pub enum BottomEvent<I, J> {
KeyInput(I),
MouseInput(J),
PasteEvent(String),
Update(Box<data_harvester::Data>),
Clean,
}
Expand Down Expand Up @@ -273,6 +274,7 @@ pub fn cleanup_terminal(
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
DisableBracketedPaste,
DisableMouseCapture,
LeaveAlternateScreen
)?;
Expand Down Expand Up @@ -311,7 +313,13 @@ pub fn panic_hook(panic_info: &PanicInfo<'_>) {
let stacktrace: String = format!("{:?}", backtrace::Backtrace::new());

disable_raw_mode().unwrap();
execute!(stdout, DisableMouseCapture, LeaveAlternateScreen).unwrap();
execute!(
stdout,
DisableBracketedPaste,
DisableMouseCapture,
LeaveAlternateScreen
)
.unwrap();

// Print stack trace. Must be done after!
execute!(
Expand Down Expand Up @@ -425,6 +433,11 @@ pub fn create_input_thread(
if let Ok(event) = read() {
// FIXME: Handle all other event cases.
match event {
Event::Paste(paste) => {
if sender.send(BottomEvent::PasteEvent(paste)).is_err() {
break;
}
}
Event::Key(key) => {
if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 {
if sender.send(BottomEvent::KeyInput(key)).is_err() {
Expand Down