Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pop-up for docker #302

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
56 changes: 56 additions & 0 deletions cli/src/component/widgets/docker_detect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

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
58 changes: 58 additions & 0 deletions cli/src/component/widgets/popup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2023. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

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
Loading