Skip to content

Commit

Permalink
fix(ui): no longer interpret newlines when pasted.
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-ko committed Jun 3, 2024
1 parent 0e39e86 commit e899534
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ pub enum FocusChange {
Lost,
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub enum Event {
Tick,
Key(KeyEvent),
Mouse(MouseEvent),
Resize(u16, u16),
Focus(FocusChange),
Paste(String),
}

#[derive(Debug)]
Expand Down Expand Up @@ -60,7 +61,7 @@ impl EventHandler {
CrosstermEvent::Resize(w, h) => sender.send(Event::Resize(w, h)),
CrosstermEvent::FocusGained => sender.send(Event::Focus(FocusChange::Gained)),
CrosstermEvent::FocusLost => sender.send(Event::Focus(FocusChange::Lost)),
CrosstermEvent::Paste(_) => unimplemented!(),
CrosstermEvent::Paste(text) => sender.send(Event::Paste(text)),
}
.expect("failed to send terminal event")
}
Expand Down
12 changes: 9 additions & 3 deletions src/tui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{io, panic};
use std::collections::HashMap;

use crossterm::event::{DisableFocusChange, EnableFocusChange, KeyEvent};
use crossterm::event::{DisableBracketedPaste, DisableFocusChange, EnableBracketedPaste, EnableFocusChange, KeyEvent};
use crossterm::terminal;
use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen};
use log::debug;
Expand Down Expand Up @@ -33,7 +33,7 @@ impl<B: Backend> Tui<B> {
}
pub fn init(&mut self) -> AppResult<()> {
terminal::enable_raw_mode()?;
crossterm::execute!(io::stderr(), EnterAlternateScreen, EnableFocusChange)?;
crossterm::execute!(io::stderr(), EnterAlternateScreen, EnableFocusChange, EnableBracketedPaste)?;

let panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic| {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<B: Backend> Tui<B> {

fn reset() -> AppResult<()> {
terminal::disable_raw_mode()?;
crossterm::execute!(io::stderr(), LeaveAlternateScreen, DisableFocusChange)?;
crossterm::execute!(io::stderr(), LeaveAlternateScreen, DisableFocusChange, DisableBracketedPaste)?;
Ok(())
}

Expand Down Expand Up @@ -91,6 +91,7 @@ impl<B: Backend> Tui<B> {
}
}
}
Event::Paste(text) => { self.handle_paste(app, text); }
}
Ok(())
}
Expand All @@ -106,6 +107,11 @@ impl<B: Backend> Tui<B> {
Ok(())
}

fn handle_paste(&mut self, app: &mut App, text: String) {
let page = self.get_current_page_impl();
page.pasted(app, text);
}

#[inline]
//noinspection Duplicates
fn get_current_page_impl(&mut self) -> &mut Box<dyn Page> {
Expand Down
2 changes: 2 additions & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pub enum UIAction {
pub trait Page {
fn render(&mut self, app: &mut App, frame: &mut Frame);
fn input(&mut self, app: &mut App, event: KeyEvent) -> AppResult<UIAction>;

fn pasted(&mut self, _app: &mut App, _text: String) {}
}
11 changes: 11 additions & 0 deletions src/ui/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,17 @@ impl Page for VotingPage {
}
Ok(UIAction::Continue)
}

fn pasted(&mut self, _app: &mut App, text: String) {
match self.input_mode {
InputMode::Chat | InputMode::Vote | InputMode::Name => {
if let Some(input_buffer) = &mut self.input_buffer {
input_buffer.push_str(text.as_str());
}
}
_ => {}
}
}
}

fn render_box_colored(title: &str, color: Style, rect: Rect, frame: &mut Frame) -> Rect {
Expand Down

0 comments on commit e899534

Please sign in to comment.