Skip to content

Commit 25a49e2

Browse files
author
Stephan Dilly
committed
supporting marking commits in the log
1 parent a4bdfd1 commit 25a49e2

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ ARGS=-l
77
profile:
88
cargo run --features=timing,pprof -- ${ARGS}
99

10+
run-timing:
11+
cargo run --features=timing --release -- ${ARGS}
12+
1013
debug:
1114
RUST_BACKTRACE=true cargo run --features=timing -- ${ARGS}
1215

src/components/commitlist.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
ui::style::{SharedTheme, Theme},
1111
};
1212
use anyhow::Result;
13-
use asyncgit::sync::Tags;
13+
use asyncgit::sync::{CommitId, Tags};
1414
use chrono::{DateTime, Local};
1515
use crossterm::event::Event;
1616
use std::{
@@ -33,6 +33,7 @@ pub struct CommitList {
3333
branch: Option<String>,
3434
count_total: usize,
3535
items: ItemBatch,
36+
marked: Vec<CommitId>,
3637
scroll_state: (Instant, f32),
3738
tags: Option<Tags>,
3839
current_size: Cell<(u16, u16)>,
@@ -50,6 +51,7 @@ impl CommitList {
5051
) -> Self {
5152
Self {
5253
items: ItemBatch::default(),
54+
marked: Vec::with_capacity(2),
5355
selection: 0,
5456
branch: None,
5557
count_total: 0,
@@ -164,6 +166,17 @@ impl CommitList {
164166
Ok(needs_update)
165167
}
166168

169+
fn mark(&mut self) {
170+
if let Some(e) = self.selected_entry() {
171+
let id = e.id;
172+
if self.is_marked(&id).unwrap_or_default() {
173+
self.marked.retain(|marked| marked != &id);
174+
} else {
175+
self.marked.push(id);
176+
}
177+
}
178+
}
179+
167180
fn update_scroll_speed(&mut self) {
168181
const REPEATED_SCROLL_THRESHOLD_MILLIS: u128 = 300;
169182
const SCROLL_SPEED_START: f32 = 0.1_f32;
@@ -188,21 +201,42 @@ impl CommitList {
188201
self.scroll_state.1 = speed.min(SCROLL_SPEED_MAX);
189202
}
190203

204+
fn is_marked(&self, id: &CommitId) -> Option<bool> {
205+
if self.marked.is_empty() {
206+
None
207+
} else {
208+
let found = self.marked.iter().any(|entry| entry == id);
209+
Some(found)
210+
}
211+
}
212+
191213
fn get_entry_to_add<'a>(
192214
e: &'a LogEntry,
193215
selected: bool,
194216
tags: Option<String>,
195217
theme: &Theme,
196218
width: usize,
197219
now: DateTime<Local>,
220+
marked: Option<bool>,
198221
) -> Spans<'a> {
199222
let mut txt: Vec<Span> = Vec::new();
200-
txt.reserve(ELEMENTS_PER_LINE);
223+
txt.reserve(
224+
ELEMENTS_PER_LINE + if marked.is_some() { 2 } else { 0 },
225+
);
201226

202227
let splitter_txt = Cow::from(" ");
203228
let splitter =
204229
Span::styled(splitter_txt, theme.text(true, selected));
205230

231+
// marked
232+
if let Some(marked) = marked {
233+
txt.push(Span::styled(
234+
Cow::from(if marked { "X" } else { " " }),
235+
theme.text(true, selected),
236+
));
237+
txt.push(splitter.clone());
238+
}
239+
206240
// commit hash
207241
txt.push(Span::styled(
208242
Cow::from(e.hash_short.as_str()),
@@ -258,6 +292,8 @@ impl CommitList {
258292

259293
let now = Local::now();
260294

295+
let any_marked = !self.marked.is_empty();
296+
261297
for (idx, e) in self
262298
.items
263299
.iter()
@@ -270,13 +306,21 @@ impl CommitList {
270306
.as_ref()
271307
.and_then(|t| t.get(&e.id))
272308
.map(|tags| tags.join(" "));
309+
310+
let marked = if any_marked {
311+
self.is_marked(&e.id)
312+
} else {
313+
None
314+
};
315+
273316
txt.push(Self::get_entry_to_add(
274317
e,
275318
idx + self.scroll_top.get() == selection,
276319
tags,
277320
&self.theme,
278321
width,
279322
now,
323+
marked,
280324
));
281325
}
282326

@@ -368,6 +412,9 @@ impl Component for CommitList {
368412
self.move_selection(ScrollType::PageUp)?
369413
} else if k == self.key_config.page_down {
370414
self.move_selection(ScrollType::PageDown)?
415+
} else if k == self.key_config.log_mark_commit {
416+
self.mark();
417+
true
371418
} else {
372419
false
373420
};

src/keys.rs

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub struct KeyConfig {
6767
pub stash_drop: KeyEvent,
6868
pub cmd_bar_toggle: KeyEvent,
6969
pub log_tag_commit: KeyEvent,
70+
pub log_mark_commit: KeyEvent,
7071
pub commit_amend: KeyEvent,
7172
pub copy: KeyEvent,
7273
pub create_branch: KeyEvent,
@@ -135,6 +136,7 @@ impl Default for KeyConfig {
135136
stash_drop: KeyEvent { code: KeyCode::Char('D'), modifiers: KeyModifiers::SHIFT},
136137
cmd_bar_toggle: KeyEvent { code: KeyCode::Char('.'), modifiers: KeyModifiers::empty()},
137138
log_tag_commit: KeyEvent { code: KeyCode::Char('t'), modifiers: KeyModifiers::empty()},
139+
log_mark_commit: KeyEvent { code: KeyCode::Char(' '), modifiers: KeyModifiers::empty()},
138140
commit_amend: KeyEvent { code: KeyCode::Char('a'), modifiers: KeyModifiers::CONTROL},
139141
copy: KeyEvent { code: KeyCode::Char('y'), modifiers: KeyModifiers::empty()},
140142
create_branch: KeyEvent { code: KeyCode::Char('c'), modifiers: KeyModifiers::empty()},

vim_style_key_config.ron

+3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272
stash_drop: ( code: Char('D'), modifiers: ( bits: 1,),),
7373

7474
cmd_bar_toggle: ( code: Char('.'), modifiers: ( bits: 0,),),
75+
7576
log_tag_commit: ( code: Char('t'), modifiers: ( bits: 0,),),
77+
log_mark_commit: ( code: Char(' '), modifiers: ( bits: 0,),),
78+
7679
commit_amend: ( code: Char('a'), modifiers: ( bits: 2,),),
7780
copy: ( code: Char('y'), modifiers: ( bits: 0,),),
7881
create_branch: ( code: Char('c'), modifiers: ( bits: 0,),),

0 commit comments

Comments
 (0)