-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the containers scene (#224)
- Loading branch information
1 parent
04a95bf
commit 038a9bb
Showing
13 changed files
with
250 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use std::{borrow::Cow, cell::RefCell}; | ||
|
||
use tari_sdm::ids::{ManagedTask, TaskId}; | ||
use tari_sdm_launchpad::resources::images; | ||
use tui::{ | ||
backend::Backend, | ||
layout::{Constraint, Layout, Rect}, | ||
style::{Color, Style}, | ||
widgets::{Row, Table, TableState}, | ||
}; | ||
|
||
use crate::{ | ||
component::{elements::block_with_title, AppState, Component, ComponentEvent, Frame, Input, Pass}, | ||
state::focus, | ||
}; | ||
|
||
pub struct ContainersScene { | ||
table_state: RefCell<TableState>, | ||
containers: Vec<TaskId>, | ||
} | ||
|
||
impl ContainersScene { | ||
pub fn new() -> Self { | ||
Self { | ||
table_state: RefCell::new(TableState::default()), | ||
containers: containers(), | ||
} | ||
} | ||
} | ||
|
||
impl Input for ContainersScene { | ||
fn on_event(&mut self, event: ComponentEvent, state: &mut AppState) { | ||
if state.focus_on == focus::CONTAINERS_TABLE { | ||
match event.pass() { | ||
Pass::Up | Pass::Leave => { | ||
state.focus_on(focus::ROOT); | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<B: Backend> Component<B> for ContainersScene { | ||
type State = AppState; | ||
|
||
fn draw(&self, f: &mut Frame<B>, rect: Rect, state: &Self::State) { | ||
let block = block_with_title(Some("Containers"), state.focus_on == focus::CONTAINERS_TABLE); | ||
let rects = Layout::default() | ||
.constraints([Constraint::Percentage(100)].as_ref()) | ||
.split(rect); | ||
let mut rows = Vec::new(); | ||
for task_id in &self.containers { | ||
if let Some(task_state) = state.state.containers.get(task_id) { | ||
let col_1 = Cow::Borrowed(task_id.as_ref()); | ||
let mut col_2 = Cow::Borrowed("-"); | ||
let mut col_3 = Cow::Borrowed("-"); | ||
let mut col_4 = Cow::Borrowed("Inactive"); | ||
if task_state.status.is_started() { | ||
if let Some(stat_data) = task_state.stats.last() { | ||
let cpu_usage = task_state.stats.last_cpu().unwrap_or_default(); | ||
let usage = format!("{:.2} %", cpu_usage); | ||
let mem = stat_data.mem_usage.get_appropriate_unit(false).to_string(); | ||
col_2 = Cow::Owned(usage); | ||
col_3 = Cow::Owned(mem); | ||
col_4 = Cow::Owned(task_state.status.to_string()); | ||
} | ||
} | ||
let items = vec![col_1, col_2, col_3, col_4]; | ||
let row = Row::new(items).height(2); | ||
rows.push(row); | ||
} | ||
} | ||
let header_cells = ["Container", "CPU", "Memory", "Status"]; | ||
let header = Row::new(header_cells) | ||
.style(Style::default().fg(Color::Yellow)) | ||
.height(1) | ||
.bottom_margin(1); | ||
let table = Table::new(rows) | ||
.block(block) | ||
.header(header) | ||
.widths(&[ | ||
Constraint::Length(14), | ||
Constraint::Length(10), | ||
Constraint::Length(10), | ||
Constraint::Min(100), | ||
]) | ||
.column_spacing(2); | ||
f.render_stateful_widget(table, rects[0], &mut *self.table_state.borrow_mut()); | ||
} | ||
} | ||
|
||
fn containers() -> Vec<TaskId> { | ||
vec![ | ||
images::Tor::id(), | ||
images::TariBaseNode::id(), | ||
images::TariWallet::id(), | ||
images::TariSha3Miner::id(), | ||
images::MmProxy::id(), | ||
images::Monerod::id(), | ||
images::XMRig::id(), | ||
images::Grafana::id(), | ||
images::Loki::id(), | ||
images::Promtail::id(), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::time::Duration; | ||
|
||
use tui::{ | ||
backend::Backend, | ||
layout::{Constraint, Direction, Layout, Rect}, | ||
}; | ||
|
||
use crate::{ | ||
component::{ | ||
elements::{block_with_title, logo}, | ||
normal::chrono_button::{ChronoButton, ChronoGetter}, | ||
Component, | ||
ComponentEvent, | ||
Frame, | ||
Input, | ||
Pass, | ||
}, | ||
state::{focus, AppState}, | ||
}; | ||
|
||
const LOGO: &str = r#" | ||
╦ ╦┌─┐┬ ┬ ┌─┐┌┬┐ | ||
║║║├─┤│ │ ├┤ │ | ||
╚╩╝┴ ┴┴─┘┴─┘└─┘ ┴ | ||
"#; | ||
|
||
struct WalletContainerGetter; | ||
|
||
impl ChronoGetter for WalletContainerGetter { | ||
fn get_duration(&self, _state: &AppState) -> Option<Duration> { | ||
None | ||
} | ||
|
||
fn get_label(&self, state: &AppState) -> &str { | ||
if state.state.config.session.is_wallet_active() { | ||
"Pause" | ||
} else { | ||
"Start wallet" | ||
} | ||
} | ||
} | ||
|
||
pub struct WalletContainerWidget { | ||
button: ChronoButton<WalletContainerGetter>, | ||
} | ||
|
||
impl WalletContainerWidget { | ||
pub fn new() -> Self { | ||
Self { | ||
button: ChronoButton::new(WalletContainerGetter), | ||
} | ||
} | ||
} | ||
|
||
impl Input for WalletContainerWidget { | ||
fn on_event(&mut self, event: ComponentEvent, state: &mut AppState) { | ||
if state.focus_on == focus::WALLET_CONTAINER { | ||
match event.pass() { | ||
Pass::Up | Pass::Leave => { | ||
state.focus_on(focus::ROOT); | ||
}, | ||
Pass::Enter | Pass::Space => { | ||
let session = &mut state.state.config.session; | ||
session.wallet_active = !session.wallet_active; | ||
state.update_state(); | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<B: Backend> Component<B> for WalletContainerWidget { | ||
type State = AppState; | ||
|
||
fn draw(&self, f: &mut Frame<B>, rect: Rect, state: &Self::State) { | ||
let block = block_with_title(Some("Wallet"), state.focus_on == focus::WALLET_CONTAINER); | ||
let inner_rect = block.inner(rect); | ||
f.render_widget(block, rect); | ||
|
||
let constraints = [ | ||
Constraint::Length(1), | ||
Constraint::Length(3), | ||
// Constraint::Percentage(50), | ||
Constraint::Length(1), | ||
Constraint::Min(0), | ||
Constraint::Length(1), | ||
]; | ||
let v_chunks = Layout::default() | ||
.direction(Direction::Vertical) | ||
.constraints(constraints) | ||
.split(inner_rect); | ||
// self.status_badge.draw(f, v_chunks[0], state); | ||
|
||
let logo = logo(LOGO); | ||
f.render_widget(logo, v_chunks[1]); | ||
|
||
// self.tari_amount.draw(f, v_chunks[2], state); | ||
|
||
self.button.draw(f, v_chunks[4], state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.