wip: gui_status, should use a hashset?

This commit is contained in:
Jack Wills
2022-10-14 21:26:20 +00:00
parent 46ffbeef6a
commit 90e26c300e
7 changed files with 240 additions and 192 deletions
+43 -33
View File
@@ -15,6 +15,7 @@ use tui::{
};
use crate::app_data::{Header, SortedOrder};
use crate::ui::Status;
use crate::{
app_data::{AppData, ByteStats, Columns, CpuStats, State, Stats},
app_error::AppError,
@@ -215,37 +216,43 @@ pub fn logs<B: Backend>(
loading_icon: &str,
) {
let block = generate_block(app_data, area, gui_state, SelectablePanel::Logs);
let status = gui_state.lock().get_status();
match status {
Status::Init => {
let paragraph = Paragraph::new(format!("parsing logs {}", loading_icon))
.style(Style::default())
.block(block)
.alignment(Alignment::Center);
f.render_widget(paragraph, area);
}
let init = app_data.lock().init;
if !init {
let paragraph = Paragraph::new(format!("parsing logs {}", loading_icon))
.style(Style::default())
.block(block)
.alignment(Alignment::Center);
f.render_widget(paragraph, area);
} else if let Some(index) = index {
let items = app_data.lock().containers.items[index]
.logs
.items
.iter()
.enumerate()
.map(|i| i.1.clone())
.collect::<Vec<_>>();
_ => {
if let Some(index) = index {
let items = app_data.lock().containers.items[index]
.logs
.items
.iter()
.enumerate()
.map(|i| i.1.clone())
.collect::<Vec<_>>();
let items = List::new(items)
.block(block)
.highlight_symbol(ARROW)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
f.render_stateful_widget(
items,
area,
&mut app_data.lock().containers.items[index].logs.state,
);
} else {
let paragraph = Paragraph::new("no logs found")
.block(block)
.alignment(Alignment::Center);
f.render_widget(paragraph, area);
let items = List::new(items)
.block(block)
.highlight_symbol(ARROW)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
f.render_stateful_widget(
items,
area,
&mut app_data.lock().containers.items[index].logs.state,
);
} else {
let paragraph = Paragraph::new("no logs found")
.block(block)
.alignment(Alignment::Center);
f.render_widget(paragraph, area);
// }
}
}
}
}
@@ -349,7 +356,7 @@ pub fn heading_bar<B: Backend>(
gui_state: &Arc<Mutex<GuiState>>,
) {
let block = |fg: Color| Block::default().style(Style::default().bg(Color::Magenta).fg(fg));
let info_visible = gui_state.lock().show_help;
let info_visible = gui_state.lock().get_status() == Status::Help;
f.render_widget(block(Color::Black), area);
@@ -430,7 +437,12 @@ pub fn heading_bar<B: Backend>(
.collect::<Vec<_>>();
let suffix = if info_visible { "exit" } else { "show" };
let info_text = format!("( h ) {} help {}", suffix, MARGIN);
let info_text = format!(
"( h ) {} help {} {:?}",
suffix,
MARGIN,
gui_state.lock().get_status()
);
let info_width = info_text.chars().count();
let column_width = usize::from(area.width) - info_width;
@@ -457,8 +469,6 @@ pub fn heading_bar<B: Backend>(
.alignment(Alignment::Center);
f.render_widget(loading_paragraph, split_bar[0]);
let container_splits = header_data.iter().map(|i| i.2).collect::<Vec<_>>();
let headers_section = Layout::default()
.direction(Direction::Horizontal)
+21 -2
View File
@@ -173,6 +173,17 @@ impl fmt::Display for Loading {
}
}
/// The application can be in these four states
/// Various functions (e.g input handler), operate differently depending upon current Status
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Status {
Init,
Help,
DockerConnect,
Error,
Normal,
}
/// Global gui_state, stored in an Arc<Mutex>
#[derive(Debug, Clone)]
pub struct GuiState {
@@ -180,8 +191,8 @@ pub struct GuiState {
heading_map: HashMap<Header, Rect>,
loading_icon: Loading,
is_loading: HashSet<Uuid>,
status: Status,
pub selected_panel: SelectablePanel,
pub show_help: bool,
pub info_box_text: Option<String>,
}
impl GuiState {
@@ -192,9 +203,9 @@ impl GuiState {
heading_map: HashMap::new(),
loading_icon: Loading::One,
selected_panel: SelectablePanel::Containers,
show_help: false,
is_loading: HashSet::new(),
info_box_text: None,
status: Status::Init,
}
}
@@ -242,6 +253,14 @@ impl GuiState {
};
}
pub fn set_status(&mut self, status: Status) {
self.status = status
}
pub fn get_status(&self) -> Status {
self.status
}
/// Change to next selectable panel
pub fn next_panel(&mut self) {
self.selected_panel = self.selected_panel.next();
+7 -6
View File
@@ -25,7 +25,7 @@ mod draw_blocks;
mod gui_state;
pub use self::color_match::*;
pub use self::gui_state::{GuiState, SelectablePanel};
pub use self::gui_state::{GuiState, SelectablePanel, Status};
use crate::{
app_data::AppData, app_error::AppError, docker_data::DockerMessage,
input_handler::InputMessages,
@@ -56,7 +56,7 @@ pub async fn create_ui(
update_duration,
)
.await;
terminal.clear()?;
terminal.clear()?;
disable_raw_mode()?;
execute!(
@@ -85,8 +85,9 @@ async fn run_app<B: Backend + Send>(
let input_poll_rate = std::time::Duration::from_millis(75);
// Check for docker connect errors before attempting to draw the gui
let e = app_data.lock().get_error();
if let Some(AppError::DockerConnect) = e {
let status = gui_state.lock().get_status();
if status == Status::DockerConnect {
let mut seconds = 5;
loop {
if seconds < 1 {
@@ -138,6 +139,7 @@ async fn run_app<B: Backend + Send>(
break;
}
}
// }
}
Ok(())
}
@@ -157,7 +159,7 @@ fn ui<B: Backend>(
let log_index = app_data.lock().get_selected_log_index();
let sorted_by = app_data.lock().get_sorted();
let show_help = gui_state.lock().show_help;
let show_help = gui_state.lock().get_status() == Status::Help;
let info_text = gui_state.lock().info_box_text.clone();
let loading_icon = gui_state.lock().get_loading();
@@ -241,7 +243,6 @@ fn ui<B: Backend>(
}
if let Some(error) = has_error {
app_data.lock().show_error = true;
draw_blocks::error(f, error, None);
}
}