From 171677d44206030db8bcf1154972733b5eb458fa Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 17 Jul 2024 14:01:30 +0200 Subject: [PATCH] bin: Adjust text view props and show help as markdown --- CHANGELOG.md | 9 +- bin/commands/inbox/select.rs | 156 +++++-------------- bin/commands/inbox/select/ui.rs | 2 +- bin/commands/issue/select.rs | 266 ++++++-------------------------- bin/commands/patch/select.rs | 160 +++++-------------- bin/commands/patch/select/ui.rs | 1 - 6 files changed, 134 insertions(+), 460 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acbc20c1..8806d626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,15 +26,16 @@ ### Changed +**Binary features** + +- Selection interfaces don't show their browser scroll progress anymore +- Selection interfaces show their help as unstyled markdown + **Library features:** - Use container focus for table highlighting - Default keybindings for switching sections -**Binary features** - -- Selection interfaces don't show their browser scroll progress anymore - ### Removed **Library features:** diff --git a/bin/commands/inbox/select.rs b/bin/commands/inbox/select.rs index 1fae687b..8798828f 100644 --- a/bin/commands/inbox/select.rs +++ b/bin/commands/inbox/select.rs @@ -9,8 +9,6 @@ use termion::event::Key; use ratatui::layout::Constraint; use ratatui::style::Stylize; -use ratatui::text::Line; -use ratatui::text::Span; use ratatui::text::Text; use radicle::identity::Project; @@ -28,6 +26,7 @@ use tui::ui::span; use tui::ui::widget::container::{Column, Container, Footer, FooterProps, Header, HeaderProps}; use tui::ui::widget::input::TextView; use tui::ui::widget::input::TextViewProps; +use tui::ui::widget::input::TextViewState; use tui::ui::widget::window::{Page, PageProps, Shortcuts, ShortcutsProps, Window, WindowProps}; use tui::ui::widget::{ToWidget, Widget}; use tui::{BoxedAny, Channel, Exit, PageStack}; @@ -83,8 +82,7 @@ impl BrowserState { #[derive(Clone, Debug)] pub struct HelpState { - scroll: usize, - cursor: (usize, usize), + text: TextViewState, } #[derive(Clone, Debug)] @@ -198,32 +196,22 @@ impl TryFrom<&Context> for State { show_search: false, }, help: HelpState { - scroll: 0, - cursor: (0, 0), + text: TextViewState::default().content(help_text()), }, }) } } pub enum Message { - Exit { - selection: Option, - }, - Select { - selected: Option, - }, + Exit { selection: Option }, + Select { selected: Option }, OpenSearch, - UpdateSearch { - value: String, - }, + UpdateSearch { value: String }, ApplySearch, CloseSearch, OpenHelp, LeavePage, - ScrollHelp { - scroll: usize, - cursor: (usize, usize), - }, + ScrollHelp { state: TextViewState }, } impl store::State for State { @@ -274,9 +262,8 @@ impl store::State for State { self.pages.pop(); None } - Message::ScrollHelp { scroll, cursor } => { - self.help.scroll = scroll; - self.help.cursor = cursor; + Message::ScrollHelp { state } => { + self.help.text = state; None } } @@ -379,18 +366,13 @@ fn help_page(_state: &State, channel: &Channel) -> Widget) -> Widget) -> Widget Text<'static> { - Text::from( - [ - Line::from(Span::raw("Generic keybindings").cyan()), - Line::raw(""), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "↑,k")).gray(), - Span::raw(": "), - Span::raw("move cursor one line up").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "↓,j")).gray(), - Span::raw(": "), - Span::raw("move cursor one line down").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "PageUp")).gray(), - Span::raw(": "), - Span::raw("move cursor one page up").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "PageDown")).gray(), - Span::raw(": "), - Span::raw("move cursor one page down").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Home")).gray(), - Span::raw(": "), - Span::raw("move cursor to the first line").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "End")).gray(), - Span::raw(": "), - Span::raw("move cursor to the last line").gray().dim(), - ]), - Line::raw(""), - Line::from(Span::raw("Specific keybindings").cyan()), - Line::raw(""), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "enter")).gray(), - Span::raw(": "), - Span::raw("Select notification (if --mode id)").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "enter")).gray(), - Span::raw(": "), - Span::raw("Show notification").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "c")).gray(), - Span::raw(": "), - Span::raw("Clear notifications").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "/")).gray(), - Span::raw(": "), - Span::raw("Search").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "?")).gray(), - Span::raw(": "), - Span::raw("Show help").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Esc")).gray(), - Span::raw(": "), - Span::raw("Quit / cancel").gray().dim(), - ]), - Line::raw(""), - Line::from(Span::raw("Searching").cyan()), - Line::raw(""), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Pattern")).gray(), - Span::raw(": "), - Span::raw("is: | is:patch | is:issue | ") - .gray() - .dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Example")).gray(), - Span::raw(": "), - Span::raw("is:unseen is:patch Print").gray().dim(), - ]), - ] - .to_vec(), - ) +fn help_text() -> String { + r#"# Generic keybindings + +`↑,k`: move cursor one line up +`↓,j: move cursor one line down +`PageUp`: move cursor one page up +`PageDown`: move cursor one page down +`Home`: move cursor to the first line +`End`: move cursor to the last line +`Esc`: Quit / cancel + +# Specific keybindings + +`enter`: Select notification (if --mode id) +`enter`: Show notification +`c`: Clear notifications +`/`: Search +`?`: Show help + +# Searching + +Pattern: is: | is:patch | is:issue | +Example: is:unseen is:patch Print"# + .into() } diff --git a/bin/commands/inbox/select/ui.rs b/bin/commands/inbox/select/ui.rs index d58cec8b..d8681b2a 100644 --- a/bin/commands/inbox/select/ui.rs +++ b/bin/commands/inbox/select/ui.rs @@ -181,7 +181,7 @@ impl View for Browser { type Message = Message; type State = State; - fn handle_event(&mut self, props: Option<&ViewProps>, key: Key) -> Option { + fn handle_event(&mut self, props: Option<&ViewProps>, key: Key) -> Option { let default = BrowserProps::default(); let props = props .and_then(|props| props.inner_ref::()) diff --git a/bin/commands/issue/select.rs b/bin/commands/issue/select.rs index 49bf6876..64b907a2 100644 --- a/bin/commands/issue/select.rs +++ b/bin/commands/issue/select.rs @@ -10,7 +10,7 @@ use termion::event::Key; use ratatui::layout::Constraint; use ratatui::style::Stylize; -use ratatui::text::{Line, Span, Text}; +use ratatui::text::Text; use radicle::cob::thread::CommentId; use radicle::git::Oid; @@ -28,7 +28,7 @@ use tui::ui::widget::container::{ Column, Container, ContainerProps, Footer, FooterProps, Header, HeaderProps, SectionGroup, SectionGroupProps, SplitContainer, SplitContainerFocus, SplitContainerProps, }; -use tui::ui::widget::input::{TextView, TextViewProps}; +use tui::ui::widget::input::{TextView, TextViewProps, TextViewState}; use tui::ui::widget::list::{Tree, TreeProps}; use tui::ui::widget::window::{Page, PageProps, Shortcuts, ShortcutsProps, Window, WindowProps}; use tui::ui::widget::{PredefinedLayout, ToWidget, Widget}; @@ -149,22 +149,6 @@ impl BrowserState { } } -#[derive(Clone, Debug)] -pub struct CommentState { - /// Current text view cursor. - cursor: (usize, usize), -} - -impl CommentState { - pub fn reset_cursor(&mut self) { - self.cursor = (0, 0); - } - - pub fn update_cursor(&mut self, cursor: (usize, usize)) { - self.cursor = cursor; - } -} - #[derive(Clone, Debug)] pub struct PreviewState { /// If preview is visible. @@ -174,7 +158,7 @@ pub struct PreviewState { /// Tree selection per issue. selected_comments: HashMap>, /// State of currently selected comment - comment: CommentState, + comment: TextViewState, } impl PreviewState { @@ -222,8 +206,7 @@ impl PreviewState { #[derive(Clone, Debug)] pub struct HelpState { - scroll: usize, - cursor: (usize, usize), + text: TextViewState, } #[derive(Clone, Debug)] @@ -297,12 +280,11 @@ impl TryFrom<(&Context, &TerminalInfo)> for State { show: true, issue: items.first().cloned(), selected_comments, - comment: CommentState { cursor: (0, 0) }, + comment: TextViewState::default(), }, section: Some(Section::Browser), help: HelpState { - scroll: 0, - cursor: (0, 0), + text: TextViewState::default().content(help_text()), }, theme, }) @@ -311,35 +293,20 @@ impl TryFrom<(&Context, &TerminalInfo)> for State { pub enum Message { Quit, - Exit { - operation: Option, - }, + Exit { operation: Option }, ExitFromMode, - SelectIssue { - selected: Option, - }, + SelectIssue { selected: Option }, OpenSearch, - UpdateSearch { - value: String, - }, + UpdateSearch { value: String }, ApplySearch, CloseSearch, TogglePreview, - FocusSection { - section: Option
, - }, - SelectComment { - selected: Option>, - }, - ScrollComment { - cursor: (usize, usize), - }, + FocusSection { section: Option
}, + SelectComment { selected: Option> }, + ScrollComment { state: TextViewState }, OpenHelp, LeavePage, - ScrollHelp { - scroll: usize, - cursor: (usize, usize), - }, + ScrollHelp { state: TextViewState }, } impl store::State for State { @@ -393,8 +360,8 @@ impl store::State for State { self.preview.comment.reset_cursor(); None } - Message::ScrollComment { cursor } => { - self.preview.comment.update_cursor(cursor); + Message::ScrollComment { state } => { + self.preview.comment = state; None } Message::OpenSearch => { @@ -439,9 +406,8 @@ impl store::State for State { self.pages.pop(); None } - Message::ScrollHelp { scroll, cursor } => { - self.help.scroll = scroll; - self.help.cursor = cursor; + Message::ScrollHelp { state } => { + self.help.text = state; None } } @@ -637,10 +603,8 @@ fn comment(channel: &Channel) -> Widget { TextView::default() .to_widget(tx.clone()) .on_event(|_, vs, _| { - let textview = vs.and_then(|p| p.unwrap_textview()).unwrap_or_default(); - Some(Message::ScrollComment { - cursor: textview.cursor, - }) + let state = vs.and_then(|p| p.unwrap_textview()).unwrap_or_default(); + Some(Message::ScrollComment { state }) }) .on_update(|state: &State| { let comment = state.preview.selected_comment(); @@ -664,9 +628,8 @@ fn comment(channel: &Channel) -> Widget { .unwrap_or_default(); TextViewProps::default() - .content(body) + .state(Some(state.preview.comment.clone().content(body))) .footer(Some(reactions)) - .cursor(state.preview.comment.cursor) .show_scroll_progress(true) .dim(state.theme.dim_no_focus) .to_boxed_any() @@ -699,15 +662,11 @@ fn help_page(channel: &Channel) -> Widget { .on_event(|_, view_state, _| { view_state .and_then(|tv| tv.unwrap_textview()) - .map(|tvs| Message::ScrollHelp { - scroll: tvs.scroll, - cursor: tvs.cursor, - }) + .map(|tvs| Message::ScrollHelp { state: tvs }) }) .on_update(|state: &State| { TextViewProps::default() - .content(help_text()) - .cursor(state.help.cursor) + .state(Some(state.help.text.clone())) .dim(state.theme.dim_no_focus) .to_boxed_any() .into() @@ -722,7 +681,7 @@ fn help_page(channel: &Channel) -> Widget { [ Column::new(Text::raw(""), Constraint::Fill(1)), Column::new( - span::default(&format!("{}%", state.help.scroll)).dim(), + span::default(&format!("{}%", state.help.text.scroll)).dim(), Constraint::Min(4), ), ] @@ -760,158 +719,33 @@ fn help_page(channel: &Channel) -> Widget { .on_update(|_| PageProps::default().handle_keys(true).to_boxed_any().into()) } -fn help_text() -> Text<'static> { - Text::from( - [ - Line::from(Span::raw("Generic keybindings").cyan()), - Line::raw(""), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "↑,k")).gray(), - Span::raw(": "), - Span::raw("move cursor one line up").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "↓,j")).gray(), - Span::raw(": "), - Span::raw("move cursor one line down").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "PageUp")).gray(), - Span::raw(": "), - Span::raw("move cursor one page up").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "PageDown")).gray(), - Span::raw(": "), - Span::raw("move cursor one page down").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "Home")).gray(), - Span::raw(": "), - Span::raw("move cursor to the first line").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "End")).gray(), - Span::raw(": "), - Span::raw("move cursor to the last line").gray().dim(), - ] - .to_vec(), - ), - Line::raw(""), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "Tab")).gray(), - Span::raw(": "), - Span::raw("focus next section").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "Backtab")).gray(), - Span::raw(": "), - Span::raw("focus previous section").gray().dim(), - ] - .to_vec(), - ), - Line::raw(""), - Line::from(Span::raw("Specific keybindings").cyan()), - Line::raw(""), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "enter")).gray(), - Span::raw(": "), - Span::raw("Select issue (if --mode id)").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "enter")).gray(), - Span::raw(": "), - Span::raw("Show issue").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "e")).gray(), - Span::raw(": "), - Span::raw("Edit issue").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "p")).gray(), - Span::raw(": "), - Span::raw("Toggle issue preview").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "/")).gray(), - Span::raw(": "), - Span::raw("Search").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "?")).gray(), - Span::raw(": "), - Span::raw("Show help").gray().dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "Esc")).gray(), - Span::raw(": "), - Span::raw("Quit / cancel").gray().dim(), - ] - .to_vec(), - ), - Line::raw(""), - Line::from(Span::raw("Searching").cyan()), - Line::raw(""), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "Pattern")).gray(), - Span::raw(": "), - Span::raw("is: | is:authored | is:assigned | authors:[, ...] | assignees:[, ...] | ") - .gray() - .dim(), - ] - .to_vec(), - ), - Line::from( - [ - Span::raw(format!("{key:>10}", key = "Example")).gray(), - Span::raw(": "), - Span::raw("is:solved is:authored alias").gray().dim(), - ] - .to_vec(), - ), - ] - .to_vec()) +fn help_text() -> String { + r#"# Generic keybindings + +`↑,k`: move cursor one line up +`↓,j: move cursor one line down +`PageUp`: move cursor one page up +`PageDown`: move cursor one page down +`Home`: move cursor to the first line +`End`: move cursor to the last line +`Tab`: focus next section +`BackTab`: focus previous section +`Esc`: Quit / cancel + +# Specific keybindings + +`Enter`: Select issue (if --mode id) +`Enter`: Show issue +`e`: Edit issue +`p`: Toggle issue preview +`/`: Search +`?`: Show help + +# Searching + +Pattern: is: | is:authored | is:assigned | authors:[, ...] | assignees:[, ...] | +Example: is:solved is:authored alias"# + .into() } fn append_opened(all: &mut HashSet>, path: Vec, comment: CommentItem) { diff --git a/bin/commands/patch/select.rs b/bin/commands/patch/select.rs index e2a9b0a9..60e014b9 100644 --- a/bin/commands/patch/select.rs +++ b/bin/commands/patch/select.rs @@ -13,13 +13,13 @@ use radicle_tui as tui; use ratatui::layout::Constraint; use ratatui::style::Stylize; -use ratatui::text::{Line, Span, Text}; +use ratatui::text::Text; use termion::event::Key; use tui::store; use tui::ui::span; use tui::ui::widget::container::{Column, Container, Footer, FooterProps, Header, HeaderProps}; -use tui::ui::widget::input::{TextView, TextViewProps}; +use tui::ui::widget::input::{TextView, TextViewProps, TextViewState}; use tui::ui::widget::window::{Page, PageProps, Shortcuts, ShortcutsProps, Window, WindowProps}; use tui::ui::widget::{ToWidget, Widget}; @@ -72,8 +72,7 @@ impl BrowserState { #[derive(Clone, Debug)] pub struct HelpState { - scroll: usize, - cursor: (usize, usize), + text: TextViewState, } #[derive(Clone, Debug)] @@ -112,32 +111,22 @@ impl TryFrom<&Context> for State { show_search: false, }, help: HelpState { - scroll: 0, - cursor: (0, 0), + text: TextViewState::default().content(help_text()), }, }) } } pub enum Message { - Exit { - selection: Option, - }, - Select { - selected: Option, - }, + Exit { selection: Option }, + Select { selected: Option }, OpenSearch, - UpdateSearch { - value: String, - }, + UpdateSearch { value: String }, ApplySearch, CloseSearch, OpenHelp, LeavePage, - ScrollHelp { - scroll: usize, - cursor: (usize, usize), - }, + ScrollHelp { state: TextViewState }, } impl store::State for State { @@ -181,16 +170,17 @@ impl store::State for State { None } Message::OpenHelp => { + log::warn!("OpenHelp"); self.pages.push(AppPage::Help); None } Message::LeavePage => { + log::warn!("LeavePage"); self.pages.pop(); None } - Message::ScrollHelp { scroll, cursor } => { - self.help.scroll = scroll; - self.help.cursor = cursor; + Message::ScrollHelp { state } => { + self.help.text = state; None } } @@ -297,15 +287,11 @@ fn help_page(_state: &State, channel: &Channel) -> Widget) -> Widget) -> Widget Text<'static> { - Text::from( - [ - Line::from(Span::raw("Generic keybindings").cyan()), - Line::raw(""), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "↑,k")).gray(), - Span::raw(": "), - Span::raw("move cursor one line up").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "↓,j")).gray(), - Span::raw(": "), - Span::raw("move cursor one line down").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "PageUp")).gray(), - Span::raw(": "), - Span::raw("move cursor one page up").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "PageDown")).gray(), - Span::raw(": "), - Span::raw("move cursor one page down").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Home")).gray(), - Span::raw(": "), - Span::raw("move cursor to the first line").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "End")).gray(), - Span::raw(": "), - Span::raw("move cursor to the last line").gray().dim(), - ]), - Line::raw(""), - Line::from(Span::raw("Specific keybindings").cyan()), - Line::raw(""), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "enter")).gray(), - Span::raw(": "), - Span::raw("Select patch (if --mode id)").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "enter")).gray(), - Span::raw(": "), - Span::raw("Show patch").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "c")).gray(), - Span::raw(": "), - Span::raw("Checkout patch").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "d")).gray(), - Span::raw(": "), - Span::raw("Show patch diff").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "/")).gray(), - Span::raw(": "), - Span::raw("Search").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "?")).gray(), - Span::raw(": "), - Span::raw("Show help").gray().dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Esc")).gray(), - Span::raw(": "), - Span::raw("Quit / cancel").gray().dim(), - ]), - Line::raw(""), - Line::from(Span::raw("Searching").cyan()), - Line::raw(""), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Pattern")).gray(), - Span::raw(": "), - Span::raw("is: | is:authored | authors:[, ] | ") - .gray() - .dim(), - ]), - Line::from(vec![ - Span::raw(format!("{key:>10}", key = "Example")).gray(), - Span::raw(": "), - Span::raw("is:open is:authored improve").gray().dim(), - ]), - ] - .to_vec(), - ) +fn help_text() -> String { + r#"# Generic keybindings + +`↑,k`: move cursor one line up +`↓,j: move cursor one line down +`PageUp`: move cursor one page up +`PageDown`: move cursor one page down +`Home`: move cursor to the first line +`End`: move cursor to the last line +`Esc`: Quit / cancel + +# Specific keybindings + +`enter`: Select patch (if --mode id) +`enter`: Show patch +`c`: Checkout patch +`d`: Show patch diff +`/`: Search +`?`: Show help + +# Searching + +Pattern: is: | is:authored | authors:[, ] | +Example: is:open is:authored improve"# + .into() } diff --git a/bin/commands/patch/select/ui.rs b/bin/commands/patch/select/ui.rs index 3c5b417f..c6d8080b 100644 --- a/bin/commands/patch/select/ui.rs +++ b/bin/commands/patch/select/ui.rs @@ -219,7 +219,6 @@ impl View for Browser { } else { match key { Key::Esc | Key::Ctrl('c') => Some(Message::Exit { selection: None }), - Key::Char('?') => Some(Message::OpenHelp), Key::Char('/') => Some(Message::OpenSearch), Key::Char('\n') => { let operation = match props.mode {