refactor: FrameData

This commit is contained in:
Jack Wills
2024-12-03 23:25:30 +00:00
parent 356ea5549b
commit 57781701ff
3 changed files with 28 additions and 19 deletions
+3 -3
View File
@@ -627,11 +627,11 @@ impl AppData {
} }
/// Get mutable Vec of current containers logs /// Get mutable Vec of current containers logs
pub fn get_logs(&mut self) -> Vec<ListItem<'static>> { pub fn get_logs(&self) -> Vec<ListItem<'static>> {
self.containers self.containers
.state .state
.selected() .selected()
.and_then(|i| self.containers.items.get_mut(i)) .and_then(|i| self.containers.items.get(i))
.map_or(vec![], |i| i.logs.to_vec()) .map_or(vec![], |i| i.logs.to_vec())
} }
@@ -1739,7 +1739,7 @@ mod tests {
assert_eq!( assert_eq!(
app_data.get_filter(), app_data.get_filter(),
(FilterBy::All, Some(&"image_2".to_string())) (FilterBy::All, Some(&"x".to_string()))
); );
app_data.filter_containers(); app_data.filter_containers();
+14 -8
View File
@@ -231,7 +231,7 @@ pub fn containers(
if items.is_empty() { if items.is_empty() {
let text = if fd.filter_term.is_some() { let text = if fd.filter_term.is_some() {
"no containers match filter" "no containers match filter"
} else if gui_state.lock().is_loading() { } else if fd.is_loading {
&format!("loading {}", fd.loading_icon) &format!("loading {}", fd.loading_icon)
} else { } else {
"no containers running" "no containers running"
@@ -250,7 +250,8 @@ pub fn containers(
} }
} }
/// Draw the logs panel // /// Draw the logs panel
// PREV WORKING
pub fn logs( pub fn logs(
app_data: &Arc<Mutex<AppData>>, app_data: &Arc<Mutex<AppData>>,
area: Rect, area: Rect,
@@ -267,14 +268,13 @@ pub fn logs(
f.render_widget(paragraph, area); f.render_widget(paragraph, area);
} else { } else {
let logs = app_data.lock().get_logs(); let logs = app_data.lock().get_logs();
if logs.is_empty() { if logs.is_empty() {
let paragraph = Paragraph::new("no logs found") let paragraph = Paragraph::new("no logs found")
.block(block) .block(block)
.alignment(Alignment::Center); .alignment(Alignment::Center);
f.render_widget(paragraph, area); f.render_widget(paragraph, area);
} else { } else {
let items = List::new(logs) let items = List::new(app_data.lock().get_logs())
.block(block) .block(block)
.highlight_symbol(RIGHT_ARROW) .highlight_symbol(RIGHT_ARROW)
.highlight_style(Style::default().add_modifier(Modifier::BOLD)); .highlight_style(Style::default().add_modifier(Modifier::BOLD));
@@ -1004,13 +1004,13 @@ pub fn error(f: &mut Frame, error: AppError, seconds: Option<u8>) {
/// Draw info box in one of the 9 BoxLocations /// Draw info box in one of the 9 BoxLocations
// TODO is this broken - I don't think so // TODO is this broken - I don't think so
pub fn info(f: &mut Frame, text: &str, instant: Instant, gui_state: &Arc<Mutex<GuiState>>) { pub fn info(f: &mut Frame, text: String, instant: Instant, gui_state: &Arc<Mutex<GuiState>>) {
let block = Block::default() let block = Block::default()
.title("") .title("")
.title_alignment(Alignment::Center) .title_alignment(Alignment::Center)
.borders(Borders::NONE); .borders(Borders::NONE);
let mut max_line_width = max_line_width(text); let mut max_line_width = max_line_width(&text);
let mut lines = text.lines().count(); let mut lines = text.lines().count();
// Add some horizontal & vertical margins // Add some horizontal & vertical margins
@@ -2804,7 +2804,12 @@ mod tests {
setup setup
.terminal .terminal
.draw(|f| { .draw(|f| {
super::info(f, "test", std::time::Instant::now(), &setup.gui_state); super::info(
f,
"test".to_owned(),
std::time::Instant::now(),
&setup.gui_state,
);
}) })
.unwrap(); .unwrap();
@@ -2883,11 +2888,12 @@ mod tests {
// Test when char added to search term // Test when char added to search term
setup.app_data.lock().filter_term_push('c'); setup.app_data.lock().filter_term_push('c');
setup.app_data.lock().filter_term_push('d'); setup.app_data.lock().filter_term_push('d');
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock()));
setup setup
.terminal .terminal
.draw(|f| { .draw(|f| {
super::filter_bar(setup.area, f, &setup.fd); super::filter_bar(setup.area, f, &fd);
}) })
.unwrap(); .unwrap();
+11 -8
View File
@@ -221,21 +221,23 @@ impl Ui {
/// Frequent data required by multiple framde drawing functions, can reduce mutex reads by placing it all in here /// Frequent data required by multiple framde drawing functions, can reduce mutex reads by placing it all in here
/// TODO add more items to this, and split up into parts /// TODO add more items to this, and split up into parts
#[derive(Debug)] #[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct FrameData { pub struct FrameData {
columns: Columns, columns: Columns,
container_title: String, container_title: String,
log_title: String,
delete_confirm: Option<ContainerId>, delete_confirm: Option<ContainerId>,
has_containers: bool,
filter_by: FilterBy, filter_by: FilterBy,
filter_term: Option<String>, filter_term: Option<String>,
has_containers: bool,
has_error: Option<AppError>, has_error: Option<AppError>,
height: u16, height: u16,
help_visible: bool, help_visible: bool,
init: bool,
info_text: Option<(String, Instant)>, info_text: Option<(String, Instant)>,
init: bool,
is_loading: bool,
loading_icon: String, loading_icon: String,
log_title: String,
selected_panel: SelectablePanel, selected_panel: SelectablePanel,
sorted_by: Option<(Header, SortedOrder)>, sorted_by: Option<(Header, SortedOrder)>,
} }
@@ -252,17 +254,18 @@ impl From<(MutexGuard<'_, AppData>, MutexGuard<'_, GuiState>)> for FrameData {
let (filter_by, filter_term) = data.0.get_filter(); let (filter_by, filter_term) = data.0.get_filter();
Self { Self {
filter_by,
filter_term: filter_term.cloned(),
height,
columns: data.0.get_width(), columns: data.0.get_width(),
container_title: data.0.get_container_title(), container_title: data.0.get_container_title(),
delete_confirm: data.1.get_delete_container(), delete_confirm: data.1.get_delete_container(),
filter_by,
filter_term: filter_term.cloned(),
has_containers: data.0.get_container_len() > 0, has_containers: data.0.get_container_len() > 0,
has_error: data.0.get_error(), has_error: data.0.get_error(),
height,
help_visible: data.1.status_contains(&[Status::Help]), help_visible: data.1.status_contains(&[Status::Help]),
info_text: data.1.info_box_text.clone(), info_text: data.1.info_box_text.clone(),
init: data.1.status_contains(&[Status::Init]), init: data.1.status_contains(&[Status::Init]),
is_loading: data.1.is_loading(),
loading_icon: data.1.get_loading().to_string(), loading_icon: data.1.get_loading().to_string(),
log_title: data.0.get_log_title(), log_title: data.0.get_log_title(),
selected_panel: data.1.get_selected_panel(), selected_panel: data.1.get_selected_panel(),
@@ -358,7 +361,7 @@ fn draw_frame(f: &mut Frame, app_data: &Arc<Mutex<AppData>>, gui_state: &Arc<Mut
} }
if let Some((text, instant)) = fd.info_text { if let Some((text, instant)) = fd.info_text {
draw_blocks::info(f, &text, instant, gui_state); draw_blocks::info(f, text, instant, gui_state);
} }
// Check if error, and show popup if so // Check if error, and show popup if so