Skip to content

Commit

Permalink
feat: 'Branches' header in 'y' menu
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed Feb 26, 2024
1 parent 58acadf commit a71bb6f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 48 deletions.
62 changes: 37 additions & 25 deletions src/screen/show_refs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter;

use super::Screen;
use crate::{
git,
Expand All @@ -17,34 +19,44 @@ pub(crate) fn create(config: &Config, size: Rect) -> Res<Screen> {
size,
Box::new(move || {
// TODO Replace with libgit2
Ok(git::show_refs(&path_buf)?
.into_iter()
.map(|(local, remote, subject)| {
let columns = [
Some(Span::styled(
local.clone(),
Style::new().fg(CURRENT_THEME.branch).bold(),
)),
(!remote.is_empty()).then_some(Span::styled(
remote,
Style::new().fg(CURRENT_THEME.remote).bold(),
)),
Some(Span::raw(subject)),
]
Ok(iter::once(Item {
id: "branches".into(),
display: Text::from("Branches".to_string().fg(CURRENT_THEME.section).bold()),
section: true,
depth: 0,
..Default::default()
})
.chain(
git::show_refs(&path_buf)?
.into_iter()
.flatten();
.map(|(local, remote, subject)| {
let columns = [
Some(Span::styled(
local.clone(),
Style::new().fg(CURRENT_THEME.branch).bold(),
)),
(!remote.is_empty()).then_some(Span::styled(
remote,
Style::new().fg(CURRENT_THEME.remote).bold(),
)),
Some(Span::raw(subject)),
]
.into_iter()
.flatten();

let spans = itertools::intersperse(columns, Span::raw(" ")).collect::<Vec<_>>();
let spans =
itertools::intersperse(columns, Span::raw(" ")).collect::<Vec<_>>();

Item {
id: local.clone().into(),
display: Text::from(vec![Line::from(spans)]),
depth: 0,
target_data: Some(TargetData::Branch(local.to_string())),
..Default::default()
}
})
.collect())
Item {
id: local.clone().into(),
display: Text::from(vec![Line::from(spans)]),
depth: 1,
target_data: Some(TargetData::Branch(local.to_string())),
..Default::default()
}
}),
)
.collect())
}),
)
}
4 changes: 2 additions & 2 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ fn pull() {
#[test]
fn discard_branch_confirm() {
let mut ctx = TestContext::setup_clone(60, 10);
ctx.update(&[key('y'), key('K')]);
ctx.update(&[key('y'), key('j'), key('K')]);
insta::assert_snapshot!(ctx.redact_buffer());
}

#[test]
fn discard_branch() {
let mut ctx = TestContext::setup_clone(60, 10);
ctx.update(&[key('y'), key('K'), key('y')]);
ctx.update(&[key('y'), key('j'), key('K'), key('y')]);
insta::assert_snapshot!(ctx.redact_buffer());
}

Expand Down
17 changes: 10 additions & 7 deletions tests/snapshots/r#mod__discard_branch.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ expression: ctx.redact_buffer()
Buffer {
area: Rect { x: 0, y: 0, width: 60, height: 10 },
content: [
" Branches ",
"🢒main origin/main add initial-file ",
" ",
" ",
" ",
" ",
" ",
" ",
"────────────────────────────────────────────────────────────",
"$ git branch -d main ",
"error: cannot delete branch 'main' used by worktree at '/tmp",
],
styles: [
x: 0, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 1, y: 0, fg: Rgb(169, 182, 101), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 5, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 6, y: 0, fg: Rgb(234, 105, 98), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 17, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 1, y: 0, fg: Rgb(216, 166, 87), bg: Reset, underline: Reset, modifier: BOLD,
x: 9, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 1, y: 1, fg: Rgb(169, 182, 101), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 5, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 6, y: 1, fg: Rgb(234, 105, 98), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 17, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 7, fg: Rgb(80, 73, 69), bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 8, fg: Rgb(125, 174, 163), bg: Reset, underline: Reset, modifier: NONE,
x: 20, y: 8, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
Expand Down
17 changes: 10 additions & 7 deletions tests/snapshots/r#mod__discard_branch_confirm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ expression: ctx.redact_buffer()
Buffer {
area: Rect { x: 0, y: 0, width: 60, height: 10 },
content: [
" Branches ",
"🢒main origin/main add initial-file ",
" ",
" ",
" ",
" ",
" ",
" ",
"────────────────────────────────────────────────────────────",
"Discard main origin/main ad",
"n Quit y Discard ",
],
styles: [
x: 0, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 1, y: 0, fg: Rgb(169, 182, 101), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 5, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 6, y: 0, fg: Rgb(234, 105, 98), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 17, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 1, y: 0, fg: Rgb(216, 166, 87), bg: Reset, underline: Reset, modifier: BOLD,
x: 9, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 1, y: 1, fg: Rgb(169, 182, 101), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 5, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 6, y: 1, fg: Rgb(234, 105, 98), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 17, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 7, fg: Rgb(80, 73, 69), bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 8, fg: Rgb(125, 174, 163), bg: Reset, underline: Reset, modifier: BOLD,
x: 19, y: 8, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
Expand Down
17 changes: 10 additions & 7 deletions tests/snapshots/r#mod__show_refs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ expression: ctx.redact_buffer()
Buffer {
area: Rect { x: 0, y: 0, width: 60, height: 10 },
content: [
"🢒main origin/main add initial-file ",
" ",
"🢒Branches ",
" main origin/main add initial-file ",
" ",
" ",
" ",
Expand All @@ -18,10 +18,13 @@ Buffer {
],
styles: [
x: 0, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 1, y: 0, fg: Rgb(169, 182, 101), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 5, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 6, y: 0, fg: Rgb(234, 105, 98), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 17, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 1, y: 0, fg: Rgb(216, 166, 87), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 9, y: 0, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Rgb(42, 40, 39), underline: Reset, modifier: NONE,
x: 1, y: 1, fg: Rgb(169, 182, 101), bg: Rgb(42, 40, 39), underline: Reset, modifier: BOLD,
x: 5, y: 1, fg: Reset, bg: Rgb(42, 40, 39), underline: Reset, modifier: NONE,
x: 6, y: 1, fg: Rgb(234, 105, 98), bg: Rgb(42, 40, 39), underline: Reset, modifier: BOLD,
x: 17, y: 1, fg: Reset, bg: Rgb(42, 40, 39), underline: Reset, modifier: NONE,
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
]
}

0 comments on commit a71bb6f

Please sign in to comment.