murmur-core 0.1.3

Core transcription engine for murmur — audio capture, Whisper transcription, VAD, and context abstractions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Auto-detect programming language and generate prompt context from window titles.
//!
//! Editors typically include the filename in the window title, e.g.:
//! - `"auth.rs — Visual Studio Code"`
//! - `"main.py - PyCharm"`
//! - `"App.tsx - WebStorm"`
//! - `"vim ~/.config/fish/config.fish"`
//!
//! This module extracts the filename, maps its extension to a programming language,
//! and generates a descriptive prompt prefix for Whisper biasing — all without
//! any user configuration.

use super::provider::DictationMode;

/// Result of analyzing a window title.
#[derive(Debug, Clone, PartialEq)]
pub struct TitleContext {
    /// Detected programming language (e.g. "Rust", "Python")
    pub language: Option<String>,
    /// File extension that was matched (e.g. "rs", "py")
    pub extension: Option<String>,
    /// Extracted filename from the title (e.g. "auth.rs")
    pub filename: Option<String>,
    /// Auto-generated prompt prefix for Whisper (e.g. "Rust programming.")
    pub prompt_prefix: Option<String>,
    /// Suggested dictation mode based on detected context
    pub suggested_mode: Option<DictationMode>,
}

/// Known file extension → (language name, prompt prefix, dictation mode) mappings.
const EXTENSION_MAP: &[(&str, &str, &str)] = &[
    // Systems
    ("rs", "Rust", "Rust programming"),
    ("go", "Go", "Go programming"),
    ("c", "C", "C programming"),
    ("h", "C", "C programming"),
    ("cpp", "C++", "C++ programming"),
    ("cc", "C++", "C++ programming"),
    ("cxx", "C++", "C++ programming"),
    ("hpp", "C++", "C++ programming"),
    ("zig", "Zig", "Zig programming"),
    // JVM
    ("java", "Java", "Java programming"),
    ("kt", "Kotlin", "Kotlin programming"),
    ("kts", "Kotlin", "Kotlin programming"),
    ("scala", "Scala", "Scala programming"),
    ("groovy", "Groovy", "Groovy programming"),
    // .NET
    ("cs", "C#", "C# programming"),
    ("fs", "F#", "F# programming"),
    ("vb", "Visual Basic", "Visual Basic programming"),
    // Web / JS
    ("js", "JavaScript", "JavaScript programming"),
    ("jsx", "JavaScript React", "JavaScript React programming"),
    ("ts", "TypeScript", "TypeScript programming"),
    ("tsx", "TypeScript React", "TypeScript React programming"),
    ("mjs", "JavaScript", "JavaScript programming"),
    ("cjs", "JavaScript", "JavaScript programming"),
    // Python
    ("py", "Python", "Python programming"),
    ("pyi", "Python", "Python programming"),
    ("pyx", "Cython", "Cython programming"),
    // Ruby
    ("rb", "Ruby", "Ruby programming"),
    ("erb", "Ruby", "Ruby template"),
    // PHP
    ("php", "PHP", "PHP programming"),
    // Swift / Obj-C
    ("swift", "Swift", "Swift programming"),
    ("m", "Objective-C", "Objective-C programming"),
    ("mm", "Objective-C++", "Objective-C++ programming"),
    // Shell
    ("sh", "Shell", "Shell scripting"),
    ("bash", "Bash", "Bash scripting"),
    ("zsh", "Zsh", "Zsh scripting"),
    ("fish", "Fish", "Fish shell scripting"),
    ("ps1", "PowerShell", "PowerShell scripting"),
    // Config / Data
    ("json", "JSON", "JSON configuration"),
    ("yaml", "YAML", "YAML configuration"),
    ("yml", "YAML", "YAML configuration"),
    ("toml", "TOML", "TOML configuration"),
    ("xml", "XML", "XML markup"),
    ("ini", "INI", "INI configuration"),
    // Markup / Docs
    ("md", "Markdown", "Markdown documentation"),
    ("mdx", "MDX", "MDX documentation"),
    ("rst", "reStructuredText", "reStructuredText documentation"),
    ("tex", "LaTeX", "LaTeX document"),
    ("html", "HTML", "HTML markup"),
    ("htm", "HTML", "HTML markup"),
    ("css", "CSS", "CSS styling"),
    ("scss", "SCSS", "SCSS styling"),
    ("sass", "Sass", "Sass styling"),
    ("less", "Less", "Less styling"),
    // Functional
    ("hs", "Haskell", "Haskell programming"),
    ("ml", "OCaml", "OCaml programming"),
    ("mli", "OCaml", "OCaml programming"),
    ("ex", "Elixir", "Elixir programming"),
    ("exs", "Elixir", "Elixir programming"),
    ("erl", "Erlang", "Erlang programming"),
    ("clj", "Clojure", "Clojure programming"),
    ("lisp", "Lisp", "Lisp programming"),
    ("el", "Emacs Lisp", "Emacs Lisp programming"),
    // Data / Query
    ("sql", "SQL", "SQL database queries"),
    ("graphql", "GraphQL", "GraphQL queries"),
    ("gql", "GraphQL", "GraphQL queries"),
    ("proto", "Protocol Buffers", "Protocol Buffers definition"),
    // DevOps / Infra
    ("tf", "Terraform", "Terraform infrastructure"),
    ("hcl", "HCL", "HCL configuration"),
    ("dockerfile", "Dockerfile", "Docker configuration"),
    ("nix", "Nix", "Nix configuration"),
    // Misc
    ("r", "R", "R programming"),
    ("jl", "Julia", "Julia programming"),
    ("lua", "Lua", "Lua programming"),
    ("dart", "Dart", "Dart programming"),
    ("v", "V", "V programming"),
    ("nim", "Nim", "Nim programming"),
    ("cr", "Crystal", "Crystal programming"),
];

/// Terminal app bundle IDs — suggest command mode for these.
const TERMINAL_APP_IDS: &[&str] = &[
    "com.googlecode.iterm2",
    "com.apple.Terminal",
    "org.alacritty",
    "io.warp.warpterm",
    "net.kovidgoyal.kitty",
    "com.github.wez.wezterm",
];

/// Analyze a window title to extract language and context information.
pub fn analyze_title(title: &str) -> TitleContext {
    let filename = extract_filename(title);

    if let Some(ref name) = filename {
        if let Some((ext, lang, prefix)) = lookup_extension(name) {
            let mode = if is_doc_extension(ext) {
                Some(DictationMode::Prose)
            } else {
                Some(DictationMode::Code)
            };
            return TitleContext {
                language: Some(lang.to_string()),
                extension: Some(ext.to_string()),
                filename: Some(name.clone()),
                prompt_prefix: Some(format!("{prefix}.")),
                suggested_mode: mode,
            };
        }
    }

    TitleContext {
        language: None,
        extension: None,
        filename,
        prompt_prefix: None,
        suggested_mode: None,
    }
}

/// Check if an app is a terminal emulator.
pub fn is_terminal_app(app_id: &str) -> bool {
    TERMINAL_APP_IDS.contains(&app_id)
}

/// Extract a filename from a window title.
///
/// Handles common editor title formats:
/// - `"filename.ext — App Name"` (VS Code, em dash)
/// - `"filename.ext - App Name"` (JetBrains, hyphen)
/// - `"filename.ext — Edited — App Name"` (multiple separators)
/// - `"~/path/to/filename.ext"` (terminals)
/// - `"App Name — filename.ext"` (some editors put filename last)
fn extract_filename(title: &str) -> Option<String> {
    let title = title.trim();
    if title.is_empty() {
        return None;
    }

    // Split on common title separators (em dash, en dash, hyphen with spaces)
    let segments: Vec<&str> = title
        .split(&['\u{2014}', '\u{2013}'][..]) // em dash, en dash
        .flat_map(|s| s.split(" - "))
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .collect();

    // Check each segment for something that looks like a filename
    for segment in &segments {
        if let Some(name) = try_extract_filename_from_segment(segment) {
            return Some(name);
        }
    }

    // If no segment matched, try the whole title (e.g. terminal showing a path)
    try_extract_filename_from_segment(title)
}

/// Try to find a filename with a known extension in a title segment.
fn try_extract_filename_from_segment(segment: &str) -> Option<String> {
    // Handle paths: take the last path component (supports / and \)
    let candidate = segment
        .rsplit(&['/', '\\'][..])
        .next()
        .unwrap_or(segment)
        .trim();

    // Strip common prefixes/suffixes editors add
    let candidate = candidate
        .trim_start_matches("") // VS Code modified indicator
        .trim_start_matches("")
        .trim_start_matches("* ")
        .trim_end_matches(" [Modified]")
        .trim_end_matches(" [+]")
        .trim_end_matches("")
        .trim();

    // Must contain a dot for an extension
    if candidate.rfind('.').is_some() {
        // Only accept files with known programming/config extensions
        // to avoid false matches like "report.pdf" or "Mr. Smith"
        if lookup_extension(candidate).is_some() {
            return Some(candidate.to_string());
        }
    }

    // Handle extension-less known filenames
    let lower = candidate.to_lowercase();
    if matches!(
        lower.as_str(),
        "dockerfile" | "makefile" | "justfile" | "rakefile" | "gemfile" | "cmakelists.txt"
    ) {
        return Some(candidate.to_string());
    }

    None
}

/// Look up a filename's extension in the known language map.
fn lookup_extension(filename: &str) -> Option<(&'static str, &'static str, &'static str)> {
    // Handle "Dockerfile" and similar extensionless files
    let lower = filename.to_lowercase();
    if lower == "dockerfile" {
        return Some(("dockerfile", "Dockerfile", "Docker configuration"));
    }
    if lower == "makefile" || lower == "justfile" {
        return Some(("makefile", "Make", "Build system configuration"));
    }

    let ext = filename.rsplit('.').next()?.to_lowercase();
    EXTENSION_MAP
        .iter()
        .find(|(e, _, _)| *e == ext.as_str())
        .map(|&(e, l, p)| (e, l, p))
}

/// Check if an extension is for documentation/prose (should use prose mode).
fn is_doc_extension(ext: &str) -> bool {
    matches!(ext, "md" | "mdx" | "rst" | "tex" | "txt")
}

#[cfg(test)]
mod tests {
    use super::*;

    // -- analyze_title --

    #[test]
    fn test_analyze_rust_file() {
        let ctx = analyze_title("auth.rs — Visual Studio Code");
        assert_eq!(ctx.language.as_deref(), Some("Rust"));
        assert_eq!(ctx.extension.as_deref(), Some("rs"));
        assert_eq!(ctx.filename.as_deref(), Some("auth.rs"));
        assert!(ctx.prompt_prefix.unwrap().contains("Rust"));
        assert_eq!(ctx.suggested_mode, Some(DictationMode::Code));
    }

    #[test]
    fn test_analyze_python_file() {
        let ctx = analyze_title("main.py - PyCharm");
        assert_eq!(ctx.language.as_deref(), Some("Python"));
        assert_eq!(ctx.extension.as_deref(), Some("py"));
        assert_eq!(ctx.filename.as_deref(), Some("main.py"));
        assert_eq!(ctx.suggested_mode, Some(DictationMode::Code));
    }

    #[test]
    fn test_analyze_typescript_react() {
        let ctx = analyze_title("App.tsx — WebStorm");
        assert_eq!(ctx.language.as_deref(), Some("TypeScript React"));
        assert_eq!(ctx.extension.as_deref(), Some("tsx"));
    }

    #[test]
    fn test_analyze_markdown_gets_prose_mode() {
        let ctx = analyze_title("README.md — Visual Studio Code");
        assert_eq!(ctx.language.as_deref(), Some("Markdown"));
        assert_eq!(ctx.suggested_mode, Some(DictationMode::Prose));
    }

    #[test]
    fn test_analyze_no_filename() {
        let ctx = analyze_title("Google Chrome");
        assert!(ctx.language.is_none());
        assert!(ctx.prompt_prefix.is_none());
        assert!(ctx.suggested_mode.is_none());
    }

    #[test]
    fn test_analyze_empty_title() {
        let ctx = analyze_title("");
        assert!(ctx.language.is_none());
        assert!(ctx.filename.is_none());
    }

    #[test]
    fn test_analyze_path_in_title() {
        let ctx = analyze_title("~/src/murmur/src/main.rs");
        assert_eq!(ctx.language.as_deref(), Some("Rust"));
        assert_eq!(ctx.filename.as_deref(), Some("main.rs"));
    }

    #[test]
    fn test_analyze_modified_indicator() {
        let ctx = analyze_title("● config.toml — Visual Studio Code");
        assert_eq!(ctx.language.as_deref(), Some("TOML"));
        assert_eq!(ctx.filename.as_deref(), Some("config.toml"));
    }

    #[test]
    fn test_analyze_dockerfile() {
        let ctx = analyze_title("Dockerfile — Visual Studio Code");
        assert_eq!(ctx.language.as_deref(), Some("Dockerfile"));
        assert_eq!(ctx.filename.as_deref(), Some("Dockerfile"));
    }

    #[test]
    fn test_analyze_multiple_separators() {
        let ctx = analyze_title("lib.rs — myproject — Visual Studio Code");
        assert_eq!(ctx.language.as_deref(), Some("Rust"));
        assert_eq!(ctx.filename.as_deref(), Some("lib.rs"));
    }

    #[test]
    fn test_analyze_go_file() {
        let ctx = analyze_title("handler.go — GoLand");
        assert_eq!(ctx.language.as_deref(), Some("Go"));
        assert_eq!(ctx.suggested_mode, Some(DictationMode::Code));
    }

    #[test]
    fn test_analyze_sql_file() {
        let ctx = analyze_title("schema.sql — DataGrip");
        assert_eq!(ctx.language.as_deref(), Some("SQL"));
        assert!(ctx.prompt_prefix.unwrap().contains("SQL"));
    }

    #[test]
    fn test_analyze_shell_script() {
        let ctx = analyze_title("deploy.sh — Terminal");
        assert_eq!(ctx.language.as_deref(), Some("Shell"));
    }

    // -- extract_filename --

    #[test]
    fn test_extract_filename_em_dash() {
        assert_eq!(
            extract_filename("file.rs — App"),
            Some("file.rs".to_string())
        );
    }

    #[test]
    fn test_extract_filename_hyphen() {
        assert_eq!(
            extract_filename("file.py - App"),
            Some("file.py".to_string())
        );
    }

    #[test]
    fn test_extract_filename_path() {
        assert_eq!(
            extract_filename("/Users/me/src/main.rs"),
            Some("main.rs".to_string())
        );
    }

    #[test]
    fn test_extract_filename_windows_path() {
        assert_eq!(
            extract_filename("C:\\Users\\me\\src\\main.rs"),
            Some("main.rs".to_string())
        );
    }

    #[test]
    fn test_extract_filename_none_for_no_extension() {
        assert!(extract_filename("Google Chrome").is_none());
    }

    #[test]
    fn test_extract_filename_ignores_unknown_extensions() {
        // Should not match non-programming extensions
        assert!(extract_filename("report.pdf - Preview").is_none());
        assert!(extract_filename("photo.jpg — Photos").is_none());
        assert!(extract_filename("document.docx - Word").is_none());
    }

    #[test]
    fn test_analyze_non_editor_app() {
        // Browser tabs, email, etc. should not produce false matches
        let ctx = analyze_title("GitHub - Pull Request #18 - Google Chrome");
        assert!(ctx.language.is_none());
        assert!(ctx.suggested_mode.is_none());
    }

    // -- is_terminal_app --

    #[test]
    fn test_is_terminal_app() {
        assert!(is_terminal_app("com.apple.Terminal"));
        assert!(is_terminal_app("com.googlecode.iterm2"));
        assert!(!is_terminal_app("com.microsoft.VSCode"));
    }

    // -- lookup_extension --

    #[test]
    fn test_lookup_known_extensions() {
        assert!(lookup_extension("file.rs").is_some());
        assert!(lookup_extension("file.py").is_some());
        assert!(lookup_extension("file.tsx").is_some());
        assert!(lookup_extension("file.go").is_some());
    }

    #[test]
    fn test_lookup_case_insensitive() {
        assert!(lookup_extension("FILE.RS").is_some());
        assert!(lookup_extension("Main.PY").is_some());
    }

    #[test]
    fn test_lookup_unknown_extension() {
        assert!(lookup_extension("file.xyz123").is_none());
    }

    // -- is_doc_extension --

    #[test]
    fn test_is_doc_extension() {
        assert!(is_doc_extension("md"));
        assert!(is_doc_extension("rst"));
        assert!(is_doc_extension("tex"));
        assert!(!is_doc_extension("rs"));
        assert!(!is_doc_extension("py"));
    }
}