chore: added link copy to tui
1255f6f7
1 file(s) · +38 −6
| 49 | 49 | create_name: String, |
|
| 50 | 50 | create_content: String, |
|
| 51 | 51 | is_remote: bool, |
|
| 52 | + | remote_url: Option<String>, |
|
| 52 | 53 | } |
|
| 53 | 54 | ||
| 54 | 55 | impl App { |
|
| 55 | - | fn new(snippets: Vec<Snippet>, is_remote: bool) -> Self { |
|
| 56 | + | fn new(snippets: Vec<Snippet>, is_remote: bool, remote_url: Option<String>) -> Self { |
|
| 56 | 57 | let mut list_state = ListState::default(); |
|
| 57 | 58 | if !snippets.is_empty() { |
|
| 58 | 59 | list_state.select(Some(0)); |
|
| 75 | 76 | create_name: String::new(), |
|
| 76 | 77 | create_content: String::new(), |
|
| 77 | 78 | is_remote, |
|
| 79 | + | remote_url, |
|
| 78 | 80 | } |
|
| 79 | 81 | } |
|
| 80 | 82 | ||
| 127 | 129 | } |
|
| 128 | 130 | } |
|
| 129 | 131 | ||
| 132 | + | fn copy_link(&mut self) { |
|
| 133 | + | match &self.remote_url { |
|
| 134 | + | Some(url) => { |
|
| 135 | + | if let Some(snippet) = self.selected_snippet() { |
|
| 136 | + | let link = format!("{}/s/{}", url.trim_end_matches('/'), snippet.short_id); |
|
| 137 | + | if let Ok(mut clipboard) = Clipboard::new() { |
|
| 138 | + | let _ = clipboard.set_text(&link); |
|
| 139 | + | self.status_message = |
|
| 140 | + | Some(("Link copied!".to_string(), Instant::now())); |
|
| 141 | + | } |
|
| 142 | + | } |
|
| 143 | + | } |
|
| 144 | + | None => { |
|
| 145 | + | self.status_message = |
|
| 146 | + | Some(("No remote URL configured".to_string(), Instant::now())); |
|
| 147 | + | } |
|
| 148 | + | } |
|
| 149 | + | } |
|
| 150 | + | ||
| 130 | 151 | fn delete_selected(&mut self, backend: &Backend) { |
|
| 131 | 152 | if let Some(selected_index) = self.list_state.selected() { |
|
| 132 | 153 | if let Some(snippet) = self.snippets.get(selected_index) { |
|
| 254 | 275 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
|
| 255 | 276 | let cli = Cli::parse(); |
|
| 256 | 277 | ||
| 257 | - | let (backend, is_remote) = match cli.remote { |
|
| 258 | - | Some(url) => (Backend::remote(url, cli.api_key), true), |
|
| 259 | - | None => (Backend::local(), false), |
|
| 278 | + | let (backend, is_remote, remote_url) = match cli.remote { |
|
| 279 | + | Some(url) => (Backend::remote(url.clone(), cli.api_key), true, Some(url)), |
|
| 280 | + | None => (Backend::local(), false, None), |
|
| 260 | 281 | }; |
|
| 261 | 282 | ||
| 262 | 283 | let snippets = match backend.list_snippets() { |
|
| 267 | 288 | } |
|
| 268 | 289 | }; |
|
| 269 | 290 | ||
| 270 | - | ratatui::run(|terminal| run_app(terminal, App::new(snippets, is_remote), &backend)) |
|
| 291 | + | ratatui::run(|terminal| run_app(terminal, App::new(snippets, is_remote, remote_url), &backend)) |
|
| 271 | 292 | } |
|
| 272 | 293 | ||
| 273 | 294 | fn run_app( |
|
| 432 | 453 | if app.show_help { |
|
| 433 | 454 | let area = frame.area(); |
|
| 434 | 455 | let popup_width = 44u16.min(area.width.saturating_sub(4)); |
|
| 435 | - | let popup_height = 18u16.min(area.height.saturating_sub(4)); |
|
| 456 | + | let popup_height = 19u16.min(area.height.saturating_sub(4)); |
|
| 436 | 457 | let popup_area = ratatui::layout::Rect { |
|
| 437 | 458 | x: (area.width.saturating_sub(popup_width)) / 2, |
|
| 438 | 459 | y: (area.height.saturating_sub(popup_height)) / 2, |
|
| 489 | 510 | ]), |
|
| 490 | 511 | Line::from(vec![ |
|
| 491 | 512 | Span::styled( |
|
| 513 | + | " Y ", |
|
| 514 | + | Style::default() |
|
| 515 | + | .fg(Color::Yellow) |
|
| 516 | + | .add_modifier(Modifier::BOLD), |
|
| 517 | + | ), |
|
| 518 | + | Span::raw("Copy link"), |
|
| 519 | + | ]), |
|
| 520 | + | Line::from(vec![ |
|
| 521 | + | Span::styled( |
|
| 492 | 522 | " d ", |
|
| 493 | 523 | Style::default() |
|
| 494 | 524 | .fg(Color::Yellow) |
|
| 569 | 599 | KeyCode::Char('j') | KeyCode::Down => app.move_down(), |
|
| 570 | 600 | KeyCode::Char('k') | KeyCode::Up => app.move_up(), |
|
| 571 | 601 | KeyCode::Char('y') => app.copy_selected(), |
|
| 602 | + | KeyCode::Char('Y') => app.copy_link(), |
|
| 572 | 603 | KeyCode::Char('d') => app.delete_selected(backend), |
|
| 573 | 604 | KeyCode::Char('c') => app.start_create(), |
|
| 574 | 605 | KeyCode::Char('r') if app.is_remote => app.refresh(backend), |
|
| 589 | 620 | } |
|
| 590 | 621 | KeyCode::Char('k') | KeyCode::Up => app.scroll_up(), |
|
| 591 | 622 | KeyCode::Char('y') => app.copy_selected(), |
|
| 623 | + | KeyCode::Char('Y') => app.copy_link(), |
|
| 592 | 624 | KeyCode::Char('?') => app.show_help = true, |
|
| 593 | 625 | _ => {} |
|
| 594 | 626 | }, |
|