From 9498b9e94e7a07b532096e6c0a4197a15d607847 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Mon, 23 Dec 2024 01:16:54 +0300 Subject: [PATCH] feat: Hide files in `.gitignore` and `.git` folder from path node - Updated path node to exclude files listed in `.gitignore` - Added functionality to hide the `.git` folder Signed-off-by: Awiteb --- src/model/path_node/mod.rs | 58 ++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/model/path_node/mod.rs b/src/model/path_node/mod.rs index 5b7e701..7eadf15 100644 --- a/src/model/path_node/mod.rs +++ b/src/model/path_node/mod.rs @@ -1,12 +1,13 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2019-2022 golmman +// Copyright (c) 2024 Awiteb use crate::model::compare_functions::PathNodeCompare; use crate::model::config::Config; use crate::model::tree_index::TreeIndex; use log::info; -use std::fs::canonicalize; -use std::path::PathBuf; +use std::fs::{self, canonicalize}; +use std::path::{Path, PathBuf}; mod debug; @@ -18,30 +19,36 @@ pub struct PathNode { pub is_err: bool, pub is_expanded: bool, pub path: PathBuf, + pub gitignore: Vec, } -impl From<&str> for PathNode { - fn from(working_dir: &str) -> Self { +impl From for PathNode +where + T: AsRef, +{ + fn from(working_dir: T) -> Self { + let gitignore_path = Path::new(working_dir.as_ref()).join(".gitignore"); + let mut gitignore = if Path::exists(&gitignore_path) { + fs::read_to_string(&gitignore_path) + .map(|content| { + content + .split('\n') + .map(|path| path.trim_matches('/').to_owned()) + .collect() + }) + .unwrap_or_default() + } else { + Vec::new() + }; + gitignore.push(".git".to_owned()); Self { children: Vec::new(), - display_text: String::from(working_dir), + display_text: working_dir.as_ref().display().to_string(), is_dir: true, is_err: false, is_expanded: false, - path: PathBuf::from(working_dir), - } - } -} - -impl From for PathNode { - fn from(working_dir: String) -> Self { - Self { - children: Vec::new(), - display_text: working_dir.clone(), - is_dir: true, - is_err: false, - is_expanded: false, - path: PathBuf::from(working_dir), + path: PathBuf::from(working_dir.as_ref()), + gitignore, } } } @@ -50,7 +57,7 @@ impl PathNode { pub fn new_expanded(config: Config) -> Self { info!("initializing path node"); - let mut path_node = Self::from(config.setup.working_dir.clone()); + let mut path_node = Self::from(&config.setup.working_dir); let path_node_compare = Self::get_path_node_compare(&config); path_node.expand_dir(&TreeIndex::new(), path_node_compare); @@ -72,17 +79,18 @@ impl PathNode { let mut path_nodes = dirs .unwrap() - .map(|dir_entry| { + .filter_map(|dir_entry| { let dir_entry = dir_entry.unwrap(); - - PathNode { + let entry_name = dir_entry.file_name().into_string().unwrap(); + (!self.gitignore.contains(&entry_name)).then(|| PathNode { children: Vec::new(), - display_text: dir_entry.file_name().into_string().unwrap(), + display_text: entry_name, is_dir: dir_entry.path().is_dir(), is_err: false, is_expanded: false, path: dir_entry.path(), - } + gitignore: self.gitignore.clone(), + }) }) .collect::>();