Skip to content

Commit

Permalink
feat: fix windows copy-paste issue (#328)
Browse files Browse the repository at this point in the history
* Fix Windows copy-paste issue
- Fixed the Windows copy/paste issue where a `v` will always be added to the paste
  buffer when using `Ctrl-v` to paste.
- Changed the wallet address labeled input to always display the wallet address
  using the emoji ID string.

* Update libs/sdm-assets/assets/log4rs-cli.yml

---------

Co-authored-by: C.Lee Taylor <47312074+leet4tari@users.noreply.github.com>
Co-authored-by: SW van Heerden <swvheerden@gmail.com>
  • Loading branch information
3 people committed Mar 11, 2024
1 parent 976fd13 commit efdcdef
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 63 deletions.
3 changes: 1 addition & 2 deletions cli/src/component/main_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ use ratatui::{
Frame,
};

use crate::component::expert::logs::LogsScene;
use crate::{
component::{
expert::ExpertScene,
expert::{logs::LogsScene, ExpertScene},
header::{mode::Mode, Header},
normal::NormalScene,
settings::SettingsScene,
Expand Down
24 changes: 14 additions & 10 deletions cli/src/component/normal/legend_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

use crate::component::elements::block_with_title;
use crate::component::Component;
use crate::state::AppState;
use ratatui::backend::Backend;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::prelude::Line;
use ratatui::style::{Color, Style};
use ratatui::text::Span;
use ratatui::widgets::{List, ListItem, Padding};
use ratatui::Frame;
use ratatui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
prelude::Line,
style::{Color, Style},
text::Span,
widgets::{List, ListItem, Padding},
Frame,
};

use crate::{
component::{elements::block_with_title, Component},
state::AppState,
};

pub struct LegendWidget {}

Expand Down
3 changes: 1 addition & 2 deletions cli/src/component/normal/mining/mining_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use log::warn;
use ratatui::prelude::*;
use tari_launchpad_protocol::settings::LaunchpadSettings;

use crate::component::widgets::popup::Popup;
use crate::{
component::{
elements::block_with_title,
normal::mining::{
helpers::{MergeMiningStatus, ShaMiningStatus},
status_badge::StatusBadge,
},
widgets::LabeledInput,
widgets::{popup::Popup, LabeledInput},
Component, ComponentEvent,
ComponentEvent::KeyEvent,
Input, Pass,
Expand Down
3 changes: 1 addition & 2 deletions cli/src/component/normal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
};

use crate::component::normal::legend_widget::LegendWidget;
use crate::{
component::{
normal::{base_node::BaseNodeWidget, containers::ContainersScene},
normal::{base_node::BaseNodeWidget, containers::ContainersScene, legend_widget::LegendWidget},
Component, ComponentEvent, Frame, Input,
},
state::AppState,
Expand Down
3 changes: 1 addition & 2 deletions cli/src/component/settings/base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
};

use crate::component::widgets::qr_code::QrCodePreview;
use crate::{
component::{
elements::block_with_title,
widgets::{LabeledInput, Separator},
widgets::{qr_code::QrCodePreview, LabeledInput, Separator},
Component, ComponentEvent, Frame, Input, Pass,
},
focus_id,
Expand Down
3 changes: 1 addition & 2 deletions cli/src/component/settings/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use ratatui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
};
use tari_launchpad_protocol::settings::LaunchpadSettings;
use tari_launchpad_protocol::{settings::LaunchpadSettings, OptionUsizeWrapper};

use crate::{
component::{
Expand All @@ -40,7 +40,6 @@ use crate::{
AppState,
},
};
use tari_launchpad_protocol::OptionUsizeWrapper;

pub static MINING_SETTINGS: Focus = focus_id!();
static MONERO_ADDRESS: Focus = focus_id!();
Expand Down
7 changes: 6 additions & 1 deletion cli/src/component/widgets/labeled_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use ratatui::{
style::{Color, Style},
widgets::{Block, Borders, Paragraph},
};
use tari_common_types::tari_address::TariAddress;

use crate::{
component::{widgets::Label, Component, ComponentEvent, Frame, Input},
Expand Down Expand Up @@ -135,7 +136,11 @@ where
}

pub fn set(&mut self, value: T) {
self.content = value.to_string();
self.content = if let Ok(val) = TariAddress::from_str(&value.to_string()) {
TariAddress::to_emoji_string(&val)
} else {
value.to_string()
};
self.value = Value::Valid { value };
}

Expand Down
16 changes: 8 additions & 8 deletions cli/src/component/widgets/qr_code.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2023. The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use qrcode::render::unicode;
use qrcode::QrCode;
use ratatui::widgets::Block;
use ratatui::{backend::Backend, layout::Rect, widgets::Paragraph};
use qrcode::{render::unicode, QrCode};
use ratatui::{
backend::Backend,
layout::Rect,
widgets::{Block, Paragraph},
};
use tari_common_types::types::PublicKey;
use tari_launchpad_protocol::settings::TariNetwork;
use tari_launchpad_protocol::wallet::InvalidPublicKey;
use tari_utilities::hex::Hex;
use tari_utilities::ByteArray;
use tari_launchpad_protocol::{settings::TariNetwork, wallet::InvalidPublicKey};
use tari_utilities::{hex::Hex, ByteArray};

use crate::{
component::{Component, ComponentEvent, Frame, Input},
Expand Down
6 changes: 2 additions & 4 deletions cli/src/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ impl Actor for Dashboard {
let url = "https://docs.docker.com/engine/install/ubuntu/";

let msg = format!(
"\nThe Docker process is not detected.\nIs it installed and running?\n\n\
Download docker at {url}\n\n\
Press any key to quit."
"\nThe Docker process is not detected.\nIs it installed and running?\n\nDownload docker at \
{url}\n\nPress any key to quit."
);
if self
.terminal
Expand Down Expand Up @@ -235,7 +234,6 @@ impl Do<TermEvent> for Dashboard {
if key.kind != crossterm::event::KeyEventKind::Release {
return Ok(());
}

let state = self.state.as_mut().ok_or_else(|| DashboardError::State)?;
self.main_view.on_event(key.into(), state);
let changed = state.process_events();
Expand Down
54 changes: 52 additions & 2 deletions cli/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use std::{
};

use anyhow::Error;
use crossterm::event::{poll, read, Event};
use crossterm::event::{poll, read, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use log::trace;
use tact::Address;

use crate::dashboard::Dashboard;
Expand All @@ -52,12 +53,61 @@ impl EventHandle {
let handle = std::thread::spawn({
let interrupted = interrupted.clone();
move || -> Result<(), Error> {
let mut v_pressed = false;
while !interrupted.load(Ordering::Relaxed) {
let duration = Duration::from_millis(200);
let has_event = poll(duration)?;
if has_event {
let event = read()?;
addr.send(TermEvent::Event(event))?;
trace!(target: "crossterm_events", "EventHandle: KeyEvent '{:?}'", event);
match event {
// 'Ctrl-v' (to paste) does not have the same behaviour on Windows and Linux. This
// difference is evident in events `Event::Key(KeyEvent)`. When a key is pressed on Windows,
// it sends a `KeyEvent` with `KeyEventKind::Press` and then a `KeyEvent` with
// `KeyEventKind::Release`. On Linux, it sends a `KeyEvent` with `KeyEventKind::Press` only.
// The difference with 'Ctrl-v' on Linux is that the buffer is pasted with a series of
// `KeyEventKind::Press` events for each character, but on Windows it is pasted with a
// series of `KeyEventKind::Press` and `KeyEventKind::Release` events for each character,
// however, on Windows the 'v' in the 'Ctrl-v' is accompanied by a `KeyEventKind::Release`
// without the preceding `KeyEventKind::Press`. In some cases, the `KeyEventKind::Release`
// has a modifier `KeyModifiers::CONTROL` and in other cases it does not. Ultimately, this
// determines which key events should be handled as terminal events further down the line.
// The approach followed here is that `KeyCode::Char('v')` with modifier
// `KeyModifiers::CONTROL` or `KeyCode::Char('v')` with `KeyEventKind::Release` without a
// preceding `KeyEventKind::Press` should be ignored. All other cases should be handled as
// terminal events.
Event::Key(KeyEvent {
code,
modifiers,
kind,
state: _,
}) => {
let ctrl = modifiers.contains(KeyModifiers::CONTROL);
match code {
KeyCode::Char('v') => {
if kind == KeyEventKind::Press {
v_pressed = true;
}
if ctrl {
// 'Ctrl-v' do nothing
v_pressed = false;
trace!(target: "crossterm_events", "EventHandle: KeyEvent '{:?}' (Ctrl-v, do nothing)", code);
} else if kind == KeyEventKind::Release && !v_pressed {
// 'v' released without being pressed, do nothing
trace!(target: "crossterm_events", "EventHandle: KeyEvent '{:?}' (v released without being pressed, do nothing)", code);
} else {
// Process all other cases
if kind == KeyEventKind::Release && v_pressed {
v_pressed = false;
}
addr.send(TermEvent::Event(event))?;
}
},
_ => addr.send(TermEvent::Event(event))?,
}
},
_ => addr.send(TermEvent::Event(event))?,
}
}
}
addr.send(TermEvent::End)?;
Expand Down
3 changes: 2 additions & 1 deletion libs/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

use serde::{Deserialize, Serialize};
use std::str::FromStr;

use serde::{Deserialize, Serialize};

pub mod config;
pub mod container;
pub mod errors;
Expand Down
3 changes: 1 addition & 2 deletions libs/protocol/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use minotari_node_grpc_client::grpc::NodeIdentity;
use serde::{Deserialize, Serialize};
use tari_common_types::{emoji::EmojiId, types::PublicKey};
use tari_utilities::byte_array::ByteArray;
use tari_utilities::hex::Hex;
use tari_utilities::{byte_array::ByteArray, hex::Hex};

use crate::wallet::InvalidPublicKey;

Expand Down
13 changes: 8 additions & 5 deletions libs/protocol/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

use crate::OptionUsizeWrapper;
use std::{fmt::Display, path::PathBuf, str::FromStr};

use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::{path::PathBuf, str::FromStr};
use tari_common_types::tari_address::TariAddress;
use thiserror::Error;

use crate::OptionUsizeWrapper;

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct BaseNodeConfig {
/// Should node be started in interactive mode.
Expand All @@ -46,6 +47,7 @@ pub struct XmRigConfig {
pub struct Sha3MinerConfig {
/// The number of threads to employ for SHA3 mining
pub num_mining_threads: usize,
/// The address that will accept Tari mining rewards
pub wallet_payment_address: Option<String>,
}

Expand All @@ -60,7 +62,7 @@ pub struct MmProxyConfig {
pub monero_password: String,
/// If true, provide the monero username and password to the daemon. Otherwise those strings are ignored.
pub monero_use_auth: bool,

/// The address that will accept Tari mining rewards
pub wallet_payment_address: Option<String>,
}

Expand Down Expand Up @@ -152,11 +154,12 @@ impl PersistentSettings {
payment_address: S,
) -> Result<(), InvalidTariAddress> {
let address = payment_address.into();

let address = if address.is_empty() {
None
} else {
match TariAddress::from_str(&address) {
Ok(_address) => Some(address),
Ok(val) => Some(val.to_emoji_string()),
Err(e) => return Err(InvalidTariAddress(e.to_string())),
}
};
Expand Down
5 changes: 5 additions & 0 deletions libs/sdm-assets/assets/log4rs-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ loggers:
appenders:
- core
additive: false
crossterm_events:
level: trace
appenders:
- core
additive: false
3 changes: 1 addition & 2 deletions libs/sdm-launchpad/src/resources/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ use anyhow::{anyhow, Error};
use minotari_wallet_grpc_client::grpc::GetIdentityResponse;
use serde::Serialize;
use tari_common_types::{emoji::EmojiId, types::PublicKey};
use tari_launchpad_protocol::node::BaseNodeIdentity;
use tari_launchpad_protocol::session::LaunchpadSession;
pub use tari_launchpad_protocol::{
config::LaunchpadConfig,
settings::{LaunchpadSettings, TariNetwork},
};
use tari_launchpad_protocol::{node::BaseNodeIdentity, session::LaunchpadSession};
use tari_sdm::{config::ManagedProtocol, image::Envs};
use tari_utilities::ByteArray;

Expand Down
5 changes: 2 additions & 3 deletions libs/sdm-launchpad/src/resources/images/l2_base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use anyhow::Error;
use async_trait::async_trait;
use log::debug;
use minotari_node_grpc_client::{grpc, BaseNodeGrpcClient};
use tari_launchpad_protocol::container::TaskProgress;
use tari_launchpad_protocol::settings::BaseNodeConfig;
use tari_launchpad_protocol::{container::TaskProgress, settings::BaseNodeConfig};
use tari_sdm::{
ids::{ManagedTask, TaskId},
image::{
Expand All @@ -39,9 +38,9 @@ use super::{
sync_progress::SyncProgress, Tor, BLOCKCHAIN_PATH, BLOCKCHAIN_VOLUME, DEFAULT_REGISTRY, GENERAL_VOLUME,
VAR_TARI_PATH,
};
use crate::resources::images::sync_progress::SyncType;
use crate::resources::{
config::{ConnectionSettings, LaunchpadConfig, LaunchpadInnerEvent, LaunchpadProtocol},
images::sync_progress::SyncType,
networks::LocalNet,
volumes::SharedVolume,
};
Expand Down
16 changes: 9 additions & 7 deletions libs/sdm-launchpad/src/resources/images/l3_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

use std::str::FromStr;

use log::{info, warn};
use tari_common_types::tari_address::TariAddress;
use tari_sdm::{
ids::{ManagedTask, TaskId},
image::{Args, Envs, ManagedContainer, Mounts, Networks, Volumes},
};

use super::{TariBaseNode, DEFAULT_REGISTRY, GENERAL_VOLUME};
use crate::resources::{
config::{ConnectionSettings, LaunchpadConfig, LaunchpadProtocol},
images::VAR_TARI_PATH,
networks::LocalNet,
volumes::SharedVolume,
};
use log::{info, warn};
use std::str::FromStr;
use tari_common_types::tari_address::TariAddress;
use tari_sdm::{
ids::{ManagedTask, TaskId},
image::{Args, Envs, ManagedContainer, Mounts, Networks, Volumes},
};

#[derive(Debug, Default)]
pub struct TariSha3Miner {
Expand Down
3 changes: 1 addition & 2 deletions libs/sdm-launchpad/src/resources/images/l5_mmproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ use std::ops::Deref;

use log::*;
use tari_launchpad_protocol::settings::MmProxyConfig;
use tari_sdm::image::Ports;
use tari_sdm::{
ids::{ManagedTask, TaskId},
image::{Envs, ManagedContainer, Mounts, Networks, Volumes},
image::{Envs, ManagedContainer, Mounts, Networks, Ports, Volumes},
};

use super::{TariBaseNode, DEFAULT_REGISTRY, GENERAL_VOLUME, VAR_TARI_PATH};
Expand Down
Loading

0 comments on commit efdcdef

Please sign in to comment.