diff --git a/src/model/config/mod.rs b/src/model/config/mod.rs index 8ff86dc..a0b77bf 100644 --- a/src/model/config/mod.rs +++ b/src/model/config/mod.rs @@ -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 { 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( diff --git a/src/model/path_node/mod.rs b/src/model/path_node/mod.rs index 5b7e701..fd07898 100644 --- a/src/model/path_node/mod.rs +++ b/src/model/path_node/mod.rs @@ -1,12 +1,14 @@ // 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 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, } -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 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 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::>(); diff --git a/src/utils.rs b/src/utils.rs index f9f0d8f..6d8463a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,19 +1,13 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2019-2022 golmman +// Copyright (c) 2024 Awiteb 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 { - 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 { )) } } + +pub fn parse_gitignore(gitignore_path: impl AsRef) -> Vec { + 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() + } +}