confirm destive command: reset file

This commit is contained in:
Stephan Dilly 2020-04-03 23:11:46 +02:00
parent c838721544
commit 923f7a46d6
7 changed files with 150 additions and 6 deletions

View File

@ -59,7 +59,6 @@ GITUI_LOGGING=true gitui
# todo for 0.1 (first release)
* [ ] fix: run in non-git folder -> crash
* [ ] confirm destructive commands (revert/reset)
* [ ] (un)staging selected hunks
* [ ] publish as homebrew-tap

View File

@ -2,7 +2,7 @@ use crate::{
components::{
ChangesComponent, CommandBlocking, CommandInfo,
CommitComponent, Component, DiffComponent, DrawableComponent,
EventUpdate, HelpComponent,
EventUpdate, HelpComponent, ResetComponent,
},
keys,
queue::{InternalEvent, Queue},
@ -46,6 +46,7 @@ pub struct App {
focus: Focus,
diff_target: DiffTarget,
do_quit: bool,
reset: ResetComponent,
commit: CommitComponent,
help: HelpComponent,
index: ChangesComponent,
@ -66,6 +67,7 @@ impl App {
focus: Focus::WorkDir,
diff_target: DiffTarget::WorkingDir,
do_quit: false,
reset: ResetComponent::new(queue.clone()),
commit: CommitComponent::default(),
help: HelpComponent::default(),
index_wd: ChangesComponent::new(
@ -151,6 +153,7 @@ impl App {
self.commit.draw(f, f.size());
self.help.draw(f, f.size());
self.reset.draw(f, f.size());
}
///
@ -277,6 +280,10 @@ impl App {
self.update();
}
}
InternalEvent::ConfirmResetFile(p) => {
self.reset.open_for_path(p);
self.update_commands();
}
};
}
@ -292,8 +299,9 @@ impl App {
}
}
let main_cmds_available =
!self.commit.is_visible() && !self.help.is_visible();
let main_cmds_available = !self.commit.is_visible()
&& !self.help.is_visible()
&& !self.reset.is_visible();
{
{
@ -349,6 +357,7 @@ impl App {
fn components(&self) -> Vec<&dyn Component> {
vec![
&self.reset,
&self.commit,
&self.help,
&self.index,
@ -359,6 +368,7 @@ impl App {
fn components_mut(&mut self) -> Vec<&mut dyn Component> {
vec![
&mut self.reset,
&mut self.commit,
&mut self.help,
&mut self.index,

View File

@ -124,7 +124,7 @@ impl ChangesComponent {
if let Some(i) = self.selection() {
self.queue
.borrow_mut()
.push_back(InternalEvent::ResetFile(i.path));
.push_back(InternalEvent::ConfirmResetFile(i.path));
return true;
}

View File

@ -6,11 +6,13 @@ mod command;
mod commit;
mod diff;
mod help;
mod reset;
pub use changes::ChangesComponent;
pub use command::{CommandInfo, CommandText};
pub use commit::CommitComponent;
pub use diff::DiffComponent;
pub use help::HelpComponent;
pub use reset::ResetComponent;
///
pub enum EventUpdate {

124
src/components/reset.rs Normal file
View File

@ -0,0 +1,124 @@
use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventUpdate,
};
use crate::{
queue::{InternalEvent, Queue},
strings, ui,
};
use crossterm::event::{Event, KeyCode};
use std::borrow::Cow;
use strings::commands;
use tui::{
backend::Backend,
layout::{Alignment, Rect},
style::{Color, Style},
widgets::{Block, Borders, Paragraph, Text, Widget},
Frame,
};
pub struct ResetComponent {
path: String,
visible: bool,
queue: Queue,
}
impl DrawableComponent for ResetComponent {
fn draw<B: Backend>(&self, f: &mut Frame<B>, _rect: Rect) {
if self.visible {
let mut txt = Vec::new();
txt.push(Text::Styled(
Cow::from(strings::RESET_MSG),
Style::default().fg(Color::Red),
));
ui::Clear::new(
Paragraph::new(txt.iter())
.block(
Block::default()
.title(strings::RESET_TITLE)
.borders(Borders::ALL),
)
.alignment(Alignment::Left),
)
.render(f, ui::centered_rect(30, 20, f.size()));
}
}
}
impl Component for ResetComponent {
fn commands(
&self,
out: &mut Vec<CommandInfo>,
_force_all: bool,
) -> CommandBlocking {
out.push(CommandInfo::new(
commands::RESET_CONFIRM,
true,
self.visible,
));
out.push(CommandInfo::new(
commands::CLOSE_POPUP,
true,
self.visible,
));
visibility_blocking(self)
}
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
if self.visible {
if let Event::Key(e) = ev {
return Some(match e.code {
KeyCode::Esc => {
self.hide();
EventUpdate::Commands
}
KeyCode::Enter => {
self.confirm();
EventUpdate::None
}
_ => EventUpdate::None,
});
}
}
None
}
fn is_visible(&self) -> bool {
self.visible
}
fn hide(&mut self) {
self.visible = false
}
fn show(&mut self) {
self.visible = true
}
}
impl ResetComponent {
///
pub fn new(queue: Queue) -> Self {
Self {
path: String::default(),
visible: false,
queue,
}
}
///
pub fn open_for_path(&mut self, path: &str) {
self.path = path.to_string();
self.show();
}
///
pub fn confirm(&mut self) {
self.hide();
self.queue
.borrow_mut()
.push_back(InternalEvent::ResetFile(self.path.clone()));
}
}

View File

@ -2,6 +2,8 @@ use std::{cell::RefCell, collections::VecDeque, rc::Rc};
///
pub enum InternalEvent {
///
ConfirmResetFile(String),
///
ResetFile(String),
}

View File

@ -6,10 +6,11 @@ pub static TAB_STATUS: &str = "Status";
pub static TAB_DIVIDER: &str = " | ";
pub static CMD_SPLITTER: &str = " ";
// 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 RESET_TITLE: &str = "Reset";
pub static RESET_MSG: &str = "confirm file reset?";
pub static HELP_TITLE: &str = "Help";
@ -98,4 +99,10 @@ pub mod commands {
"quit gitui application",
CMD_GROUP_GENERAL,
);
///
pub static RESET_CONFIRM: CommandText = CommandText::new(
"Confirm [enter]",
"resets the file in question",
CMD_GROUP_GENERAL,
);
}