use crate::{
Exit,
app::{self, App, Focus, KeyCode, Split, Status},
};
impl App {
fn overview_key(&mut self, code: KeyCode) -> Option<Exit> {
let rows = self.overview_rows();
let at = self.overview?;
let step = |from: usize, down: bool| {
let mut i = from;
loop {
let next = if down { i + 1 } else { i.checked_sub(1)? };
if next >= rows.len() {
return None;
}
i = next;
if rows[i].hunk.is_some() {
return Some(i);
}
}
};
match code {
KeyCode::Char('f' | 'q') | KeyCode::Esc | KeyCode::Enter => self.overview = None,
KeyCode::Char('j') | KeyCode::Down => {
if let Some(i) = step(at, true) {
self.overview = Some(i);
}
}
KeyCode::Char('k') | KeyCode::Up => {
if let Some(i) = step(at, false) {
self.overview = Some(i);
}
}
KeyCode::Char('g') => self.overview = rows.iter().position(|r| r.hunk.is_some()),
KeyCode::Char('G') => self.overview = rows.iter().rposition(|r| r.hunk.is_some()),
KeyCode::Char(c @ '1'..='9') => self.split.as_mut()?.toggle(digit(c)),
KeyCode::Char(' ') => self.split.as_mut()?.toggle(1),
KeyCode::Char('0') => self.split.as_mut()?.assign(None),
KeyCode::Char('a') => {
let commit = self.split.as_ref()?.box_of().unwrap_or(1);
self.split.as_mut()?.assign_file(Some(commit));
}
KeyCode::Char('d') => self.split.as_mut()?.assign_file(None),
KeyCode::Char('A') => self.split.as_mut()?.assign_all(Some(1)),
KeyCode::Char('C') => self.split.as_mut()?.assign_all(None),
_ => return None,
}
if let (Some(at), Some(split)) = (self.overview, self.split.as_mut()) {
if let Some(id) = rows.get(at).and_then(|r| r.hunk) {
split.go_to(id);
}
let hunk = split.at();
self.diff.show_hunk(hunk);
}
None
}
pub(super) fn split_key(&mut self, code: KeyCode, ctrl: bool) -> Option<Exit> {
if self.overview.is_some() {
return self.overview_key(code);
}
if code == KeyCode::Char('f') {
let rows = self.overview_rows();
let at = self.split.as_ref().map_or(0, Split::at);
self.overview = rows
.iter()
.enumerate()
.filter(|(_, r)| r.hunk.is_some())
.nth(at)
.map(|(i, _)| i);
if self.symbols.is_empty() {
self.pending = Some(app::Request::Symbols);
}
return None;
}
match code {
KeyCode::Char('d') if ctrl => {
self.diff.scroll_down(self.diff.half_page());
return None;
}
KeyCode::Char('u') if ctrl => {
self.diff.scroll_up(self.diff.half_page());
return None;
}
KeyCode::Char('j') | KeyCode::Down => {
self.diff.scroll_down(1);
return None;
}
KeyCode::Char('k') | KeyCode::Up => {
self.diff.scroll_up(1);
return None;
}
KeyCode::Esc | KeyCode::Char('q') => {
self.cancel_split();
return None;
}
KeyCode::Enter => {
self.write_message();
return None;
}
_ => {}
}
let split = self.split.as_mut()?;
match code {
KeyCode::Char(c @ '1'..='9') => split.toggle(digit(c)),
KeyCode::Char(' ') => split.toggle(1),
KeyCode::Char('0') => split.assign(None),
KeyCode::Char('y') => {
split.assign(Some(1));
split.next();
}
KeyCode::Char('n' | '}') | KeyCode::Right => split.next(),
KeyCode::Char('p' | '{') | KeyCode::Left => split.prev(),
KeyCode::Char('a') => {
let commit = split.box_of().unwrap_or(1);
split.assign_file(Some(commit));
}
KeyCode::Char('d') => split.assign_file(None),
KeyCode::Char('A') => split.assign_all(Some(1)),
KeyCode::Char('C') => split.assign_all(None),
KeyCode::Char('g') => split.first(),
KeyCode::Char('G') => split.last(),
_ => return None,
}
if let Some(at) = self.split.as_ref().map(Split::at) {
self.diff.show_hunk(at);
}
None
}
pub(super) fn start_split(&mut self) {
match Split::new(self.diff.hunks().iter().map(|h| (h.id, h.path.clone()))) {
Some(split) => self.enter_split(split),
None => {
self.status = Some(Status::Error(
"nothing to split: this change has fewer than two hunks".into(),
));
}
}
}
fn cancel_split(&mut self) {
self.split = None;
self.zoomed = false;
self.focus = Focus::Editor;
self.status = Some(Status::Info("Left split mode.".into()));
}
fn write_message(&mut self) {
let (commit, held, planned) = self.split.as_ref().map_or((0, 0, 0), |split| {
(
split.next_box().unwrap_or(0),
split.next_box().map_or(0, |n| split.count(n)),
split.pending().len(),
)
});
self.zoomed = false;
self.focus = Focus::Editor;
self.status = Some(Status::Info(if planned == 0 {
"Nothing assigned yet. 1-9 puts the hunk on screen in a commit.".to_owned()
} else {
format!(
"Describing commit {commit} of {planned}: {held} {}. ^s commits it.",
app::plural(held, "hunk")
)
}));
}
pub(super) fn immutable_warning(&self) -> Option<Status> {
if !self.target.immutable {
return None;
}
let tags: Vec<&str> = self.at_risk.iter().map(|t| t.tag.as_str()).collect();
Some(Status::Error(if tags.is_empty() {
"immutable revision: rewriting it gives every later commit a new hash".to_owned()
} else {
format!(
"immutable revision: a rewrite strands {} {} ({})",
tags.len(),
app::plural(tags.len(), "tag"),
tags.join(", ")
)
}))
}
}
fn digit(c: char) -> u8 {
u8::try_from(c.to_digit(10).unwrap_or(1)).unwrap_or(1)
}