Skip to content

Commit 6274777

Browse files
author
Stephan Dilly
authored
simplify toggling remote/local via tabs (and tab key) (#635)
* simplify toggling remote/local via tabs (and tab key)
1 parent b546900 commit 6274777

File tree

5 files changed

+100
-57
lines changed

5 files changed

+100
-57
lines changed

assets/checkout-remote.gif

-39.7 KB
Loading

assets/vim_style_key_config.ron

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
rename_branch: ( code: Char('r'), modifiers: ( bits: 0,),),
7272
select_branch: ( code: Char('b'), modifiers: ( bits: 0,),),
7373
delete_branch: ( code: Char('D'), modifiers: ( bits: 1,),),
74-
toggle_remote_branches: ( code: Char('t'), modifiers: ( bits: 0,),),
7574
push: ( code: Char('p'), modifiers: ( bits: 0,),),
7675
force_push: ( code: Char('P'), modifiers: ( bits: 1,),),
7776
pull: ( code: Char('f'), modifiers: ( bits: 0,),),

src/components/branchlist.rs

Lines changed: 96 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ use crossterm::event::Event;
2121
use std::{cell::Cell, convert::TryInto};
2222
use tui::{
2323
backend::Backend,
24-
layout::{Alignment, Rect},
24+
layout::{
25+
Alignment, Constraint, Direction, Layout, Margin, Rect,
26+
},
2527
text::{Span, Spans, Text},
26-
widgets::{Block, BorderType, Borders, Clear, Paragraph},
28+
widgets::{Block, BorderType, Borders, Clear, Paragraph, Tabs},
2729
Frame,
2830
};
2931
use ui::style::SharedTheme;
@@ -49,7 +51,7 @@ impl DrawableComponent for BranchListComponent {
4951
rect: Rect,
5052
) -> Result<()> {
5153
if self.visible {
52-
const PERCENT_SIZE: Size = Size::new(80, 25);
54+
const PERCENT_SIZE: Size = Size::new(80, 50);
5355
const MIN_SIZE: Size = Size::new(60, 20);
5456

5557
let area = ui::centered_rect(
@@ -61,41 +63,32 @@ impl DrawableComponent for BranchListComponent {
6163
ui::rect_inside(MIN_SIZE, f.size().into(), area);
6264
let area = area.intersection(rect);
6365

64-
let height_in_lines =
65-
(area.height as usize).saturating_sub(2);
66-
67-
self.scroll_top.set(calc_scroll_top(
68-
self.scroll_top.get(),
69-
height_in_lines,
70-
self.selection as usize,
71-
));
72-
7366
f.render_widget(Clear, area);
67+
7468
f.render_widget(
75-
Paragraph::new(self.get_text(
76-
&self.theme,
77-
area.width,
78-
height_in_lines,
79-
))
80-
.block(
81-
Block::default()
82-
.title(strings::title_branches(self.local))
83-
.border_type(BorderType::Thick)
84-
.borders(Borders::ALL),
85-
)
86-
.alignment(Alignment::Left),
69+
Block::default()
70+
.title(strings::title_branches())
71+
.border_type(BorderType::Thick)
72+
.borders(Borders::ALL),
8773
area,
8874
);
8975

90-
ui::draw_scrollbar(
91-
f,
92-
area,
93-
&self.theme,
94-
self.branches.len(),
95-
self.scroll_top.get(),
96-
);
76+
let area = area.inner(&Margin {
77+
vertical: 1,
78+
horizontal: 1,
79+
});
80+
81+
let chunks = Layout::default()
82+
.direction(Direction::Vertical)
83+
.constraints(
84+
[Constraint::Length(2), Constraint::Min(1)]
85+
.as_ref(),
86+
)
87+
.split(area);
88+
89+
self.draw_tabs(f, chunks[0]);
9790

98-
self.current_height.set(height_in_lines.try_into()?);
91+
self.draw_list(f, chunks[1])?;
9992
}
10093

10194
Ok(())
@@ -123,6 +116,15 @@ impl Component for BranchListComponent {
123116
true,
124117
));
125118

119+
out.push(CommandInfo::new(
120+
strings::commands::toggle_branch_popup(
121+
&self.key_config,
122+
self.local,
123+
),
124+
true,
125+
true,
126+
));
127+
126128
out.push(CommandInfo::new(
127129
strings::commands::select_branch_popup(
128130
&self.key_config,
@@ -154,15 +156,6 @@ impl Component for BranchListComponent {
154156
true,
155157
self.local,
156158
));
157-
158-
out.push(CommandInfo::new(
159-
strings::commands::toggle_branch_popup(
160-
&self.key_config,
161-
self.local,
162-
),
163-
true,
164-
true,
165-
));
166159
}
167160
visibility_blocking(self)
168161
}
@@ -215,8 +208,7 @@ impl Component for BranchListComponent {
215208
),
216209
),
217210
);
218-
} else if e == self.key_config.toggle_remote_branches
219-
{
211+
} else if e == self.key_config.tab_toggle {
220212
self.local = !self.local;
221213
self.update_branches()?;
222214
}
@@ -462,4 +454,65 @@ impl BranchListComponent {
462454

463455
Ok(())
464456
}
457+
458+
fn draw_tabs<B: Backend>(&self, f: &mut Frame<B>, r: Rect) {
459+
let tabs = [Span::raw("Local"), Span::raw("Remote")]
460+
.iter()
461+
.cloned()
462+
.map(Spans::from)
463+
.collect();
464+
465+
f.render_widget(
466+
Tabs::new(tabs)
467+
.block(
468+
Block::default()
469+
.borders(Borders::BOTTOM)
470+
.border_style(self.theme.block(false)),
471+
)
472+
.style(self.theme.tab(false))
473+
.highlight_style(self.theme.tab(true))
474+
.divider(strings::tab_divider(&self.key_config))
475+
.select(if self.local { 0 } else { 1 }),
476+
r,
477+
);
478+
}
479+
480+
fn draw_list<B: Backend>(
481+
&self,
482+
f: &mut Frame<B>,
483+
r: Rect,
484+
) -> Result<()> {
485+
let height_in_lines = r.height as usize;
486+
487+
self.scroll_top.set(calc_scroll_top(
488+
self.scroll_top.get(),
489+
height_in_lines,
490+
self.selection as usize,
491+
));
492+
493+
f.render_widget(
494+
Paragraph::new(self.get_text(
495+
&self.theme,
496+
r.width,
497+
height_in_lines,
498+
))
499+
.alignment(Alignment::Left),
500+
r,
501+
);
502+
503+
let mut r = r;
504+
r.width += 1;
505+
506+
ui::draw_scrollbar(
507+
f,
508+
r,
509+
&self.theme,
510+
self.branches.len(),
511+
self.scroll_top.get(),
512+
);
513+
514+
self.current_height.set(height_in_lines.try_into()?);
515+
516+
Ok(())
517+
}
465518
}

src/keys.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ pub struct KeyConfig {
6767
pub rename_branch: KeyEvent,
6868
pub select_branch: KeyEvent,
6969
pub delete_branch: KeyEvent,
70-
pub toggle_remote_branches: KeyEvent,
7170
pub push: KeyEvent,
7271
pub force_push: KeyEvent,
7372
pub pull: KeyEvent,
@@ -124,7 +123,6 @@ impl Default for KeyConfig {
124123
rename_branch: KeyEvent { code: KeyCode::Char('r'), modifiers: KeyModifiers::NONE},
125124
select_branch: KeyEvent { code: KeyCode::Char('b'), modifiers: KeyModifiers::NONE},
126125
delete_branch: KeyEvent{code: KeyCode::Char('D'), modifiers: KeyModifiers::SHIFT},
127-
toggle_remote_branches: KeyEvent{code: KeyCode::Char('t'), modifiers: KeyModifiers::NONE},
128126
push: KeyEvent { code: KeyCode::Char('p'), modifiers: KeyModifiers::empty()},
129127
force_push: KeyEvent { code: KeyCode::Char('P'), modifiers: KeyModifiers::SHIFT},
130128
pull: KeyEvent { code: KeyCode::Char('f'), modifiers: KeyModifiers::empty()},

src/strings.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ pub static PUSH_TAGS_STATES_FETCHING: &str = "fetching";
1919
pub static PUSH_TAGS_STATES_PUSHING: &str = "pushing";
2020
pub static PUSH_TAGS_STATES_DONE: &str = "done";
2121

22-
pub fn title_branches(local: bool) -> String {
23-
if local {
24-
"Branches (local)"
25-
} else {
26-
"Branches (remote)"
27-
}
28-
.to_string()
22+
pub fn title_branches() -> String {
23+
"Branches".to_string()
2924
}
30-
3125
pub fn title_status(_key_config: &SharedKeyConfig) -> String {
3226
"Unstaged Changes".to_string()
3327
}
@@ -925,10 +919,9 @@ pub mod commands {
925919
) -> CommandText {
926920
CommandText::new(
927921
format!(
928-
"{} Branches [{}]",
922+
"{} [{}]",
929923
if local { "Remote" } else { "Local" },
930-
key_config
931-
.get_hint(key_config.toggle_remote_branches),
924+
key_config.get_hint(key_config.tab_toggle),
932925
),
933926
"toggle branch type (remote/local)",
934927
CMD_GROUP_GENERAL,

0 commit comments

Comments
 (0)