ratatui_toolkit/widgets/code_diff/widget/methods/
refresh.rs

1//! Refresh diff from git.
2
3use crate::widgets::code_diff::helpers::get_git_diff;
4
5use super::super::CodeDiff;
6
7impl CodeDiff {
8    /// Refresh the diff from the current git repository.
9    ///
10    /// This re-fetches the git diff and updates the widget state while
11    /// preserving the current configuration and sidebar visibility.
12    ///
13    /// # Example
14    ///
15    /// ```rust,no_run
16    /// use ratatui_toolkit::code_diff::CodeDiff;
17    ///
18    /// let mut diff = CodeDiff::from_git();
19    ///
20    /// // User makes changes to files...
21    ///
22    /// // Refresh to pick up new changes
23    /// diff.refresh();
24    /// ```
25    pub fn refresh(&mut self) {
26        let diff = get_git_diff();
27        let new_diff = if diff.trim().is_empty() {
28            CodeDiff::new()
29        } else {
30            CodeDiff::from_multi_file_diff(&diff)
31        };
32
33        // Preserve config and some state
34        let config = self.config.clone();
35        let show_sidebar = self.show_sidebar;
36
37        *self = new_diff;
38        self.config = config;
39        self.show_sidebar = show_sidebar;
40    }
41}