From 2282305e7ccae28360dd10e36009b6a8ccf8f01e Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Fri, 3 Apr 2020 16:24:52 +0200 Subject: [PATCH] switch to new command texts --- src/app.rs | 31 +++++------ src/components/changes.rs | 22 ++++---- src/components/command.rs | 46 ++++++++++------- src/components/commit.rs | 18 +++---- src/components/diff.rs | 7 +-- src/components/help.rs | 27 +++++----- src/components/mod.rs | 9 ++-- src/strings.rs | 106 +++++++++++++++++++++++++++++++------- 8 files changed, 169 insertions(+), 97 deletions(-) diff --git a/src/app.rs b/src/app.rs index 2ee16da2..deb9d9b4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -15,6 +15,7 @@ use crossterm::event::Event; use itertools::Itertools; use log::trace; use std::borrow::Cow; +use strings::commands; use tui::{ backend::Backend, layout::{Alignment, Constraint, Direction, Layout, Rect}, @@ -256,7 +257,8 @@ impl App { let mut res = Vec::new(); for c in self.components() { - if c.commands(&mut res) != CommandBlocking::PassingOn + if c.commands(&mut res, force_all) + != CommandBlocking::PassingOn && !force_all { break; @@ -271,9 +273,8 @@ impl App { let focus_on_stage = self.focus == Focus::Stage; let focus_not_diff = self.focus != Focus::Diff; res.push( - CommandInfo::new( - strings::CMD_STATUS_FOCUS_UNSTAGED, - strings::CMD_GROUP_GENERAL, + CommandInfo::new_new( + commands::STATUS_FOCUS_UNSTAGED, true, main_cmds_available && focus_on_stage @@ -282,9 +283,8 @@ impl App { .hidden(), ); res.push( - CommandInfo::new( - strings::CMD_STATUS_FOCUS_STAGED, - strings::CMD_GROUP_GENERAL, + CommandInfo::new_new( + commands::STATUS_FOCUS_STAGED, true, main_cmds_available && !focus_on_stage @@ -295,24 +295,21 @@ impl App { } { let focus_on_diff = self.focus == Focus::Diff; - res.push(CommandInfo::new( - strings::CMD_STATUS_LEFT, - strings::CMD_GROUP_GENERAL, + res.push(CommandInfo::new_new( + commands::STATUS_FOCUS_LEFT, true, main_cmds_available && focus_on_diff, )); - res.push(CommandInfo::new( - strings::CMD_STATUS_RIGHT, - strings::CMD_GROUP_GENERAL, + res.push(CommandInfo::new_new( + commands::STATUS_FOCUS_RIGHT, true, main_cmds_available && !focus_on_diff, )); } res.push( - CommandInfo::new( - strings::CMD_STATUS_QUIT, - strings::CMD_GROUP_GENERAL, + CommandInfo::new_new( + commands::QUIT, true, main_cmds_available, ) @@ -376,7 +373,7 @@ impl App { .filter_map(|c| { if c.show_in_quickbar() { Some(Text::Styled( - Cow::from(c.name.clone()), + Cow::from(c.text.name.clone()), if c.enabled { style_enabled } else { diff --git a/src/components/changes.rs b/src/components/changes.rs index 78df9235..ab504ae6 100644 --- a/src/components/changes.rs +++ b/src/components/changes.rs @@ -11,6 +11,7 @@ use std::{ convert::{From, TryFrom}, path::Path, }; +use strings::commands; use tui::{ backend::Backend, layout::Rect, @@ -173,33 +174,30 @@ impl Component for ChangesComponent { fn commands( &self, out: &mut Vec, + _force_all: bool, ) -> CommandBlocking { let some_selection = self.selection().is_some(); if self.is_working_dir { - out.push(CommandInfo::new( - strings::CMD_STATUS_STAGE, - strings::CMD_GROUP_CHANGES, + out.push(CommandInfo::new_new( + commands::STAGE_FILE, some_selection, self.focused, )); - out.push(CommandInfo::new( - strings::CMD_STATUS_RESET, - strings::CMD_GROUP_CHANGES, + out.push(CommandInfo::new_new( + commands::RESET_FILE, some_selection, self.focused, )); } else { - out.push(CommandInfo::new( - strings::CMD_STATUS_UNSTAGE, - strings::CMD_GROUP_CHANGES, + out.push(CommandInfo::new_new( + commands::UNSTAGE_FILE, some_selection, self.focused, )); } - out.push(CommandInfo::new( - strings::CMD_SCROLL, - strings::CMD_GROUP_GENERAL, + out.push(CommandInfo::new_new( + commands::SCROLL, self.items.len() > 1, self.focused, )); diff --git a/src/components/command.rs b/src/components/command.rs index 44fa3394..5bf459c9 100644 --- a/src/components/command.rs +++ b/src/components/command.rs @@ -1,13 +1,28 @@ +/// +#[derive(Copy, Clone)] +pub struct CommandText { + /// + pub name: &'static str, + /// + pub desc: &'static str, + /// + pub group: &'static str, +} + +impl CommandText { + pub const fn new( + name: &'static str, + desc: &'static str, + group: &'static str, + ) -> Self { + Self { name, desc, group } + } +} + /// pub struct CommandInfo { /// - pub name: String, - /// - pub group: String, - /// - pub desc: String, - /// - // pub keys: + pub text: CommandText, /// available but not active in the context pub enabled: bool, /// will show up in the quick bar @@ -20,16 +35,13 @@ pub struct CommandInfo { impl CommandInfo { /// - pub fn new( - name: &str, - group: &str, + pub fn new_new( + text: CommandText, enabled: bool, available: bool, ) -> Self { Self { - name: name.to_string(), - group: group.to_string(), - desc: String::default(), + text, enabled, quick_bar: true, available, @@ -43,12 +55,6 @@ impl CommandInfo { res } /// - pub fn desc(self, txt: &str) -> Self { - let mut res = self; - res.desc = txt.to_string(); - res - } - /// pub fn hidden(self) -> Self { let mut res = self; res.quick_bar = false; @@ -56,7 +62,7 @@ impl CommandInfo { } /// pub fn print(&self, out: &mut String) { - out.push_str(self.name.as_str()); + out.push_str(self.text.name); } /// pub fn show_in_quickbar(&self) -> bool { diff --git a/src/components/commit.rs b/src/components/commit.rs index 527328fa..97ca39ce 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -6,6 +6,7 @@ use crate::{keys, strings, ui}; use asyncgit::{sync, CWD}; use crossterm::event::{Event, KeyCode}; use std::borrow::Cow; +use strings::commands; use tui::{ backend::Backend, layout::{Alignment, Rect}, @@ -17,7 +18,6 @@ use tui::{ #[derive(Default)] pub struct CommitComponent { msg: String, - // focused: bool, visible: bool, stage_empty: bool, } @@ -52,22 +52,20 @@ impl Component for CommitComponent { fn commands( &self, out: &mut Vec, + _force_all: bool, ) -> CommandBlocking { - out.push(CommandInfo::new( - strings::COMMIT_CMD_OPEN, - strings::CMD_GROUP_COMMIT, + out.push(CommandInfo::new_new( + commands::COMMIT_OPEN, !self.stage_empty, !self.visible, )); - out.push(CommandInfo::new( - strings::COMMIT_CMD_ENTER, - strings::CMD_GROUP_COMMIT, + out.push(CommandInfo::new_new( + commands::COMMIT_ENTER, self.can_commit(), self.visible, )); - out.push(CommandInfo::new( - strings::COMMIT_CMD_CLOSE, - strings::CMD_GROUP_COMMIT, + out.push(CommandInfo::new_new( + commands::CLOSE_POPUP, true, self.visible, )); diff --git a/src/components/diff.rs b/src/components/diff.rs index 7160a25a..90bae2a7 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -6,6 +6,7 @@ use crate::{ use asyncgit::{hash, Diff, DiffLine, DiffLineType}; use crossterm::event::{Event, KeyCode}; use std::{borrow::Cow, cmp, convert::TryFrom}; +use strings::commands; use tui::{ backend::Backend, layout::{Alignment, Rect}, @@ -245,10 +246,10 @@ impl Component for DiffComponent { fn commands( &self, out: &mut Vec, + _force_all: bool, ) -> CommandBlocking { - out.push(CommandInfo::new( - strings::CMD_SCROLL, - strings::CMD_GROUP_DIFF, + out.push(CommandInfo::new_new( + commands::SCROLL, self.can_scroll(), self.focused, )); diff --git a/src/components/help.rs b/src/components/help.rs index 36884bb3..3c5f6c7a 100644 --- a/src/components/help.rs +++ b/src/components/help.rs @@ -7,6 +7,7 @@ use asyncgit::hash; use crossterm::event::Event; use itertools::Itertools; use std::{borrow::Cow, cmp, convert::TryFrom}; +use strings::commands; use tui::{ backend::Backend, layout::{Alignment, Rect}, @@ -58,33 +59,30 @@ impl Component for HelpComponent { fn commands( &self, out: &mut Vec, + force_all: bool, ) -> CommandBlocking { // only if help is open we have no other commands available - if self.visible { + if self.visible && !force_all { out.clear(); } out.push( - CommandInfo::new( - strings::CMD_STATUS_HELP, - strings::CMD_GROUP_GENERAL, + CommandInfo::new_new( + commands::HELP_OPEN, true, !self.visible, ) - .desc("open this help screen") .order(99), ); - out.push(CommandInfo::new( - strings::CMD_SCROLL, - strings::CMD_GROUP_GENERAL, + out.push(CommandInfo::new_new( + commands::SCROLL, true, self.visible, )); - out.push(CommandInfo::new( - strings::COMMIT_CMD_CLOSE, - strings::CMD_GROUP_GENERAL, + out.push(CommandInfo::new_new( + commands::CLOSE_POPUP, true, self.visible, )); @@ -129,7 +127,7 @@ impl HelpComponent { /// pub fn set_cmds(&mut self, cmds: Vec) { self.cmds = cmds; - self.cmds.sort_by_key(|e| hash(&e.group)); + self.cmds.sort_by_key(|e| hash(&e.text.group)); } fn move_selection(&mut self, inc: bool) { @@ -154,7 +152,7 @@ impl HelpComponent { let mut selected_line = 0_u16; for (key, group) in - &self.cmds.iter().group_by(|e| e.group.clone()) + &self.cmds.iter().group_by(|e| e.text.group.clone()) { txt.push(Text::Styled( Cow::from(format!(" {}\n", key)), @@ -182,7 +180,8 @@ impl HelpComponent { if is_selected { out.push_str( - format!(" {}\n", e.desc).as_str(), + format!(" {}\n", e.text.desc) + .as_str(), ); } diff --git a/src/components/mod.rs b/src/components/mod.rs index 97760327..1cc519f8 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -7,7 +7,7 @@ mod commit; mod diff; mod help; pub use changes::ChangesComponent; -pub use command::CommandInfo; +pub use command::{CommandInfo, CommandText}; pub use commit::CommitComponent; pub use diff::DiffComponent; pub use help::HelpComponent; @@ -45,8 +45,11 @@ pub trait DrawableComponent { /// pub trait Component { /// - fn commands(&self, out: &mut Vec) - -> CommandBlocking; + fn commands( + &self, + out: &mut Vec, + force_all: bool, + ) -> CommandBlocking; /// fn event(&mut self, ev: Event) -> Option; /// diff --git a/src/strings.rs b/src/strings.rs index 9bd11a77..b637090e 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -5,27 +5,97 @@ pub static TITLE_INDEX: &str = "Staged Changes [2]"; pub static TAB_STATUS: &str = "Status"; pub static TAB_DIVIDER: &str = " | "; -pub static CMD_GROUP_GENERAL: &str = "General"; -pub static CMD_GROUP_DIFF: &str = "Diff"; -pub static CMD_GROUP_CHANGES: &str = "Changes"; -pub static CMD_GROUP_COMMIT: &str = "Commit"; - -pub static CMD_STATUS_FOCUS_UNSTAGED: &str = "Unstaged [1]"; -pub static CMD_STATUS_FOCUS_STAGED: &str = "Staged [2]"; -pub static CMD_STATUS_STAGE: &str = "Stage File [enter]"; -pub static CMD_STATUS_UNSTAGE: &str = "Unstage File [enter]"; -pub static CMD_STATUS_RESET: &str = "Reset File [D]"; -pub static CMD_STATUS_QUIT: &str = "Quit [esc,q]"; -pub static CMD_STATUS_HELP: &str = "Help [h]"; -pub static CMD_STATUS_LEFT: &str = "Back [\u{2190}]"; //← -pub static CMD_STATUS_RIGHT: &str = "Diff [\u{2192}]"; //→ pub static CMD_SPLITTER: &str = " "; -pub static CMD_SCROLL: &str = "Scroll [\u{2191}\u{2193}]"; //↑↓ +// pub static CMD_SCROLL: &str = "Scroll [\u{2191}\u{2193}]"; //↑↓ pub static COMMIT_TITLE: &str = "Commit"; pub static COMMIT_MSG: &str = "type commit message.."; -pub static COMMIT_CMD_OPEN: &str = "Commit [c]"; -pub static COMMIT_CMD_ENTER: &str = "Commit [enter]"; -pub static COMMIT_CMD_CLOSE: &str = "Close [esc]"; pub static HELP_TITLE: &str = "Help"; + +pub mod commands { + use crate::components::CommandText; + + static CMD_GROUP_GENERAL: &str = "General"; + static CMD_GROUP_CHANGES: &str = "Changes"; + static CMD_GROUP_COMMIT: &str = "Commit"; + + /// + pub static HELP_OPEN: CommandText = CommandText::new( + "Help [h]", + "open this help screen", + CMD_GROUP_GENERAL, + ); + /// + pub static SCROLL: CommandText = CommandText::new( + "Scroll [\u{2191}\u{2193}]", + "scroll up or down in focused view", + CMD_GROUP_GENERAL, + ); + /// + pub static CLOSE_POPUP: CommandText = CommandText::new( + "Close [esc]", + "close popup", + CMD_GROUP_GENERAL, + ); + /// + pub static COMMIT_OPEN: CommandText = CommandText::new( + "Commit [c]", + "open commit view (available in non-empty stage)", + CMD_GROUP_COMMIT, + ); + /// + pub static COMMIT_ENTER: CommandText = CommandText::new( + "Commit [enter]", + "commit (available when commit message is non-empty)", + CMD_GROUP_COMMIT, + ); + /// + pub static STAGE_FILE: CommandText = CommandText::new( + "Stage File [enter]", + "stage currently selected file", + CMD_GROUP_CHANGES, + ); + /// + pub static UNSTAGE_FILE: CommandText = CommandText::new( + "Unstage File [enter]", + "remove currently selected file from stage", + CMD_GROUP_CHANGES, + ); + /// + pub static RESET_FILE: CommandText = CommandText::new( + "Reset File [D]", + "revert changes in selected file", + CMD_GROUP_CHANGES, + ); + /// + pub static STATUS_FOCUS_UNSTAGED: CommandText = CommandText::new( + "Unstaged [1]", + "view changes in working dir", + CMD_GROUP_GENERAL, + ); + /// + pub static STATUS_FOCUS_STAGED: CommandText = CommandText::new( + "Staged [2]", + "view staged changes", + CMD_GROUP_GENERAL, + ); + /// + pub static STATUS_FOCUS_LEFT: CommandText = CommandText::new( + "Back [\u{2190}]", //← + "view staged changes", + CMD_GROUP_GENERAL, + ); + /// + pub static STATUS_FOCUS_RIGHT: CommandText = CommandText::new( + "Diff [\u{2192}]", //→ + "inspect file diff", + CMD_GROUP_GENERAL, + ); + /// + pub static QUIT: CommandText = CommandText::new( + "Quit [esc,q]", + "quit gitui application", + CMD_GROUP_GENERAL, + ); +}