summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-12-30 14:02:10 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-12-30 14:02:10 -0500
commite4faa16c8a7f2a51303ad3fe9f3891d8f7eabb82 (patch)
tree3d1be976d3a23d2aad667b1106668263cfd5ce6d /src/util
parent72f5e1eaf4c0fcbe39bd3f0d80cf9ab45d504057 (diff)
add resize notification support and preliminary mouse support
- currently scroll is hardmapped to cursor_up and cursor_down
Diffstat (limited to 'src/util')
-rw-r--r--src/util/event.rs50
-rw-r--r--src/util/input_process.rs (renamed from src/util/worker.rs)14
-rw-r--r--src/util/key_mapping.rs66
-rw-r--r--src/util/mod.rs2
4 files changed, 112 insertions, 20 deletions
diff --git a/src/util/event.rs b/src/util/event.rs
index 9e7f120..4d79035 100644
--- a/src/util/event.rs
+++ b/src/util/event.rs
@@ -2,16 +2,21 @@ use std::io;
use std::sync::mpsc;
use std::thread;
-use termion::event::Key;
+use signal_hook::consts::signal;
+use signal_hook::iterator::exfiltrator::SignalOnly;
+use signal_hook::iterator::SignalsInfo;
+
+use termion::event::Event;
use termion::input::TermRead;
use crate::io::IOWorkerProgress;
#[derive(Debug)]
-pub enum Event {
- Input(Key),
+pub enum JoshutoEvent {
+ Termion(Event),
IOWorkerProgress(IOWorkerProgress),
IOWorkerResult(io::Result<IOWorkerProgress>),
+ Signal(i32),
// Filesystem(notify::Result),
}
@@ -27,10 +32,9 @@ impl Default for Config {
/// A small event handler that wrap termion input and tick events. Each event
/// type is handled in its own thread and returned to a common `Receiver`
pub struct Events {
- pub event_tx: mpsc::Sender<Event>,
- event_rx: mpsc::Receiver<Event>,
+ pub event_tx: mpsc::Sender<JoshutoEvent>,
+ event_rx: mpsc::Receiver<JoshutoEvent>,
pub input_tx: mpsc::SyncSender<()>,
- fileio_handle: thread::JoinHandle<()>,
}
impl Events {
@@ -42,15 +46,28 @@ impl Events {
let (input_tx, input_rx) = mpsc::sync_channel(1);
let (event_tx, event_rx) = mpsc::channel();
+ // signal thread
let event_tx2 = event_tx.clone();
+ let _ = thread::spawn(move || {
+ let sigs = vec![signal::SIGWINCH];
+ let mut signals = SignalsInfo::<SignalOnly>::new(&sigs).unwrap();
+ for signal in &mut signals {
+ if let Err(e) = event_tx2.send(JoshutoEvent::Signal(signal)) {
+ eprintln!("Signal thread send err: {:#?}", e);
+ return;
+ }
+ }
+ });
- let fileio_handle = thread::spawn(move || {
+ // input thread
+ let event_tx2 = event_tx.clone();
+ let _ = thread::spawn(move || {
let stdin = io::stdin();
- let mut keys = stdin.keys();
- match keys.next() {
- Some(key) => match key {
- Ok(key) => {
- if let Err(e) = event_tx2.send(Event::Input(key)) {
+ let mut events = stdin.events();
+ match events.next() {
+ Some(event) => match event {
+ Ok(event) => {
+ if let Err(e) = event_tx2.send(JoshutoEvent::Termion(event)) {
eprintln!("Input thread send err: {:#?}", e);
return;
}
@@ -61,9 +78,9 @@ impl Events {
}
while input_rx.recv().is_ok() {
- if let Some(key) = keys.next() {
- if let Ok(key) = key {
- if let Err(e) = event_tx2.send(Event::Input(key)) {
+ if let Some(event) = events.next() {
+ if let Ok(event) = event {
+ if let Err(e) = event_tx2.send(JoshutoEvent::Termion(event)) {
eprintln!("Input thread send err: {:#?}", e);
return;
}
@@ -76,11 +93,10 @@ impl Events {
event_tx,
event_rx,
input_tx,
- fileio_handle,
}
}
- pub fn next(&self) -> Result<Event, mpsc::RecvError> {
+ pub fn next(&self) -> Result<JoshutoEvent, mpsc::RecvError> {
let event = self.event_rx.recv()?;
Ok(event)
}
diff --git a/src/util/worker.rs b/src/util/input_process.rs
index f3631fd..362d87b 100644
--- a/src/util/worker.rs
+++ b/src/util/input_process.rs
@@ -1,8 +1,20 @@
+use signal_hook::consts::signal;
+
use crate::context::JoshutoContext;
use crate::history::DirectoryHistory;
use crate::io::{FileOp, IOWorkerProgress};
+use crate::ui;
+use crate::util::event::JoshutoEvent;
+use crate::util::format;
-use super::format;
+pub fn process_noninteractive(event: JoshutoEvent, context: &mut JoshutoContext) {
+ match event {
+ JoshutoEvent::IOWorkerProgress(res) => process_worker_progress(context, res),
+ JoshutoEvent::IOWorkerResult(res) => process_finished_worker(context, res),
+ JoshutoEvent::Signal(signal::SIGWINCH) => {}
+ _ => {}
+ }
+}
pub fn process_worker_progress(context: &mut JoshutoContext, res: IOWorkerProgress) {
context.set_worker_progress(res);
diff --git a/src/util/key_mapping.rs b/src/util/key_mapping.rs
index e1049aa..d4c16f9 100644
--- a/src/util/key_mapping.rs
+++ b/src/util/key_mapping.rs
@@ -1,4 +1,60 @@
-use termion::event::Key;
+use termion::event::{Event, Key, MouseButton, MouseEvent};
+
+pub trait ToString {
+ fn to_string(&self) -> String;
+}
+
+impl ToString for Key {
+ fn to_string(&self) -> String {
+ match *self {
+ Key::Char(c) => format!("{}", c),
+ Key::Ctrl(c) => format!("ctrl+{}", c),
+ Key::Left => "arrow_left".to_string(),
+ Key::Right => "arrow_right".to_string(),
+ Key::Up => "arrow_up".to_string(),
+ Key::Down => "arrow_down".to_string(),
+ Key::Backspace => "backspace".to_string(),
+ Key::Home => "home".to_string(),
+ Key::End => "end".to_string(),
+ Key::PageUp => "page_up".to_string(),
+ Key::PageDown => "page_down".to_string(),
+ Key::BackTab => "backtab".to_string(),
+ Key::Insert => "insert".to_string(),
+ Key::Delete => "delete".to_string(),
+ Key::Esc => "escape".to_string(),
+ Key::F(i) => format!("f{}", i),
+ k => format!("{:?}", k),
+ }
+ }
+}
+
+impl ToString for MouseEvent {
+ fn to_string(&self) -> String {
+ match *self {
+ k => format!("{:?}", k),
+ }
+ }
+}
+
+impl ToString for Event {
+ fn to_string(&self) -> String {
+ match self {
+ Event::Key(key) => key.to_string(),
+ Event::Mouse(mouse) => mouse.to_string(),
+ Event::Unsupported(v) => format!("{:?}", v),
+ }
+ }
+}
+
+pub fn str_to_event(s: &str) -> Option<Event> {
+ if let Some(k) = str_to_key(s) {
+ Some(Event::Key(k))
+ } else if let Some(m) = str_to_mouse(s) {
+ Some(Event::Mouse(m))
+ } else {
+ None
+ }
+}
pub fn str_to_key(s: &str) -> Option<Key> {
if s.is_empty() {
@@ -62,3 +118,11 @@ pub fn str_to_key(s: &str) -> Option<Key> {
}
None
}
+
+pub fn str_to_mouse(s: &str) -> Option<MouseEvent> {
+ match s {
+ "scroll_up" => Some(MouseEvent::Press(MouseButton::WheelUp, 0, 0)),
+ "scroll_down" => Some(MouseEvent::Press(MouseButton::WheelDown, 0, 0)),
+ s => None,
+ }
+}
diff --git a/src/util/mod.rs b/src/util/mod.rs
index 8955c76..bca6387 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -1,8 +1,8 @@
pub mod devicons;
pub mod event;
pub mod format;
+pub mod input_process;
pub mod key_mapping;
pub mod load_child;
pub mod sort;
pub mod unix;
-pub mod worker;