Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct GeneralConfig {
pub collapsed_sections: Vec<String>,
pub stash_list_limit: usize,
pub recent_commits_limit: usize,
pub log_author_width: usize,
pub mouse_support: bool,
pub mouse_scroll_lines: usize,
}
Expand Down Expand Up @@ -112,6 +113,8 @@ pub struct StyleConfig {
pub branch: StyleConfigEntry,
pub remote: StyleConfigEntry,
pub tag: StyleConfigEntry,
pub author: StyleConfigEntry,
pub age: StyleConfigEntry,

#[serde(default)]
pub blame: BlameStyleConfig,
Expand Down
3 changes: 3 additions & 0 deletions src/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ collapsed_sections = []
refresh_on_file_change.enabled = true
stash_list_limit = 10
recent_commits_limit = 10
log_author_width = 15
mouse_support = false
mouse_scroll_lines = 3

Expand Down Expand Up @@ -99,6 +100,8 @@ hash = { fg = "yellow" }
branch = { fg = "green" }
remote = { fg = "red" }
tag = { fg = "yellow" }
author = { fg = "magenta" }
age = { mods = "DIM" }

blame.line_num = { mods = "DIM" }
blame.code_line = { mods = "DIM" }
Expand Down
2 changes: 2 additions & 0 deletions src/item_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub(crate) enum ItemData {
short_id: String,
associated_references: Vec<Ref>,
summary: String,
author: String,
age: String,
},
Untracked(PathBuf),
Delta {
Expand Down
33 changes: 33 additions & 0 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::hash::Hash;
use std::hash::Hasher;
use std::iter;
use std::rc::Rc;
use std::time::SystemTime;

pub type ItemId = u64;

Expand Down Expand Up @@ -144,6 +145,36 @@ pub(crate) fn stash_list(repo: &Repository, limit: usize) -> Res<Vec<Item>> {
.collect::<Vec<_>>())
}

fn short_age(time: git2::Time) -> String {
const MINUTE: i64 = 60;
const HOUR: i64 = 60 * MINUTE;
const DAY: i64 = 24 * HOUR;
const WEEK: i64 = 7 * DAY;
const MONTH: i64 = 30 * DAY;
const YEAR: i64 = 365 * DAY;

let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.map_or(0, |since_epoch| since_epoch.as_secs() as i64);

// A commit dated in the future reads as brand new, rather than as a negative age.
let age = (now - time.seconds()).max(0);

if age < HOUR {
format!("{}m", age / MINUTE)
} else if age < DAY {
format!("{}h", age / HOUR)
} else if age < WEEK {
format!("{}d", age / DAY)
} else if age < MONTH {
format!("{}w", age / WEEK)
} else if age < YEAR {
format!("{}M", age / MONTH)
} else {
format!("{}y", age / YEAR)
}
}

pub(crate) fn log(
repo: &Repository,
limit: usize,
Expand Down Expand Up @@ -210,6 +241,8 @@ pub(crate) fn log(
short_id,
associated_references,
summary: commit.summary().unwrap_or("").to_string(),
author: commit.author().name().unwrap_or("").to_string(),
age: short_age(commit.author().when()),
};

Ok(Some(Item {
Expand Down
46 changes: 42 additions & 4 deletions src/screen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::StyleConfig;
use crate::style::Style;
use crate::ui::layout::{LayoutTree, OPTS};
use crate::ui::layout::{LayoutTree, opts};
use crate::ui::{UiItem, UiTree, layout_span};
use crate::{item_data::ItemData, ui};
use itertools::Itertools;
Expand Down Expand Up @@ -349,7 +349,8 @@ impl Screen {

fn move_cursor_to_screen_center(&mut self) {
let half_screen = self.size.1 as usize / 2;
self.cursor = self.line_index[self.scroll + half_screen];
let center = (self.scroll + half_screen).min(self.line_index.len().saturating_sub(1));
self.cursor = self.line_index[center];
}

fn clamp_cursor(&mut self) {
Expand Down Expand Up @@ -550,7 +551,7 @@ struct ItemView {
}

pub(crate) fn layout_screen<'a>(layout: &mut UiTree<'a>, screen: &'a Screen, hide_cursor: bool) {
layout.vertical(None, OPTS.fill_x(), |layout| {
layout.vertical(None, opts().fill_x(), |layout| {
for view in screen.item_views(screen.size) {
layout_item(layout, screen, hide_cursor, view);
}
Expand All @@ -565,7 +566,7 @@ fn layout_item<'a>(layout: &mut UiTree<'a>, screen: &'a Screen, hide_cursor: boo
let line_sel = line_selection_highlight(style, &line, is_line_sel);
let bg = area_sel.patch(line_sel);

layout.horizontal(Some(UiItem::Style(bg)), OPTS.fill_x(), |layout| {
layout.horizontal(Some(UiItem::Style(bg)), opts().fill_x(), |layout| {
let gutter_char = if !hide_cursor && line.highlighted {
gutter_char(style, is_line_sel, bg)
} else {
Expand Down Expand Up @@ -613,3 +614,40 @@ fn area_selection_highlight(style: &StyleConfig, line: &ItemView) -> Style {
Style::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::config::init_test_config;

fn screen_of(item_count: usize, size: (u16, u16)) -> Screen {
let config = Arc::new(init_test_config().unwrap());

Screen::new(
config,
size,
Box::new(move || {
Ok((0..item_count)
.map(|i| Item {
id: i as u64,
data: ItemData::Raw(format!("item {i}")),
..Default::default()
})
.collect())
}),
)
.unwrap()
}

/// Scrolling is allowed a couple of lines past the content, so on a screen
/// the content doesn't fill, its center lands past the last line.
#[test]
fn recenter_cursor_on_a_screen_the_content_doesnt_fill() {
let mut screen = screen_of(3, (80, 20));

screen.scroll_view_down(2);
screen.refresh().unwrap();

assert_eq!(2, screen.cursor);
}
}
17 changes: 17 additions & 0 deletions src/tests/helpers/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ impl TestContext {

redact(&mut debug_output, "From file://(.*)\n");
redact(&mut debug_output, "To file://(/.*)\n");
// The fixtures' commit dates are fixed, so their age grows as time
// passes. Only its width, which the layout depends on, is kept.
redact_all(&mut debug_output, r"(\d+[mhdwMy]) \|");

debug_output
}
Expand All @@ -126,6 +129,20 @@ fn redact(debug_output: &mut String, regex: &str) {
}
}

/// Replaces every capture with `_`, so that what was redacted stays visible.
fn redact_all(debug_output: &mut String, regex: &str) {
let re = Regex::new(regex).unwrap();
let ranges = re
.captures_iter(debug_output)
.map(|caps| caps.get(1).unwrap().range())
.collect::<Vec<_>>();

for range in ranges.into_iter().rev() {
let placeholder = "_".repeat(range.len());
debug_output.replace_range(range, &placeholder);
}
}

pub fn keys(input: &str) -> Vec<Event> {
let ("", keys) = parse_test_keys(input).unwrap() else {
unreachable!();
Expand Down
4 changes: 2 additions & 2 deletions src/tests/snapshots/gitu__tests__binary_file.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ expression: ctx.redact_buffer()
▌added binary-file |
|
Recent commits |
b66a0bf main origin/main add initial-file |
b66a0bf main origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
|
|
|
styles_hash: 667054dbc230d885
styles_hash: a4136f2f4fbe0a68
4 changes: 2 additions & 2 deletions src/tests/snapshots/gitu__tests__branch__branch_menu.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: ctx.redact_buffer()
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
K Delete branch |
m Rename branch |
q/esc Quit/Close |
styles_hash: f3500db39944be2f
styles_hash: 932fdf42b6b7789f
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: ctx.redact_buffer()
▌On branch new |
|
Recent commits |
b66a0bf main new origin/main add initial-file |
b66a0bf main new origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
────────────────────────────────────────────────────────────────────────────────|
$ git checkout -b new |
Switched to a new branch 'new' |
styles_hash: afd3b8aa17fb4c8f
styles_hash: b25c3a955ed49870
4 changes: 2 additions & 2 deletions src/tests/snapshots/gitu__tests__branch__checkout_picker.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: ctx.redact_buffer()
Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
|
|
|
styles_hash: b250c9a77d4d5cec
styles_hash: 44b69210eca90db6
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: ctx.redact_buffer()
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
|
|
|
styles_hash: 7b605cf80abe284
styles_hash: d6ae4172afe777b1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ expression: ctx.redact_buffer()
▌On branch feature-a |
|
Recent commits |
3b23a7d feature-a add feature-a commit |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
3b23a7d feature-a add feature-a commit Author Name __ |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
────────────────────────────────────────────────────────────────────────────────|
$ git checkout feature-a |
Switched to branch 'feature-a' |
styles_hash: 2f476764ee015367
styles_hash: 9ca73e6aebfb5b3
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: ctx.redact_buffer()
▌No branch |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ Or undo this operation with:
git switch - |
Turn off this advice by setting config variable advice.detachedHead to false |
HEAD is now at b66a0bf add initial-file |
styles_hash: d9a7a96bb61e275e
styles_hash: c397223f348cda2a
4 changes: 2 additions & 2 deletions src/tests/snapshots/gitu__tests__branch__delete_picker.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: ctx.redact_buffer()
Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
|
|
|
styles_hash: 950ad1ca1b25dd92
styles_hash: 665845d168c258bd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: ctx.redact_buffer()
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
|
|
|
styles_hash: 7b605cf80abe284
styles_hash: d6ae4172afe777b1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: ctx.redact_buffer()
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
|
────────────────────────────────────────────────────────────────────────────────|
? Branch is not fully merged. Really delete? (y or n) › █ |
styles_hash: cac1287c177321c0
styles_hash: dd00d7ffeafd6b61
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: ctx.redact_buffer()
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file |
b66a0bf main v1.0.0 v2.0.0 origin/main add initial-file Author Name __ |
|
|
|
Expand All @@ -22,4 +22,4 @@ expression: ctx.redact_buffer()
────────────────────────────────────────────────────────────────────────────────|
$ git branch -d -f bugfix-123 |
Deleted branch bugfix-123 (was 33a8c4d). |
styles_hash: 52f5b75cab04a311
styles_hash: 759341d48e2c00dc
Loading