diff --git a/src/app.rs b/src/app.rs index 47384ca4..b89c27b0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,8 +3,7 @@ use crate::{ CommandInfo, CommitComponent, Component, DiffComponent, IndexComponent, }, - git_utils::{self, Diff}, - keys, strings, + git_utils, keys, strings, }; use crossterm::event::Event; use git2::StatusShow; @@ -210,15 +209,19 @@ impl App { DiffTarget::WorkingDir => (&self.index_wd, false), }; - let new_diff = match idx.selection() { - Some(i) => git_utils::get_diff( - Path::new(i.path.as_str()), - is_stage, - ), - None => Diff::default(), - }; + if let Some(i) = idx.selection() { + let path = i.path; - self.diff.update(new_diff); + if self.diff.path() != path { + self.diff.update( + path.clone(), + git_utils::get_diff( + Path::new(path.as_str()), + is_stage, + ), + ); + } + } } fn commands(&self) -> Vec { diff --git a/src/components/diff.rs b/src/components/diff.rs index e4b4e1e9..528f46ba 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -18,6 +18,7 @@ pub struct DiffComponent { diff: Diff, scroll: u16, focused: bool, + current_path: String, } impl DiffComponent { @@ -26,7 +27,12 @@ impl DiffComponent { self.diff.0.len() > 1 } /// - pub fn update(&mut self, diff: Diff) { + pub fn path(&self) -> String { + self.current_path.clone() + } + /// + pub fn update(&mut self, path: String, diff: Diff) { + self.current_path = path; if diff != self.diff { self.diff = diff; self.scroll = 0;