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
8 changes: 8 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ impl App {
} else {
self.selected = self.selected.min(self.files.len() - 1);
}

self.list_state.select(Some(self.selected));

let same_file = previous.as_deref() == self.selected_file().map(|f| f.path.as_str());
Expand Down Expand Up @@ -214,6 +215,7 @@ impl App {
self.diff_source = None;
}
}

self.clamp_scroll();
}

Expand Down Expand Up @@ -252,6 +254,7 @@ impl App {
if self.files.is_empty() || index == self.selected && self.diff_source.is_some() {
return;
}

self.selected = index.min(self.files.len() - 1);
self.list_state.select(Some(self.selected));
self.diff_scroll = 0;
Expand Down Expand Up @@ -290,6 +293,7 @@ impl App {
if row < inner_top || row >= self.files_area.bottom().saturating_sub(1) {
return;
}

let index = self.list_state.offset() + usize::from(row - inner_top);
if index < self.files.len() {
self.select(index);
Expand All @@ -303,6 +307,7 @@ impl App {
let Some(file) = self.selected_file().cloned() else {
return;
};

let result = action(&self.repo, &file);
self.report(result);
self.refresh();
Expand Down Expand Up @@ -369,6 +374,7 @@ impl App {
let Mode::Confirm(action) = self.mode else {
return;
};

self.mode = Mode::Normal;
match action {
Confirm::Revert => self.with_selected(|repo, file| repo.revert(file)),
Expand Down Expand Up @@ -476,6 +482,7 @@ impl App {
if self.is_pushing() {
return;
}

let (tx, rx) = mpsc::channel();
let repo = self.repo.clone();
thread::spawn(move || {
Expand All @@ -498,6 +505,7 @@ impl App {
}
Err(e) => self.set_message(MessageKind::Error, e),
}

self.refresh();
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ pub fn build(sections: &[DiffSection], path: &str, theme: &Theme) -> DiffView {
if i > 0 {
raw.push(raw_row(RowKind::Info, ""));
}

if let Some(title) = &section.title {
raw.push(raw_row(RowKind::Section, title));
}
}

let before = raw.len();
parse(&section.text, &mut raw);
if raw.len() == before {
Expand Down Expand Up @@ -177,6 +179,7 @@ pub fn build(sections: &[DiffSection], path: &str, theme: &Theme) -> DiffView {
RowKind::Removed => view.removed += 1,
_ => {}
}

view.max_line_no = view
.max_line_no
.max(row.old.unwrap_or(0))
Expand All @@ -195,9 +198,11 @@ pub fn build(sections: &[DiffSection], path: &str, theme: &Theme) -> DiffView {
if let Some(old) = old_hl.as_mut() {
let _ = old.highlight_line(&row.text, set);
}

new_hl.as_mut()
}
};

hl.and_then(|h| h.highlight_line(&row.text, set).ok())
.map(|ranges| {
ranges
Expand All @@ -206,6 +211,7 @@ pub fn build(sections: &[DiffSection], path: &str, theme: &Theme) -> DiffView {
.collect::<Vec<_>>()
})
};

let pieces = highlighted.unwrap_or_else(|| vec![(Style::default(), row.text.as_str())]);
split_emphasis(&pieces, &row.emph)
}
Expand Down Expand Up @@ -328,6 +334,7 @@ fn parse(text: &str, rows: &mut Vec<RawRow>) {
if let Some(note) = header_note(line) {
rows.push(raw_row(RowKind::Info, &note));
}

continue;
}

Expand Down Expand Up @@ -356,9 +363,11 @@ fn parse(text: &str, rows: &mut Vec<RawRow>) {
RowKind::Removed => (Some(old), None),
_ => (Some(old), Some(new)),
};

if kind != RowKind::Added {
old += 1;
}

if kind != RowKind::Removed {
new += 1;
}
Expand Down Expand Up @@ -418,6 +427,7 @@ fn header_note(line: &str) -> Option<String> {
} else {
return None;
};

Some(note)
}

Expand All @@ -430,10 +440,12 @@ fn mark_word_changes(rows: &mut [RawRow]) {
i += 1;
continue;
}

let removed_start = i;
while i < rows.len() && rows[i].kind == RowKind::Removed {
i += 1;
}

let added_start = i;
while i < rows.len() && rows[i].kind == RowKind::Added {
i += 1;
Expand Down Expand Up @@ -464,13 +476,16 @@ fn tokens(text: &str) -> Vec<Range<usize>> {
out.push(s..i);
start = None;
}

out.push(i..i + c.len_utf8());
}
}
}

if let Some(s) = start {
out.push(s..text.len());
}

out
}

Expand All @@ -485,6 +500,7 @@ fn changed_ranges(old: &str, new: &str) -> Option<(Ranges, Ranges)> {
if ot.len() > MAX_WORD_DIFF_TOKENS || nt.len() > MAX_WORD_DIFF_TOKENS {
return None;
}

let same = |i: usize, j: usize| old[ot[i].clone()] == new[nt[j].clone()];

// lcs[i][j] = LCS length of ot[i..] and nt[j..].
Expand Down Expand Up @@ -522,6 +538,7 @@ fn changed_ranges(old: &str, new: &str) -> Option<(Ranges, Ranges)> {
if total == 0 || (old_changed.is_empty() && new_changed.is_empty()) {
return None;
}

if (common as f32 / total as f32) < MIN_SIMILARITY {
return None;
}
Expand All @@ -546,12 +563,15 @@ fn merge_tokens(text: &str, tokens: &[Range<usize>], changed: &[usize]) -> Vec<R
.all(char::is_whitespace),
_ => false,
};

match ranges.last_mut() {
Some(range) if bridge => range.end = token.end,
_ => ranges.push(token),
}

last_index = Some(index);
}

ranges
}

Expand All @@ -564,12 +584,15 @@ fn convert_style(style: syntect::highlighting::Style, theme: &Theme) -> Style {
} else {
out = out.fg(Color::Reset);
}

if style.font_style.contains(FontStyle::BOLD) {
out = out.add_modifier(Modifier::BOLD);
}

if style.font_style.contains(FontStyle::ITALIC) {
out = out.add_modifier(Modifier::ITALIC);
}

out
}

Expand All @@ -580,6 +603,7 @@ fn split_emphasis(pieces: &[(Style, &str)], emph: &[Range<usize>]) -> Vec<Seg> {
if text.is_empty() {
return;
}

let text = sanitize(text);
match segs.last_mut() {
Some(last) if last.style == style && last.emph == emph => last.text.push_str(&text),
Expand All @@ -601,6 +625,7 @@ fn split_emphasis(pieces: &[(Style, &str)], emph: &[Range<usize>]) -> Vec<Seg> {
}
}
}

cuts.push(end);
cuts.sort_unstable();

Expand All @@ -610,6 +635,7 @@ fn split_emphasis(pieces: &[(Style, &str)], emph: &[Range<usize>]) -> Vec<Seg> {
push(&text[a - start..b - start], *style, emphasized);
}
}

segs
}

Expand Down
1 change: 1 addition & 0 deletions src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn handle_mouse(app: &mut App, mouse: MouseEvent) {
if app.mode != Mode::Normal {
return;
}

let pos = Position::new(mouse.column, mouse.row);
let over_files = app.files_area.contains(pos);

Expand Down
9 changes: 9 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ impl Repo {
if cached {
args.push("--cached");
}

args.push("--");
args.push(&file.path);
if cached && let Some(orig) = &file.orig_path {
args.push(orig);
}

self.run(args)
}

Expand Down Expand Up @@ -184,6 +186,7 @@ impl Repo {
if let Some(orig) = &file.orig_path {
paths.push(orig);
}

self.unstage_paths(&paths)
}

Expand Down Expand Up @@ -239,6 +242,7 @@ impl Repo {
if let Some(orig) = &file.orig_path {
self.run(["restore", "--", orig.as_str()])?;
}

Ok(format!("reverted rename of {}", file.path))
}
_ if file.has_staged() => self
Expand Down Expand Up @@ -268,6 +272,7 @@ impl Repo {
self.run(["rm", "-f", "--quiet", "--", &file.path])?;
}
}

Ok(format!("removed {}", file.path))
}

Expand All @@ -277,6 +282,7 @@ impl Repo {
if message.is_empty() {
return Err("commit message is empty".to_string());
}

if !self.has_staged_changes()? {
return Err("nothing staged to commit (Space stages a file, a stages all)".to_string());
}
Expand Down Expand Up @@ -390,6 +396,7 @@ pub fn parse_status(text: &str) -> Status {
else {
continue;
};

let path = chars.as_str().to_string();

let orig_path = if matches!(index, 'R' | 'C') {
Expand Down Expand Up @@ -432,6 +439,7 @@ pub fn sanitize(text: &str) -> String {
c => out.push(c),
}
}

out
}

Expand Down Expand Up @@ -540,6 +548,7 @@ pub(crate) mod tests {
.success();
assert!(ok, "git {args:?} failed");
};

git(&["init", "--quiet", "--initial-branch=main"]);
git(&["config", "user.name", "gitrat test"]);
git(&["config", "user.email", "test@example.com"]);
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn event_loop(terminal: &mut terminal::Tui, app: &mut App) -> io::Result<()> {
Event::FocusGained => app.refresh(),
Event::Resize(..) | Event::FocusLost => {}
}

if app.should_quit || !event::poll(Duration::ZERO)? {
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn enter(stdout: &mut Stdout, options: Options) -> io::Result<()> {
if options.mouse {
execute!(stdout, EnableMouseCapture)?;
}

Ok(())
}

Expand All @@ -67,6 +68,7 @@ pub fn restore(options: Options) -> io::Result<()> {
if options.mouse {
let _ = execute!(stdout, DisableMouseCapture);
}

let _ = execute!(
stdout,
DisableFocusChange,
Expand Down
4 changes: 4 additions & 0 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Theme {
ThemeChoice::Light => false,
ThemeChoice::Auto => detect_dark(),
};

Self::new(dark, detect_truecolor())
}

Expand All @@ -74,6 +75,7 @@ impl Theme {
} else {
EmbeddedThemeName::Github
};

let syntax = two_face::theme::extra().get(syntax_name).clone();

if dark {
Expand Down Expand Up @@ -128,6 +130,7 @@ fn detect_truecolor() -> bool {
if std::env::var_os("WT_SESSION").is_some() {
return true;
}

let program = std::env::var("TERM_PROGRAM").unwrap_or_default();
matches!(
program.as_str(),
Expand Down Expand Up @@ -202,6 +205,7 @@ fn rgb_to_256(r: u8, g: u8, b: u8) -> u8 {
_ => usize::from((v - 35) / 40),
}
};

let (ri, gi, bi) = (level(r), level(g), level(b));
let cube = (LEVELS[ri], LEVELS[gi], LEVELS[bi]);

Expand Down
Loading
Loading