-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.rs
More file actions
470 lines (448 loc) · 12.1 KB
/
terminal.rs
File metadata and controls
470 lines (448 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crossterm::{
event::{
self, Event,
KeyCode::{self, Char},
KeyEvent, KeyModifiers,
},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use git2::{Oid, Repository};
use std::{
error::Error,
io::{self, Stdout},
path::{Path, PathBuf},
};
use tui::{
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Line, Span, Text},
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
Frame, Terminal,
};
use crate::git;
pub struct App<'a> {
pub blame: Vec<git::BlameHunk<'a>>,
blame_state: ListState,
repo: &'a Repository,
commit_stack: Vec<CommitPath>, // pushed by `b`, popped by `B`
right_panel: Option<Text<'static>>, // activated by `w` or <enter>
line_history_scroll: u16,
popup: Option<Text<'static>>,
search: Option<Search>,
line_number: Option<String>,
}
struct Search {
editing: bool,
query: String,
}
struct CommitPath {
commit: Oid,
path: PathBuf,
}
impl App<'_> {
pub fn new<'a>(repo: &'a Repository, rel_path: &'a Path, commit: Oid) -> App<'a> {
App {
blame: vec![],
blame_state: ListState::default(),
repo,
commit_stack: vec![CommitPath {
commit,
path: rel_path.to_owned(),
}],
right_panel: None,
line_history_scroll: 0,
popup: None,
search: None,
line_number: None,
}
}
}
type CrosstermTerm = Terminal<CrosstermBackend<Stdout>>;
pub fn setup() -> Result<CrosstermTerm, Box<dyn Error>> {
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(stdout);
Ok(Terminal::new(backend)?)
}
pub fn teardown(terminal: &mut CrosstermTerm) {
_ = disable_raw_mode();
_ = execute!(terminal.backend_mut(), LeaveAlternateScreen);
_ = terminal.show_cursor();
}
pub fn run_app(terminal: &mut CrosstermTerm, mut app: App) -> Result<(), Box<dyn Error>> {
loop {
terminal.draw(|frame| ui(frame, &mut app))?;
if let Event::Key(key) = event::read()? {
match handle_input(&key, &mut app, &terminal.size()?) {
Ok(false) => {
return Ok(());
}
Ok(true) => {} // ignored
Err(err) => app.popup = Some(format!("{}", err).into()),
}
}
}
}
// returns whether to continue running the app
fn handle_input(key: &KeyEvent, app: &mut App, term_size: &Rect) -> Result<bool, Box<dyn Error>> {
if app.popup.is_some() {
// clear the popup on any key press
app.popup = None;
return Ok(true);
}
if let Some(search) = &mut app.search {
if search.editing {
match key {
KeyEvent { code: KeyCode::Esc, .. }
| KeyEvent {
code: Char('c'),
modifiers: KeyModifiers::CONTROL,
..
} => {
app.search = None;
}
KeyEvent {
code: Char('u'),
modifiers: KeyModifiers::CONTROL,
..
} => {
search.query.clear();
}
KeyEvent { code: Char(c), .. } => {
search.query.push(*c);
}
KeyEvent {
code: KeyCode::Backspace,
..
} => {
search.query.pop();
}
KeyEvent {
code: KeyCode::Enter, ..
} => {
search.editing = false;
handle_search(&app.blame, &search.query, &mut app.blame_state, true);
}
_ => {} // ignored
}
return Ok(true);
}
} else if let Some(line_number) = &mut app.line_number {
match key {
KeyEvent { code: KeyCode::Esc, .. }
| KeyEvent {
code: Char('c'),
modifiers: KeyModifiers::CONTROL,
..
} => app.line_number = None,
KeyEvent {
code: Char('u'),
modifiers: KeyModifiers::CONTROL,
..
} => {
line_number.clear();
}
KeyEvent { code: Char(c), .. } => {
if '0' <= *c && *c <= '9' {
line_number.push(*c);
}
}
KeyEvent {
code: KeyCode::Backspace,
..
} => {
line_number.pop();
}
KeyEvent {
code: KeyCode::Enter, ..
} => {
if let Ok(index) = line_number.parse::<usize>() {
app.blame_state.select(Some(index.clamp(1, app.blame.len()) - 1));
app.line_number = None;
}
}
_ => {} // ignored
}
return Ok(true);
}
match key {
// scroll
KeyEvent {
code: Char('j') | KeyCode::Down,
..
} => scroll(app, term_size, 1),
KeyEvent {
code: Char('k') | KeyCode::Up,
..
} => scroll(app, term_size, -1),
KeyEvent { code: Char('d'), .. }
| KeyEvent {
code: KeyCode::PageDown,
..
} => scroll(app, term_size, (term_size.height / 2).try_into().unwrap()),
KeyEvent { code: Char('u'), .. }
| KeyEvent {
code: KeyCode::PageUp, ..
} => scroll(app, term_size, -i16::try_from(term_size.height / 2).unwrap()),
KeyEvent { code: Char('g'), .. }
| KeyEvent {
code: KeyCode::Home, ..
} => match &app.right_panel {
Some(_) => app.line_history_scroll = 0,
None => app.blame_state.select(Some(0)),
},
KeyEvent { code: Char('G'), .. } | KeyEvent { code: KeyCode::End, .. } => match &app.right_panel {
Some(line_history) => {
app.line_history_scroll = u16::try_from(line_history.height())
.unwrap()
.saturating_sub(term_size.height)
}
None => app.blame_state.select(Some(app.blame.len() - 1)),
},
KeyEvent { code: Char(':'), .. } => {
app.line_number = Some(String::new());
}
// search
KeyEvent { code: Char('/'), .. } => {
app.search = Some(Search {
editing: true,
query: String::new(),
});
}
KeyEvent { code: Char('n'), .. } => {
if let Some(search) = &app.search {
handle_search(&app.blame, &search.query, &mut app.blame_state, true);
}
}
KeyEvent { code: Char('N'), .. } => {
if let Some(search) = &app.search {
handle_search(&app.blame, &search.query, &mut app.blame_state, false);
}
}
// other interactions
KeyEvent {
code: KeyCode::Enter, ..
} => {
if let Some(index) = app.blame_state.selected() {
let blame = &app.blame[index];
let line_path = match blame.path.as_ref() {
Some(p) => p,
None => &app.commit_stack.last().unwrap().path,
};
app.right_panel = Some(git::show(app.repo, blame.commit, line_path));
}
}
KeyEvent { code: Char('w'), .. } => {
if let Some(index) = app.blame_state.selected() {
let commit_path = app.commit_stack.last().unwrap();
app.right_panel = Some(git::log_follow(app.repo, &commit_path.path, index, commit_path.commit));
}
}
KeyEvent { code: Char('b'), .. } => {
if let Some(index) = app.blame_state.selected() {
let blame = &app.blame[index];
let parent = app.repo.find_commit(blame.commit)?.parent_id(0)?;
let line_path = match blame.path.to_owned() {
Some(p) => p,
None => app.commit_stack.last().unwrap().path.to_owned(),
};
app.blame = git::blame(app.repo, &line_path, parent)?;
app.blame_state.select(Some(index.min(app.blame.len() - 1)));
app.commit_stack.push(CommitPath {
commit: parent,
path: line_path,
});
}
}
KeyEvent { code: Char('B'), .. } => {
if app.commit_stack.len() > 1 {
app.commit_stack.pop();
let commit_path = app.commit_stack.last().unwrap();
app.blame = git::blame(app.repo, &commit_path.path, commit_path.commit)?;
if let Some(index) = app.blame_state.selected() {
app.blame_state.select(Some(index.min(app.blame.len() - 1)));
}
}
}
KeyEvent { code: Char('h'), .. } => app.popup = Some(make_help_text()),
KeyEvent {
code: Char('q') | KeyCode::Esc,
..
} => {
if app.right_panel.is_some() {
app.right_panel = None;
app.line_history_scroll = 0;
} else {
return Ok(false);
}
}
_ => {} // ignored
};
Ok(true)
}
fn scroll(app: &mut App, term_size: &Rect, amount: i16) {
match &app.right_panel {
Some(line_history) => {
let max = u16::try_from(line_history.height())
.unwrap()
.saturating_sub(term_size.height);
app.line_history_scroll = app.line_history_scroll.saturating_add_signed(amount).clamp(0, max);
}
None => {
match app.blame_state.selected() {
Some(index) => {
let new_index = index.saturating_add_signed(amount.into());
app.blame_state.select(Some(new_index.clamp(0, app.blame.len() - 1)));
}
None => {
app.blame_state.select(Some(0));
}
};
}
}
}
fn handle_search(blame: &[git::BlameHunk<'_>], query: &str, blame_state: &mut ListState, forward: bool) {
let range: Box<dyn Iterator<Item = usize>> = if forward {
let start = match blame_state.selected() {
Some(index) => index + 1,
None => 0,
};
Box::new(start..blame.len())
} else {
let end = blame_state.selected().unwrap_or(0);
Box::new((0..end).rev())
};
for i in range {
let line = &blame[i].line.spans.last().unwrap().content;
if line.contains(query) {
blame_state.select(Some(i));
return;
}
}
}
fn make_help_text() -> Text<'static> {
let mut help = vec![
"h this help",
"q esc close window",
"",
" moving",
"",
"j ↓ down one line",
"k ↑ up one line",
"d pgdown down half a window",
"u pgup up half a window",
"G end to last line",
"g home to first line",
":123 to line 123",
"",
" search",
"",
"/ start searching",
"enter search forward",
"n repeat search forward",
"N repeat search backward",
"",
" git",
"",
"enter show commit",
"w trace line through history (git -L)",
"b reblame line at parent commit",
"B undo/pop blame stack",
];
(help.drain(..).map(Line::from).collect::<Vec<_>>()).into()
}
fn ui(frame: &mut Frame, app: &mut App) {
let constraints = if app.right_panel.is_none() {
[Constraint::Percentage(100)].as_ref()
} else {
[Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()
};
let size = Rect::new(
frame.size().x,
frame.size().y,
frame.size().width,
frame.size().height - 1,
);
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints(constraints)
.split(size);
let items: Vec<ListItem> = app.blame.iter().map(|line| ListItem::new(line.line.clone())).collect();
let commit_path = app.commit_stack.last().unwrap();
let title = Line::from(vec![
Span::styled(
commit_path.commit.to_string(),
Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::styled(
commit_path.path.to_str().unwrap(),
Style::default().fg(Color::LightBlue).add_modifier(Modifier::BOLD),
),
]);
let list = List::new(items)
.block(Block::default().title(title))
.highlight_style(Style::default().bg(Color::Indexed(237))); // 232 is black, 255 is white; 237 is dark gray
frame.render_stateful_widget(list, chunks[0], &mut app.blame_state);
if let Some(log) = &app.right_panel {
let paragraph = Paragraph::new(log.clone())
.block(Block::default().borders(Borders::LEFT))
.scroll((app.line_history_scroll, 0));
frame.render_widget(paragraph, chunks[1]);
}
let command = match &app.search {
Some(search) => Some(format!("/{}", search.query.as_str())),
None => app.line_number.as_ref().map(|ln| format!(":{}", ln)),
};
if let Some(cmd_str) = command {
let paragraph = Paragraph::new(cmd_str).wrap(Wrap { trim: false });
let size = Rect::new(
frame.size().x,
frame.size().y + frame.size().height - 1,
frame.size().width,
1,
);
frame.render_widget(paragraph, size);
}
if let Some(popup) = &app.popup {
let paragraph = Paragraph::new(popup.clone()).wrap(Wrap { trim: false });
let area = centered_rect(80, 80, frame.size());
frame.render_widget(Clear, area);
frame.render_widget(Block::default().borders(Borders::all()), area);
frame.render_widget(
paragraph,
area.inner(&tui::layout::Margin {
vertical: 2,
horizontal: 3,
}),
);
}
}
// from https://github.com/tui-rs-revival/ratatui/blob/main/examples/popup.rs
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
]
.as_ref(),
)
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}