Skip to content

Commit 7350484

Browse files
author
Stephan Dilly
committed
don't close branchlist every time (#550)
* do not close branchlist after branch rename * do not close branchlist after deleting a branch * closes #543
1 parent e2e949d commit 7350484

File tree

7 files changed

+57
-57
lines changed

7 files changed

+57
-57
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
![charcount](assets/char_count.gif)
1919

2020
### Fixed
21+
- don't close branchlist every time ([#550](https://github.com/extrawurst/gitui/issues/550))
2122
- fixed key binding for *external exitor* in vim key bindings [[@yanganto](https://github.com/yanganto)] ([#549](https://github.com/extrawurst/gitui/issues/549))
2223
- fix some potential errors when deleting files while they are being diffed ([#490](https://github.com/extrawurst/gitui/issues/490))
2324
- push defaults to 'origin' remote if it exists ([#494](https://github.com/extrawurst/gitui/issues/494))

Diff for: asyncgit/src/sync/branch.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) fn get_branch_name(repo_path: &str) -> Result<String> {
3333
}
3434

3535
///
36-
pub struct BranchForDisplay {
36+
pub struct BranchInfo {
3737
///
3838
pub name: String,
3939
///
@@ -48,12 +48,9 @@ pub struct BranchForDisplay {
4848
pub has_upstream: bool,
4949
}
5050

51-
/// Used to return only the nessessary information for displaying a branch
52-
/// rather than an iterator over the actual branches
53-
pub fn get_branches_to_display(
54-
repo_path: &str,
55-
) -> Result<Vec<BranchForDisplay>> {
56-
scope_time!("get_branches_to_display");
51+
/// returns a list of `BranchInfo` with a simple summary of info about a single branch
52+
pub fn get_branches_info(repo_path: &str) -> Result<Vec<BranchInfo>> {
53+
scope_time!("get_branches_info");
5754

5855
let cur_repo = utils::repo(repo_path)?;
5956
let branches_for_display = cur_repo
@@ -62,7 +59,7 @@ pub fn get_branches_to_display(
6259
let branch = b?.0;
6360
let top_commit = branch.get().peel_to_commit()?;
6461

65-
Ok(BranchForDisplay {
62+
Ok(BranchInfo {
6663
name: bytes2string(branch.name_bytes()?)?,
6764
reference: bytes2string(branch.get().name_bytes())?,
6865
top_commit_message: bytes2string(
@@ -299,7 +296,7 @@ mod tests_branches {
299296
let repo_path = root.as_os_str().to_str().unwrap();
300297

301298
assert_eq!(
302-
get_branches_to_display(repo_path)
299+
get_branches_info(repo_path)
303300
.unwrap()
304301
.iter()
305302
.map(|b| b.name.clone())
@@ -317,7 +314,7 @@ mod tests_branches {
317314
create_branch(repo_path, "test").unwrap();
318315

319316
assert_eq!(
320-
get_branches_to_display(repo_path)
317+
get_branches_info(repo_path)
321318
.unwrap()
322319
.iter()
323320
.map(|b| b.name.clone())

Diff for: asyncgit/src/sync/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ pub mod utils;
2323

2424
pub use branch::{
2525
branch_compare_upstream, checkout_branch, create_branch,
26-
delete_branch, get_branches_to_display, rename_branch,
27-
BranchCompare, BranchForDisplay,
26+
delete_branch, get_branches_info, rename_branch, BranchCompare,
27+
BranchInfo,
2828
};
2929
pub use commit::{amend, commit, tag};
3030
pub use commit_details::{

Diff for: src/app.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use crate::{
22
accessors, any_popup_visible,
33
cmdbar::CommandBar,
44
components::{
5-
event_pump, CommandBlocking, CommandInfo, CommitComponent,
6-
Component, CreateBranchComponent, DrawableComponent,
5+
event_pump, BranchListComponent, CommandBlocking,
6+
CommandInfo, CommitComponent, Component,
7+
CreateBranchComponent, DrawableComponent,
78
ExternalEditorComponent, HelpComponent,
89
InspectCommitComponent, MsgComponent, PushComponent,
9-
RenameBranchComponent, ResetComponent, SelectBranchComponent,
10-
StashMsgComponent, TagCommitComponent,
10+
RenameBranchComponent, ResetComponent, StashMsgComponent,
11+
TagCommitComponent,
1112
},
1213
draw_popups,
1314
input::{Input, InputEvent, InputState},
@@ -49,7 +50,7 @@ pub struct App {
4950
tag_commit_popup: TagCommitComponent,
5051
create_branch_popup: CreateBranchComponent,
5152
rename_branch_popup: RenameBranchComponent,
52-
select_branch_popup: SelectBranchComponent,
53+
select_branch_popup: BranchListComponent,
5354
cmdbar: RefCell<CommandBar>,
5455
tab: usize,
5556
revlog: Revlog,
@@ -127,7 +128,7 @@ impl App {
127128
theme.clone(),
128129
key_config.clone(),
129130
),
130-
select_branch_popup: SelectBranchComponent::new(
131+
select_branch_popup: BranchListComponent::new(
131132
queue.clone(),
132133
theme.clone(),
133134
key_config.clone(),
@@ -510,7 +511,7 @@ impl App {
510511
)
511512
} else {
512513
flags.insert(NeedsUpdate::ALL);
513-
self.select_branch_popup.hide();
514+
self.select_branch_popup.update_branches()?;
514515
}
515516
}
516517
Action::ForcePush(branch, force) => self

Diff for: src/components/select_branch.rs renamed to src/components/branchlist.rs

+36-36
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ use crate::{
66
components::ScrollType,
77
keys::SharedKeyConfig,
88
queue::{Action, InternalEvent, NeedsUpdate, Queue},
9-
strings,
9+
strings, try_or_popup,
1010
ui::{self, calc_scroll_top},
1111
};
1212
use asyncgit::{
13-
sync::{
14-
checkout_branch, get_branches_to_display, BranchForDisplay,
15-
},
13+
sync::{checkout_branch, get_branches_info, BranchInfo},
1614
CWD,
1715
};
1816
use crossterm::event::Event;
@@ -33,8 +31,8 @@ use anyhow::Result;
3331
use ui::style::SharedTheme;
3432

3533
///
36-
pub struct SelectBranchComponent {
37-
branch_names: Vec<BranchForDisplay>,
34+
pub struct BranchListComponent {
35+
branch_names: Vec<BranchInfo>,
3836
visible: bool,
3937
selection: u16,
4038
scroll_top: Cell<usize>,
@@ -44,7 +42,7 @@ pub struct SelectBranchComponent {
4442
key_config: SharedKeyConfig,
4543
}
4644

47-
impl DrawableComponent for SelectBranchComponent {
45+
impl DrawableComponent for BranchListComponent {
4846
fn draw<B: Backend>(
4947
&self,
5048
f: &mut Frame<B>,
@@ -104,7 +102,7 @@ impl DrawableComponent for SelectBranchComponent {
104102
}
105103
}
106104

107-
impl Component for SelectBranchComponent {
105+
impl Component for BranchListComponent {
108106
fn commands(
109107
&self,
110108
out: &mut Vec<CommandInfo>,
@@ -166,15 +164,12 @@ impl Component for SelectBranchComponent {
166164
} else if e == self.key_config.page_up {
167165
return self.move_selection(ScrollType::PageUp);
168166
} else if e == self.key_config.enter {
169-
if let Err(e) = self.switch_to_selected_branch() {
170-
log::error!("switch branch error: {}", e);
171-
self.queue.borrow_mut().push_back(
172-
InternalEvent::ShowErrorMsg(format!(
173-
"switch branch error:\n{}",
174-
e
175-
)),
176-
);
177-
}
167+
try_or_popup!(
168+
self,
169+
"switch branch error:",
170+
self.switch_to_selected_branch()
171+
);
172+
178173
self.hide()
179174
} else if e == self.key_config.create_branch {
180175
self.queue
@@ -190,7 +185,8 @@ impl Component for SelectBranchComponent {
190185
cur_branch.name.clone(),
191186
),
192187
);
193-
self.hide();
188+
189+
self.update_branches()?;
194190
} else if e == self.key_config.delete_branch
195191
&& !self.selection_is_cur_branch()
196192
{
@@ -228,7 +224,7 @@ impl Component for SelectBranchComponent {
228224
}
229225
}
230226

231-
impl SelectBranchComponent {
227+
impl BranchListComponent {
232228
pub fn new(
233229
queue: Queue,
234230
theme: SharedTheme,
@@ -245,10 +241,6 @@ impl SelectBranchComponent {
245241
current_height: Cell::new(0),
246242
}
247243
}
248-
/// Get all the names of the branches in the repo
249-
pub fn get_branch_names() -> Result<Vec<BranchForDisplay>> {
250-
Ok(get_branches_to_display(CWD)?)
251-
}
252244

253245
///
254246
pub fn open(&mut self) -> Result<()> {
@@ -258,14 +250,14 @@ impl SelectBranchComponent {
258250
Ok(())
259251
}
260252

261-
////
253+
/// fetch list of branches
262254
pub fn update_branches(&mut self) -> Result<()> {
263-
self.branch_names = Self::get_branch_names()?;
255+
self.branch_names = get_branches_info(CWD)?;
256+
self.set_selection(self.selection)?;
264257
Ok(())
265258
}
266259

267-
///
268-
pub fn selection_is_cur_branch(&self) -> bool {
260+
fn selection_is_cur_branch(&self) -> bool {
269261
self.branch_names
270262
.iter()
271263
.enumerate()
@@ -278,10 +270,7 @@ impl SelectBranchComponent {
278270

279271
///
280272
fn move_selection(&mut self, scroll: ScrollType) -> Result<bool> {
281-
let num_branches: u16 = self.branch_names.len().try_into()?;
282-
let num_branches = num_branches.saturating_sub(1);
283-
284-
let mut new_selection = match scroll {
273+
let new_selection = match scroll {
285274
ScrollType::Up => self.selection.saturating_add(1),
286275
ScrollType::Down => self.selection.saturating_sub(1),
287276
ScrollType::PageDown => self
@@ -293,15 +282,26 @@ impl SelectBranchComponent {
293282
_ => self.selection,
294283
};
295284

296-
if new_selection > num_branches {
297-
new_selection = num_branches;
298-
}
299-
300-
self.selection = new_selection;
285+
self.set_selection(new_selection)?;
301286

302287
Ok(true)
303288
}
304289

290+
fn set_selection(&mut self, selection: u16) -> Result<()> {
291+
let num_branches: u16 = self.branch_names.len().try_into()?;
292+
let num_branches = num_branches.saturating_sub(1);
293+
294+
let selection = if selection > num_branches {
295+
num_branches
296+
} else {
297+
selection
298+
};
299+
300+
self.selection = selection;
301+
302+
Ok(())
303+
}
304+
305305
/// Get branches to display
306306
fn get_text(
307307
&self,

Diff for: src/components/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod branchlist;
12
mod changes;
23
mod command;
34
mod commit;
@@ -14,12 +15,12 @@ mod msg;
1415
mod push;
1516
mod rename_branch;
1617
mod reset;
17-
mod select_branch;
1818
mod stashmsg;
1919
mod tag_commit;
2020
mod textinput;
2121
mod utils;
2222

23+
pub use branchlist::BranchListComponent;
2324
pub use changes::ChangesComponent;
2425
pub use command::{CommandInfo, CommandText};
2526
pub use commit::CommitComponent;
@@ -35,7 +36,6 @@ pub use msg::MsgComponent;
3536
pub use push::PushComponent;
3637
pub use rename_branch::RenameBranchComponent;
3738
pub use reset::ResetComponent;
38-
pub use select_branch::SelectBranchComponent;
3939
pub use stashmsg::StashMsgComponent;
4040
pub use tag_commit::TagCommitComponent;
4141
pub use textinput::{InputType, TextInputComponent};

Diff for: src/components/utils/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod statustree;
1010
macro_rules! try_or_popup {
1111
($self:ident, $msg:literal, $e:expr) => {
1212
if let Err(err) = $e {
13+
::log::error!("{} {}", $msg, err);
1314
$self.queue.borrow_mut().push_back(
1415
InternalEvent::ShowErrorMsg(format!(
1516
"{}\n{}",

0 commit comments

Comments
 (0)