Skip to content

Commit d8ba787

Browse files
committed
feat: add file-specific speed rules with --speed-rule option
Add ability to set different typing speeds for different file patterns. This allows users to slow down for important files and speed up for others. Usage: gitlogue --speed-rule "*.java:50" --speed-rule "*.xml:5" The first matching rule wins. Files not matching any rule use the base speed.
1 parent 8e7173b commit d8ba787

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎src/animation.rs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
use std::cell::RefCell;
22
use std::time::{Duration, Instant};
33

4+
use globset::{Glob, GlobMatcher};
45
use rand::Rng;
56
use unicode_width::UnicodeWidthStr;
67

78
use crate::git::{CommitMetadata, DiffHunk, FileChange, FileStatus, LineChangeType};
89
use crate::syntax::Highlighter;
910

11+
/// A rule that specifies typing speed for files matching a glob pattern
12+
#[derive(Debug, Clone)]
13+
pub struct SpeedRule {
14+
pub matcher: GlobMatcher,
15+
pub speed_ms: u64,
16+
}
17+
18+
impl SpeedRule {
19+
/// Parse a speed rule from string format "PATTERN:SPEED_MS"
20+
/// Example: "*.java:50" or "src/**/*.rs:30"
21+
pub fn parse(s: &str) -> Option<Self> {
22+
let parts: Vec<&str> = s.rsplitn(2, ':').collect();
23+
if parts.len() != 2 {
24+
return None;
25+
}
26+
let speed_ms = parts[0].parse::<u64>().ok()?;
27+
let pattern_str = parts[1];
28+
let glob = Glob::new(pattern_str).ok()?;
29+
let matcher = glob.compile_matcher();
30+
Some(Self { matcher, speed_ms })
31+
}
32+
33+
/// Check if a file path matches this rule
34+
pub fn matches(&self, path: &str) -> bool {
35+
self.matcher.is_match(path)
36+
}
37+
}
38+
1039
// Duration multipliers relative to typing speed
1140
const CURSOR_MOVE_PAUSE: f64 = 0.5; // Cursor movement between lines (base speed)
1241
const CURSOR_MOVE_SHORT_MULTIPLIER: f64 = 1.0; // Speed for short distances (1-50 lines)
@@ -188,6 +217,7 @@ pub struct AnimationEngine {
188217
current_step: usize,
189218
last_update: Instant,
190219
speed_ms: u64,
220+
base_speed_ms: u64,
191221
next_step_delay: u64,
192222
pause_until: Option<Instant>,
193223
pub cursor_visible: bool,
@@ -216,6 +246,8 @@ pub struct AnimationEngine {
216246
current_metadata: Option<CommitMetadata>,
217247
/// Pending metadata to be applied on ResetState
218248
pending_metadata: Option<CommitMetadata>,
249+
/// Speed rules for different file patterns
250+
speed_rules: Vec<SpeedRule>,
219251
}
220252

221253
impl AnimationEngine {
@@ -230,6 +262,7 @@ impl AnimationEngine {
230262
current_step: 0,
231263
last_update: now,
232264
speed_ms,
265+
base_speed_ms: speed_ms,
233266
next_step_delay: speed_ms,
234267
pause_until: None,
235268
cursor_visible: true,
@@ -249,7 +282,24 @@ impl AnimationEngine {
249282
dialog_typing_text: String::new(),
250283
current_metadata: None,
251284
pending_metadata: None,
285+
speed_rules: Vec::new(),
286+
}
287+
}
288+
289+
/// Set speed rules for file-specific typing speeds
290+
pub fn set_speed_rules(&mut self, rules: Vec<SpeedRule>) {
291+
self.speed_rules = rules;
292+
}
293+
294+
/// Get the speed for a given file path based on speed rules
295+
/// Returns the first matching rule's speed, or the base speed if no match
296+
fn get_speed_for_file(&self, path: &str) -> u64 {
297+
for rule in &self.speed_rules {
298+
if rule.matches(path) {
299+
return rule.speed_ms;
300+
}
252301
}
302+
self.base_speed_ms
253303
}
254304

255305
pub fn set_viewport_height(&mut self, height: usize) {
@@ -917,6 +967,9 @@ impl AnimationEngine {
917967
self.current_file_path = Some(path.clone());
918968
self.buffer = EditorBuffer::from_content(&old_content);
919969

970+
// Update typing speed based on file-specific rules
971+
self.speed_ms = self.get_speed_for_file(&path);
972+
920973
// Update syntax highlighter for new file
921974
// This will clear language settings if not supported
922975
self.highlighter.borrow_mut().set_language_from_path(&path);

‎src/main.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod ui;
88
mod widgets;
99

1010
use anyhow::{Context, Result};
11+
use animation::SpeedRule;
1112
use clap::{Parser, Subcommand, ValueEnum};
1213
use config::Config;
1314
use git::GitRepository;
@@ -135,6 +136,14 @@ pub struct Args {
135136
)]
136137
pub ignore_file: Option<PathBuf>,
137138

139+
#[arg(
140+
long = "speed-rule",
141+
value_name = "PATTERN:MS",
142+
action = clap::ArgAction::Append,
143+
help = "Set typing speed for files matching pattern (e.g., '*.java:50', '*.xml:5'). Can be specified multiple times."
144+
)]
145+
pub speed_rule: Vec<String>,
146+
138147
#[command(subcommand)]
139148
pub command: Option<Commands>,
140149
}
@@ -323,6 +332,18 @@ fn main() -> Result<()> {
323332
}
324333
};
325334

335+
// Parse speed rules from CLI arguments
336+
let speed_rules: Vec<SpeedRule> = args
337+
.speed_rule
338+
.iter()
339+
.filter_map(|s| {
340+
SpeedRule::parse(s).or_else(|| {
341+
eprintln!("Warning: Invalid speed rule '{}', skipping", s);
342+
None
343+
})
344+
})
345+
.collect();
346+
326347
// Create UI with repository reference
327348
// Filtered modes (range/author/date) always need repo ref for iteration
328349
let repo_ref = if is_range_mode || is_filtered {
@@ -340,6 +361,7 @@ fn main() -> Result<()> {
340361
loop_playback,
341362
args.commit.clone(),
342363
is_range_mode,
364+
speed_rules,
343365
);
344366
ui.load_commit(metadata);
345367
ui.run()?;

‎src/ui.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ratatui::{
1919
};
2020
use unicode_width::UnicodeWidthStr;
2121

22-
use crate::animation::AnimationEngine;
22+
use crate::animation::{AnimationEngine, SpeedRule};
2323
use crate::git::{CommitMetadata, GitRepository};
2424
use crate::panes::{EditorPane, FileTreePane, StatusBarPane, TerminalPane};
2525
use crate::theme::Theme;
@@ -58,18 +58,22 @@ impl<'a> UI<'a> {
5858
loop_playback: bool,
5959
commit_spec: Option<String>,
6060
is_range_mode: bool,
61+
speed_rules: Vec<SpeedRule>,
6162
) -> Self {
6263
let should_exit = Arc::new(AtomicBool::new(false));
6364
Self::setup_signal_handler(should_exit.clone());
6465

66+
let mut engine = AnimationEngine::new(speed_ms);
67+
engine.set_speed_rules(speed_rules);
68+
6569
Self {
6670
state: UIState::Playing,
6771
speed_ms,
6872
file_tree: FileTreePane::new(),
6973
editor: EditorPane,
7074
terminal: TerminalPane,
7175
status_bar: StatusBarPane,
72-
engine: AnimationEngine::new(speed_ms),
76+
engine,
7377
repo,
7478
should_exit,
7579
theme,

0 commit comments

Comments
 (0)