Skip to content

Commit

Permalink
Add pop-up for Docker
Browse files Browse the repository at this point in the history
Added a pop-up message for Docker if it is not running when the application starts
and then exits.
  • Loading branch information
hansieodendaal committed Feb 2, 2024
1 parent c8909af commit 911c420
Show file tree
Hide file tree
Showing 41 changed files with 260 additions and 104 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ tokio = "1.28.1"
ratatui = "0.23.0"
byte-unit = "5.1.3"

derive_setters = "0.1.0"

tari_launchpad_protocol = { path = "../libs/protocol" }
tari_sdm_assets = { path = "../libs/sdm-assets" }
tari_sdm = { path = "../libs/sdm" }
Expand Down
5 changes: 4 additions & 1 deletion cli/src/component/expert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ use strum::{Display, EnumCount, EnumIter, FromRepr};
use crate::{
component::{
tabs::{AppTabs, TabGetter},
Component, ComponentEvent, Frame, Input,
Component,
ComponentEvent,
Frame,
Input,
},
state::{focus, AppState, Focus},
};
Expand Down
10 changes: 9 additions & 1 deletion cli/src/component/main_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ use crate::{
header::{mode::Mode, Header},
normal::NormalScene,
settings::SettingsScene,
Component, ComponentEvent, Input, Pass,
widgets::docker_detect::is_docker_running,
Component,
ComponentEvent,
Input,
Pass,
},
state::{focus, AppState},
};
Expand Down Expand Up @@ -66,6 +70,10 @@ impl Input for MainView {
state.terminate();
state.focus_on(focus::TERMINATION);
state.update_state();

if !is_docker_running() {
std::process::exit(0);
}
} else if matches!(event, ComponentEvent::StateChanged) {
self.normal_scene.on_event(event, state);
self.settings_scene.on_event(event, state);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ mod settings;
mod tabs;
mod termination;
mod widgets;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use derive_more::From;
pub use main_view::MainView;
use ratatui::{backend::Backend, layout::Rect, Frame};
pub use termination::TerminationView;
pub use widgets::docker_detect::{display_docker_notice, is_docker_running, wait_for_keypress};

use crate::state::AppState;

Expand Down
6 changes: 4 additions & 2 deletions cli/src/component/normal/base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use crate::{
component::{
elements::block_with_title,
widgets::status_line::{StatusLine, StatusReportGetter},
Component, ComponentEvent,
Component,
ComponentEvent,
ComponentEvent::KeyEvent,
Frame, Input,
Frame,
Input,
},
state::AppState,
};
Expand Down
4 changes: 2 additions & 2 deletions cli/src/component/normal/mining/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl StatusGetter for MergeMiningStatus {
if state.state.config.session.merge_layer_active {
("⚒️ Press [M] to stop Merge Mining", Color::Green)
} else {
("Press [M] to start Merge mining", Color::Gray)
("Press [M] to start Merge mining ", Color::Gray)
}
}
}
Expand All @@ -24,7 +24,7 @@ impl StatusGetter for ShaMiningStatus {
if state.state.config.session.sha3x_layer_active {
("⚒️ Press [T] to stop SHA3X mining", Color::Yellow)
} else {
("Press [T] to start SHA3X mining", Color::Gray)
("Press [T] to start SHA3X mining ", Color::Gray)
}
}
}
10 changes: 5 additions & 5 deletions cli/src/component/normal/mining/mining_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ use log::warn;
use ratatui::prelude::*;
use tari_launchpad_protocol::settings::LaunchpadSettings;

use crate::component::elements::block_with_title;
use crate::component::Pass;
use crate::state::{focus, Focus};
use crate::{
component::{
elements::block_with_title,
normal::mining::{
helpers::{MergeMiningStatus, ShaMiningStatus},
status_badge::StatusBadge,
},
widgets::LabeledInput,
Component, ComponentEvent,
Component,
ComponentEvent,
ComponentEvent::KeyEvent,
Input,
Pass,
},
focus_id,
state::AppState,
state::{focus, AppState, Focus},
};

static MONERO_ADDRESS: Focus = focus_id!();
Expand Down
3 changes: 1 addition & 2 deletions cli/src/component/normal/mining/status_badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ impl<G> Input for StatusBadge<G> {
}

impl<B: Backend, G> Component<B> for StatusBadge<G>
where
G: StatusGetter,
where G: StatusGetter
{
type State = AppState;

Expand Down
5 changes: 4 additions & 1 deletion cli/src/component/normal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use ratatui::{
use crate::{
component::{
normal::{base_node::BaseNodeWidget, containers::ContainersScene},
Component, ComponentEvent, Frame, Input,
Component,
ComponentEvent,
Frame,
Input,
},
state::AppState,
};
Expand Down
6 changes: 5 additions & 1 deletion cli/src/component/settings/base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ use crate::{
component::{
elements::block_with_title,
widgets::{LabeledInput, Separator},
Component, ComponentEvent, Frame, Input, Pass,
Component,
ComponentEvent,
Frame,
Input,
Pass,
},
focus_id,
state::{
Expand Down
6 changes: 5 additions & 1 deletion cli/src/component/settings/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ use crate::{
component::{
elements::block_with_title,
widgets::{LabeledInput, Separator},
Component, ComponentEvent, Frame, Input, Pass,
Component,
ComponentEvent,
Frame,
Input,
Pass,
},
focus_id,
state::{
Expand Down
6 changes: 5 additions & 1 deletion cli/src/component/settings/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ use crate::{
component::{
elements::block_with_title,
widgets::{LabeledInput, Separator},
Component, ComponentEvent, Frame, Input, Pass,
Component,
ComponentEvent,
Frame,
Input,
Pass,
},
focus_id,
state::{
Expand Down
6 changes: 5 additions & 1 deletion cli/src/component/settings/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ use crate::{
component::{
elements::block_with_title,
widgets::{LabeledInput, Separator},
Component, ComponentEvent, Frame, Input, Pass,
Component,
ComponentEvent,
Frame,
Input,
Pass,
},
focus_id,
state::{
Expand Down
5 changes: 4 additions & 1 deletion cli/src/component/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ use strum::{Display, EnumCount, EnumIter, FromRepr};
use crate::{
component::{
tabs::{AppTabs, TabGetter},
Component, ComponentEvent, Frame, Input,
Component,
ComponentEvent,
Frame,
Input,
},
state::{focus, AppState, Focus},
};
Expand Down
6 changes: 2 additions & 4 deletions cli/src/component/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ pub struct AppTabs<T> {
}

impl<T> AppTabs<T>
where
T: IntoEnumIterator,
where T: IntoEnumIterator
{
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -90,8 +89,7 @@ impl<T> AppTabs<T> {
}

impl<T> Input for AppTabs<T>
where
T: TabGetter,
where T: TabGetter
{
type Output = ();

Expand Down
33 changes: 33 additions & 0 deletions cli/src/component/widgets/docker_detect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use ratatui::prelude::*;

use crate::component::{widgets::popup::Popup, Frame};

pub fn is_docker_running() -> bool {
match std::process::Command::new("docker").arg("version").output() {
Ok(output) => output.stderr.is_empty(),
Err(_) => false,
}
}

pub fn display_docker_notice<B: Backend>(f: &mut Frame<B>, title: &str, msg: &str) {
let popup_area = Rect {
x: 4,
y: 2,
width: 50,
height: 8,
};
let popup = Popup::default()
.content(msg)
.style(Style::new().yellow())
.title(title)
.title_style(Style::new().white().bold())
.border_style(Style::new().red());
f.render_widget(popup, popup_area);
}

pub fn wait_for_keypress() {
use std::io::{stdin, Read};
let mut stdin = stdin();
let buf: &mut [u8] = &mut [0; 2];
let _unused = stdin.read(buf).expect("Error reading keypress");
}
3 changes: 1 addition & 2 deletions cli/src/component/widgets/labeled_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ pub struct LabeledInput<T: Eq + FromStr = String> {
}

impl<T> LabeledInput<T>
where
T: Eq + FromStr + ToString,
where T: Eq + FromStr + ToString
{
pub fn new(label: impl ToString, focus: Focus) -> Self {
Self::new_with_filter(label, focus, |_| true)
Expand Down
2 changes: 2 additions & 0 deletions cli/src/component/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

pub mod docker_detect;
mod label;
mod labeled_input;
pub mod popup;
mod separator;
pub mod status_line;

Expand Down
35 changes: 35 additions & 0 deletions cli/src/component/widgets/popup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use derive_setters::Setters;
use ratatui::{
buffer::Buffer,
layout::Rect,
prelude::{Line, Style, Text},
widgets::{Block, Borders, Clear, Paragraph, Widget, Wrap},
};

#[derive(Debug, Default, Setters)]
pub struct Popup<'a> {
#[setters(into)]
title: Line<'a>,
#[setters(into)]
content: Text<'a>,
border_style: Style,
title_style: Style,
style: Style,
}

impl Widget for Popup<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
// Ensure that all cells under the popup are cleared to avoid leaking content
Clear.render(area, buf);
let block = Block::new()
.title(self.title)
.title_style(self.title_style)
.borders(Borders::ALL)
.border_style(self.border_style);
Paragraph::new(self.content)
.wrap(Wrap { trim: true })
.style(self.style)
.block(block)
.render(area, buf);
}
}
3 changes: 1 addition & 2 deletions cli/src/component/widgets/status_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ impl<T> Input for StatusLine<T> {
}

impl<B: Backend, T> Component<B> for StatusLine<T>
where
T: StatusReportGetter,
where T: StatusReportGetter
{
type State = AppState;

Expand Down
30 changes: 29 additions & 1 deletion cli/src/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ use tari_sim_launchpad::bus::{BusTx, LaunchpadBus};
use thiserror::Error;

use crate::{
component::{Component, ComponentEvent, Input, MainView, TerminationView},
component::{
display_docker_notice,
is_docker_running,
wait_for_keypress,
Component,
ComponentEvent,
Input,
MainView,
TerminationView,
},
events::{EventHandle, TermEvent},
state::{focus, AppState},
};
Expand Down Expand Up @@ -108,6 +117,25 @@ impl Actor for Dashboard {
let handle = EventHandle::new(addr);
self.event_handle = Some(handle);

if !is_docker_running() {
let msg = "\nThe Docker process is not detected.\nPlease ensure it is installed and running.\n\n'Ctrl Q' \
to quit.";
if self
.terminal
.as_mut()
.unwrap()
.draw(|f| display_docker_notice(f, "Docker Not Running!", msg))
.is_err()
{
println!("{}", msg);
}

wait_for_keypress();
println!();
println!();
std::process::exit(0);
}

self.connect_to_bus()?;

ctx.do_next(Redraw)?;
Expand Down
Loading

0 comments on commit 911c420

Please sign in to comment.