Skip to content

Commit 520456d

Browse files
author
Stephan Dilly
committed
visualize commit-msg hook when failed
1 parent 19b540c commit 520456d

File tree

10 files changed

+165
-9
lines changed

10 files changed

+165
-9
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ GITUI_LOGGING=true gitui
5656

5757
# todo for 0.2 (first release)
5858

59-
* [ ] visualize commit-msg hook result
6059
* [ ] publish as homebrew-tap
6160

6261
# inspiration

src/app.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
components::{
33
ChangesComponent, CommandBlocking, CommandInfo,
44
CommitComponent, Component, DiffComponent, DrawableComponent,
5-
EventUpdate, HelpComponent, ResetComponent,
5+
EventUpdate, HelpComponent, MsgComponent, ResetComponent,
66
},
77
keys,
88
queue::{InternalEvent, Queue},
@@ -52,6 +52,7 @@ pub struct App {
5252
index: ChangesComponent,
5353
index_wd: ChangesComponent,
5454
diff: DiffComponent,
55+
msg: MsgComponent,
5556
git_diff: AsyncDiff,
5657
git_status: AsyncStatus,
5758
current_commands: Vec<CommandInfo>,
@@ -68,7 +69,7 @@ impl App {
6869
diff_target: DiffTarget::WorkingDir,
6970
do_quit: false,
7071
reset: ResetComponent::new(queue.clone()),
71-
commit: CommitComponent::default(),
72+
commit: CommitComponent::new(queue.clone()),
7273
help: HelpComponent::default(),
7374
index_wd: ChangesComponent::new(
7475
strings::TITLE_STATUS,
@@ -83,6 +84,7 @@ impl App {
8384
queue.clone(),
8485
),
8586
diff: DiffComponent::new(queue.clone()),
87+
msg: MsgComponent::default(),
8688
git_diff: AsyncDiff::new(sender.clone()),
8789
git_status: AsyncStatus::new(sender),
8890
current_commands: Vec::new(),
@@ -152,8 +154,9 @@ impl App {
152154
);
153155

154156
self.commit.draw(f, f.size());
155-
self.help.draw(f, f.size());
156157
self.reset.draw(f, f.size());
158+
self.help.draw(f, f.size());
159+
self.msg.draw(f, f.size());
157160
}
158161

159162
///
@@ -302,6 +305,10 @@ impl App {
302305
}
303306
}
304307
}
308+
InternalEvent::ShowMsg(msg) => {
309+
self.msg.show_msg(msg);
310+
self.update();
311+
}
305312
};
306313
}
307314

@@ -317,9 +324,11 @@ impl App {
317324
}
318325
}
319326

327+
//TODO: use a new popups_list call for this
320328
let main_cmds_available = !self.commit.is_visible()
321329
&& !self.help.is_visible()
322-
&& !self.reset.is_visible();
330+
&& !self.reset.is_visible()
331+
&& !self.msg.is_visible();
323332

324333
{
325334
{
@@ -375,6 +384,7 @@ impl App {
375384

376385
fn components(&self) -> Vec<&dyn Component> {
377386
vec![
387+
&self.msg,
378388
&self.reset,
379389
&self.commit,
380390
&self.help,
@@ -386,6 +396,7 @@ impl App {
386396

387397
fn components_mut(&mut self) -> Vec<&mut dyn Component> {
388398
vec![
399+
&mut self.msg,
389400
&mut self.reset,
390401
&mut self.commit,
391402
&mut self.help,

src/components/command.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@ pub struct CommandText {
77
pub desc: &'static str,
88
///
99
pub group: &'static str,
10+
///
11+
pub hide_help: bool,
1012
}
1113

1214
impl CommandText {
15+
///
1316
pub const fn new(
1417
name: &'static str,
1518
desc: &'static str,
1619
group: &'static str,
1720
) -> Self {
18-
Self { name, desc, group }
21+
Self {
22+
name,
23+
desc,
24+
group,
25+
hide_help: false,
26+
}
27+
}
28+
///
29+
pub const fn hide_help(self) -> Self {
30+
let mut tmp = self;
31+
tmp.hide_help = true;
32+
tmp
1933
}
2034
}
2135

src/components/commit.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use super::{
22
visibility_blocking, CommandBlocking, CommandInfo, Component,
33
DrawableComponent, EventUpdate,
44
};
5-
use crate::{keys, strings, ui};
5+
use crate::{
6+
keys,
7+
queue::{InternalEvent, Queue},
8+
strings, ui,
9+
};
610
use asyncgit::{sync, CWD};
711
use crossterm::event::{Event, KeyCode};
812
use log::error;
@@ -17,11 +21,11 @@ use tui::{
1721
Frame,
1822
};
1923

20-
#[derive(Default)]
2124
pub struct CommitComponent {
2225
msg: String,
2326
visible: bool,
2427
stage_empty: bool,
28+
queue: Queue,
2529
}
2630

2731
impl DrawableComponent for CommitComponent {
@@ -122,17 +126,39 @@ impl Component for CommitComponent {
122126
}
123127

124128
impl CommitComponent {
129+
///
130+
pub fn new(queue: Queue) -> Self {
131+
Self {
132+
queue,
133+
msg: String::default(),
134+
stage_empty: true,
135+
visible: false,
136+
}
137+
}
138+
125139
fn commit(&mut self) {
126140
if let HookResult::NotOk(e) =
127141
sync::hooks_commit_msg(CWD, &mut self.msg)
128142
{
129143
error!("commit-msg hook error: {}", e);
144+
self.queue.borrow_mut().push_back(
145+
InternalEvent::ShowMsg(format!(
146+
"commit-msg hook error:\n{}",
147+
e
148+
)),
149+
);
130150
return;
131151
}
132152

133153
sync::commit(CWD, &self.msg);
134154
if let HookResult::NotOk(e) = sync::hooks_post_commit(CWD) {
135155
error!("post-commit hook error: {}", e);
156+
self.queue.borrow_mut().push_back(
157+
InternalEvent::ShowMsg(format!(
158+
"post-commit hook error:\n{}",
159+
e
160+
)),
161+
);
136162
}
137163

138164
self.msg.clear();

src/components/help.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ impl Component for HelpComponent {
126126
impl HelpComponent {
127127
///
128128
pub fn set_cmds(&mut self, cmds: Vec<CommandInfo>) {
129-
self.cmds = cmds;
129+
self.cmds = cmds
130+
.into_iter()
131+
.filter(|e| !e.text.hide_help)
132+
.collect::<Vec<_>>();
130133
self.cmds.sort_by_key(|e| e.text);
131134
self.cmds.dedup_by_key(|e| e.text);
132135
self.cmds.sort_by_key(|e| hash(&e.text.group));

src/components/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ mod command;
66
mod commit;
77
mod diff;
88
mod help;
9+
mod msg;
910
mod reset;
1011
pub use changes::ChangesComponent;
1112
pub use command::{CommandInfo, CommandText};
1213
pub use commit::CommitComponent;
1314
pub use diff::DiffComponent;
1415
pub use help::HelpComponent;
16+
pub use msg::MsgComponent;
1517
pub use reset::ResetComponent;
1618

1719
///

src/components/msg.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use super::{
2+
visibility_blocking, CommandBlocking, CommandInfo, Component,
3+
DrawableComponent, EventUpdate,
4+
};
5+
use crate::{keys, strings, ui};
6+
use crossterm::event::Event;
7+
use std::borrow::Cow;
8+
use strings::commands;
9+
use tui::{
10+
backend::Backend,
11+
layout::{Alignment, Rect},
12+
widgets::{Block, Borders, Paragraph, Text, Widget},
13+
Frame,
14+
};
15+
16+
#[derive(Default)]
17+
pub struct MsgComponent {
18+
msg: String,
19+
visible: bool,
20+
}
21+
22+
impl DrawableComponent for MsgComponent {
23+
fn draw<B: Backend>(&self, f: &mut Frame<B>, _rect: Rect) {
24+
if self.visible {
25+
let txt = vec![Text::Raw(Cow::from(self.msg.as_str()))];
26+
27+
ui::Clear::new(
28+
Paragraph::new(txt.iter())
29+
.block(
30+
Block::default()
31+
.title(strings::MSG_TITLE)
32+
.borders(Borders::ALL),
33+
)
34+
.wrap(true)
35+
.alignment(Alignment::Left),
36+
)
37+
.render(f, ui::centered_rect_absolute(65, 25, f.size()));
38+
}
39+
}
40+
}
41+
42+
impl Component for MsgComponent {
43+
fn commands(
44+
&self,
45+
out: &mut Vec<CommandInfo>,
46+
_force_all: bool,
47+
) -> CommandBlocking {
48+
out.push(CommandInfo::new(
49+
commands::CLOSE_MSG,
50+
true,
51+
self.visible,
52+
));
53+
54+
visibility_blocking(self)
55+
}
56+
57+
fn event(&mut self, ev: Event) -> Option<EventUpdate> {
58+
if self.visible {
59+
if let Event::Key(e) = ev {
60+
if let keys::CLOSE_MSG = e {
61+
self.hide();
62+
}
63+
}
64+
65+
Some(EventUpdate::Commands)
66+
} else {
67+
None
68+
}
69+
}
70+
71+
fn is_visible(&self) -> bool {
72+
self.visible
73+
}
74+
75+
fn hide(&mut self) {
76+
self.visible = false
77+
}
78+
79+
fn show(&mut self) {
80+
self.visible = true
81+
}
82+
}
83+
84+
impl MsgComponent {
85+
///
86+
pub fn show_msg(&mut self, msg: &str) {
87+
self.msg = msg.to_string();
88+
self.show();
89+
}
90+
}

src/keys.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub const STATUS_STAGE_FILE: KeyEvent = no_mod(KeyCode::Enter);
1616
pub const EXIT_1: KeyEvent = no_mod(KeyCode::Esc);
1717
pub const EXIT_POPUP: KeyEvent = no_mod(KeyCode::Esc);
1818
pub const EXIT_2: KeyEvent = no_mod(KeyCode::Char('q'));
19+
pub const CLOSE_MSG: KeyEvent = no_mod(KeyCode::Enter);
1920
pub const OPEN_COMMIT: KeyEvent = no_mod(KeyCode::Char('c'));
2021
pub const OPEN_HELP: KeyEvent = no_mod(KeyCode::Char('h'));
2122
pub const MOVE_UP: KeyEvent = no_mod(KeyCode::Up);

src/queue.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub enum InternalEvent {
88
ResetFile(String),
99
///
1010
AddHunk(u64),
11+
///
12+
ShowMsg(String),
1113
}
1214

1315
///

src/strings.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub static TAB_DIVIDER: &str = " | ";
77

88
pub static CMD_SPLITTER: &str = " ";
99

10+
pub static MSG_TITLE: &str = "Info";
1011
pub static COMMIT_TITLE: &str = "Commit";
1112
pub static COMMIT_MSG: &str = "type commit message..";
1213
pub static RESET_TITLE: &str = "Reset";
@@ -53,6 +54,13 @@ pub mod commands {
5354
CMD_GROUP_GENERAL,
5455
);
5556
///
57+
pub static CLOSE_MSG: CommandText = CommandText::new(
58+
"Close [enter]",
59+
"close msg popup (e.g msg)",
60+
CMD_GROUP_GENERAL,
61+
)
62+
.hide_help();
63+
///
5664
pub static COMMIT_OPEN: CommandText = CommandText::new(
5765
"Commit [c]",
5866
"open commit view (available in non-empty stage)",

0 commit comments

Comments
 (0)