Prepare memory usage
This commit is contained in:
parent
1f6787d526
commit
9bad829054
|
@ -5,12 +5,14 @@ use tool_helper::Error;
|
||||||
use tui::{
|
use tui::{
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::{Color, Modifier, Style},
|
style::{Color, Modifier, Style},
|
||||||
|
symbols,
|
||||||
text::{Span, Spans},
|
text::{Span, Spans},
|
||||||
widgets::{Block, Borders, BorderType, Paragraph, Tabs}
|
widgets::{Block, Borders, BorderType, LineGauge, Paragraph, Tabs}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type EventReceiver = std::sync::mpsc::Receiver<Event<crossterm::event::KeyEvent>>;
|
pub type EventReceiver = std::sync::mpsc::Receiver<Event<crossterm::event::KeyEvent>>;
|
||||||
pub type Terminal = tui::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>;
|
pub type Terminal = tui::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>;
|
||||||
|
type ConsoleFrame<'a> = tui::Frame<'a, tui::backend::CrosstermBackend<std::io::Stdout>>;
|
||||||
|
|
||||||
pub enum Event<I> {
|
pub enum Event<I> {
|
||||||
Input(I),
|
Input(I),
|
||||||
|
@ -50,16 +52,17 @@ impl MenuSelection {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ConsoleUI {
|
pub struct ConsoleUI {
|
||||||
rx: EventReceiver,
|
rx: EventReceiver,
|
||||||
terminal: Terminal,
|
terminal: Terminal,
|
||||||
frame: Rect,
|
frame: Rect,
|
||||||
sub_frames: Vec<Rect>,
|
sub_frames: Vec<Rect>,
|
||||||
menu_selection: MenuSelection,
|
stat_sub_frames: Vec<Rect>,
|
||||||
|
menu_selection: MenuSelection,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConsoleUI {
|
impl ConsoleUI {
|
||||||
pub fn new(rx: EventReceiver, terminal: Terminal) -> ConsoleUI {
|
pub fn new(rx: EventReceiver, terminal: Terminal) -> ConsoleUI {
|
||||||
ConsoleUI{rx, terminal, frame: Rect::default(), sub_frames: Vec::default(), menu_selection: MenuSelection::Stats}
|
ConsoleUI{rx, terminal, frame: Rect::default(), sub_frames: Vec::default(), stat_sub_frames: Vec::default(), menu_selection: MenuSelection::Stats}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) -> Result<UIState, Error> {
|
pub fn update(&mut self) -> Result<UIState, Error> {
|
||||||
|
@ -86,23 +89,33 @@ impl ConsoleUI {
|
||||||
_ => Ok(UIState::Alive)
|
_ => Ok(UIState::Alive)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Event::Tick => Ok(UIState::Alive),
|
Event::Tick => {
|
||||||
|
if self.terminal.size().expect("Getting size of terminal") != self.frame {
|
||||||
|
Ok(UIState::Render)
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
Ok(UIState::Alive)
|
||||||
|
}
|
||||||
|
},
|
||||||
Event::ForceRender => Ok(UIState::Render)
|
Event::ForceRender => Ok(UIState::Render)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&mut self) -> Result<(), Error> {
|
pub fn render(&mut self) -> Result<(), Error> {
|
||||||
self.terminal.draw(|frame| {
|
self.terminal.draw(|frame| {
|
||||||
Self::update_sub_frames(&mut self.sub_frames, &mut self.frame, frame.size());
|
Self::update_sub_frames(&mut self.sub_frames, &mut self.stat_sub_frames, &mut self.frame, frame.size());
|
||||||
|
|
||||||
frame.render_widget(Self::create_titel(), self.sub_frames[0]);
|
frame.render_widget(Self::create_titel(), self.sub_frames[0]);
|
||||||
frame.render_widget(Self::create_menu(&self.menu_selection), self.sub_frames[1]);
|
frame.render_widget(Self::create_menu(&self.menu_selection), self.sub_frames[1]);
|
||||||
frame.render_widget({
|
match self.menu_selection {
|
||||||
match self.menu_selection {
|
MenuSelection::Stats => {
|
||||||
MenuSelection::Stats => Self::create_titel(),
|
Self::render_stats(frame, &self.stat_sub_frames);
|
||||||
MenuSelection::Quit => Self::create_exit_message(),
|
},
|
||||||
|
MenuSelection::Quit => {
|
||||||
|
frame.render_widget(Self::create_exit_message(), self.sub_frames[2]);
|
||||||
}
|
}
|
||||||
}, self.sub_frames[2]);
|
}
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -115,13 +128,19 @@ impl ConsoleUI {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_sub_frames(sub_frames: &mut Vec<Rect>, frame: &mut Rect, new_frame: Rect) {
|
fn update_sub_frames(sub_frames: &mut Vec<Rect>, stats_frames: &mut Vec<Rect>, frame: &mut Rect, new_frame: Rect) {
|
||||||
if new_frame != *frame {
|
if new_frame != *frame {
|
||||||
*sub_frames = Layout::default().direction(Direction::Vertical).constraints([
|
*sub_frames = Layout::default().direction(Direction::Vertical).constraints([
|
||||||
Constraint::Length(3),
|
Constraint::Length(3),
|
||||||
Constraint::Length(3),
|
Constraint::Length(3),
|
||||||
Constraint::Min(3)
|
Constraint::Min(3)
|
||||||
]).split(new_frame);
|
]).split(new_frame);
|
||||||
|
|
||||||
|
*stats_frames = Layout::default().direction(Direction::Vertical).constraints([
|
||||||
|
Constraint::Min(3),
|
||||||
|
Constraint::Length(3)
|
||||||
|
]).split(sub_frames[2]);
|
||||||
|
|
||||||
*frame = new_frame;
|
*frame = new_frame;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,6 +176,29 @@ impl ConsoleUI {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_stats(frame: &mut ConsoleFrame, frames: &Vec<Rect>) {
|
||||||
|
let content_text = Paragraph::new("<EMPTY>")
|
||||||
|
.style(Style::default().fg(Color::White))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.block(Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.style(Style::default().fg(Color::White))
|
||||||
|
.border_type(BorderType::Plain)
|
||||||
|
);
|
||||||
|
let mem_gauge = Self::create_overall_mem_gauge();
|
||||||
|
|
||||||
|
frame.render_widget(content_text, frames[0]);
|
||||||
|
frame.render_widget(mem_gauge, frames[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_overall_mem_gauge<'a>() -> LineGauge<'a> {
|
||||||
|
|
||||||
|
LineGauge::default().block(Block::default().borders(Borders::ALL).title("Progress"))
|
||||||
|
.gauge_style(Style::default().fg(Color::White).bg(Color::Black).add_modifier(Modifier::BOLD))
|
||||||
|
.line_set(symbols::line::THICK)
|
||||||
|
.ratio(0.4)
|
||||||
|
}
|
||||||
|
|
||||||
fn create_exit_message<'a>() -> Paragraph<'a> {
|
fn create_exit_message<'a>() -> Paragraph<'a> {
|
||||||
Paragraph::new("Press \"ENTER\" to exit")
|
Paragraph::new("Press \"ENTER\" to exit")
|
||||||
.style(Style::default().fg(Color::White))
|
.style(Style::default().fg(Color::White))
|
||||||
|
|
|
@ -26,7 +26,7 @@ pub fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_main(cmd: CommandLine) -> Result<(), Error> {
|
fn run_main(_cmd: CommandLine) -> Result<(), Error> {
|
||||||
let _sections = load_memory_map(Some(cmd.input))?;
|
let _sections = load_memory_map(Some(cmd.input))?;
|
||||||
let rx = start_event_loop();
|
let rx = start_event_loop();
|
||||||
let terminal = setup_console()?;
|
let terminal = setup_console()?;
|
||||||
|
|
Loading…
Reference in New Issue