summaryrefslogtreecommitdiffstats
path: root/src/canvas/widgets/process_table.rs
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2020-09-26 20:04:34 -0400
committerClementTsang <cjhtsang@uwaterloo.ca>2020-09-26 20:21:59 -0400
commit750d8f3cb747103cca7a2a4afe8ff41ff1318b6f (patch)
treeac34a6a538c1f94cb395b20a23c9186b749ca6e6 /src/canvas/widgets/process_table.rs
parent6db76029e2419d53c81cb2111e487f83ee248a2f (diff)
refactor: tui-rs 0.11.0 refactor (#253)
Refactors tui-rs usage to the new 0.11.0 release. This release also fixes the highlighting bug from #249, and now, expanding a widget no longer overrides the widget title colour. This commit also introduces #255, but that seems to be easy to bandaid so hopefully it will get fixed soon?
Diffstat (limited to 'src/canvas/widgets/process_table.rs')
-rw-r--r--src/canvas/widgets/process_table.rs120
1 files changed, 57 insertions, 63 deletions
diff --git a/src/canvas/widgets/process_table.rs b/src/canvas/widgets/process_table.rs
index 89a27ba9..970dcda1 100644
--- a/src/canvas/widgets/process_table.rs
+++ b/src/canvas/widgets/process_table.rs
@@ -11,7 +11,8 @@ use tui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
terminal::Frame,
- widgets::{Block, Borders, Paragraph, Row, Table, Text},
+ text::{Span, Spans},
+ widgets::{Block, Borders, Paragraph, Row, Table},
};
use std::borrow::Cow;
@@ -122,7 +123,7 @@ impl ProcessTableWidget for Painter {
.direction(Direction::Horizontal)
.split(draw_loc)[0];
- let (border_and_title_style, highlight_style) = if is_on_widget {
+ let (border_style, highlight_style) = if is_on_widget {
(
self.colours.highlighted_border_style,
self.colours.currently_selected_text_style,
@@ -131,41 +132,35 @@ impl ProcessTableWidget for Painter {
(self.colours.border_style, self.colours.text_style)
};
- let title = if draw_border {
- if app_state.is_expanded
- && !proc_widget_state
- .process_search_state
- .search_state
- .is_enabled
- && !proc_widget_state.is_sort_open
- {
- const TITLE_BASE: &str = " Processes ── Esc to go back ";
- format!(
- " Processes ─{}─ Esc to go back ",
- "─".repeat(
- usize::from(draw_loc.width)
- .saturating_sub(TITLE_BASE.chars().count() + 2)
- )
- )
- } else {
- " Processes ".to_string()
- }
- } else {
- String::default()
- };
-
- let title_style = if app_state.is_expanded {
- border_and_title_style
+ let title = if app_state.is_expanded
+ && !proc_widget_state
+ .process_search_state
+ .search_state
+ .is_enabled
+ && !proc_widget_state.is_sort_open
+ {
+ const TITLE_BASE: &str = " Processes ── Esc to go back ";
+ Spans::from(vec![
+ Span::styled(" Processes ", self.colours.widget_title_style),
+ Span::styled(
+ format!(
+ "─{}─ Esc to go back ",
+ "─".repeat(usize::from(draw_loc.width).saturating_sub(
+ UnicodeSegmentation::graphemes(TITLE_BASE, true).count() + 2
+ ))
+ ),
+ border_style,
+ ),
+ ])
} else {
- self.colours.widget_title_style
+ Spans::from(Span::styled(" Processes ", self.colours.widget_title_style))
};
let process_block = if draw_border {
Block::default()
- .title(&title)
- .title_style(title_style)
+ .title(title)
.borders(Borders::ALL)
- .border_style(border_and_title_style)
+ .border_style(border_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
@@ -459,7 +454,7 @@ impl ProcessTableWidget for Painter {
is_on_widget: bool, grapheme_indices: GraphemeIndices<'a>, start_position: usize,
cursor_position: usize, query: &str, currently_selected_text_style: tui::style::Style,
text_style: tui::style::Style,
- ) -> Vec<Text<'a>> {
+ ) -> Vec<Span<'a>> {
let mut current_grapheme_posn = 0;
if is_on_widget {
@@ -471,9 +466,9 @@ impl ProcessTableWidget for Painter {
None
} else {
let styled = if grapheme.0 == cursor_position {
- Text::styled(grapheme.1, currently_selected_text_style)
+ Span::styled(grapheme.1, currently_selected_text_style)
} else {
- Text::styled(grapheme.1, text_style)
+ Span::styled(grapheme.1, text_style)
};
Some(styled)
}
@@ -481,7 +476,7 @@ impl ProcessTableWidget for Painter {
.collect::<Vec<_>>();
if cursor_position >= query.len() {
- res.push(Text::styled(" ", currently_selected_text_style))
+ res.push(Span::styled(" ", currently_selected_text_style))
}
res
@@ -495,7 +490,7 @@ impl ProcessTableWidget for Painter {
if current_grapheme_posn <= start_position {
None
} else {
- let styled = Text::styled(grapheme.1, text_style);
+ let styled = Span::styled(grapheme.1, text_style);
Some(styled)
}
})
@@ -543,8 +538,8 @@ impl ProcessTableWidget for Painter {
self.colours.text_style,
);
- let mut search_text = {
- let mut search_vec = vec![Text::styled(
+ let mut search_text = vec![Spans::from({
+ let mut search_vec = vec![Span::styled(
search_title,
if is_on_widget {
self.colours.table_header_style
@@ -554,7 +549,7 @@ impl ProcessTableWidget for Painter {
)];
search_vec.extend(query_with_cursor);
search_vec
- };
+ })];
// Text options shamelessly stolen from VS Code.
let case_style = if !proc_widget_state.process_search_state.is_ignoring_case {
@@ -583,26 +578,24 @@ impl ProcessTableWidget for Painter {
// FIXME: [MOUSE] Mouse support for these in search
// FIXME: [MOVEMENT] Movement support for these in search
- let option_text = vec![
- Text::raw("\n"),
- Text::styled(
+ let option_text = Spans::from(vec![
+ Span::styled(
format!("Case({})", if self.is_mac_os { "F1" } else { "Alt+C" }),
case_style,
),
- Text::raw(" "),
- Text::styled(
+ Span::raw(" "),
+ Span::styled(
format!("Whole({})", if self.is_mac_os { "F2" } else { "Alt+W" }),
whole_word_style,
),
- Text::raw(" "),
- Text::styled(
+ Span::raw(" "),
+ Span::styled(
format!("Regex({})", if self.is_mac_os { "F3" } else { "Alt+R" }),
regex_style,
),
- ];
+ ]);
- search_text.push(Text::raw("\n"));
- search_text.push(Text::styled(
+ search_text.push(Spans::from(Span::styled(
if let Some(err) = &proc_widget_state
.process_search_state
.search_state
@@ -613,8 +606,8 @@ impl ProcessTableWidget for Painter {
""
},
self.colours.invalid_query_style,
- ));
- search_text.extend(option_text);
+ )));
+ search_text.push(option_text);
let current_border_style = if proc_widget_state
.process_search_state
@@ -628,20 +621,21 @@ impl ProcessTableWidget for Painter {
self.colours.border_style
};
- let title = if draw_border {
- const TITLE_BASE: &str = " Esc to close ";
-
- let repeat_num =
- usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2);
- format!("{} Esc to close ", "─".repeat(repeat_num))
- } else {
- String::new()
- };
+ let title = Span::styled(
+ if draw_border {
+ const TITLE_BASE: &str = " Esc to close ";
+ let repeat_num =
+ usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2);
+ format!("{} Esc to close ", "─".repeat(repeat_num))
+ } else {
+ String::new()
+ },
+ current_border_style,
+ );
let process_search_block = if draw_border {
Block::default()
- .title(&title)
- .title_style(current_border_style)
+ .title(title)
.borders(Borders::ALL)
.border_style(current_border_style)
} else if is_on_widget {
@@ -659,7 +653,7 @@ impl ProcessTableWidget for Painter {
.split(draw_loc)[0];
f.render_widget(
- Paragraph::new(search_text.iter())
+ Paragraph::new(search_text)
.block(process_search_block)
.style(self.colours.text_style)
.alignment(Alignment::Left),