refactor: FrameData::from()
use &Arc<Mutex<T>> instead of MutexGuard<T>
This commit is contained in:
+27
-27
@@ -1119,7 +1119,7 @@ mod tests {
|
||||
let app_data = Arc::new(Mutex::new(app_data));
|
||||
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);
|
||||
TuiTestSetup {
|
||||
app_data,
|
||||
@@ -1325,7 +1325,7 @@ mod tests {
|
||||
|
||||
// Control panel now selected, should have a blue border
|
||||
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
|
||||
.terminal
|
||||
.draw(|f| {
|
||||
@@ -1374,7 +1374,7 @@ mod tests {
|
||||
];
|
||||
|
||||
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
|
||||
.terminal
|
||||
@@ -1392,7 +1392,7 @@ mod tests {
|
||||
}
|
||||
|
||||
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
|
||||
.terminal
|
||||
@@ -1461,7 +1461,7 @@ mod tests {
|
||||
|
||||
// Change selected panel, border is now no longer blue
|
||||
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
|
||||
.terminal
|
||||
.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
|
||||
.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
|
||||
@@ -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
|
||||
@@ -1653,7 +1653,7 @@ mod tests {
|
||||
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
|
||||
];
|
||||
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
|
||||
.terminal
|
||||
@@ -1680,7 +1680,7 @@ mod tests {
|
||||
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
|
||||
];
|
||||
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
|
||||
.terminal
|
||||
@@ -1706,7 +1706,7 @@ mod tests {
|
||||
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
|
||||
];
|
||||
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
|
||||
.terminal
|
||||
@@ -1733,7 +1733,7 @@ mod tests {
|
||||
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
|
||||
];
|
||||
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
|
||||
.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
|
||||
.terminal
|
||||
@@ -1857,7 +1857,7 @@ mod tests {
|
||||
"╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯",
|
||||
];
|
||||
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
|
||||
.terminal
|
||||
@@ -1905,7 +1905,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));
|
||||
|
||||
// When selected, has a blue border
|
||||
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;
|
||||
|
||||
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;
|
||||
setup
|
||||
.terminal
|
||||
@@ -1999,7 +1999,7 @@ mod tests {
|
||||
|
||||
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
|
||||
.terminal
|
||||
.draw(|f| {
|
||||
@@ -2031,7 +2031,7 @@ mod tests {
|
||||
|
||||
// Change selected log line
|
||||
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
|
||||
.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
|
||||
.terminal
|
||||
.draw(|f| {
|
||||
@@ -2373,7 +2373,7 @@ mod tests {
|
||||
let mut setup = test_setup(w, h, true, true);
|
||||
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 "];
|
||||
|
||||
@@ -2417,7 +2417,7 @@ mod tests {
|
||||
fn test_draw_blocks_headers_some_containers() {
|
||||
let (w, h) = (140, 1);
|
||||
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 "];
|
||||
setup
|
||||
@@ -2448,7 +2448,7 @@ mod tests {
|
||||
fn test_draw_blocks_headers_some_containers_reduced_width() {
|
||||
let (w, h) = (80, 1);
|
||||
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 ( h ) show help "];
|
||||
@@ -2480,7 +2480,7 @@ mod tests {
|
||||
fn test_draw_blocks_headers_sort_containers() {
|
||||
let (w, h) = (140, 1);
|
||||
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
|
||||
let mut test =
|
||||
@@ -2549,7 +2549,7 @@ mod tests {
|
||||
let mut setup = test_setup(w, h, true, true);
|
||||
let uuid = Uuid::new_v4();
|
||||
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 "];
|
||||
|
||||
@@ -2888,7 +2888,7 @@ mod tests {
|
||||
// Test when char added to search term
|
||||
setup.app_data.lock().filter_term_push('c');
|
||||
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
|
||||
.terminal
|
||||
@@ -2934,7 +2934,7 @@ mod tests {
|
||||
|
||||
// Test when filter_by chances
|
||||
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
|
||||
.terminal
|
||||
.draw(|f| {
|
||||
|
||||
+21
-20
@@ -4,7 +4,7 @@ use crossterm::{
|
||||
execute,
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
};
|
||||
use parking_lot::{Mutex, MutexGuard};
|
||||
use parking_lot::Mutex;
|
||||
use ratatui::{
|
||||
backend::CrosstermBackend,
|
||||
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
|
||||
/// TODO add more items to this, and split up into parts
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(clippy::struct_excessive_bools)]
|
||||
pub struct FrameData {
|
||||
@@ -242,41 +241,43 @@ pub struct FrameData {
|
||||
sorted_by: Option<(Header, SortedOrder)>,
|
||||
}
|
||||
|
||||
impl From<(MutexGuard<'_, AppData>, MutexGuard<'_, GuiState>)> for FrameData {
|
||||
fn from(data: (MutexGuard<'_, AppData>, MutexGuard<'_, GuiState>)) -> Self {
|
||||
impl From<(&Arc<Mutex<AppData>>, &Arc<Mutex<GuiState>>)> for FrameData {
|
||||
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
|
||||
let height = data.0.get_container_len();
|
||||
let height = app_data.get_container_len();
|
||||
let height = if height < 12 {
|
||||
u16::try_from(height + 5).unwrap_or_default()
|
||||
} else {
|
||||
12
|
||||
};
|
||||
|
||||
let (filter_by, filter_term) = data.0.get_filter();
|
||||
let (filter_by, filter_term) = app_data.get_filter();
|
||||
Self {
|
||||
columns: data.0.get_width(),
|
||||
container_title: data.0.get_container_title(),
|
||||
delete_confirm: data.1.get_delete_container(),
|
||||
columns: app_data.get_width(),
|
||||
container_title: app_data.get_container_title(),
|
||||
delete_confirm: gui_data.get_delete_container(),
|
||||
filter_by,
|
||||
filter_term: filter_term.cloned(),
|
||||
has_containers: data.0.get_container_len() > 0,
|
||||
has_error: data.0.get_error(),
|
||||
has_containers: app_data.get_container_len() > 0,
|
||||
has_error: app_data.get_error(),
|
||||
height,
|
||||
help_visible: data.1.status_contains(&[Status::Help]),
|
||||
info_text: data.1.info_box_text.clone(),
|
||||
init: data.1.status_contains(&[Status::Init]),
|
||||
is_loading: data.1.is_loading(),
|
||||
loading_icon: data.1.get_loading().to_string(),
|
||||
log_title: data.0.get_log_title(),
|
||||
selected_panel: data.1.get_selected_panel(),
|
||||
sorted_by: data.0.get_sorted(),
|
||||
help_visible: gui_data.status_contains(&[Status::Help]),
|
||||
info_text: gui_data.info_box_text.clone(),
|
||||
init: gui_data.status_contains(&[Status::Init]),
|
||||
is_loading: gui_data.is_loading(),
|
||||
loading_icon: gui_data.get_loading().to_string(),
|
||||
log_title: app_data.get_log_title(),
|
||||
selected_panel: gui_data.get_selected_panel(),
|
||||
sorted_by: app_data.get_sorted(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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>>) {
|
||||
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 whole_constraints = if contains_filter {
|
||||
|
||||
Reference in New Issue
Block a user