refactor: Impl Copy where able to

This commit is contained in:
Jack Wills
2022-10-01 19:57:27 +00:00
parent 15597dbe69
commit e76878f424
14 changed files with 138 additions and 118 deletions
+5 -5
View File
@@ -7,7 +7,7 @@ pub mod log_sanitizer {
};
/// Attempt to colorize the given string to tui-rs standards
pub fn colorize_logs(input: &str) -> Vec<Spans<'static>> {
pub fn colorize_logs<'a>(input: &str) -> Vec<Spans<'a>> {
vec![Spans::from(
categorise_text(input)
.into_iter()
@@ -40,17 +40,17 @@ pub mod log_sanitizer {
}
/// Remove all ansi formatting from a given string and create tui-rs spans
pub fn remove_ansi(input: &str) -> Vec<Spans<'static>> {
pub fn remove_ansi<'a>(input: &str) -> Vec<Spans<'a>> {
let mut output = String::from("");
for i in categorise_text(input) {
output.push_str(i.text);
}
raw(output)
raw(&output)
}
/// create tui-rs spans that exactly match the given strings
pub fn raw(input: String) -> Vec<Spans<'static>> {
vec![Spans::from(Span::raw(input))]
pub fn raw<'a>(input: &str) -> Vec<Spans<'a>> {
vec![Spans::from(Span::raw(input.to_owned()))]
}
/// Change from ansi to tui colors
+16 -12
View File
@@ -39,6 +39,8 @@ const REPO: &str = env!("CARGO_PKG_REPOSITORY");
const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
const ORANGE: Color = Color::Rgb(255, 178, 36);
const MARGIN: &str = " ";
const ARROW: &str = "";
const CIRCLE: &str ="";
/// Generate block, add a border if is the selected panel,
/// add custom title based on state of each panel
@@ -99,7 +101,7 @@ pub fn commands<B: Backend>(
let items = List::new(items)
.block(block)
.highlight_style(Style::default().add_modifier(Modifier::BOLD))
.highlight_symbol("");
.highlight_symbol(ARROW);
f.render_stateful_widget(
items,
@@ -201,7 +203,7 @@ pub fn containers<B: Backend>(
let items = List::new(items)
.block(block)
.highlight_style(Style::default().add_modifier(Modifier::BOLD))
.highlight_symbol("");
.highlight_symbol(CIRCLE);
f.render_stateful_widget(items, area, &mut app_data.lock().containers.state);
}
@@ -237,7 +239,7 @@ pub fn logs<B: Backend>(
let items = List::new(items)
.block(block)
.highlight_symbol("")
.highlight_symbol(ARROW)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
f.render_stateful_widget(
items,
@@ -281,8 +283,8 @@ pub fn chart<B: Backend>(
.data(&mem.0)];
let cpu_stats = CpuStats::new(cpu.0.last().map_or(0.00, |f| f.1));
let mem_stats = ByteStats::new(mem.0.last().map_or(0, |f| f.1 as u64));
let cpu_chart = make_chart(&cpu.2, "cpu", cpu_dataset, &cpu_stats, &cpu.1);
let mem_chart = make_chart(&mem.2, "memory", mem_dataset, &mem_stats, &mem.1);
let cpu_chart = make_chart(cpu.2, "cpu", cpu_dataset, &cpu_stats, &cpu.1);
let mem_chart = make_chart(mem.2, "memory", mem_dataset, &mem_stats, &mem.1);
f.render_widget(cpu_chart, area[0]);
f.render_widget(mem_chart, area[1]);
@@ -292,7 +294,7 @@ pub fn chart<B: Backend>(
/// Create charts
fn make_chart<'a, T: Stats + Display>(
state: &State,
state: State,
name: &'a str,
dataset: Vec<Dataset<'a>>,
current: &'a T,
@@ -340,14 +342,14 @@ fn make_chart<'a, T: Stats + Display>(
)
}
/// Draw heading bar at top of program, always visible
/// Draw heading bar at top of program, always visible
pub fn heading_bar<B: Backend>(
area: Rect,
columns: &Columns,
f: &mut Frame<'_, B>,
has_containers: bool,
loading_icon: &str,
sorted_by: &Option<(Header, SortedOrder)>,
sorted_by: Option<(Header, SortedOrder)>,
gui_state: &Arc<Mutex<GuiState>>,
) {
let block = || Block::default().style(Style::default().bg(Color::Magenta).fg(Color::Black));
@@ -412,7 +414,7 @@ pub fn heading_bar<B: Backend>(
(status, count)
};
// Meta data for iterate over to create blocks and correct widths
// Meta data to iterate over to create blocks with correct widths
let header_meta = [
(Header::State, columns.state.1),
(Header::Status, columns.status.1),
@@ -429,7 +431,7 @@ pub fn heading_bar<B: Backend>(
.iter()
.map(|i| {
let header_block = gen_header(&i.0, i.1);
(header_block.0, i.0.clone(), Constraint::Max(header_block.1))
(header_block.0, i.0, Constraint::Max(header_block.1))
})
.collect::<Vec<_>>();
@@ -469,11 +471,12 @@ pub fn heading_bar<B: Backend>(
.block(block())
.alignment(Alignment::Right);
// If no containers, don't display the headers, could maybe do this first?
let index = if has_containers { 1 } else { 0 };
f.render_widget(paragraph, split_bar[index]);
}
/// From a given &String, return the maximum number of chars on a single line
/// From a given &str, return the maximum number of chars on a single line
fn max_line_width(text: &str) -> usize {
let mut max_line_width = 0;
text.lines().into_iter().for_each(|line| {
@@ -486,6 +489,7 @@ fn max_line_width(text: &str) -> usize {
}
/// Draw the help box in the centre of the screen
/// TODO this is message, should make every line it's own renderable span
pub fn help_box<B: Backend>(f: &mut Frame<'_, B>) {
let title = format!(" {} ", VERSION);
@@ -564,7 +568,7 @@ pub fn help_box<B: Backend>(f: &mut Frame<'_, B>) {
}
/// Draw an error popup over whole screen
pub fn error<B: Backend>(f: &mut Frame<'_, B>, error: &AppError, seconds: Option<u8>) {
pub fn error<B: Backend>(f: &mut Frame<'_, B>, error: AppError, seconds: Option<u8>) {
let block = Block::default()
.title(" Error ")
.border_type(BorderType::Rounded)
+5 -4
View File
@@ -7,7 +7,7 @@ use uuid::Uuid;
use crate::app_data::Header;
#[derive(Debug, PartialEq, std::hash::Hash, std::cmp::Eq, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum SelectablePanel {
Containers,
Commands,
@@ -38,6 +38,7 @@ impl SelectablePanel {
}
}
#[derive(Debug, Copy, Clone)]
pub enum Region {
Panel(SelectablePanel),
Header(Header),
@@ -72,7 +73,7 @@ impl BoxLocation {
}
}
// Should combine and just return a tuple?
// Should combine with get_vertical_constraints and just return a tuple of (vc, hc)?
pub const fn get_horizontal_constraints(
self,
blank_vertical: u16,
@@ -221,7 +222,7 @@ impl GuiState {
.filter(|i| i.1.intersects(rect))
.collect::<Vec<_>>()
.get(0)
.map(|data| data.0.clone())
.map(|data| *data.0)
}
/// Insert, or updates header area panel into heading_map
@@ -260,7 +261,7 @@ impl GuiState {
pub fn get_loading(&mut self) -> String {
if self.is_loading.is_empty() {
String::from(" ")
} else {
} else {
self.loading_icon.to_string()
}
}
+7 -5
View File
@@ -93,7 +93,7 @@ async fn run_app<B: Backend + Send>(
break;
}
if terminal
.draw(|f| draw_blocks::error(f, &AppError::DockerConnect, Some(seconds)))
.draw(|f| draw_blocks::error(f, AppError::DockerConnect, Some(seconds)))
.is_err()
{
return Err(AppError::Terminal);
@@ -104,9 +104,11 @@ async fn run_app<B: Backend + Send>(
} else {
let mut now = Instant::now();
loop {
if terminal.draw(|f| ui(f, &app_data, &gui_state)).is_err() {
return Err(AppError::Terminal);
if terminal.draw(|f| ui(f, &app_data, &gui_state)).is_err() {
return Err(AppError::Terminal);
}
// TODO could only draw if in gui mode, that way all inputs & docker commands will run, and can just trace!("{event"}) all over the place
// refactor this into own function, so can be called without drawing to the terminal
if crossterm::event::poll(input_poll_rate).unwrap_or(false) {
if let Ok(event) = event::read() {
if let Event::Key(key) = event {
@@ -213,7 +215,7 @@ fn ui<B: Backend>(
f,
has_containers,
&loading_icon,
&sorted_by,
sorted_by,
gui_state,
);
@@ -233,6 +235,6 @@ fn ui<B: Backend>(
if let Some(error) = has_error {
app_data.lock().show_error = true;
draw_blocks::error(f, &error, None);
draw_blocks::error(f, error, None);
}
}