summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2019-11-16 01:36:58 +0200
committerManos Pitsidianakis <el13635@mail.ntua.gr>2019-11-16 01:39:07 +0200
commit80630ae9ba9739b263fe096573cb05d5661b4549 (patch)
tree53d5b31b070e7308f8580f454c7e77022b394365
parent21987e5fba19c1e054f5e522e3308976a90a5348 (diff)
Change mode when expecting input
Otherwise pressing 'q' would exit even when expecting input, oof. Closes #14
-rw-r--r--src/main.rs2
-rw-r--r--src/ui/components.rs2
-rw-r--r--src/ui/components/kernel.rs2
-rw-r--r--src/ui/components/processes.rs46
-rw-r--r--src/ui/components/utilities.rs6
-rw-r--r--src/ui/state.rs10
6 files changed, 44 insertions, 24 deletions
diff --git a/src/main.rs b/src/main.rs
index 28bb412..ef3e467 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -111,7 +111,7 @@ fn main() -> Result<(), Error> {
},
ThreadEvent::Input(k) => {
match k {
- Key::Char('q') | Key::Char('Q') => {
+ Key::Char('q') | Key::Char('Q') if state.mode == UIMode::Normal => {
drop(state);
break 'main;
},
diff --git a/src/ui/components.rs b/src/ui/components.rs
index a413afe..4853bc4 100644
--- a/src/ui/components.rs
+++ b/src/ui/components.rs
@@ -58,7 +58,7 @@ pub trait Component: Display + Debug + Send {
dirty_areas: &mut VecDeque<Area>,
tick: bool,
);
- fn process_event(&mut self, event: &mut UIEvent);
+ fn process_event(&mut self, event: &mut UIEvent, ui_mode: &mut UIMode);
fn is_dirty(&self) -> bool {
true
}
diff --git a/src/ui/components/kernel.rs b/src/ui/components/kernel.rs
index bd02f9b..0ca7cd5 100644
--- a/src/ui/components/kernel.rs
+++ b/src/ui/components/kernel.rs
@@ -503,7 +503,7 @@ impl Component for KernelMetrics {
}
}
- fn process_event(&mut self, event: &mut UIEvent) {
+ fn process_event(&mut self, event: &mut UIEvent, _ui_mode: &mut UIMode) {
match event {
UIEvent::Resize => {
self.dirty = true;
diff --git a/src/ui/components/processes.rs b/src/ui/components/processes.rs
index c548113..b290ba2 100644
--- a/src/ui/components/processes.rs
+++ b/src/ui/components/processes.rs
@@ -186,6 +186,18 @@ enum Input {
Inactive,
}
+impl Input {
+ fn set_inactive(&mut self, ui_mode: &mut UIMode) {
+ *self = Inactive;
+ *ui_mode = UIMode::Normal;
+ }
+
+ fn set_active(&mut self, ui_mode: &mut UIMode) {
+ *self = Active;
+ *ui_mode = UIMode::Input;
+ }
+}
+
impl Default for Input {
fn default() -> Self {
Inactive
@@ -1415,7 +1427,7 @@ impl Component for ProcessList {
self.dirty = false;
}
- fn process_event(&mut self, event: &mut UIEvent) {
+ fn process_event(&mut self, event: &mut UIEvent, ui_mode: &mut UIMode) {
let map = &self.get_shortcuts()[""];
match event {
UIEvent::Input(Key::Up) => {
@@ -1487,7 +1499,7 @@ impl Component for ProcessList {
*n = 0;
*n_prev = 1;
}
- self.mode.input = Input::Inactive;
+ self.mode.input.set_inactive(ui_mode);
self.force_redraw = true;
self.dirty = true;
}
@@ -1504,13 +1516,13 @@ impl Component for ProcessList {
if filter.is_empty() {
self.mode.active &= !FILTER_ACTIVE;
}
- self.mode.input = Input::Inactive;
+ self.mode.input.set_inactive(ui_mode);
self.force_redraw = true;
self.dirty = true;
}
}
UIEvent::Input(k) if *k == map["filter"] && self.mode.input == Inactive => {
- self.mode.input = Active;
+ self.mode.input.set_active(ui_mode);
self.mode.active |= FILTER_ACTIVE;
self.mode.active &= !SEARCH_ACTIVE;
self.mode.search.0.clear();
@@ -1549,7 +1561,7 @@ impl Component for ProcessList {
self.mode.active &= !LOCATE_ACTIVE;
} else {
self.mode.active |= LOCATE_ACTIVE;
- self.mode.input = Active;
+ self.mode.input.set_active(ui_mode);
self.freeze = true;
}
self.dirty = true;
@@ -1564,13 +1576,13 @@ impl Component for ProcessList {
self.mode.search.1 = 0;
self.mode.search.2 = 1;
}
- self.mode.input = Active;
+ self.mode.input.set_active(ui_mode);
self.force_redraw = true;
self.dirty = true;
}
UIEvent::Input(k) if *k == map["kill process"] && !self.mode.is_active(KILL_ACTIVE) => {
self.mode.active |= KILL_ACTIVE;
- self.mode.input = Active;
+ self.mode.input.set_active(ui_mode);
self.freeze = true;
self.dirty = true;
self.force_redraw = true;
@@ -1581,23 +1593,23 @@ impl Component for ProcessList {
self.mode.active &= !HELP_ACTIVE;
} else if self.mode.is_active(KILL_ACTIVE) {
self.mode.active &= !KILL_ACTIVE;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
} else if self.mode.is_active(LOCATE_ACTIVE) {
self.mode.active &= !LOCATE_ACTIVE;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
} else if self.mode.is_active(FOLLOW_ACTIVE) {
self.mode.active &= !FOLLOW_ACTIVE;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
} else if self.mode.is_active(SEARCH_ACTIVE) {
self.mode.active &= !SEARCH_ACTIVE;
self.mode.search.0.clear();
self.mode.search.1 = 0;
self.mode.search.2 = 1;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
} else if self.mode.is_active(FILTER_ACTIVE) {
self.mode.active &= !FILTER_ACTIVE;
self.mode.filter.clear();
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
} else if self.freeze {
self.freeze = false;
} else {
@@ -1643,7 +1655,7 @@ impl Component for ProcessList {
self.mode.active &= !SEARCH_ACTIVE;
*n = 0;
*n_prev = 1;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
} else {
p.pop();
}
@@ -1653,7 +1665,7 @@ impl Component for ProcessList {
} else if self.mode.is_active(FILTER_ACTIVE) {
let filter = &mut self.mode.filter;
if filter.is_empty() {
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
self.mode.active &= !FILTER_ACTIVE;
} else {
// TODO pop grapheme
@@ -1678,14 +1690,14 @@ impl Component for ProcessList {
self.mode.active &= !KILL_ACTIVE;
self.dirty = true;
self.force_redraw = true;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
}
UIEvent::Input(Key::Char('\n'))
if self.mode.input == Active && self.mode.is_active(LOCATE_ACTIVE) =>
{
self.dirty = true;
self.force_redraw = true;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
}
UIEvent::Input(Key::Char(c))
if self.mode.is_active(SEARCH_ACTIVE) && self.mode.input == Inactive =>
@@ -1718,7 +1730,7 @@ impl Component for ProcessList {
self.mode.search.0.clear();
self.mode.search.1 = 0;
self.mode.search.2 = 1;
- self.mode.input = Inactive;
+ self.mode.input.set_inactive(ui_mode);
self.force_redraw = true;
self.dirty = true;
}
diff --git a/src/ui/components/utilities.rs b/src/ui/components/utilities.rs
index 2b65bf7..409dbb6 100644
--- a/src/ui/components/utilities.rs
+++ b/src/ui/components/utilities.rs
@@ -237,9 +237,9 @@ impl Component for Window {
);
}
- fn process_event(&mut self, event: &mut UIEvent) {
- self.top_bars.process_event(event);
- self.list.process_event(event);
+ fn process_event(&mut self, event: &mut UIEvent, ui_mode: &mut UIMode) {
+ self.top_bars.process_event(event, ui_mode);
+ self.list.process_event(event, ui_mode);
}
fn is_dirty(&self) -> bool {
diff --git a/src/ui/state.rs b/src/ui/state.rs
index ff2cb1c..f6766c8 100644
--- a/src/ui/state.rs
+++ b/src/ui/state.rs
@@ -37,6 +37,12 @@ use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use termion::{clear, cursor, style};
+#[derive(PartialEq)]
+pub enum UIMode {
+ Normal,
+ Input,
+}
+
type StateStdout = termion::screen::AlternateScreen<termion::raw::RawTerminal<std::io::Stdout>>;
struct InputHandler {
@@ -79,6 +85,7 @@ pub struct State {
sender: Sender<ThreadEvent>,
receiver: Receiver<ThreadEvent>,
input: InputHandler,
+ pub mode: UIMode,
}
impl Drop for State {
@@ -138,6 +145,7 @@ impl State {
rx: input_receiver,
tx: input_sender,
},
+ mode: UIMode::Normal,
};
write!(
@@ -335,7 +343,7 @@ impl State {
pub fn rcv_event(&mut self, mut event: UIEvent) {
/* inform each component */
for i in 0..self.components.len() {
- self.components[i].process_event(&mut event);
+ self.components[i].process_event(&mut event, &mut self.mode);
}
}