Skip to content

Commit

Permalink
refactor: Revert tui upgrade to 0.10
Browse files Browse the repository at this point in the history
Reverts tui upgrade, there are some bugs and issues - namely, issues with rendering text.
We can revert this commit when those bugs are dealt with (should be fine after 0.10.1, tested building from the repo).
  • Loading branch information
ClementTsang authored Aug 16, 2020
1 parent 9068108 commit 08e49b6
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 426 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ serde = {version = "1.0", features = ["derive"] }
unicode-segmentation = "1.6.0"
unicode-width = "0.1.7"
# tui = {version = "0.10.0", features = ["crossterm"], default-features = false, git = "https://github.com/fdehau/tui-rs.git"}
tui = {version = "0.10.0", features = ["crossterm"], default-features = false }
tui = {version = "0.9.5", features = ["crossterm"], default-features = false }

# For debugging only...
fern = "0.6.0"
Expand Down
79 changes: 44 additions & 35 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
text::{Span, Spans},
widgets::Text,
Frame, Terminal,
};

Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct Painter {
pub colours: CanvasColours,
height: u16,
width: u16,
styled_help_text: Vec<Spans<'static>>,
styled_help_text: Vec<Text<'static>>,
is_mac_os: bool,
row_constraints: Vec<Constraint>,
col_constraints: Vec<Vec<Constraint>>,
Expand Down Expand Up @@ -163,27 +163,28 @@ impl Painter {
styled_help_spans.extend(
section
.iter()
.map(|&text| Span::styled(text, self.colours.text_style))
.map(|&text| Text::styled(text, self.colours.text_style))
.collect::<Vec<_>>(),
);
} else {
// Not required check but it runs only a few times... so whatever ig, prevents me from
// being dumb and leaving a help text section only one line long.
if section.len() > 1 {
styled_help_spans.push(Span::from(""));
styled_help_spans.push(Text::raw("\n\n"));
styled_help_spans
.push(Span::styled(section[0], self.colours.table_header_style));
.push(Text::styled(section[0], self.colours.table_header_style));
styled_help_spans.extend(
section[1..]
.iter()
.map(|&text| Span::styled(text, self.colours.text_style))
.map(|&text| Text::styled(text, self.colours.text_style))
.collect::<Vec<_>>(),
);
}
}
});

self.styled_help_text = styled_help_spans.into_iter().map(Spans::from).collect();
// self.styled_help_text = styled_help_spans.into_iter().map(Spans::from).collect();
self.styled_help_text = styled_help_spans;
}

pub fn draw_data<B: Backend>(
Expand Down Expand Up @@ -251,36 +252,44 @@ impl Painter {

let dd_text = self.get_dd_spans(app_state);

let (text_width, text_height) = if let Some(dd_text) = &dd_text {
let width = if f.size().width < 100 {
let (text_width, text_height) = (
if f.size().width < 100 {
f.size().width * 90 / 100
} else {
let min_possible_width = (f.size().width * 50 / 100) as usize;
let mut width = dd_text.width();

// This should theoretically never allow width to be 0... we can be safe and do an extra check though.
while width > (f.size().width as usize) && width / 2 > min_possible_width {
width /= 2;
}

std::cmp::max(width, min_possible_width) as u16
};

(
width,
(dd_text.height() + 2 + (dd_text.width() / width as usize)) as u16,
)
} else {
// AFAIK this shouldn't happen, unless something went wrong...
(
if f.size().width < 100 {
f.size().width * 90 / 100
} else {
f.size().width * 50 / 100
},
7,
)
};
f.size().width * 50 / 100
},
7,
);
// let (text_width, text_height) = if let Some(dd_text) = &dd_text {
// let width = if f.size().width < 100 {
// f.size().width * 90 / 100
// } else {
// let min_possible_width = (f.size().width * 50 / 100) as usize;
// let mut width = dd_text.width();

// // This should theoretically never allow width to be 0... we can be safe and do an extra check though.
// while width > (f.size().width as usize) && width / 2 > min_possible_width {
// width /= 2;
// }

// std::cmp::max(width, min_possible_width) as u16
// };

// (
// width,
// (dd_text.height() + 2 + (dd_text.width() / width as usize)) as u16,
// )
// } else {
// // AFAIK this shouldn't happen, unless something went wrong...
// (
// if f.size().width < 100 {
// f.size().width * 90 / 100
// } else {
// f.size().width * 50 / 100
// },
// 7,
// )
// };

let vertical_bordering = f.size().height.saturating_sub(text_height) / 2;
let vertical_dialog_chunk = Layout::default()
Expand Down
2 changes: 1 addition & 1 deletion src/canvas/canvas_colours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl CanvasColours {
}

pub fn set_table_header_colour(&mut self, colour: &str) -> error::Result<()> {
self.table_header_style = get_style_from_config(colour)?.add_modifier(Modifier::BOLD);
self.table_header_style = get_style_from_config(colour)?.modifier(Modifier::BOLD);
Ok(())
}

Expand Down
149 changes: 82 additions & 67 deletions src/canvas/dialogs/dd_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use tui::{
backend::Backend,
layout::{Alignment, Rect},
terminal::Frame,
text::{Span, Spans, Text},
widgets::{Block, Borders, Paragraph, Wrap},
widgets::{Block, Borders, Paragraph, Text},
};

use crate::{app::App, canvas::Painter};
Expand All @@ -12,106 +11,122 @@ const DD_BASE: &str = " Confirm Kill Process ── Esc to close ";
const DD_ERROR_BASE: &str = " Error ── Esc to close ";

pub trait KillDialog {
fn get_dd_spans(&self, app_state: &App) -> Option<Text<'_>>;
fn get_dd_spans(&self, app_state: &App) -> Option<Vec<Text<'_>>>;

fn draw_dd_dialog<B: Backend>(
&self, f: &mut Frame<'_, B>, dd_text: Option<Text<'_>>, app_state: &App, draw_loc: Rect,
&self, f: &mut Frame<'_, B>, dd_text: Option<Vec<Text<'_>>>, app_state: &App,
draw_loc: Rect,
) -> bool;
}

impl KillDialog for Painter {
fn get_dd_spans(&self, app_state: &App) -> Option<Text<'_>> {
fn get_dd_spans(&self, app_state: &App) -> Option<Vec<Text<'_>>> {
if let Some(dd_err) = &app_state.dd_err {
return Some(Text::from(Spans::from(format!(
"Failure to properly kill the process - {}",
dd_err
))));
return Some(vec![
Text::raw("\n"),
Text::raw(format!("Failure to properly kill the process - {}", dd_err)),
]);
} else if let Some(to_kill_processes) = app_state.get_to_delete_processes() {
if let Some(first_pid) = to_kill_processes.1.first() {
return Some(Text::from(vec![
Spans::from(vec![]),
Spans::from(vec![
if app_state.is_grouped(app_state.current_widget.widget_id) {
if to_kill_processes.1.len() != 1 {
Span::from(format!(
"Kill {} processes with the name \"{}\"?",
to_kill_processes.1.len(),
to_kill_processes.0
))
} else {
Span::from(format!(
"Kill 1 process with the name \"{}\"?",
to_kill_processes.0
))
}
} else {
Span::from(format!(
"Kill process \"{}\" with PID {}?",
to_kill_processes.0, first_pid
return Some(vec![
Text::raw("\n"),
if app_state.is_grouped(app_state.current_widget.widget_id) {
if to_kill_processes.1.len() != 1 {
Text::raw(format!(
"Kill {} processes with the name \"{}\"?",
to_kill_processes.1.len(),
to_kill_processes.0
))
},
]),
Spans::from(vec![]),
Spans::from(vec![
if app_state.delete_dialog_state.is_on_yes {
Span::styled("Yes", self.colours.currently_selected_text_style)
} else {
Span::from("Yes")
},
Span::from(" "),
if app_state.delete_dialog_state.is_on_yes {
Span::from("No")
} else {
Span::styled("No", self.colours.currently_selected_text_style)
},
]),
Spans::from(vec![]),
]));
Text::raw(format!(
"Kill 1 process with the name \"{}\"?",
to_kill_processes.0
))
}
} else {
Text::raw(format!(
"Kill process \"{}\" with PID {}?",
to_kill_processes.0, first_pid
))
},
Text::raw("\n\n"),
if app_state.delete_dialog_state.is_on_yes {
Text::styled("Yes", self.colours.currently_selected_text_style)
} else {
Text::raw("Yes")
},
Text::raw(" "),
if app_state.delete_dialog_state.is_on_yes {
Text::raw("No")
} else {
Text::styled("No", self.colours.currently_selected_text_style)
},
Text::raw("\n"),
]);
}
}

None
}

fn draw_dd_dialog<B: Backend>(
&self, f: &mut Frame<'_, B>, dd_text: Option<Text<'_>>, app_state: &App, draw_loc: Rect,
&self, f: &mut Frame<'_, B>, dd_text: Option<Vec<Text<'_>>>, app_state: &App,
draw_loc: Rect,
) -> bool {
if let Some(dd_text) = dd_text {
// let dd_title = if app_state.dd_err.is_some() {
// Text::styled(
// format!(
// " Error ─{}─ Esc to close ",
// "─".repeat(
// usize::from(draw_loc.width)
// .saturating_sub(DD_ERROR_BASE.chars().count() + 2)
// )
// ),
// self.colours.border_style,
// )
// } else {
// Text::styled(
// format!(
// " Confirm Kill Process ─{}─ Esc to close ",
// "─".repeat(
// usize::from(draw_loc.width).saturating_sub(DD_BASE.chars().count() + 2)
// )
// ),
// self.colours.border_style,
// )
// };

let dd_title = if app_state.dd_err.is_some() {
Span::styled(
format!(
" Error ─{}─ Esc to close ",
"─".repeat(
usize::from(draw_loc.width)
.saturating_sub(DD_ERROR_BASE.chars().count() + 2)
)
),
self.colours.border_style,
format!(
" Error ─{}─ Esc to close ",
"─".repeat(
usize::from(draw_loc.width)
.saturating_sub(DD_ERROR_BASE.chars().count() + 2)
)
)
} else {
Span::styled(
format!(
" Confirm Kill Process ─{}─ Esc to close ",
"─".repeat(
usize::from(draw_loc.width).saturating_sub(DD_BASE.chars().count() + 2)
)
),
self.colours.border_style,
format!(
" Confirm Kill Process ─{}─ Esc to close ",
"─".repeat(
usize::from(draw_loc.width).saturating_sub(DD_BASE.chars().count() + 2)
)
)
};

f.render_widget(
Paragraph::new(dd_text)
Paragraph::new(dd_text.iter())
.block(
Block::default()
.title(dd_title)
.title(&dd_title)
.title_style(self.colours.border_style)
.style(self.colours.border_style)
.borders(Borders::ALL)
.border_style(self.colours.border_style),
)
.style(self.colours.text_style)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true }),
.wrap(true),
draw_loc,
);

Expand Down
Loading

0 comments on commit 08e49b6

Please sign in to comment.