diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-09-04 21:55:48 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-09-04 21:56:00 -0400 |
commit | 85742f39a7407e3cbeb5977aa78589ddc4ec10cc (patch) | |
tree | bb45ed0b191316527481b69b8337cac017bf68d4 | |
parent | f0ba5842d26275ee2b7c15240495de9561382582 (diff) |
messages now have color support
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | src/commands/delete_files.rs | 2 | ||||
-rw-r--r-- | src/commands/sub_process.rs | 2 | ||||
-rw-r--r-- | src/context/app_context.rs | 16 | ||||
-rw-r--r-- | src/context/message_queue.rs | 59 | ||||
-rw-r--r-- | src/context/mod.rs | 12 | ||||
-rw-r--r-- | src/run.rs | 16 | ||||
-rw-r--r-- | src/ui/views/tui_folder_view.rs | 6 | ||||
-rw-r--r-- | src/util/input.rs | 14 |
9 files changed, 93 insertions, 35 deletions
@@ -58,7 +58,6 @@ sudo dnf install joshuto ``` ## Usage - ``` ~ $ joshuto ``` diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index 2ab2763..46751dc 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -107,7 +107,7 @@ fn delete_files(context: &mut AppContext, backend: &mut TuiBackend) -> std::io:: } reload::reload(context, tab_index)?; let msg = format!("Deleted {} files", paths_len); - context.push_msg(msg); + context.message_queue_mut().push_success(msg); } Ok(()) } diff --git a/src/commands/sub_process.rs b/src/commands/sub_process.rs index e3e8f66..c69dfff 100644 --- a/src/commands/sub_process.rs +++ b/src/commands/sub_process.rs @@ -53,7 +53,7 @@ pub fn sub_process( backend.terminal_drop(); let res = execute_sub_process(context, words, spawn); reload::soft_reload(context.tab_context_ref().index, context)?; - context.push_msg(format!( + context.message_queue_mut().push_info(format!( "{}: {}", if spawn { "Spawned" } else { "Finished" }, words.join(" ") diff --git a/src/context/app_context.rs b/src/context/app_context.rs index 112b63b..df6035e 100644 --- a/src/context/app_context.rs +++ b/src/context/app_context.rs @@ -1,8 +1,7 @@ -use std::collections::VecDeque; use std::sync::mpsc; use crate::config; -use crate::context::{LocalStateContext, PreviewContext, TabContext, WorkerContext}; +use crate::context::{LocalStateContext, MessageQueue, PreviewContext, TabContext, WorkerContext}; use crate::event::{AppEvent, Events}; use crate::util::search::SearchPattern; @@ -27,7 +26,7 @@ pub struct AppContext { // context related to searching search_context: Option<SearchPattern>, // message queue for displaying messages - message_queue: VecDeque<String>, + message_queue: MessageQueue, // context related to io workers worker_context: WorkerContext, // context related to previews @@ -44,7 +43,7 @@ impl AppContext { tab_context: TabContext::new(), local_state: None, search_context: None, - message_queue: VecDeque::with_capacity(4), + message_queue: MessageQueue::new(), worker_context: WorkerContext::new(event_tx), preview_context: PreviewContext::new(), config, @@ -76,14 +75,11 @@ impl AppContext { &mut self.tab_context } - pub fn message_queue_ref(&self) -> &VecDeque<String> { + pub fn message_queue_ref(&self) -> &MessageQueue { &self.message_queue } - pub fn push_msg(&mut self, msg: String) { - self.message_queue.push_back(msg); - } - pub fn pop_msg(&mut self) -> Option<String> { - self.message_queue.pop_front() + pub fn message_queue_mut(&mut self) -> &mut MessageQueue { + &mut self.message_queue } // local state related diff --git a/src/context/message_queue.rs b/src/context/message_queue.rs new file mode 100644 index 0000000..f2995c0 --- /dev/null +++ b/src/context/message_queue.rs @@ -0,0 +1,59 @@ +use std::collections::VecDeque; + +use tui::style::{Color, Style}; + +pub struct Message { + pub content: String, + pub style: Style, +} + +impl Message { + pub fn new(content: String, style: Style) -> Self { + Self { content, style } + } +} + +pub struct MessageQueue { + contents: VecDeque<Message>, +} + +impl MessageQueue { + pub fn new() -> Self { + Self::default() + } + + pub fn push_success(&mut self, msg: String) { + let message = Message::new(msg, Style::default().fg(Color::Green)); + self.push_msg(message); + } + + pub fn push_info(&mut self, msg: String) { + let message = Message::new(msg, Style::default().fg(Color::Yellow)); + self.push_msg(message); + } + + pub fn push_error(&mut self, msg: String) { + let message = Message::new(msg, Style::default().fg(Color::Red)); + self.push_msg(message); + } + + fn push_msg(&mut self, msg: Message) { + self.contents.push_back(msg); + } + + pub fn pop_front(&mut self) -> Option<Message> { + self.contents.pop_front() + } + + pub fn current_message(&self) -> Option<&Message> { + self.contents.front() + } +} + +impl std::default::Default for MessageQueue { + fn default() -> Self { + Self { + contents: VecDeque::new(), + } + } +} diff --git a/src/context/mod.rs b/src/context/mod.rs index 3ba9a56..d5ba100 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,11 +1,13 @@ mod app_context; mod local_state; +mod message_queue; mod preview_context; mod tab_context; mod worker_context; -pub use self::app_context::{AppContext, QuitType}; -pub use self::local_state::LocalStateContext; -pub use self::preview_context::PreviewContext; -pub use self::tab_context::TabContext; -pub use self::worker_context::WorkerContext; +pub use self::app_context::*; +pub use self::local_state::*; +pub use self::message_queue::*; +pub use self::preview_context::*; +pub use self::tab_context::*; +pub use self::worker_context::*; @@ -43,29 +43,31 @@ pub fn run( preview_default::load_preview(context, backend); } AppEvent::Termion(key) => { - if !context.message_queue_ref().is_empty() { - context.pop_msg(); + if let Some(_) = context.message_queue_ref().current_message() { + context.message_queue_mut().pop_front(); } match key { Event::Unsupported(s) if s.as_slice() == [27, 79, 65] => { let command = KeyCommand::CursorMoveUp(1); if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } Event::Unsupported(s) if s.as_slice() == [27, 79, 66] => { let command = KeyCommand::CursorMoveDown(1); if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } key => match keymap_t.as_ref().get(&key) { None => { - context.push_msg(format!("Unmapped input: {}", key.to_string())); + context + .message_queue_mut() + .push_info(format!("Unmapped input: {}", key.to_string())); } Some(CommandKeybind::SimpleKeybind(command)) => { if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } Some(CommandKeybind::CompositeKeybind(m)) => { @@ -76,7 +78,7 @@ pub fn run( if let Some(command) = cmd { if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } } diff --git a/src/ui/views/tui_folder_view.rs b/src/ui/views/tui_folder_view.rs index d1e420e..9b08bc2 100644 --- a/src/ui/views/tui_folder_view.rs +++ b/src/ui/views/tui_folder_view.rs @@ -136,15 +136,15 @@ impl<'a> Widget for TuiFolderView<'a> { }; if self.show_bottom_status { - let message_style = Style::default().fg(Color::Yellow); /* draw the bottom status bar */ if let Some(msg) = self.context.worker_context_ref().get_msg() { + let message_style = Style::default().fg(Color::Yellow); let text = Span::styled(msg, message_style); Paragraph::new(text) .wrap(Wrap { trim: true }) .render(rect, buf); - } else if !self.context.message_queue_ref().is_empty() { - let text = Span::styled(&self.context.message_queue_ref()[0], message_style); + } else if let Some(msg) = self.context.message_queue_ref().current_message() { + let text = Span::styled(msg.content.as_str(), msg.style.clone()); Paragraph::new(text) .wrap(Wrap { trim: true }) .render(rect, buf); diff --git a/src/util/input.rs b/src/util/input.rs index 48d023c..b11f635 100644 --- a/src/util/input.rs +++ b/src/util/input.rs @@ -55,11 +55,11 @@ pub fn process_finished_worker(context: &mut AppContext, res: std::io::Result<Io processed_size, total_size, ); - context.push_msg(msg); + context.message_queue_mut().push_success(msg); } Err(e) => { let msg = format!("{}", e); - context.push_msg(msg); + context.message_queue_mut().push_error(msg); } } } @@ -113,12 +113,12 @@ pub fn process_mouse(event: MouseEvent, context: &mut AppContext, backend: &mut if x < layout_rect[1].x { let command = KeyCommand::ParentCursorMoveUp(1); if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } else if x < layout_rect[2].x { let command = KeyCommand::CursorMoveUp(1); if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } else { // TODO: scroll in child list @@ -128,12 +128,12 @@ pub fn process_mouse(event: MouseEvent, context: &mut AppContext, backend: &mut if x < layout_rect[1].x { let command = KeyCommand::ParentCursorMoveDown(1); if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } else if x < layout_rect[2].x { let command = KeyCommand::CursorMoveDown(1); if let Err(e) = command.execute(context, backend) { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } else { // TODO: scroll in child list @@ -163,7 +163,7 @@ pub fn process_mouse(event: MouseEvent, context: &mut AppContext, backend: &mut } else { cursor_move::cursor_move(new_index, context) } { - context.push_msg(e.to_string()); + context.message_queue_mut().push_error(e.to_string()); } } } else { |