diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-08-29 22:06:19 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-08-29 22:08:23 -0400 |
commit | 5be4a5f472655a76e1430bad09a19f6ad111e474 (patch) | |
tree | 1fcffa6c8d37cc6d538b29b6fbd773e8de58512d /src/ui | |
parent | 4f3842b56f1729dcd8e81c77f98253ed9dfb23b3 (diff) |
big rework and dependency update
- abstract JoshutoContext implementation behind functions
- rework io workers in an attempt to fix a bug
- update dependencies
- remove JoshutoContextWorker
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/tui_backend.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_dirlist.rs | 6 | ||||
-rw-r--r-- | src/ui/widgets/tui_dirlist_detailed.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_footer.rs | 23 | ||||
-rw-r--r-- | src/ui/widgets/tui_menu.rs | 6 | ||||
-rw-r--r-- | src/ui/widgets/tui_prompt.rs | 11 | ||||
-rw-r--r-- | src/ui/widgets/tui_tab.rs | 18 | ||||
-rw-r--r-- | src/ui/widgets/tui_textfield.rs | 32 | ||||
-rw-r--r-- | src/ui/widgets/tui_topbar.rs | 24 | ||||
-rw-r--r-- | src/ui/widgets/tui_view.rs | 38 |
10 files changed, 98 insertions, 64 deletions
diff --git a/src/ui/tui_backend.rs b/src/ui/tui_backend.rs index 7ab348d..395f081 100644 --- a/src/ui/tui_backend.rs +++ b/src/ui/tui_backend.rs @@ -30,7 +30,7 @@ impl TuiBackend { where W: Widget, { - self.terminal_mut().draw(|mut frame| { + self.terminal_mut().draw(|frame| { let rect = frame.size(); frame.render_widget(widget, rect); }); diff --git a/src/ui/widgets/tui_dirlist.rs b/src/ui/widgets/tui_dirlist.rs index 8f75317..05f6fc9 100644 --- a/src/ui/widgets/tui_dirlist.rs +++ b/src/ui/widgets/tui_dirlist.rs @@ -55,7 +55,7 @@ impl<'a> Widget for TuiDirList<'a> { let name_width = name.width(); let style = if i == screen_index { - entry.get_style().modifier(Modifier::REVERSED) + entry.get_style().add_modifier(Modifier::REVERSED) } else { entry.get_style() }; @@ -75,6 +75,10 @@ impl<'a> Widget for TuiDirList<'a> { None => { buf.set_stringn(x, y + i as u16, name, area_width, style); } + Some(0) => { + let file_name_width = area_width; + buf.set_stringn(x, y + i as u16, &name, file_name_width, style); + } Some(p_ind) => { let ext_width = name[p_ind..].width(); let file_name_width = area_width - ext_width - 1; diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs index 114b499..27ddbc8 100644 --- a/src/ui/widgets/tui_dirlist_detailed.rs +++ b/src/ui/widgets/tui_dirlist_detailed.rs @@ -55,7 +55,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> { let name_width = name.width(); let style = if i == screen_index { - entry.get_style().modifier(Modifier::REVERSED) + entry.get_style().add_modifier(Modifier::REVERSED) } else { entry.get_style() }; diff --git a/src/ui/widgets/tui_footer.rs b/src/ui/widgets/tui_footer.rs index e8297ed..b9d3a08 100644 --- a/src/ui/widgets/tui_footer.rs +++ b/src/ui/widgets/tui_footer.rs @@ -1,7 +1,8 @@ use tui::buffer::Buffer; use tui::layout::Rect; use tui::style::{Color, Style}; -use tui::widgets::{Paragraph, Text, Widget}; +use tui::text::{Span, Spans}; +use tui::widgets::{Paragraph, Widget}; use crate::fs::{FileType, JoshutoDirEntry}; use crate::util::format; @@ -38,22 +39,22 @@ impl<'a> Widget for TuiFooter<'a> { }; let mut text = vec![ - Text::styled(mode, mode_style), - Text::raw(" "), - Text::raw(mtime), - Text::raw(" "), - Text::raw(size), + Span::styled(mode, mode_style), + Span::raw(" "), + Span::raw(mtime), + Span::raw(" "), + Span::raw(size), #[cfg(unix)] - Text::raw(" "), + Span::raw(" "), #[cfg(unix)] - Text::raw(mimetype), + Span::raw(mimetype), ]; if let FileType::Symlink(s) = &self.entry.metadata.file_type { - text.push(Text::styled(" -> ", mode_style)); - text.push(Text::styled(s, mode_style)); + text.push(Span::styled(" -> ", mode_style)); + text.push(Span::styled(s, mode_style)); } - Paragraph::new(text.iter()).wrap(true).render(area, buf); + Paragraph::new(Spans::from(text)).render(area, buf); } } diff --git a/src/ui/widgets/tui_menu.rs b/src/ui/widgets/tui_menu.rs index 36de8ac..09c1f75 100644 --- a/src/ui/widgets/tui_menu.rs +++ b/src/ui/widgets/tui_menu.rs @@ -4,7 +4,7 @@ use termion::event::Key; use tui::buffer::Buffer; use tui::layout::Rect; use tui::style::Style; -use tui::widgets::Widget; +use tui::widgets::{Block, Borders, Widget}; use unicode_width::UnicodeWidthStr; use super::TuiView; @@ -35,7 +35,7 @@ impl TuiCommandMenu { context.events.flush(); loop { - terminal.draw(|mut frame| { + terminal.draw(|frame| { let f_size: Rect = frame.size(); { @@ -123,6 +123,8 @@ impl<'a> Widget for TuiMenu<'a> { let area_x = area.x + 1; let area_y = area.y + 1; + Block::default().borders(Borders::TOP).render(area, buf); + for (i, text) in text_iter.enumerate() { let width = text.width(); buf.set_stringn(area_x, area_y + i as u16, text, width, style); diff --git a/src/ui/widgets/tui_prompt.rs b/src/ui/widgets/tui_prompt.rs index 491c95c..2da3ba9 100644 --- a/src/ui/widgets/tui_prompt.rs +++ b/src/ui/widgets/tui_prompt.rs @@ -1,7 +1,8 @@ use termion::event::Key; use tui::layout::Rect; use tui::style::{Color, Style}; -use tui::widgets::{Paragraph, Text}; +use tui::text::Span; +use tui::widgets::{Paragraph, Wrap}; use crate::context::JoshutoContext; use crate::ui::TuiBackend; @@ -23,7 +24,7 @@ impl<'a> TuiPrompt<'a> { context.events.flush(); loop { - terminal.draw(|mut frame| { + terminal.draw(|frame| { let f_size: Rect = frame.size(); if f_size.height == 0 { return; @@ -37,7 +38,7 @@ impl<'a> TuiPrompt<'a> { let prompt_style = Style::default().fg(Color::LightYellow); - let text = [Text::styled(self.prompt, prompt_style)]; + let text = Span::styled(self.prompt, prompt_style); let textfield_rect = Rect { x: 0, @@ -46,7 +47,9 @@ impl<'a> TuiPrompt<'a> { height: 1, }; - frame.render_widget(Paragraph::new(text.iter()).wrap(true), textfield_rect); + frame.render_widget( + Paragraph::new(text).wrap(Wrap { trim: true }), + textfield_rect); }); if let Ok(event) = context.events.next() { diff --git a/src/ui/widgets/tui_tab.rs b/src/ui/widgets/tui_tab.rs index fd74a9c..76fb6e7 100644 --- a/src/ui/widgets/tui_tab.rs +++ b/src/ui/widgets/tui_tab.rs @@ -1,7 +1,8 @@ use tui::buffer::Buffer; use tui::layout::Rect; use tui::style::{Modifier, Style}; -use tui::widgets::{Paragraph, Text, Widget}; +use tui::text::{Span, Spans}; +use tui::widgets::{Paragraph, Widget, Wrap}; pub struct TuiTabBar<'a> { name: &'a str, @@ -17,13 +18,16 @@ impl<'a> TuiTabBar<'a> { impl<'a> Widget for TuiTabBar<'a> { fn render(self, area: Rect, buf: &mut Buffer) { - let selected = Style::default().modifier(Modifier::REVERSED); + let selected = Style::default() + .add_modifier(Modifier::REVERSED); - let text = [ - Text::styled(format!("{}: {}", self.curr + 1, self.name), selected), - Text::raw(format!("/{}", self.len)), - ]; + let text = Spans::from(vec![ + Span::styled(format!("{}: {}", self.curr + 1, self.name), selected), + Span::raw(format!("/{}", self.len)), + ]); - Paragraph::new(text.iter()).wrap(true).render(area, buf); + Paragraph::new(text) + .wrap(Wrap { trim: true }) + .render(area, buf); } } diff --git a/src/ui/widgets/tui_textfield.rs b/src/ui/widgets/tui_textfield.rs index a57da5d..08696e6 100644 --- a/src/ui/widgets/tui_textfield.rs +++ b/src/ui/widgets/tui_textfield.rs @@ -4,7 +4,8 @@ use rustyline::line_buffer; use termion::event::Key; use tui::layout::Rect; use tui::style::{Color, Modifier, Style}; -use tui::widgets::{Paragraph, Text}; +use tui::text::{Span, Spans}; +use tui::widgets::{Paragraph, Wrap}; use crate::context::JoshutoContext; use crate::ui::TuiBackend; @@ -38,22 +39,22 @@ pub struct TuiTextField<'a> { } impl<'a> TuiTextField<'a> { - pub fn menu(mut self, menu: TuiMenu<'a>) -> Self { + pub fn menu(&mut self, menu: TuiMenu<'a>) -> &mut Self { self._menu = Some(menu); self } - pub fn prompt(mut self, prompt: &'a str) -> Self { + pub fn prompt(&mut self, prompt: &'a str) -> &mut Self { self._prompt = prompt; self } - pub fn prefix(mut self, prefix: &'a str) -> Self { + pub fn prefix(&mut self, prefix: &'a str) -> &mut Self { self._prefix = prefix; self } - pub fn suffix(mut self, suffix: &'a str) -> Self { + pub fn suffix(&mut self, suffix: &'a str) -> &mut Self { self._suffix = suffix; self } @@ -80,7 +81,7 @@ impl<'a> TuiTextField<'a> { loop { terminal - .draw(|mut frame| { + .draw(|frame| { let f_size: Rect = frame.size(); if f_size.height == 0 { return; @@ -112,7 +113,8 @@ impl<'a> TuiTextField<'a> { let cursor_xpos = line_buffer.pos(); let cmd_prompt_style = Style::default().fg(Color::LightGreen); - let cursor_style = Style::default().modifier(Modifier::REVERSED); + let cursor_style = Style::default() + .add_modifier(Modifier::REVERSED); let prefix = &line_buffer.as_str()[..cursor_xpos]; @@ -127,12 +129,12 @@ impl<'a> TuiTextField<'a> { let curr_string = curr.to_string(); - let text = [ - Text::styled(self._prompt, cmd_prompt_style), - Text::raw(prefix), - Text::styled(curr_string, cursor_style), - Text::raw(suffix), - ]; + let text = Spans::from(vec![ + Span::styled(self._prompt, cmd_prompt_style), + Span::raw(prefix), + Span::styled(curr_string, cursor_style), + Span::raw(suffix), + ]); let textfield_rect = Rect { x: 0, @@ -141,7 +143,9 @@ impl<'a> TuiTextField<'a> { height: 1, }; - frame.render_widget(Paragraph::new(text.iter()).wrap(true), textfield_rect); + frame.render_widget( + Paragraph::new(text).wrap(Wrap { trim: true }), + textfield_rect); }) .unwrap(); diff --git a/src/ui/widgets/tui_topbar.rs b/src/ui/widgets/tui_topbar.rs index eabbb96..eb9b3a0 100644 --- a/src/ui/widgets/tui_topbar.rs +++ b/src/ui/widgets/tui_topbar.rs @@ -3,7 +3,8 @@ use std::path::Path; use tui::buffer::Buffer; use tui::layout::Rect; use tui::style::{Color, Modifier, Style}; -use tui::widgets::{Paragraph, Text, Widget}; +use tui::text::{Span, Spans}; +use tui::widgets::{Paragraph, Widget, Wrap}; use crate::{HOSTNAME, USERNAME}; @@ -21,19 +22,24 @@ impl<'a> Widget for TuiTopBar<'a> { fn render(self, area: Rect, buf: &mut Buffer) { let username_style = Style::default() .fg(Color::LightGreen) - .modifier(Modifier::BOLD); + .add_modifier(Modifier::BOLD); let path_style = Style::default() .fg(Color::LightBlue) - .modifier(Modifier::BOLD); + .add_modifier(Modifier::BOLD); let curr_path_str = self.path.to_string_lossy(); - let text = [ - Text::styled(format!("{}@{} ", *USERNAME, *HOSTNAME), username_style), - Text::styled(curr_path_str, path_style), - ]; - - Paragraph::new(text.iter()).wrap(true).render(area, buf); + let text = Spans::from(vec![ + Span::styled(USERNAME.as_str(), username_style), + Span::styled("@", username_style), + Span::styled(HOSTNAME.as_str(), username_style), + Span::styled(" ", username_style), + Span::styled(curr_path_str, path_style), + ]); + + Paragraph::new(text) + .wrap(Wrap { trim: true }) + .render(area, buf); } } diff --git a/src/ui/widgets/tui_view.rs b/src/ui/widgets/tui_view.rs index 54bfe58..48345bc 100644 --- a/src/ui/widgets/tui_view.rs +++ b/src/ui/widgets/tui_view.rs @@ -1,7 +1,8 @@ use tui::buffer::Buffer; use tui::layout::{Direction, Layout, Rect}; use tui::style::{Color, Style}; -use tui::widgets::{Paragraph, Text, Widget}; +use tui::text::Span; +use tui::widgets::{Paragraph, Widget, Wrap}; use super::{TuiDirList, TuiDirListDetailed, TuiFooter, TuiTabBar, TuiTopBar}; use crate::context::JoshutoContext; @@ -28,7 +29,7 @@ impl<'a> Widget for TuiView<'a> { fn render(self, area: Rect, buf: &mut Buffer) { let f_size = area; - let curr_tab = self.context.curr_tab_ref(); + let curr_tab = self.context.tab_context_ref().curr_tab_ref(); let curr_list = curr_tab.curr_list_ref(); let parent_list = curr_tab.parent_list_ref(); @@ -45,9 +46,7 @@ impl<'a> Widget for TuiView<'a> { .split(f_size); { - let curr_path = curr_tab.curr_path.as_path(); - - if self.context.tabs.len() > 1 { + if self.context.tab_context_ref().len() > 1 { let topbar_width = if f_size.width > TAB_VIEW_WIDTH { f_size.width - TAB_VIEW_WIDTH } else { @@ -60,7 +59,7 @@ impl<'a> Widget for TuiView<'a> { width: topbar_width, height: 1, }; - TuiTopBar::new(curr_path).render(rect, buf); + TuiTopBar::new(curr_tab.pwd()).render(rect, buf); let rect = Rect { x: topbar_width, @@ -68,13 +67,17 @@ impl<'a> Widget for TuiView<'a> { width: TAB_VIEW_WIDTH, height: 1, }; - let name = if let Some(ostr) = curr_path.file_name() { + let name = if let Some(ostr) = curr_tab.pwd().file_name() { ostr.to_str().unwrap_or("") } else { "" }; - TuiTabBar::new(name, self.context.curr_tab_index, self.context.tabs.len()) - .render(rect, buf); + TuiTabBar::new( + name, + self.context.tab_context_ref().get_index(), + self.context.tab_context_ref().len(), + ) + .render(rect, buf); } else { let topbar_width = f_size.width; @@ -84,7 +87,7 @@ impl<'a> Widget for TuiView<'a> { width: topbar_width, height: 1, }; - TuiTopBar::new(curr_path).render(rect, buf); + TuiTopBar::new(curr_tab.pwd()).render(rect, buf); } } @@ -105,10 +108,17 @@ impl<'a> Widget for TuiView<'a> { if self.show_bottom_status { /* draw the bottom status bar */ - if !self.context.message_queue.is_empty() { - let text = [Text::styled(&self.context.message_queue[0], message_style)]; - - Paragraph::new(text.iter()).wrap(true).render(rect, buf); + if let Some(msg) = self.context.worker_msg() { + let text = Span::styled(msg.as_str(), message_style); + Paragraph::new(text) + .wrap(Wrap { trim: true }) + .render(rect, buf); + } else if !self.context.message_queue.is_empty() { + let text = Span::styled(&self.context.message_queue[0], + message_style); + Paragraph::new(text) + .wrap(Wrap { trim: true }) + .render(rect, buf); } else if let Some(entry) = curr_list.get_curr_ref() { TuiFooter::new(entry).render(rect, buf); } |