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 9cdcbd9
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 15 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: 5 additions & 0 deletions cli/src/component/main_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
header::{mode::Mode, Header},
normal::NormalScene,
settings::SettingsScene,
widgets::docker_detect::is_docker_running,
Component, ComponentEvent, Input, Pass,
},
state::{focus, AppState},
Expand Down Expand Up @@ -66,6 +67,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
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)
}
}
}
8 changes: 3 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,20 @@ 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,
ComponentEvent::KeyEvent,
Input,
Input, Pass,
},
focus_id,
state::AppState,
state::{focus, AppState, Focus},
};

static MONERO_ADDRESS: Focus = focus_id!();
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 @@ -32,10 +32,9 @@ use ratatui::{
widgets::{Block, Borders, Paragraph},
};

use crate::state::AppEvent::UpdateState;
use crate::{
component::{widgets::Label, Component, ComponentEvent, Frame, Input},
state::{AppState, Focus},
state::{AppEvent::UpdateState, AppState, Focus},
};

pub enum Value<T> {
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);
}
}
24 changes: 23 additions & 1 deletion cli/src/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ 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 +111,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
3 changes: 1 addition & 2 deletions libs/protocol/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

use std::path::PathBuf;
use std::str::FromStr;
use std::{path::PathBuf, str::FromStr};

use serde::{Deserialize, Serialize};
use tari_common_types::tari_address::TariAddress;
Expand Down
1 change: 0 additions & 1 deletion libs/sdm-launchpad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ mod node_grpc;
pub mod resources;
#[cfg(feature = "tauri")]
pub mod tauri;

pub use bus::LaunchpadBus;
2 changes: 1 addition & 1 deletion libs/sdm-launchpad/src/resources/images/l5_mmproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use tari_sdm::{
};

use super::{TariBaseNode, DEFAULT_REGISTRY, GENERAL_VOLUME, VAR_TARI_PATH};
use crate::resources::volumes::SharedVolume;
use crate::resources::{
config::{ConnectionSettings, LaunchpadConfig, LaunchpadProtocol},
networks::LocalNet,
volumes::SharedVolume,
};

#[derive(Debug, Default)]
Expand Down

0 comments on commit 9cdcbd9

Please sign in to comment.