ratatui_toolkit/widgets/code_diff/widget/methods/select_next_file.rs
1//! Method to select the next file in the sidebar.
2
3use crate::widgets::code_diff::code_diff::CodeDiff;
4
5impl CodeDiff {
6 /// Selects the next file in the sidebar file tree.
7 ///
8 /// This also updates the current diff hunks to show the selected file.
9 /// If already at the last file, stays at the last file.
10 ///
11 /// # Example
12 ///
13 /// ```rust
14 /// use ratatui_toolkit::code_diff::{CodeDiff, DiffConfig};
15 /// use ratatui_toolkit::widgets::code_diff::diff_file_tree::FileStatus;
16 ///
17 /// let mut diff = CodeDiff::new()
18 /// .with_config(DiffConfig::new().sidebar_enabled(true))
19 /// .with_file("file1.rs", FileStatus::Modified, "")
20 /// .with_file("file2.rs", FileStatus::Added, "");
21 ///
22 /// diff.select_next_file();
23 /// ```
24 pub fn select_next_file(&mut self) {
25 self.file_tree.select_next();
26 self.update_current_diff();
27 }
28
29 /// Updates the current diff hunks based on the selected file in the tree.
30 fn update_current_diff(&mut self) {
31 if let Some(path) = self.file_tree.selected_path() {
32 if let Some(hunks) = self.file_diffs.get(&path) {
33 self.file_path = Some(path);
34 self.hunks = hunks.clone();
35 self.scroll_offset = 0; // Reset scroll when changing files
36 }
37 }
38 }
39}