From b0ce41430f8f3b5e2aee71c03c9cbe97c4e71130 Mon Sep 17 00:00:00 2001 From: jooaf Date: Mon, 16 Dec 2024 20:58:54 -0600 Subject: [PATCH 1/6] refactor --- src/ui.rs | 37 ++++++++++++++++----- src/ui_handler.rs | 82 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 98 insertions(+), 21 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index e810d15..5ef1947 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -23,16 +23,18 @@ impl Default for EditCommandsPopup { } } -pub struct ErrorPopup { +pub struct UiPopup { pub message: String, + pub popup_title: String, pub visible: bool, } -impl ErrorPopup { - pub fn new() -> Self { - ErrorPopup { +impl UiPopup { + pub fn new(popup_tile: String) -> Self { + UiPopup { message: String::new(), visible: false, + popup_title: popup_tile, } } @@ -46,9 +48,9 @@ impl ErrorPopup { } } -impl Default for ErrorPopup { +impl Default for UiPopup { fn default() -> Self { - Self::new() + Self::new("".to_owned()) } } @@ -226,7 +228,7 @@ pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup) { f.render_widget(paragraph, area); } -pub fn render_error_popup(f: &mut Frame, popup: &ErrorPopup) { +pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup) { if !popup.visible { return; } @@ -240,11 +242,30 @@ pub fn render_error_popup(f: &mut Frame, popup: &ErrorPopup) { Block::default() .borders(Borders::ALL) .border_style(Style::default().fg(Color::Red)) - .title("Error - Esc to exit"), + .title(format!("{} - Esc to exit", popup.popup_title)), ); f.render_widget(text, area); } +// pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup) { +// if !popup.visible { +// return; +// } + +// let area = centered_rect(60, 20, f.size()); +// f.render_widget(ratatui::widgets::Clear, area); + +// let text = Paragraph::new(popup.message.as_str()) +// .style(Style::default().fg(Color::Red)) +// .block( +// Block::default() +// .borders(Borders::ALL) +// .border_style(Style::default().fg(Color::Red)) +// .title("Block Copied - Esc to exit"), +// ); +// f.render_widget(text, area); +// } + pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { let popup_layout = Layout::default() .direction(Direction::Vertical) diff --git a/src/ui_handler.rs b/src/ui_handler.rs index 1bfee11..604fb71 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -15,8 +15,8 @@ use tui_textarea::TextArea; use crate::{ format_json, format_markdown, get_save_file_path, load_textareas, save_textareas, ui::{ - render_edit_commands_popup, render_error_popup, render_header, render_title_popup, - render_title_select_popup, EditCommandsPopup, ErrorPopup, + render_edit_commands_popup, render_header, render_title_popup, render_title_select_popup, + render_ui_popup, EditCommandsPopup, UiPopup, }, ScrollableTextArea, TitlePopup, TitleSelectPopup, }; @@ -30,7 +30,8 @@ pub struct UIState { pub scrollable_textarea: ScrollableTextArea, pub title_popup: TitlePopup, pub title_select_popup: TitleSelectPopup, - pub error_popup: ErrorPopup, + pub error_popup: UiPopup, + pub copy_popup: UiPopup, pub edit_commands_popup: EditCommandsPopup, pub clipboard: Option, pub last_draw: Instant, @@ -54,7 +55,8 @@ impl UIState { scrollable_textarea, title_popup: TitlePopup::new(), title_select_popup: TitleSelectPopup::new(), - error_popup: ErrorPopup::new(), + error_popup: UiPopup::new("Error".to_string()), + copy_popup: UiPopup::new("Block Copied".to_string()), edit_commands_popup: EditCommandsPopup::new(), clipboard: EditorClipboard::try_new(), last_draw: Instant::now(), @@ -96,7 +98,11 @@ pub fn draw_ui( } if state.error_popup.visible { - render_error_popup(f, &state.error_popup); + render_ui_popup(f, &state.error_popup); + } + + if state.copy_popup.visible { + render_ui_popup(f, &state.copy_popup); } })?; Ok(()) @@ -138,11 +144,36 @@ fn handle_full_screen_input(state: &mut UIState, key: event::KeyEvent) -> Result KeyCode::Up => handle_up_key(state, key), KeyCode::Down => handle_down_key(state, key), KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => { - if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() { - state - .error_popup - .show(format!("Failed to copy to clipboard: {}", e)); + match state.scrollable_textarea.copy_focused_textarea_contents() { + Ok(_) => { + let curr_focused_index = state.scrollable_textarea.focused_index; + let curr_title_option = + state.scrollable_textarea.titles.get(curr_focused_index); + + match curr_title_option { + Some(curr_title) => { + state + .copy_popup + .show(format!("Copied block {}", curr_title)); + } + None => { + state + .error_popup + .show(format!("Failed to copy selection with title")); + } + } + } + Err(e) => { + state + .error_popup + .show(format!("Failed to copy to clipboard: {}", e)); + } } + // if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() { + // state + // .error_popup + // .show(format!("Failed to copy to clipboard: {}", e)); + // } } KeyCode::Char('s') if key.modifiers.contains(KeyModifiers::ALT) @@ -263,10 +294,32 @@ fn handle_normal_input( // edit_with_external_editor(state)?; } KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => { - if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() { - state - .error_popup - .show(format!("Failed to copy to clipboard: {}", e)); + // if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() { + // state + // .error_popup + // .show(format!("Failed to copy to clipboard: {}", e)); + // } + + match state.scrollable_textarea.copy_focused_textarea_contents() { + Ok(_) => { + let curr_focused_index = state.scrollable_textarea.focused_index; + let curr_title_option = + state.scrollable_textarea.titles.get(curr_focused_index); + + match curr_title_option { + Some(curr_title) => { + state + .copy_popup + .show(format!("Copied block {}", curr_title)); + } + None => {} + } + } + Err(e) => { + state + .error_popup + .show(format!("Failed to copy to clipboard: {}", e)); + } } } KeyCode::Char('b') if key.modifiers.contains(KeyModifiers::CONTROL) => { @@ -361,6 +414,9 @@ fn handle_normal_input( if state.error_popup.visible { state.error_popup.hide(); } + if state.copy_popup.visible { + state.copy_popup.hide(); + } } KeyCode::Up => handle_up_key(state, key), KeyCode::Down => handle_down_key(state, key), From 0f2d8124f2710a9e8ca0f39c590da7ada2e67ef4 Mon Sep 17 00:00:00 2001 From: jooaf Date: Tue, 17 Dec 2024 15:39:40 -0700 Subject: [PATCH 2/6] cleaning up commented code --- Cargo.lock | 2 +- src/ui_handler.rs | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f7a34b..f6ba5a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1412,7 +1412,7 @@ dependencies = [ [[package]] name = "thoth-cli" -version = "0.1.66" +version = "0.1.67" dependencies = [ "anyhow", "arboard", diff --git a/src/ui_handler.rs b/src/ui_handler.rs index 604fb71..9c99e62 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -169,11 +169,6 @@ fn handle_full_screen_input(state: &mut UIState, key: event::KeyEvent) -> Result .show(format!("Failed to copy to clipboard: {}", e)); } } - // if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() { - // state - // .error_popup - // .show(format!("Failed to copy to clipboard: {}", e)); - // } } KeyCode::Char('s') if key.modifiers.contains(KeyModifiers::ALT) @@ -291,15 +286,8 @@ fn handle_normal_input( } } } - // edit_with_external_editor(state)?; } KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => { - // if let Err(e) = state.scrollable_textarea.copy_focused_textarea_contents() { - // state - // .error_popup - // .show(format!("Failed to copy to clipboard: {}", e)); - // } - match state.scrollable_textarea.copy_focused_textarea_contents() { Ok(_) => { let curr_focused_index = state.scrollable_textarea.focused_index; From 336f88e58381072231fe82400da6cf12c3208f21 Mon Sep 17 00:00:00 2001 From: jooaf Date: Tue, 17 Dec 2024 15:41:00 -0700 Subject: [PATCH 3/6] removing commented code --- src/ui.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index 5ef1947..f98726f 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -247,25 +247,6 @@ pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup) { f.render_widget(text, area); } -// pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup) { -// if !popup.visible { -// return; -// } - -// let area = centered_rect(60, 20, f.size()); -// f.render_widget(ratatui::widgets::Clear, area); - -// let text = Paragraph::new(popup.message.as_str()) -// .style(Style::default().fg(Color::Red)) -// .block( -// Block::default() -// .borders(Borders::ALL) -// .border_style(Style::default().fg(Color::Red)) -// .title("Block Copied - Esc to exit"), -// ); -// f.render_widget(text, area); -// } - pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { let popup_layout = Layout::default() .direction(Direction::Vertical) From 524b7e42522c3452b3b05436bc4d870f9fb5a468 Mon Sep 17 00:00:00 2001 From: jooaf Date: Tue, 17 Dec 2024 15:42:40 -0700 Subject: [PATCH 4/6] updating error message for system clipboard copy --- src/ui_handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui_handler.rs b/src/ui_handler.rs index 9c99e62..bf127ce 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -166,7 +166,7 @@ fn handle_full_screen_input(state: &mut UIState, key: event::KeyEvent) -> Result Err(e) => { state .error_popup - .show(format!("Failed to copy to clipboard: {}", e)); + .show(format!("Failed to copy to system clipboard: {}", e)); } } } From ab84edf076ba11e4f2588ac0b634f2b90d456567 Mon Sep 17 00:00:00 2001 From: jooaf Date: Tue, 17 Dec 2024 15:47:03 -0700 Subject: [PATCH 5/6] updating message and adding error popup on failed copy --- src/ui_handler.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ui_handler.rs b/src/ui_handler.rs index bf127ce..1db70b1 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -300,13 +300,17 @@ fn handle_normal_input( .copy_popup .show(format!("Copied block {}", curr_title)); } - None => {} + None => { + state + .error_popup + .show(format!("Failed to copy selection with title")); + } } } Err(e) => { state .error_popup - .show(format!("Failed to copy to clipboard: {}", e)); + .show(format!("Failed to copy to system clipboard: {}", e)); } } } From fb7ac6634dbbf7a465d8be8d8c5469505ae3805c Mon Sep 17 00:00:00 2001 From: jooaf Date: Tue, 17 Dec 2024 15:53:39 -0700 Subject: [PATCH 6/6] linting --- src/ui_handler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui_handler.rs b/src/ui_handler.rs index 1db70b1..899a0d6 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -159,7 +159,7 @@ fn handle_full_screen_input(state: &mut UIState, key: event::KeyEvent) -> Result None => { state .error_popup - .show(format!("Failed to copy selection with title")); + .show("Failed to copy selection with title".to_string()); } } } @@ -303,7 +303,7 @@ fn handle_normal_input( None => { state .error_popup - .show(format!("Failed to copy selection with title")); + .show("Failed to copy selection with title".to_string()); } } }