Add 'copy to clipboard' function for view page

This commit is contained in:
Ian Keane 2025-11-22 11:35:49 -05:00
parent 605d5f5792
commit 4f57c01693
5 changed files with 430 additions and 3 deletions

View file

@ -45,6 +45,7 @@ pub struct App {
pub log_view_content: Vec<String>,
pub log_view_scroll: usize,
pub log_view_selected: usize,
pub clipboard: Option<arboard::Clipboard>,
pub status_message: Option<(String, std::time::Instant)>,
}
@ -71,6 +72,7 @@ impl App {
log_view_content: Vec::new(),
log_view_scroll: 0,
log_view_selected: 0,
clipboard: arboard::Clipboard::new().ok(),
status_message: None,
})
}
@ -329,6 +331,9 @@ impl App {
KeyCode::Char('x') => {
self.delete_selected_frame()?;
}
KeyCode::Char('c') => {
self.copy_log_to_clipboard()?;
}
KeyCode::PageDown => {
self.log_view_selected = (self.log_view_selected + 10)
.min(self.log_view_frames.len().saturating_sub(1));
@ -461,6 +466,39 @@ impl App {
Ok(())
}
fn copy_log_to_clipboard(&mut self) -> anyhow::Result<()> {
if self.log_view_content.is_empty() {
return Ok(());
}
// Join all the formatted log content
let text = self.log_view_content.join("\n");
// Copy to clipboard using the persistent clipboard instance
if let Some(ref mut clipboard) = self.clipboard {
if let Err(e) = clipboard.set_text(&text) {
self.set_status_message(format!("Failed to copy: {}", e));
}
// Success - clipboard instance stays alive in App struct
} else {
// Try to create a new clipboard if we don't have one
match arboard::Clipboard::new() {
Ok(mut clipboard) => {
if let Err(e) = clipboard.set_text(&text) {
self.set_status_message(format!("Failed to copy: {}", e));
}
// Store it for future use
self.clipboard = Some(clipboard);
}
Err(e) => {
self.set_status_message(format!("Failed to access clipboard: {}", e));
}
}
}
Ok(())
}
fn move_selection(&mut self, delta: i32) {
let items_len = match self.state.current_pane {
0 => self.state.permanent_items.len(),

View file

@ -316,7 +316,7 @@ fn render_help(frame: &mut Frame, _app: &App) {
"Enter - Start/stop timer",
"d - Delete task",
"p - Reassign project",
"v - View Watson log (e to edit entries, x to delete)",
"v - View Watson log (e to edit entries, x to delete, c to copy)",
"Ctrl+e - Edit tasks config",
"c - Edit app config",
"n - New task",
@ -571,6 +571,7 @@ fn render_log_view(frame: &mut Frame, app: &App) {
("j/k", "select"),
("e", "edit"),
("x", "delete"),
("c", "copy"),
("q/ESC", "back"),
];