Skip to content

Commit 5350424

Browse files
committed
feat: introduced the pattern matching for ignoring parameters
1 parent c30a6f8 commit 5350424

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/git.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use anyhow::{Context, Result};
22
use chrono::{DateTime, Utc};
33
use git2::{Commit as Git2Commit, Delta, DiffOptions, Oid, Repository};
4+
use globset::{Glob, GlobSet, GlobSetBuilder};
45
use rand::Rng;
56
use std::cell::RefCell;
67
use std::path::Path;
8+
use std::sync::OnceLock;
9+
10+
// Thread-safe global pattern matcher for user-defined ignore patterns
11+
static USER_PATTERNS: OnceLock<GlobSet> = OnceLock::new();
712

813
// Maximum blob size to read (500KB)
914
const MAX_BLOB_SIZE: usize = 500 * 1024;
@@ -66,8 +71,38 @@ const EXCLUDED_PATTERNS: &[&str] = &[
6671
"__snapshots__",
6772
];
6873

74+
/// Initialize user-defined ignore patterns (call once at startup)
75+
pub fn init_ignore_patterns(patterns: &[String]) -> Result<()> {
76+
if patterns.is_empty() {
77+
return Ok(());
78+
}
79+
80+
let mut builder = GlobSetBuilder::new();
81+
82+
for pattern in patterns {
83+
let glob =
84+
Glob::new(pattern).with_context(|| format!("Invalid glob pattern: {}", pattern))?;
85+
builder.add(glob);
86+
}
87+
88+
let globset = builder.build().context("Failed to build glob set")?;
89+
90+
USER_PATTERNS
91+
.set(globset)
92+
.map_err(|_| anyhow::anyhow!("User patterns already initialized"))?;
93+
94+
Ok(())
95+
}
96+
6997
/// Check if a file should be excluded from diff animation
7098
pub fn should_exclude_file(path: &str) -> bool {
99+
// Check user-defined patterns first
100+
if let Some(patterns) = USER_PATTERNS.get() {
101+
if patterns.is_match(path) {
102+
return true;
103+
}
104+
}
105+
71106
let filename = path.rsplit('/').next().unwrap_or(path);
72107

73108
// Check if it's a lock file

src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ pub struct Args {
9292
#[arg(long, help = "Display third-party license information")]
9393
pub license: bool,
9494

95+
#[arg(
96+
short = 'i',
97+
long = "ignore",
98+
value_name = "PATTERN",
99+
action = clap::ArgAction::Append,
100+
help = "Ignore files matching pattern (gitignore syntax, can be specified multiple times)"
101+
)]
102+
pub ignore: Vec<String>,
103+
104+
#[arg(
105+
long = "ignore-file",
106+
value_name = "PATH",
107+
help = "Path to file containing ignore patterns (one per line, like .gitignore)"
108+
)]
109+
pub ignore_file: Option<PathBuf>,
110+
95111
#[command(subcommand)]
96112
pub command: Option<Commands>,
97113
}
@@ -205,6 +221,21 @@ fn main() -> Result<()> {
205221

206222
// Load config: CLI arguments > config file > defaults
207223
let config = Config::load()?;
224+
225+
// Initialize ignore patterns: CLI flags > ignore-file > config
226+
let mut patterns = config.ignore_patterns.clone();
227+
if let Some(path) = &args.ignore_file {
228+
if let Ok(content) = std::fs::read_to_string(path) {
229+
patterns.extend(
230+
content
231+
.lines()
232+
.filter(|l| !l.trim().is_empty() && !l.starts_with('#'))
233+
.map(String::from),
234+
);
235+
}
236+
}
237+
patterns.extend(args.ignore.clone());
238+
git::init_ignore_patterns(&patterns).ok();
208239
let theme_name = args.theme.as_deref().unwrap_or(&config.theme);
209240
let speed = args.speed.unwrap_or(config.speed);
210241
let background = args.background.unwrap_or(config.background);

0 commit comments

Comments
 (0)