hex_patch/app/files/
path_result.rs

1use std::error::Error;
2
3use ratatui::text::{Line, Span};
4
5use crate::app::settings::color_settings::ColorSettings;
6
7use super::{filesystem::FileSystem, path};
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct PathResult {
11    path: String,
12    is_dir: bool,
13}
14
15impl PathResult {
16    pub fn new(path: &str, filesystem: &FileSystem) -> Result<Self, Box<dyn Error>> {
17        let path = filesystem.canonicalize(path)?;
18        let is_dir = filesystem.is_dir(&path);
19        Ok(Self { path, is_dir })
20    }
21
22    pub fn path(&self) -> &str {
23        &self.path
24    }
25
26    pub fn is_dir(&self) -> bool {
27        self.is_dir
28    }
29
30    pub fn to_line(
31        &self,
32        color_settings: &ColorSettings,
33        is_selected: bool,
34        base_path: &str,
35    ) -> Line<'static> {
36        let mut ret = Line::raw("");
37        let style = if is_selected {
38            color_settings.path_selected
39        } else if self.is_dir() {
40            color_settings.path_dir
41        } else {
42            color_settings.path_file
43        };
44        let path = path::diff(&self.path, base_path);
45        ret.spans.push(Span::styled(path.to_string(), style));
46
47        ret.left_aligned()
48    }
49}