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 ffafb7de6a
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
3 changed files with 42 additions and 46 deletions

View file

@ -10,10 +10,10 @@ use crate::model::config::keybinding::Keybinding;
use crate::model::config::setup::Setup;
use crate::utils::get_config_dir;
use crate::utils::print_help;
use crate::utils::read_file;
use log::{info, warn};
use serde::Deserialize;
use std::env::args;
use std::fs;
use std::process::exit;
mod behavior;
@ -119,10 +119,8 @@ impl Config {
fn read_config_file() -> std::io::Result<Self> {
let config_dir = get_config_dir()?;
let config_file = format!("{}/twilight-commander.toml", config_dir);
let config_file_content = read_file(&config_file)?;
let config_file_content = fs::read_to_string(&config_file)?;
toml::from_str(&config_file_content).map_err(|_| {
std::io::Error::new(

View file

@ -1,12 +1,14 @@
// 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 crate::utils;
use log::info;
use std::fs::canonicalize;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
mod debug;
@ -18,30 +20,25 @@ 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 mut gitignore =
utils::parse_gitignore(Path::new(working_dir.as_ref()).join(".gitignore"));
gitignore.push(working_dir.as_ref().join(".git").display().to_string());
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 +47,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 +69,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>>();

View file

@ -1,19 +1,13 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019-2022 golmman
// Copyright (c) 2024 Awiteb <a@4rs.nl>
use log::info;
use std::fs::File;
use std::io::Read;
use std::fs;
use std::panic::set_hook;
use std::path::Path;
use std::process::exit;
pub fn read_file(file_name: &str) -> std::io::Result<String> {
let mut file = File::open(file_name)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
pub fn print_help() {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
println!(r#"usage: twilight-tree [--key1=value1 --key2=value2 ...]"#);
@ -51,15 +45,6 @@ pub fn setup_logger() -> Result<(), fern::InitError> {
}
}));
info!(
r#"starting...
_|_ o|o _ |__|_ _ _ ._ _ ._ _ _.._ _| _ ._
|_\/\/|||(_|| ||_ (_(_)| | || | |(_|| |(_|(/_|
_|
"#
);
info!("logger initialized");
Ok(())
@ -77,3 +62,18 @@ pub fn get_config_dir() -> std::io::Result<String> {
))
}
}
pub fn parse_gitignore(gitignore_path: impl AsRef<Path>) -> Vec<String> {
if Path::exists(gitignore_path.as_ref()) {
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()
}
}