use std::borrow::Cow;
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Style},
widgets::{Clear, Widget},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScmAction {
Pull,
Push,
Clone,
CheckoutTo,
Fetch,
ShowGitOutput,
Commit,
CommitStaged,
CommitAll,
CommitAmend,
UndoLastCommit,
CommitAndPush,
CommitAndSync,
StageAll,
UnstageAll,
DiscardAll,
Sync,
PullRebase,
PushTo,
PushForce,
PublishBranch,
CreateBranch,
CreateBranchFrom,
RenameBranch,
DeleteBranch,
Merge,
Rebase,
AddRemote,
RemoveRemote,
Stash,
StashIncludeUntracked,
StashStaged,
ApplyStash,
PopStashLatest,
PopStashPick,
DropStash,
ViewStash,
CreateTag,
DeleteTag,
PushTags,
DeleteRemoteTag,
ContinueOperation,
SkipOperation,
AbortOperation,
BisectGood,
BisectBad,
}
#[derive(Clone, Copy, Debug)]
pub struct Leaf {
pub action: ScmAction,
pub label: &'static str,
pub icon: char,
}
const fn leaf(action: ScmAction, label: &'static str, icon: char) -> Leaf {
Leaf {
action,
label,
icon,
}
}
pub enum Node {
Item(Leaf),
Sep,
Sub {
label: &'static str,
icon: char,
items: &'static [Leaf],
},
}
const ICON_CLOUD_DOWNLOAD: char = '\u{eac2}'; const ICON_CLOUD_UPLOAD: char = '\u{eac3}'; const ICON_REPO_CLONE: char = '\u{eb3e}'; const ICON_GIT_BRANCH: char = '\u{ea96}'; const ICON_GIT_FETCH: char = '\u{ec1d}'; const ICON_OUTPUT: char = '\u{eb9d}'; const ICON_CHECK: char = '\u{eab2}'; const ICON_SYNC: char = '\u{ea77}'; const ICON_ADD: char = '\u{ea60}'; const ICON_REMOVE: char = '\u{eb3b}'; const ICON_DISCARD: char = '\u{eae2}'; const ICON_ARROW_UP: char = '\u{eaa1}'; const ICON_EDIT: char = '\u{ea73}'; const ICON_TRASH: char = '\u{ea81}'; const ICON_GIT_MERGE: char = '\u{eafe}'; const ICON_GIT_PR: char = '\u{ea64}'; const ICON_BROADCAST: char = '\u{eaad}'; const ICON_ARCHIVE: char = '\u{ea98}'; const ICON_TAG: char = '\u{ea66}'; const ICON_NEW_FOLDER: char = '\u{ea80}'; const ICON_ARROW_RIGHT: char = '\u{ea9c}';
pub const ICON_ELLIPSIS: char = '\u{ea7c}';
const COMMIT_ITEMS: &[Leaf] = &[
leaf(ScmAction::Commit, "Commit", ICON_CHECK),
leaf(ScmAction::CommitStaged, "Commit Staged", ICON_CHECK),
leaf(ScmAction::CommitAll, "Commit All", ICON_CHECK),
leaf(ScmAction::CommitAmend, "Commit (Amend)", ICON_EDIT),
leaf(ScmAction::UndoLastCommit, "Undo Last Commit", ICON_DISCARD),
leaf(ScmAction::CommitAndPush, "Commit & Push", ICON_CLOUD_UPLOAD),
leaf(ScmAction::CommitAndSync, "Commit & Sync", ICON_SYNC),
];
const CHANGES_ITEMS: &[Leaf] = &[
leaf(ScmAction::StageAll, "Stage All Changes", ICON_ADD),
leaf(ScmAction::UnstageAll, "Unstage All Changes", ICON_REMOVE),
leaf(ScmAction::DiscardAll, "Discard All Changes", ICON_DISCARD),
];
const PULL_PUSH_ITEMS: &[Leaf] = &[
leaf(ScmAction::Sync, "Sync", ICON_SYNC),
leaf(ScmAction::PullRebase, "Pull (Rebase)", ICON_CLOUD_DOWNLOAD),
leaf(ScmAction::PushTo, "Push to", ICON_ARROW_UP),
leaf(ScmAction::PushForce, "Push (Force)", ICON_ARROW_UP),
leaf(
ScmAction::PublishBranch,
"Publish Branch",
ICON_CLOUD_UPLOAD,
),
];
const BRANCH_ITEMS: &[Leaf] = &[
leaf(ScmAction::CreateBranch, "Create Branch", ICON_GIT_BRANCH),
leaf(
ScmAction::CreateBranchFrom,
"Create Branch from",
ICON_NEW_FOLDER,
),
leaf(ScmAction::RenameBranch, "Rename Branch", ICON_EDIT),
leaf(ScmAction::DeleteBranch, "Delete Branch", ICON_TRASH),
leaf(ScmAction::Merge, "Merge", ICON_GIT_MERGE),
leaf(ScmAction::Rebase, "Rebase", ICON_GIT_PR),
];
const REMOTE_ITEMS: &[Leaf] = &[
leaf(ScmAction::AddRemote, "Add Remote", ICON_ADD),
leaf(ScmAction::RemoveRemote, "Remove Remote", ICON_TRASH),
];
const STASH_ITEMS: &[Leaf] = &[
leaf(ScmAction::Stash, "Stash", ICON_ARCHIVE),
leaf(
ScmAction::StashIncludeUntracked,
"Stash (Include Untracked)",
ICON_ARCHIVE,
),
leaf(ScmAction::StashStaged, "Stash Staged", ICON_ARCHIVE),
leaf(ScmAction::ApplyStash, "Apply Stash", ICON_ARCHIVE),
leaf(ScmAction::PopStashLatest, "Pop Latest Stash", ICON_ARCHIVE),
leaf(ScmAction::PopStashPick, "Pop Stash", ICON_ARCHIVE),
leaf(ScmAction::DropStash, "Drop Stash", ICON_TRASH),
leaf(ScmAction::ViewStash, "View Stash", ICON_ARCHIVE),
];
const TAGS_ITEMS: &[Leaf] = &[
leaf(ScmAction::CreateTag, "Create Tag", ICON_TAG),
leaf(ScmAction::DeleteTag, "Delete Tag", ICON_TRASH),
leaf(ScmAction::PushTags, "Push Tags", ICON_ARROW_UP),
leaf(ScmAction::DeleteRemoteTag, "Delete Remote Tag", ICON_TRASH),
];
fn operation_items(op: crate::git::RepoOp) -> Vec<Leaf> {
use crate::git::RepoOp;
let (cont, skip, abort): (Option<&str>, Option<&str>, &str) = match op {
RepoOp::Merge => (None, None, "Abort Merge"),
RepoOp::Rebase { .. } => (Some("Continue Rebase"), Some("Skip Commit"), "Abort Rebase"),
RepoOp::CherryPick => (
Some("Continue Cherry-Pick"),
Some("Skip Commit"),
"Abort Cherry-Pick",
),
RepoOp::Revert => (Some("Continue Revert"), Some("Skip Commit"), "Abort Revert"),
RepoOp::Bisect => {
return vec![
leaf(ScmAction::BisectGood, "Bisect: Mark Good", ICON_CHECK),
leaf(ScmAction::BisectBad, "Bisect: Mark Bad", ICON_DISCARD),
leaf(ScmAction::AbortOperation, "Bisect: Reset", ICON_TRASH),
];
}
};
let mut items = Vec::new();
if let Some(label) = cont {
items.push(leaf(ScmAction::ContinueOperation, label, ICON_CHECK));
}
if let Some(label) = skip {
items.push(leaf(ScmAction::SkipOperation, label, ICON_ARROW_RIGHT));
}
items.push(leaf(ScmAction::AbortOperation, abort, ICON_DISCARD));
items
}
pub fn menu_for(op: Option<crate::git::RepoOp>) -> Vec<Node> {
let mut nodes: Vec<Node> = op
.map(|op| operation_items(op).into_iter().map(Node::Item).collect())
.unwrap_or_default();
if !nodes.is_empty() {
nodes.push(Node::Sep);
}
nodes.extend(base_menu());
nodes
}
fn base_menu() -> Vec<Node> {
vec![
Node::Item(leaf(ScmAction::Pull, "Pull", ICON_CLOUD_DOWNLOAD)),
Node::Item(leaf(ScmAction::Push, "Push", ICON_CLOUD_UPLOAD)),
Node::Item(leaf(ScmAction::Clone, "Clone", ICON_REPO_CLONE)),
Node::Item(leaf(ScmAction::CheckoutTo, "Checkout to", ICON_GIT_BRANCH)),
Node::Item(leaf(ScmAction::Fetch, "Fetch", ICON_GIT_FETCH)),
Node::Sep,
Node::Sub {
label: "Commit",
icon: ICON_CHECK,
items: COMMIT_ITEMS,
},
Node::Sub {
label: "Changes",
icon: ICON_ADD,
items: CHANGES_ITEMS,
},
Node::Sub {
label: "Pull, Push",
icon: ICON_SYNC,
items: PULL_PUSH_ITEMS,
},
Node::Sub {
label: "Branch",
icon: ICON_GIT_BRANCH,
items: BRANCH_ITEMS,
},
Node::Sub {
label: "Remote",
icon: ICON_BROADCAST,
items: REMOTE_ITEMS,
},
Node::Sub {
label: "Stash",
icon: ICON_ARCHIVE,
items: STASH_ITEMS,
},
Node::Sub {
label: "Tags",
icon: ICON_TAG,
items: TAGS_ITEMS,
},
Node::Sep,
Node::Item(leaf(
ScmAction::ShowGitOutput,
"Show Git Output",
ICON_OUTPUT,
)),
]
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Row {
Leaf {
icon: char,
accent: Color,
label: Cow<'static, str>,
},
Sub {
icon: char,
accent: Color,
label: Cow<'static, str>,
},
Sep,
}
impl Row {
fn leaf(l: &Leaf) -> Self {
Row::Leaf {
icon: l.icon,
accent: action_color(l.action),
label: Cow::Borrowed(l.label),
}
}
fn label(&self) -> Option<&str> {
match self {
Row::Leaf { label, .. } | Row::Sub { label, .. } => Some(label),
Row::Sep => None,
}
}
}
pub fn rows(nodes: &[Node]) -> Vec<Row> {
nodes
.iter()
.map(|node| match node {
Node::Item(l) => Row::leaf(l),
Node::Sub { label, icon, .. } => Row::Sub {
icon: *icon,
accent: submenu_color(label),
label: Cow::Borrowed(label),
},
Node::Sep => Row::Sep,
})
.collect()
}
pub fn fly_out_rows(nodes: &[Node], parent: usize) -> Option<Vec<Row>> {
match nodes.get(parent)? {
Node::Sub { items, .. } => Some(items.iter().map(Row::leaf).collect()),
_ => None,
}
}
pub fn commit_rows(default_branch: &str) -> Vec<Row> {
use crate::widgets::source_control::CommitMenuItem as Item;
Item::ALL
.iter()
.map(|item| Row::Leaf {
icon: item.icon(),
accent: match item {
Item::CommitAndPush => ACCENT_GREY,
Item::Push | Item::Pull | Item::Sync => ACCENT_BLUE,
Item::CheckoutBranch => ACCENT_CYAN,
Item::Stash | Item::StashPop => ACCENT_AMBER,
Item::ViewStagedDiff
| Item::ViewPreviousCommitDiff
| Item::ViewDefaultBranchDiff => ACCENT_PURPLE,
},
label: Cow::Owned(item.label(default_branch)),
})
.collect()
}
pub fn commit_column(
rows: &[Row],
caret: Rect,
button: Rect,
screen: Rect,
card: bool,
) -> Option<Column> {
if caret.width == 0 || button.width == 0 {
return None;
}
let width = rows
.iter()
.filter_map(Row::label)
.map(|l| {
u16::try_from(l.chars().count())
.unwrap_or(u16::MAX)
.saturating_add(4)
})
.max()
.unwrap_or(0)
.max(caret.width);
let x = caret.right().saturating_sub(width).max(button.x);
let n = u16::try_from(rows.len()).unwrap_or(u16::MAX);
let pad = u16::from(card);
let rect = Rect::new(x, caret.bottom(), width, n.saturating_add(2 * pad)).intersection(screen);
if card && (rect.width < 3 || rect.height < 3) || rect.is_empty() {
return None;
}
Some(Column {
rect,
pad,
rows: rows.len(),
first: 0,
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Column {
pub rect: Rect,
pub pad: u16,
rows: usize,
first: usize,
}
impl Column {
pub fn row(&self, i: usize) -> Option<Rect> {
if i >= self.rows {
return None;
}
let k = u16::try_from(i.checked_sub(self.first)?).ok()?;
let y = self.rect.y + self.pad + k;
(y + self.pad < self.rect.bottom()).then(|| Rect::new(self.rect.x, y, self.rect.width, 1))
}
pub fn row_at(&self, x: u16, y: u16) -> Option<usize> {
if x < self.rect.x || x >= self.rect.right() {
return None;
}
let i = self.first + usize::from(y.checked_sub(self.rect.y + self.pad)?);
self.row(i).map(|_| i)
}
pub fn shown(&self) -> usize {
usize::from(self.rect.height.saturating_sub(2 * self.pad)).min(self.rows)
}
pub fn shown_from(self, first: usize) -> Column {
Column {
first: first.min(self.rows.saturating_sub(self.shown())),
..self
}
}
}
fn edge_pad(rows: u16, height: u16) -> u16 {
u16::from(rows.saturating_add(2) <= height)
}
pub fn top_column(rows: &[Row], anchor: Rect, screen: Rect, card: bool) -> Column {
let width = column_width(
rows.iter()
.filter_map(|r| r.label())
.map(|l| l.chars().count()),
true,
);
let n = u16::try_from(rows.len()).unwrap_or(u16::MAX);
let x = anchor.x.min(screen.x + screen.width.saturating_sub(width));
let (pad, height) = if card {
let pad = edge_pad(n, screen.height);
(pad, (n + 2 * pad).min(screen.height))
} else {
(0, n)
};
let rect = Rect {
x,
y: anchor
.bottom()
.min(screen.y + screen.height.saturating_sub(height)),
width: if card { width.min(screen.width) } else { width },
height: height.min(screen.height),
};
Column {
rect,
pad,
rows: rows.len(),
first: 0,
}
}
pub fn fly_out_column(
top: Column,
parent: usize,
rows: &[Row],
screen: Rect,
card: bool,
) -> Column {
let width = column_width(
rows.iter()
.filter_map(|r| r.label())
.map(|l| l.chars().count()),
false,
);
let right_x = top.rect.x + top.rect.width;
let x = if right_x + width <= screen.x + screen.width {
right_x
} else {
top.rect.x.saturating_sub(width)
};
let n = u16::try_from(rows.len()).unwrap_or(u16::MAX);
let parent_y = top.row(parent).map_or(top.rect.y + top.pad, |r| r.y);
let (pad, height) = if card {
let pad = edge_pad(n, screen.height);
(pad, (n + 2 * pad).min(screen.height))
} else {
(0, n)
};
let rect = Rect {
x,
y: parent_y
.saturating_sub(pad)
.min(screen.y + screen.height.saturating_sub(height)),
width,
height: height.min(screen.height),
};
Column {
rect,
pad,
rows: rows.len(),
first: 0,
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Marks {
pub open: Option<usize>,
pub cursor: Option<usize>,
pub pointer: Option<(u16, u16)>,
pub cursor_colours: (Color, Color),
}
const BG: Color = Color::Rgb(0x25, 0x2b, 0x37);
const SUB_BG: Color = Color::Rgb(0x2b, 0x32, 0x3f);
const FG: Color = Color::Rgb(0xe6, 0xe9, 0xef);
const SEP_FG: Color = Color::Rgb(0x3b, 0x42, 0x52);
const SUB_ARROW_FG: Color = Color::Rgb(0x9a, 0xa4, 0xb2);
const ACCENT_BLUE: Color = Color::Rgb(0x6c, 0xb6, 0xff);
const ACCENT_CYAN: Color = Color::Rgb(0x88, 0xc0, 0xd0);
const ACCENT_GREEN: Color = Color::Rgb(0xa3, 0xbe, 0x8c);
const ACCENT_AMBER: Color = Color::Rgb(0xeb, 0xcb, 0x8b);
const ACCENT_PURPLE: Color = Color::Rgb(0xc0, 0xa4, 0xf5);
const ACCENT_RED: Color = Color::Rgb(0xe7, 0x70, 0x70);
const ACCENT_GREY: Color = Color::Rgb(0x9a, 0xa4, 0xb2);
fn action_color(action: ScmAction) -> Color {
use ScmAction::*;
match action {
Pull | Push | Fetch | Clone | Sync | PullRebase | PushTo | PushForce | PublishBranch
| CommitAndPush | CommitAndSync | PushTags => ACCENT_BLUE,
CheckoutTo | CreateBranch | CreateBranchFrom => ACCENT_CYAN,
Commit | CommitStaged | CommitAll | StageAll | AddRemote => ACCENT_GREEN,
CommitAmend | UndoLastCommit | RenameBranch => ACCENT_AMBER,
Merge | Rebase => ACCENT_PURPLE,
DiscardAll | DeleteBranch | RemoveRemote | DropStash | DeleteTag | DeleteRemoteTag => {
ACCENT_RED
}
Stash
| StashIncludeUntracked
| StashStaged
| ApplyStash
| PopStashLatest
| PopStashPick
| CreateTag => ACCENT_AMBER,
UnstageAll | ShowGitOutput | ViewStash => ACCENT_GREY,
ContinueOperation | BisectGood => ACCENT_GREEN,
SkipOperation => ACCENT_AMBER,
AbortOperation | BisectBad => ACCENT_RED,
}
}
fn submenu_color(label: &str) -> Color {
match label {
"Commit" | "Changes" => ACCENT_GREEN,
"Pull, Push" => ACCENT_BLUE,
"Branch" | "Remote" => ACCENT_CYAN,
"Stash" | "Tags" => ACCENT_AMBER,
_ => ACCENT_GREY,
}
}
fn column_width(labels: impl Iterator<Item = usize>, with_arrow: bool) -> u16 {
let arrow = if with_arrow { 2 } else { 0 };
let max = labels.max().unwrap_or(0) as u16;
(1 + 1 + 1 + max + arrow + 1).max(12)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Panel {
Top,
FlyOut,
Commit,
}
impl Panel {
fn bg(self) -> Color {
match self {
Panel::Top | Panel::Commit => BG,
Panel::FlyOut => SUB_BG,
}
}
fn ink(self) -> Color {
match self {
Panel::Top | Panel::FlyOut => FG,
Panel::Commit => Color::White,
}
}
fn fill(self, theme: crate::theme::Theme) -> Style {
let fill = Style::default().bg(theme.ui(self.bg()));
match self {
Panel::Top | Panel::FlyOut => fill,
Panel::Commit => fill.fg(theme.ui(self.ink())),
}
}
}
pub fn paint_column(
buf: &mut Buffer,
col: Column,
rows: &[Row],
panel: Panel,
marks: Marks,
theme: crate::theme::Theme,
) {
if let Some(t) = crate::theme::tones(theme) {
paint_card_column(buf, col, rows, marks, theme, t);
return;
}
let bg = theme.ui(panel.bg());
let ink = theme.ui(panel.ink());
let arrow = theme.ui(SUB_ARROW_FG);
Widget::render(Clear, col.rect, buf);
fill(buf, col.rect, panel.fill(theme));
let (cursor_bg, cursor_ink) = marks.cursor_colours;
for (i, row) in rows.iter().enumerate().skip(col.first) {
let Some(r) = col.row(i) else {
break;
};
let cursor = marks.cursor == Some(i);
if cursor {
fill(buf, r, Style::default().bg(cursor_bg));
}
match row {
Row::Sep => {
for x in r.x + 1..r.x + r.width.saturating_sub(1) {
buf.set_string(x, r.y, "─", Style::default().fg(theme.ui(SEP_FG)).bg(bg));
}
}
Row::Leaf {
icon,
accent,
label,
} => {
let (row_bg, ink) = if cursor {
(cursor_bg, cursor_ink)
} else {
(bg, ink)
};
paint_row(buf, r, *icon, theme.ui(*accent), label, None, row_bg, ink);
}
Row::Sub {
icon,
accent,
label,
} => {
let (row_bg, ink, arrow) = if cursor {
(cursor_bg, cursor_ink, cursor_ink)
} else if marks.open == Some(i) {
(theme.ui(SUB_BG), ink, arrow)
} else {
(bg, ink, arrow)
};
paint_row(
buf,
r,
*icon,
theme.ui(*accent),
label,
Some(arrow),
row_bg,
ink,
);
}
}
}
}
fn paint_card_column(
buf: &mut Buffer,
col: Column,
rows: &[Row],
marks: Marks,
theme: crate::theme::Theme,
t: crate::theme::Tones,
) {
Widget::render(Clear, col.rect, buf);
fill(buf, col.rect, Style::default().bg(t.raised()));
crate::widgets::surface::paint_overlay_frame(buf, col.rect, theme);
for (i, row) in rows.iter().enumerate().skip(col.first) {
let Some(r) = col.row(i) else {
break;
};
let lit = marks.cursor == Some(i) || crate::widgets::hover::contains(r, marks.pointer);
match row {
Row::Sep => {
let rule = "─".repeat(usize::from(r.width));
buf.set_string(r.x, r.y, rule, Style::default().fg(t.rim()));
}
Row::Leaf { icon, label, .. } => {
paint_card_row(buf, r, t, *icon, label, false, lit, false);
}
Row::Sub { icon, label, .. } => {
paint_card_row(buf, r, t, *icon, label, true, lit, marks.open == Some(i));
}
}
}
}
#[allow(clippy::too_many_arguments)]
fn paint_card_row(
buf: &mut Buffer,
row: Rect,
t: crate::theme::Tones,
icon: char,
label: &str,
submenu: bool,
lit: bool,
selected: bool,
) {
let body = Rect::new(row.x, row.y, row.width.saturating_sub(1), 1);
if selected {
crate::widgets::surface::paint_selected_row(buf, body, t, t.raised, true);
} else if lit {
let inner = Rect::new(row.x + 1, row.y, row.width.saturating_sub(2), 1);
buf.set_style(inner, Style::default().bg(t.hover_on(t.raised)));
}
let active = selected || lit;
let icon_fg = if active { t.text() } else { t.muted() };
buf.set_string(
row.x + 1,
row.y,
icon.to_string(),
Style::default().fg(icon_fg),
);
let avail = row.width.saturating_sub(3 + if submenu { 2 } else { 1 }) as usize;
let shown: String = label.chars().take(avail).collect();
let ink = if active { t.strong() } else { t.text() };
buf.set_string(row.x + 3, row.y, &shown, Style::default().fg(ink));
if submenu && row.width >= 2 {
let fg = if active { t.text() } else { t.faint() };
buf.set_string(row.x + row.width - 2, row.y, "›", Style::default().fg(fg));
}
}
fn fill(buf: &mut Buffer, rect: Rect, style: Style) {
for ry in 0..rect.height {
for rx in 0..rect.width {
buf[(rect.x + rx, rect.y + ry)]
.set_symbol(" ")
.set_style(style);
}
}
}
#[allow(clippy::too_many_arguments)]
fn paint_row(
buf: &mut Buffer,
row: Rect,
icon: char,
icon_color: Color,
label: &str,
arrow: Option<Color>,
bg: Color,
ink: Color,
) {
buf.set_string(
row.x + 1,
row.y,
icon.to_string(),
Style::default().fg(icon_color).bg(bg),
);
let avail = row
.width
.saturating_sub(3 + if arrow.is_some() { 2 } else { 1 }) as usize;
let shown: String = label.chars().take(avail).collect();
buf.set_string(row.x + 3, row.y, &shown, Style::default().fg(ink).bg(bg));
if let Some(arrow) = arrow
&& row.width >= 2
{
buf.set_string(
row.x + row.width - 2,
row.y,
"›",
Style::default().fg(arrow).bg(bg),
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_operation_in_progress_leads_the_menu() {
use crate::git::RepoOp;
let labels = |op| -> Vec<&'static str> {
menu_for(op)
.iter()
.map_while(|n| match n {
Node::Item(l) => Some(l.label),
_ => None,
})
.collect()
};
assert_eq!(
labels(Some(RepoOp::Rebase { step: 2, total: 5 })),
["Continue Rebase", "Skip Commit", "Abort Rebase"]
);
assert_eq!(labels(Some(RepoOp::Merge)), ["Abort Merge"]);
assert!(matches!(menu_for(Some(RepoOp::Merge))[1], Node::Sep));
assert_eq!(menu_for(None).len(), base_menu().len());
}
#[test]
fn menu_has_the_expected_top_level_shape() {
let m = menu_for(None);
assert_eq!(m.len(), 15);
let subs = m.iter().filter(|n| matches!(n, Node::Sub { .. })).count();
assert_eq!(subs, 7, "Commit/Changes/Pull-Push/Branch/Remote/Stash/Tags");
}
fn top(theme: crate::theme::Theme, screen: Rect) -> (Vec<Row>, Column) {
let rows = rows(&menu_for(None));
let card = crate::theme::tones(theme).is_some();
let col = top_column(&rows, Rect::new(10, 0, 2, 1), screen, card);
(rows, col)
}
fn row_of(rows: &[Row], label: &str) -> usize {
rows.iter().position(|r| r.label() == Some(label)).unwrap()
}
#[test]
fn leaf_icons_carry_their_per_action_accent_colour() {
let screen = Rect::new(0, 0, 120, 40);
let theme = crate::theme::Theme::BLACK;
let (rows, col) = top(theme, screen);
let mut buf = Buffer::empty(screen);
paint_column(&mut buf, col, &rows, Panel::Top, Marks::default(), theme);
let pull = col.row(row_of(&rows, "Pull")).unwrap();
assert_eq!(
buf[(pull.x + 1, pull.y)].fg,
ACCENT_BLUE,
"the Pull icon must be painted in its blue accent, not the neutral foreground"
);
}
#[test]
fn croft_loch_draws_the_menu_as_a_card_with_a_hover_lift() {
let theme = crate::theme::Theme::from_id("loch");
let t = crate::theme::tones(theme).unwrap();
let screen = Rect::new(0, 0, 120, 40);
let (rows, col) = top(theme, screen);
let first = col.row(0).unwrap();
assert_eq!(first.y, 2, "a card edge above the rows, under the pill");
let mut buf = Buffer::empty(screen);
paint_column(&mut buf, col, &rows, Panel::Top, Marks::default(), theme);
assert_eq!(buf[(first.x, first.y - 1)].symbol(), "\u{2594}");
assert_eq!(buf[(first.x + 3, first.y)].bg, t.raised());
let pull = col.row(row_of(&rows, "Pull")).unwrap();
assert_eq!(buf[(pull.x + 1, pull.y)].fg, t.muted(), "icons rest muted");
let marks = Marks {
pointer: Some((pull.x + 4, pull.y)),
..Marks::default()
};
let mut buf = Buffer::empty(screen);
paint_column(&mut buf, col, &rows, Panel::Top, marks, theme);
assert_eq!(buf[(pull.x + 3, pull.y)].bg, t.hover_on(t.raised));
assert_eq!(buf[(pull.x + 1, pull.y)].fg, t.text(), "the icon lifts too");
assert_eq!(col.row_at(pull.x + 4, pull.y), Some(row_of(&rows, "Pull")));
}
#[test]
fn the_keyboards_row_shows_in_both_looks() {
let screen = Rect::new(0, 0, 120, 40);
let band = Color::Rgb(1, 2, 3);
let ink = Color::Rgb(4, 5, 6);
let plain = crate::theme::Theme::BLACK;
let (rows, col) = top(plain, screen);
let push = row_of(&rows, "Push");
let marks = Marks {
cursor: Some(push),
cursor_colours: (band, ink),
..Marks::default()
};
let mut buf = Buffer::empty(screen);
paint_column(&mut buf, col, &rows, Panel::Top, marks, plain);
let r = col.row(push).unwrap();
assert_eq!(buf[(r.x, r.y)].bg, band, "the band fills the whole row");
assert_eq!(buf[(r.x + 3, r.y)].fg, ink);
assert_eq!(
buf[(r.x + 1, r.y)].fg,
plain.ui(ACCENT_BLUE),
"the icon keeps its accent"
);
let card = crate::theme::Theme::from_id("loch");
let t = crate::theme::tones(card).unwrap();
let (rows, col) = top(card, screen);
let marks = Marks {
cursor: Some(push),
..Marks::default()
};
let mut buf = Buffer::empty(screen);
paint_column(&mut buf, col, &rows, Panel::Top, marks, card);
let r = col.row(push).unwrap();
assert_eq!(buf[(r.x + 3, r.y)].bg, t.hover_on(t.raised));
}
#[test]
fn croft_loch_keeps_every_action_reachable_on_a_short_terminal() {
let rows_n = menu_for(None).len() as u16;
let screen = Rect::new(0, 0, 120, rows_n + 1);
let (_, card) = top(crate::theme::Theme::from_id("loch"), screen);
let (_, plain) = top(crate::theme::Theme::BLACK, screen);
assert_eq!(card.shown(), menu_for(None).len(), "no action clipped");
assert!(card.shown() >= plain.shown());
}
#[test]
fn a_fly_out_sits_beside_its_parent_row() {
let nodes = menu_for(None);
let rows = rows(&nodes);
let stash = row_of(&rows, "Stash");
let items = fly_out_rows(&nodes, stash).unwrap();
assert!(items.iter().any(|r| r.label() == Some("Drop Stash")));
let screen = Rect::new(0, 0, 120, 40);
let top = top_column(&rows, Rect::new(10, 0, 2, 1), screen, false);
let fly = fly_out_column(top, stash, &items, screen, false);
assert_eq!(fly.rect.x, top.rect.right());
assert_eq!(fly.row(0).unwrap().y, top.row(stash).unwrap().y);
let pill = Rect::new(60, 0, 2, 1);
let narrow = Rect::new(
0,
0,
top_column(&rows, pill, screen, false).rect.right() + 3,
40,
);
let top = top_column(&rows, pill, narrow, false);
let fly = fly_out_column(top, stash, &items, narrow, false);
assert!(fly.rect.width < pill.x, "room on the left");
assert_eq!(fly.rect.right(), top.rect.x, "flipped to the left");
}
#[test]
fn a_column_shown_from_a_later_row() {
let rows = rows(&menu_for(None));
let short = Rect::new(0, 0, 120, 8);
let col = top_column(&rows, Rect::new(10, 0, 2, 1), short, false);
assert!(col.shown() < rows.len());
let col = col.shown_from(5);
assert_eq!(col.row(4), None);
let first = col.row(5).unwrap();
assert_eq!(first.y, col.rect.y);
assert_eq!(col.row_at(first.x + 1, first.y), Some(5));
assert_eq!(
col.shown_from(99).row(rows.len() - 1).map(|r| r.bottom()),
Some(col.rect.bottom())
);
}
#[test]
fn a_cell_maps_to_the_row_drawn_there() {
let screen = Rect::new(0, 0, 120, 40);
let (rows, col) = top(crate::theme::Theme::BLACK, screen);
for i in 0..rows.len() {
let r = col.row(i).unwrap();
assert_eq!(col.row_at(r.x, r.y), Some(i));
assert_eq!(col.row_at(r.right() - 1, r.y), Some(i));
}
assert_eq!(col.row_at(col.rect.right(), col.rect.y), None);
assert_eq!(col.row_at(col.rect.x, col.rect.bottom()), None);
}
}