From 9704af3978b495e34ed966f20a4b1c2d10a3ba5a Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Thu, 12 Dec 2024 16:49:44 +0300 Subject: [PATCH] ability to copy wallet address added Signed-off-by: Uncle Stretch --- src/components/wallet/accounts.rs | 51 ++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/components/wallet/accounts.rs b/src/components/wallet/accounts.rs index f2f877e..278d460 100644 --- a/src/components/wallet/accounts.rs +++ b/src/components/wallet/accounts.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::fs::File; use std::io::{Write, BufRead, BufReader}; +use std::process::Command; use color_eyre::Result; use crossterm::event::{KeyCode, KeyEvent}; @@ -113,6 +114,31 @@ impl Accounts { } } + fn copy_to_clipboard(&mut self) { + if let Some(index) = self.table_state.selected() { + let address = self.wallet_keys[index].address.clone(); + match Command::new("xclip") + .arg("-selection") + .arg("clipboard") + .stdin(std::process::Stdio::piped()) + .spawn() + .and_then(|child| Ok(child + .stdin + .and_then(|mut cs| cs + .write_all(address.as_bytes()) + .ok()) + )) + { + Ok(Some(())) => self.log_event( + format!("address {} copied to clipboard", &address), + ActionLevel::Warn), + _ => self.log_event( + "could not use `xclip` to copy".to_string(), + ActionLevel::Error), + } + } + } + fn create_new_account(&mut self, name: String) { let (pair, seed) = Pair::generate(); let secret_seed = hex::encode(seed); @@ -402,17 +428,20 @@ impl Component for Accounts { } fn handle_key_event(&mut self, key: KeyEvent) -> Result> { - match key.code { - KeyCode::Up | KeyCode::Char('k') if self.is_active => self.previous_row(), - KeyCode::Down | KeyCode::Char('j') if self.is_active => self.next_row(), - KeyCode::Char('K') if self.is_active => self.swap_up(), - KeyCode::Char('J') if self.is_active => self.swap_down(), - KeyCode::Char('g') if self.is_active => self.first_row(), - KeyCode::Char('G') if self.is_active => self.last_row(), - KeyCode::Char('D') if self.is_active => self.delete_row(), - KeyCode::Char('R') if self.is_active => self.update_account_name(), - _ => {}, - }; + if self.is_active { + match key.code { + KeyCode::Up | KeyCode::Char('k') => self.previous_row(), + KeyCode::Down | KeyCode::Char('j') => self.next_row(), + KeyCode::Char('K') => self.swap_up(), + KeyCode::Char('J') => self.swap_down(), + KeyCode::Char('g') => self.first_row(), + KeyCode::Char('G') => self.last_row(), + KeyCode::Char('D') => self.delete_row(), + KeyCode::Char('R') => self.update_account_name(), + KeyCode::Char('Y') => self.copy_to_clipboard(), + _ => {}, + }; + } Ok(None) }