Skip to content

Commit 4164eea

Browse files
committed
add copy to clipboard
1 parent aaf8c55 commit 4164eea

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/ui/about_widget.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
22
use async_trait::async_trait;
33
use ratatui::{prelude::*, style::palette::tailwind, widgets::*};
4+
use serde_json::{json, Map, Value};
45

56
use crate::{
67
app::{actions::Action, config::AppConfig, AppContext},
78
component::Component,
89
tui::Event,
910
ui::PALETTES,
10-
utils::{absolute_path_as_string, config_dir, data_dir, format_path_for_display},
11+
utils::{
12+
absolute_path_as_string, config_dir, copy_to_clipboard, data_dir, format_path_for_display,
13+
},
1114
};
1215

13-
const BLOCK_TITLE: &str = " About | <Esc> close ";
16+
const BLOCK_TITLE: &str = " About | <Esc> close | <Ctrl+C> Copy ";
1417

1518
#[derive(Debug)]
1619
struct TableColors {
@@ -79,6 +82,38 @@ impl AboutPage {
7982
vec!["Follow symbolic links".into(), follow_sym_links.into()],
8083
]
8184
}
85+
86+
fn copy_about_msg(&self) -> Result<()> {
87+
let config_info = self.config_info(); // Call your original function
88+
89+
let mut map = Map::new();
90+
for pair in config_info {
91+
if let [key, value] = &pair[..] {
92+
map.insert(key.clone(), Value::String(value.clone()));
93+
}
94+
}
95+
let config_info_json = json!(map);
96+
97+
let app_info = self.app_info(); // Call your original function
98+
let mut map = Map::new();
99+
for pair in app_info {
100+
if let [key, value] = &pair[..] {
101+
map.insert(key.clone(), Value::String(value.clone()));
102+
}
103+
}
104+
let app_info_json = json!(map);
105+
106+
let about_json = serde_json::to_string_pretty(&serde_json::json!(
107+
{
108+
"app": app_info_json,
109+
"configuration": config_info_json,
110+
}
111+
))
112+
.context("Failed to serialize about message")?;
113+
114+
copy_to_clipboard(&about_json).context("Failed to copy about message to clipboard")?;
115+
Ok(())
116+
}
82117
}
83118

84119
impl Default for AboutPage {
@@ -130,6 +165,14 @@ impl Component for AboutPage {
130165
self.is_active = false;
131166
Ok(Action::SwitchAppContext(self.caller_context).into())
132167
}
168+
crossterm::event::KeyCode::Char('c')
169+
if key.modifiers == crossterm::event::KeyModifiers::CONTROL =>
170+
{
171+
if let Err(copy_err) = self.copy_about_msg() {
172+
log::error!("Failed to copy about message: {copy_err}");
173+
}
174+
Ok(None)
175+
}
133176
_ => Ok(None),
134177
}
135178
}

0 commit comments

Comments
 (0)