refactor: FrameData::from()

use &Arc<Mutex<T>> instead of MutexGuard<T>
This commit is contained in:
Jack Wills
2024-12-03 23:46:38 +00:00
parent 57781701ff
commit 6e2f82db81
2 changed files with 48 additions and 47 deletions
+27 -27
View File
@@ -1119,7 +1119,7 @@ mod tests {
let app_data = Arc::new(Mutex::new(app_data)); let app_data = Arc::new(Mutex::new(app_data));
let gui_state = Arc::new(Mutex::new(gui_state)); let gui_state = Arc::new(Mutex::new(gui_state));
let fd = FrameData::from((app_data.lock(), gui_state.lock())); let fd = FrameData::from((&app_data, &gui_state));
let area = Rect::new(0, 0, w, h); let area = Rect::new(0, 0, w, h);
TuiTestSetup { TuiTestSetup {
app_data, app_data,
@@ -1325,7 +1325,7 @@ mod tests {
// Control panel now selected, should have a blue border // Control panel now selected, should have a blue border
setup.gui_state.lock().next_panel(); setup.gui_state.lock().next_panel();
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
.draw(|f| { .draw(|f| {
@@ -1374,7 +1374,7 @@ mod tests {
]; ];
setup.gui_state.lock().next_panel(); setup.gui_state.lock().next_panel();
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1392,7 +1392,7 @@ mod tests {
} }
setup.gui_state.lock().previous_panel(); setup.gui_state.lock().previous_panel();
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1461,7 +1461,7 @@ mod tests {
// Change selected panel, border is now no longer blue // Change selected panel, border is now no longer blue
setup.gui_state.lock().next_panel(); setup.gui_state.lock().next_panel();
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
.draw(|f| { .draw(|f| {
@@ -1495,7 +1495,7 @@ mod tests {
"│ │", "│ │",
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯" "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯"
]; ];
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1555,7 +1555,7 @@ mod tests {
"│ │", "│ │",
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
]; ];
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup.app_data.lock().containers.items[0].state = State::Paused; setup.app_data.lock().containers.items[0].state = State::Paused;
setup setup
@@ -1625,7 +1625,7 @@ mod tests {
"│ │", "│ │",
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
]; ];
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup.app_data.lock().containers.items[0].state = State::Paused; setup.app_data.lock().containers.items[0].state = State::Paused;
setup setup
@@ -1653,7 +1653,7 @@ mod tests {
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
]; ];
setup.app_data.lock().containers.items[0].state = State::Dead; setup.app_data.lock().containers.items[0].state = State::Dead;
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1680,7 +1680,7 @@ mod tests {
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
]; ];
setup.app_data.lock().containers.items[0].state = State::Exited; setup.app_data.lock().containers.items[0].state = State::Exited;
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1706,7 +1706,7 @@ mod tests {
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
]; ];
setup.app_data.lock().containers.items[0].state = State::Removing; setup.app_data.lock().containers.items[0].state = State::Removing;
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1733,7 +1733,7 @@ mod tests {
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
]; ];
setup.app_data.lock().containers.items[0].state = State::Restarting; setup.app_data.lock().containers.items[0].state = State::Restarting;
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1798,7 +1798,7 @@ mod tests {
"│ │", "│ │",
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯" "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯"
]; ];
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1857,7 +1857,7 @@ mod tests {
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯", "╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
]; ];
setup.app_data.lock().containers.items[0].state = State::Unknown; setup.app_data.lock().containers.items[0].state = State::Unknown;
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -1905,7 +1905,7 @@ mod tests {
setup.gui_state.lock().next_panel(); setup.gui_state.lock().next_panel();
setup.gui_state.lock().next_panel(); setup.gui_state.lock().next_panel();
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
// When selected, has a blue border // When selected, has a blue border
setup setup
@@ -1943,7 +1943,7 @@ mod tests {
"╰──────────────────────────────╯", "╰──────────────────────────────╯",
]; ];
let mut fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let mut fd = FrameData::from((&setup.app_data, &setup.gui_state));
fd.init = true; fd.init = true;
setup setup
@@ -1973,7 +1973,7 @@ mod tests {
"╰──────────────────────────────╯", "╰──────────────────────────────╯",
]; ];
let mut fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let mut fd = FrameData::from((&setup.app_data, &setup.gui_state));
fd.init = true; fd.init = true;
setup setup
.terminal .terminal
@@ -1999,7 +1999,7 @@ mod tests {
insert_logs(&setup); insert_logs(&setup);
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
.draw(|f| { .draw(|f| {
@@ -2031,7 +2031,7 @@ mod tests {
// Change selected log line // Change selected log line
setup.app_data.lock().log_previous(); setup.app_data.lock().log_previous();
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -2084,7 +2084,7 @@ mod tests {
"╰──────────────────────────────────────────────────────────────────────────────╯", "╰──────────────────────────────────────────────────────────────────────────────╯",
]; ];
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
.draw(|f| { .draw(|f| {
@@ -2373,7 +2373,7 @@ mod tests {
let mut setup = test_setup(w, h, true, true); let mut setup = test_setup(w, h, true, true);
setup.app_data.lock().containers = StatefulList::new(vec![]); setup.app_data.lock().containers = StatefulList::new(vec![]);
let mut fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let mut fd = FrameData::from((&setup.app_data, &setup.gui_state));
let expected = [" ( h ) show help "]; let expected = [" ( h ) show help "];
@@ -2417,7 +2417,7 @@ mod tests {
fn test_draw_blocks_headers_some_containers() { fn test_draw_blocks_headers_some_containers() {
let (w, h) = (140, 1); let (w, h) = (140, 1);
let mut setup = test_setup(w, h, true, true); let mut setup = test_setup(w, h, true, true);
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
let expected = [" name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "]; let expected = [" name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "];
setup setup
@@ -2448,7 +2448,7 @@ mod tests {
fn test_draw_blocks_headers_some_containers_reduced_width() { fn test_draw_blocks_headers_some_containers_reduced_width() {
let (w, h) = (80, 1); let (w, h) = (80, 1);
let mut setup = test_setup(w, h, true, true); let mut setup = test_setup(w, h, true, true);
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
let expected = let expected =
[" name state status cpu ( h ) show help "]; [" name state status cpu ( h ) show help "];
@@ -2480,7 +2480,7 @@ mod tests {
fn test_draw_blocks_headers_sort_containers() { fn test_draw_blocks_headers_sort_containers() {
let (w, h) = (140, 1); let (w, h) = (140, 1);
let mut setup = test_setup(w, h, true, true); let mut setup = test_setup(w, h, true, true);
let mut fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let mut fd = FrameData::from((&setup.app_data, &setup.gui_state));
// Actual test, used for each header and sorted type // Actual test, used for each header and sorted type
let mut test = let mut test =
@@ -2549,7 +2549,7 @@ mod tests {
let mut setup = test_setup(w, h, true, true); let mut setup = test_setup(w, h, true, true);
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
setup.gui_state.lock().next_loading(uuid); setup.gui_state.lock().next_loading(uuid);
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
let expected = [" ⠙ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "]; let expected = [" ⠙ name state status cpu memory/limit id image ↓ rx ↑ tx ( h ) show help "];
@@ -2888,7 +2888,7 @@ 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())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
@@ -2934,7 +2934,7 @@ mod tests {
// Test when filter_by chances // Test when filter_by chances
setup.app_data.lock().filter_by_next(); setup.app_data.lock().filter_by_next();
let fd = FrameData::from((setup.app_data.lock(), setup.gui_state.lock())); let fd = FrameData::from((&setup.app_data, &setup.gui_state));
setup setup
.terminal .terminal
.draw(|f| { .draw(|f| {
+21 -20
View File
@@ -4,7 +4,7 @@ use crossterm::{
execute, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
}; };
use parking_lot::{Mutex, MutexGuard}; use parking_lot::Mutex;
use ratatui::{ use ratatui::{
backend::CrosstermBackend, backend::CrosstermBackend,
layout::{Constraint, Direction, Layout, Position}, layout::{Constraint, Direction, Layout, Position},
@@ -220,7 +220,6 @@ 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
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)] #[allow(clippy::struct_excessive_bools)]
pub struct FrameData { pub struct FrameData {
@@ -242,41 +241,43 @@ pub struct FrameData {
sorted_by: Option<(Header, SortedOrder)>, sorted_by: Option<(Header, SortedOrder)>,
} }
impl From<(MutexGuard<'_, AppData>, MutexGuard<'_, GuiState>)> for FrameData { impl From<(&Arc<Mutex<AppData>>, &Arc<Mutex<GuiState>>)> for FrameData {
fn from(data: (MutexGuard<'_, AppData>, MutexGuard<'_, GuiState>)) -> Self { fn from(data: (&Arc<Mutex<AppData>>, &Arc<Mutex<GuiState>>)) -> Self {
let (app_data, gui_data) = (data.0.lock(), data.1.lock());
// set max height for container section, needs +5 to deal with docker commands list and borders // set max height for container section, needs +5 to deal with docker commands list and borders
let height = data.0.get_container_len(); let height = app_data.get_container_len();
let height = if height < 12 { let height = if height < 12 {
u16::try_from(height + 5).unwrap_or_default() u16::try_from(height + 5).unwrap_or_default()
} else { } else {
12 12
}; };
let (filter_by, filter_term) = data.0.get_filter(); let (filter_by, filter_term) = app_data.get_filter();
Self { Self {
columns: data.0.get_width(), columns: app_data.get_width(),
container_title: data.0.get_container_title(), container_title: app_data.get_container_title(),
delete_confirm: data.1.get_delete_container(), delete_confirm: gui_data.get_delete_container(),
filter_by, filter_by,
filter_term: filter_term.cloned(), filter_term: filter_term.cloned(),
has_containers: data.0.get_container_len() > 0, has_containers: app_data.get_container_len() > 0,
has_error: data.0.get_error(), has_error: app_data.get_error(),
height, height,
help_visible: data.1.status_contains(&[Status::Help]), help_visible: gui_data.status_contains(&[Status::Help]),
info_text: data.1.info_box_text.clone(), info_text: gui_data.info_box_text.clone(),
init: data.1.status_contains(&[Status::Init]), init: gui_data.status_contains(&[Status::Init]),
is_loading: data.1.is_loading(), is_loading: gui_data.is_loading(),
loading_icon: data.1.get_loading().to_string(), loading_icon: gui_data.get_loading().to_string(),
log_title: data.0.get_log_title(), log_title: app_data.get_log_title(),
selected_panel: data.1.get_selected_panel(), selected_panel: gui_data.get_selected_panel(),
sorted_by: data.0.get_sorted(), sorted_by: app_data.get_sorted(),
} }
} }
} }
/// Draw the main ui to a frame of the terminal /// Draw the main ui to a frame of the terminal
fn draw_frame(f: &mut Frame, app_data: &Arc<Mutex<AppData>>, gui_state: &Arc<Mutex<GuiState>>) { fn draw_frame(f: &mut Frame, app_data: &Arc<Mutex<AppData>>, gui_state: &Arc<Mutex<GuiState>>) {
let fd = FrameData::from((app_data.lock(), gui_state.lock())); let fd = FrameData::from((app_data, gui_state));
let contains_filter = gui_state.lock().status_contains(&[Status::Filter]); let contains_filter = gui_state.lock().status_contains(&[Status::Filter]);
let whole_constraints = if contains_filter { let whole_constraints = if contains_filter {