central place for strings/keys and better key bindings for focusing

This commit is contained in:
Stephan Dilly 2020-03-19 21:40:48 +01:00
parent 8552269ed7
commit 09f893f3b0
6 changed files with 57 additions and 33 deletions

View File

@ -4,6 +4,7 @@ use crate::{
IndexComponent, IndexComponent,
}, },
git_utils::{self, Diff}, git_utils::{self, Diff},
keys, strings,
}; };
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
use git2::StatusShow; use git2::StatusShow;
@ -51,12 +52,12 @@ impl App {
do_quit: false, do_quit: false,
commit: CommitComponent::default(), commit: CommitComponent::default(),
index_wd: IndexComponent::new( index_wd: IndexComponent::new(
"Status [s]", strings::TITLE_STATUS,
StatusShow::Workdir, StatusShow::Workdir,
true, true,
), ),
index: IndexComponent::new( index: IndexComponent::new(
"Index [i]", strings::TITLE_INDEX,
StatusShow::Index, StatusShow::Index,
false, false,
), ),
@ -80,10 +81,10 @@ impl App {
Tabs::default() Tabs::default()
.block(Block::default().borders(Borders::BOTTOM)) .block(Block::default().borders(Borders::BOTTOM))
.titles(&["Status" /*"Branches", "Stash", "Misc"*/]) .titles(&[strings::TAB_STATUS])
.style(Style::default().fg(Color::White)) .style(Style::default().fg(Color::White))
.highlight_style(Style::default().fg(Color::Yellow)) .highlight_style(Style::default().fg(Color::Yellow))
.divider(" | ") .divider(strings::TAB_DIVIDER)
.render(f, chunks_main[0]); .render(f, chunks_main[0]);
let chunks = Layout::default() let chunks = Layout::default()
@ -148,15 +149,11 @@ impl App {
self.do_quit = true; self.do_quit = true;
} }
if ev == Event::Key(KeyCode::Tab.into()) { if ev == Event::Key(keys::FOCUS_STATUS) {
self.toggle_focus();
}
if ev == Event::Key(KeyCode::Char('s').into()) {
self.switch_focus(Focus::Status); self.switch_focus(Focus::Status);
} else if ev == Event::Key(KeyCode::Char('i').into()) { } else if ev == Event::Key(keys::FOCUS_STAGE) {
self.switch_focus(Focus::Stage); self.switch_focus(Focus::Stage);
} else if ev == Event::Key(KeyCode::Char('d').into()) { } else if ev == Event::Key(keys::FOCUS_DIFF) {
self.switch_focus(Focus::Diff); self.switch_focus(Focus::Diff);
} }
@ -210,26 +207,26 @@ impl App {
let some_selection = let some_selection =
self.index_wd.selection().is_some(); self.index_wd.selection().is_some();
res.push(CommandInfo { res.push(CommandInfo {
name: "Stage File [enter]".to_string(), name: strings::CMD_STATUS_STAGE.to_string(),
enabled: some_selection, enabled: some_selection,
}); });
res.push(CommandInfo { res.push(CommandInfo {
name: "Reset File [D]".to_string(), name: strings::CMD_STATUS_RESET.to_string(),
enabled: some_selection, enabled: some_selection,
}); });
} else if self.index.focused() { } else if self.index.focused() {
res.push(CommandInfo { res.push(CommandInfo {
name: "Unstage File [enter]".to_string(), name: strings::CMD_STATUS_UNSTAGE.to_string(),
enabled: self.index.selection().is_some(), enabled: self.index.selection().is_some(),
}); });
} }
res.push(CommandInfo { res.push(CommandInfo {
name: "Next [tab]".to_string(), name: strings::CMD_STATUS_NEXT.to_string(),
enabled: true, enabled: true,
}); });
res.push(CommandInfo { res.push(CommandInfo {
name: "Quit [esc,q]".to_string(), name: strings::CMD_STATUS_QUIT.to_string(),
enabled: true, enabled: true,
}); });
} }
@ -244,7 +241,7 @@ impl App {
cmds: Vec<CommandInfo>, cmds: Vec<CommandInfo>,
) { ) {
let splitter = Text::Styled( let splitter = Text::Styled(
Cow::from(" "), Cow::from(strings::CMD_SPLITTER),
Style::default().bg(Color::Black), Style::default().bg(Color::Black),
); );
@ -272,14 +269,6 @@ impl App {
.render(f, r); .render(f, r);
} }
fn toggle_focus(&mut self) {
self.switch_focus(match self.focus {
Focus::Status => Focus::Diff,
Focus::Diff => Focus::Stage,
Focus::Stage => Focus::Status,
});
}
fn switch_focus(&mut self, f: Focus) { fn switch_focus(&mut self, f: Focus) {
if self.focus != f { if self.focus != f {
self.focus = f; self.focus = f;

View File

@ -1,5 +1,5 @@
use super::{CommandInfo, Component}; use super::{CommandInfo, Component};
use crate::{clear::Clear, git_utils, tui_utils}; use crate::{clear::Clear, git_utils, strings, tui_utils};
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
use std::borrow::Cow; use std::borrow::Cow;
use tui::{ use tui::{
@ -24,7 +24,7 @@ impl Component for CommitComponent {
[Text::Raw(Cow::from(self.msg.clone()))] [Text::Raw(Cow::from(self.msg.clone()))]
} else { } else {
[Text::Styled( [Text::Styled(
Cow::from("type commit message.."), Cow::from(strings::COMMIT_MSG),
Style::default().fg(Color::DarkGray), Style::default().fg(Color::DarkGray),
)] )]
}; };
@ -33,7 +33,7 @@ impl Component for CommitComponent {
Paragraph::new(txt.iter()) Paragraph::new(txt.iter())
.block( .block(
Block::default() Block::default()
.title("Commit") .title(strings::COMMIT_TITLE)
.borders(Borders::ALL), .borders(Borders::ALL),
) )
.alignment(Alignment::Left), .alignment(Alignment::Left),
@ -45,17 +45,17 @@ impl Component for CommitComponent {
fn commands(&self) -> Vec<CommandInfo> { fn commands(&self) -> Vec<CommandInfo> {
if !self.visible { if !self.visible {
vec![CommandInfo { vec![CommandInfo {
name: "Commit [c]".to_string(), name: strings::COMMIT_CMD_OPEN.to_string(),
enabled: !git_utils::index_empty(), enabled: !git_utils::index_empty(),
}] }]
} else { } else {
vec![ vec![
CommandInfo { CommandInfo {
name: "Commit [enter]".to_string(), name: strings::COMMIT_CMD_ENTER.to_string(),
enabled: self.can_commit(), enabled: self.can_commit(),
}, },
CommandInfo { CommandInfo {
name: "Close [esc]".to_string(), name: strings::COMMIT_CMD_CLOSE.to_string(),
enabled: true, enabled: true,
}, },
] ]

View File

@ -1,6 +1,7 @@
use crate::{ use crate::{
components::{CommandInfo, Component}, components::{CommandInfo, Component},
git_utils::{Diff, DiffLine, DiffLineType}, git_utils::{Diff, DiffLine, DiffLineType},
strings,
}; };
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
use tui::{ use tui::{
@ -85,7 +86,7 @@ impl Component for DiffComponent {
Paragraph::new(txt.iter()) Paragraph::new(txt.iter())
.block( .block(
Block::default() Block::default()
.title("Diff [d]") .title(strings::DIFF_TITLE)
.borders(Borders::ALL) .borders(Borders::ALL)
.border_style(style_border) .border_style(style_border)
.title_style(style_title), .title_style(style_title),
@ -98,7 +99,7 @@ impl Component for DiffComponent {
fn commands(&self) -> Vec<CommandInfo> { fn commands(&self) -> Vec<CommandInfo> {
if self.focused { if self.focused {
return vec![CommandInfo { return vec![CommandInfo {
name: "Scroll [↑↓]".to_string(), name: strings::DIFF_CMD_SCROLL.to_string(),
enabled: self.can_scroll(), enabled: self.can_scroll(),
}]; }];
} }

12
src/keys.rs Normal file
View File

@ -0,0 +1,12 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
const fn no_mod(code: KeyCode) -> KeyEvent {
KeyEvent {
code,
modifiers: KeyModifiers::empty(),
}
}
pub static FOCUS_STATUS: KeyEvent = no_mod(KeyCode::Char('1'));
pub static FOCUS_DIFF: KeyEvent = no_mod(KeyCode::Char('2'));
pub static FOCUS_STAGE: KeyEvent = no_mod(KeyCode::Char('3'));

View File

@ -3,7 +3,9 @@ mod clear;
mod components; mod components;
mod git_status; mod git_status;
mod git_utils; mod git_utils;
mod keys;
mod poll; mod poll;
mod strings;
mod tui_utils; mod tui_utils;
use crate::{app::App, poll::QueueEvent}; use crate::{app::App, poll::QueueEvent};

20
src/strings.rs Normal file
View File

@ -0,0 +1,20 @@
pub static TITLE_STATUS: &str = "Status [1]";
pub static TITLE_INDEX: &str = "Index [3]";
pub static TAB_STATUS: &str = "Status";
pub static TAB_DIVIDER: &str = " | ";
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_NEXT: &str = "Next [tab]";
pub static CMD_STATUS_QUIT: &str = "Quit [esc,q]";
pub static CMD_SPLITTER: &str = " ";
pub static DIFF_CMD_SCROLL: &str = "Scroll [↑↓]";
pub static DIFF_TITLE: &str = "Diff [2]";
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]";