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

View File

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

View File

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