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 <a@4rs.nl>
This commit is contained in:
Awiteb 2024-12-23 01:16:54 +03:00
parent 373fdab000
commit 9498b9e94e
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -1,12 +1,13 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019-2022 golmman
// Copyright (c) 2024 Awiteb <a@4rs.nl>
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<String>,
}
impl From<&str> for PathNode {
fn from(working_dir: &str) -> Self {
impl<T> From<T> for PathNode
where
T: AsRef<Path>,
{
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<String> 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::<Vec<PathNode>>();