Skip to content

Commit 1f5706f

Browse files
author
Stephan Dilly
committed
support stash msg
1 parent 21cbc3b commit 1f5706f

File tree

10 files changed

+171
-57
lines changed

10 files changed

+171
-57
lines changed

src/app.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
components::{
44
event_pump, CommandBlocking, CommandInfo, CommitComponent,
55
Component, DrawableComponent, HelpComponent, MsgComponent,
6-
ResetComponent,
6+
ResetComponent, StashMsgComponent,
77
},
88
keys,
99
queue::{InternalEvent, NeedsUpdate, Queue},
@@ -34,6 +34,7 @@ pub struct App {
3434
msg: MsgComponent,
3535
reset: ResetComponent,
3636
commit: CommitComponent,
37+
stashmsg_popup: StashMsgComponent,
3738
current_commands: Vec<CommandInfo>,
3839
tab: usize,
3940
revlog: Revlog,
@@ -54,6 +55,10 @@ impl App {
5455
Self {
5556
reset: ResetComponent::new(queue.clone(), &theme),
5657
commit: CommitComponent::new(queue.clone(), &theme),
58+
stashmsg_popup: StashMsgComponent::new(
59+
queue.clone(),
60+
&theme,
61+
),
5762
do_quit: false,
5863
current_commands: Vec::new(),
5964
help: HelpComponent::new(&theme),
@@ -181,7 +186,16 @@ impl App {
181186
impl App {
182187
accessors!(
183188
self,
184-
[msg, reset, commit, help, revlog, status_tab, stashing_tab]
189+
[
190+
msg,
191+
reset,
192+
commit,
193+
stashmsg_popup,
194+
help,
195+
revlog,
196+
status_tab,
197+
stashing_tab
198+
]
185199
);
186200

187201
fn check_quit(&mut self, ev: Event) -> bool {
@@ -292,6 +306,9 @@ impl App {
292306
}
293307
InternalEvent::Update(u) => flags.insert(u),
294308
InternalEvent::OpenCommit => self.commit.show(),
309+
InternalEvent::PopupStashing(_opts) => {
310+
self.stashmsg_popup.show()
311+
}
295312
};
296313

297314
flags
@@ -341,6 +358,7 @@ impl App {
341358
let size = f.size();
342359

343360
self.commit.draw(f, size);
361+
self.stashmsg_popup.draw(f, size);
344362
self.reset.draw(f, size);
345363
self.help.draw(f, size);
346364
self.msg.draw(f, size);

src/components/commit.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@ impl Component for CommitComponent {
2929
fn commands(
3030
&self,
3131
out: &mut Vec<CommandInfo>,
32-
_force_all: bool,
32+
force_all: bool,
3333
) -> CommandBlocking {
34+
self.input.commands(out, force_all);
35+
3436
out.push(CommandInfo::new(
3537
commands::COMMIT_ENTER,
3638
self.can_commit(),
3739
self.is_visible(),
3840
));
39-
out.push(CommandInfo::new(
40-
commands::CLOSE_POPUP,
41-
true,
42-
self.is_visible(),
43-
));
4441
visibility_blocking(self)
4542
}
4643

src/components/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod filetree;
66
mod help;
77
mod msg;
88
mod reset;
9+
mod stashmsg;
910
mod textinput;
1011
mod utils;
1112
pub use changes::ChangesComponent;
@@ -16,6 +17,7 @@ pub use filetree::FileTreeComponent;
1617
pub use help::HelpComponent;
1718
pub use msg::MsgComponent;
1819
pub use reset::ResetComponent;
20+
pub use stashmsg::StashMsgComponent;
1921
pub use utils::filetree::FileTreeItemKind;
2022

2123
use crossterm::event::Event;

src/components/stashmsg.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use super::{
2+
textinput::TextInputComponent, visibility_blocking,
3+
CommandBlocking, CommandInfo, Component, DrawableComponent,
4+
};
5+
use crate::{
6+
queue::{InternalEvent, NeedsUpdate, Queue},
7+
strings,
8+
tabs::StashingOptions,
9+
ui::style::Theme,
10+
};
11+
use asyncgit::{sync, CWD};
12+
use crossterm::event::{Event, KeyCode};
13+
use strings::commands;
14+
use tui::{backend::Backend, layout::Rect, Frame};
15+
16+
pub struct StashMsgComponent {
17+
options: StashingOptions,
18+
input: TextInputComponent,
19+
queue: Queue,
20+
}
21+
22+
impl DrawableComponent for StashMsgComponent {
23+
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, rect: Rect) {
24+
self.input.draw(f, rect)
25+
}
26+
}
27+
28+
impl Component for StashMsgComponent {
29+
fn commands(
30+
&self,
31+
out: &mut Vec<CommandInfo>,
32+
force_all: bool,
33+
) -> CommandBlocking {
34+
self.input.commands(out, force_all);
35+
36+
out.push(CommandInfo::new(
37+
commands::STASHING_CONFIRM_MSG,
38+
true,
39+
self.is_visible() || force_all,
40+
));
41+
visibility_blocking(self)
42+
}
43+
44+
fn event(&mut self, ev: Event) -> bool {
45+
if self.is_visible() {
46+
if self.input.event(ev) {
47+
return true;
48+
}
49+
50+
if let Event::Key(e) = ev {
51+
if let KeyCode::Enter = e.code {
52+
if sync::stash_save(
53+
CWD,
54+
if self.input.get_text().is_empty() {
55+
None
56+
} else {
57+
Some(self.input.get_text().as_str())
58+
},
59+
self.options.stash_untracked,
60+
!self.options.stash_indexed,
61+
)
62+
.is_ok()
63+
{
64+
self.queue.borrow_mut().push_back(
65+
InternalEvent::Update(NeedsUpdate::ALL),
66+
);
67+
}
68+
}
69+
70+
// stop key event propagation
71+
return true;
72+
}
73+
}
74+
false
75+
}
76+
77+
fn is_visible(&self) -> bool {
78+
self.input.is_visible()
79+
}
80+
81+
fn hide(&mut self) {
82+
self.input.hide()
83+
}
84+
85+
fn show(&mut self) {
86+
self.input.show()
87+
}
88+
}
89+
90+
impl StashMsgComponent {
91+
///
92+
pub fn new(queue: Queue, theme: &Theme) -> Self {
93+
Self {
94+
options: StashingOptions::default(),
95+
queue,
96+
input: TextInputComponent::new(theme),
97+
}
98+
}
99+
}

src/components/textinput.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ impl Component for TextInputComponent {
7575
out: &mut Vec<CommandInfo>,
7676
_force_all: bool,
7777
) -> CommandBlocking {
78-
out.push(CommandInfo::new(
79-
commands::CLOSE_POPUP,
80-
true,
81-
self.visible,
82-
));
78+
out.push(
79+
CommandInfo::new(
80+
commands::CLOSE_POPUP,
81+
true,
82+
self.visible,
83+
)
84+
.order(1),
85+
);
8386
visibility_blocking(self)
8487
}
8588

src/queue.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bitflags::bitflags;
22
use std::{cell::RefCell, collections::VecDeque, rc::Rc};
3+
use crate::tabs::StashingOptions;
34

45
bitflags! {
56
/// flags defining what part of the app need to update
@@ -35,6 +36,8 @@ pub enum InternalEvent {
3536
Update(NeedsUpdate),
3637
///
3738
OpenCommit,
39+
///
40+
PopupStashing(StashingOptions),
3841
}
3942

4043
///

src/strings.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub mod commands {
161161
///
162162
pub static STASHING_SAVE: CommandText = CommandText::new(
163163
"Save [s]",
164-
"creates a new stash",
164+
"opens stash name input popup",
165165
CMD_GROUP_STASHING,
166166
);
167167
///
@@ -178,4 +178,10 @@ pub mod commands {
178178
"toggle including untracked files into stash",
179179
CMD_GROUP_STASHING,
180180
);
181+
///
182+
pub static STASHING_CONFIRM_MSG: CommandText = CommandText::new(
183+
"Stash [enter]",
184+
"save files to stash",
185+
CMD_GROUP_STASHING,
186+
);
181187
}

src/tabs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ mod stashing;
33
mod status;
44

55
pub use revlog::Revlog;
6-
pub use stashing::Stashing;
6+
pub use stashing::{Stashing, StashingOptions};
77
pub use status::Status;

src/tabs/stashing.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use crate::{
22
accessors,
33
components::{
4-
command_pump, event_pump, CommandBlocking, CommandInfo,
5-
Component, DrawableComponent, FileTreeComponent,
4+
command_pump, event_pump, visibility_blocking,
5+
CommandBlocking, CommandInfo, Component, DrawableComponent,
6+
FileTreeComponent,
67
},
78
keys,
8-
queue::{InternalEvent, NeedsUpdate, Queue},
9+
queue::{InternalEvent, Queue},
910
strings,
1011
ui::style::Theme,
1112
};
1213
use asyncgit::{
13-
sync::{self, status::StatusType},
14-
AsyncNotification, AsyncStatus, StatusParams, CWD,
14+
sync::status::StatusType, AsyncNotification, AsyncStatus,
15+
StatusParams,
1516
};
1617
use crossbeam_channel::Sender;
1718
use crossterm::event::Event;
@@ -22,15 +23,16 @@ use tui::{
2223
widgets::{Block, Borders, Paragraph, Text},
2324
};
2425

25-
struct Options {
26-
stash_untracked: bool,
27-
stash_indexed: bool,
26+
#[derive(Default, Clone, Copy)]
27+
pub struct StashingOptions {
28+
pub stash_untracked: bool,
29+
pub stash_indexed: bool,
2830
}
2931

3032
pub struct Stashing {
31-
visible: bool,
32-
options: Options,
3333
index: FileTreeComponent,
34+
visible: bool,
35+
options: StashingOptions,
3436
theme: Theme,
3537
git_status: AsyncStatus,
3638
queue: Queue,
@@ -46,17 +48,17 @@ impl Stashing {
4648
theme: &Theme,
4749
) -> Self {
4850
Self {
49-
visible: false,
50-
options: Options {
51-
stash_indexed: true,
52-
stash_untracked: true,
53-
},
5451
index: FileTreeComponent::new(
5552
strings::STASHING_FILES_TITLE,
5653
true,
5754
queue.clone(),
5855
theme,
5956
),
57+
visible: false,
58+
options: StashingOptions {
59+
stash_indexed: true,
60+
stash_untracked: true,
61+
},
6062
theme: *theme,
6163
git_status: AsyncStatus::new(sender.clone()),
6264
queue: queue.clone(),
@@ -144,8 +146,6 @@ impl DrawableComponent for Stashing {
144146
)
145147
.split(chunks[1]);
146148

147-
self.index.draw(f, chunks[0]);
148-
149149
f.render_widget(
150150
Paragraph::new(self.get_option_text().iter())
151151
.block(
@@ -156,6 +156,8 @@ impl DrawableComponent for Stashing {
156156
.alignment(Alignment::Left),
157157
right_chunks[0],
158158
);
159+
160+
self.index.draw(f, chunks[0]);
159161
}
160162
}
161163

@@ -183,11 +185,7 @@ impl Component for Stashing {
183185
self.visible || force_all,
184186
));
185187

186-
if self.visible {
187-
CommandBlocking::Blocking
188-
} else {
189-
CommandBlocking::PassingOn
190-
}
188+
visibility_blocking(self)
191189
}
192190

193191
fn event(&mut self, ev: crossterm::event::Event) -> bool {
@@ -199,20 +197,12 @@ impl Component for Stashing {
199197
if let Event::Key(k) = ev {
200198
return match k {
201199
keys::STASHING_SAVE if !self.index.is_empty() => {
202-
if sync::stash_save(
203-
CWD,
204-
None,
205-
self.options.stash_untracked,
206-
!self.options.stash_indexed,
207-
)
208-
.is_ok()
209-
{
210-
self.queue.borrow_mut().push_back(
211-
InternalEvent::Update(
212-
NeedsUpdate::ALL,
213-
),
214-
);
215-
}
200+
self.queue.borrow_mut().push_back(
201+
InternalEvent::PopupStashing(
202+
self.options,
203+
),
204+
);
205+
216206
true
217207
}
218208
keys::STASHING_TOGGLE_INDEX => {

0 commit comments

Comments
 (0)