rosetree 0.2.1

A fast command-line tool for scanning directories, analyzing file structures, and extracting file contents with gitignore support
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fs;
use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;

use chrono::Local;
use content_inspector::inspect;
use dashmap::DashMap;
use ignore::WalkBuilder;
use rayon::prelude::*;

#[derive(Clone)]
struct FileInfo {
    path: PathBuf,
    relative_path: String,
    extension: String,
}

#[derive(Clone, Debug)]
struct GitIgnoreInfo {
    relative_path: String,
}

struct Timings {
    find_gitignore: u128,
    collect_files: u128,
    read_contents: u128,
    generate_tree: u128,
    generate_output_string: u128,
    write_file: u128,
    total: u128,
}

impl Timings {
    fn new() -> Self {
        Timings {
            find_gitignore: 0,
            collect_files: 0,
            read_contents: 0,
            generate_tree: 0,
            generate_output_string: 0,
            write_file: 0,
            total: 0,
        }
    }
}

#[allow(clippy::too_many_lines)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut timings = Timings::new();

    println!("Scanning current directory and subdirectories...");

    let current_dir =
        std::env::current_dir().map_err(|e| format!("Unable to get current directory: {e}"))?;

    let stage_start_time = Instant::now();
    let gitignore_files = find_gitignore_files(&current_dir);
    timings.find_gitignore = stage_start_time.elapsed().as_micros();

    let use_gitignore = if gitignore_files.is_empty() {
        false
    } else {
        println!("\nFound the following .gitignore files:");
        for info in &gitignore_files {
            println!("  - {}", info.relative_path);
        }

        println!("\nApply .gitignore rules? (y/n):");
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        input.trim().to_lowercase() == "y"
    };

    let stage_start_time = Instant::now();
    let files = if use_gitignore {
        collect_files_with_gitignore(&current_dir)
    } else {
        collect_files_without_gitignore(&current_dir)
    };
    timings.collect_files = stage_start_time.elapsed().as_micros();

    if files.is_empty() {
        println!("No UTF-8 readable files found.");
        timings.total = timings.find_gitignore + timings.collect_files;
        print_timings(&timings);
        return Ok(());
    }

    let extensions_set: HashSet<String> = files.iter().map(|f| f.extension.clone()).collect();
    let mut extensions_vec: Vec<String> = extensions_set.into_iter().collect();
    extensions_vec.sort();

    println!("\nFound the following UTF-8 file types:");
    for (i, ext) in extensions_vec.iter().enumerate() {
        println!(
            "{}. {}",
            i + 1,
            if ext.is_empty() { "no extension" } else { ext }
        );
    }

    println!("\nEnter file type numbers to extract (space-separated, 'a' for all types):");
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .map_err(|e| format!("Failed to read input: {e}"))?;

    let selected_extensions: HashSet<String> = if input.trim().to_lowercase() == "a" {
        extensions_vec.iter().cloned().collect()
    } else {
        input
            .split_whitespace()
            .filter_map(|s| s.parse::<usize>().ok())
            .filter_map(|i| extensions_vec.get(i.saturating_sub(1)).cloned())
            .collect()
    };

    if selected_extensions.is_empty() {
        println!("No file types selected.");
        timings.total = timings.find_gitignore + timings.collect_files;
        print_timings(&timings);
        return Ok(());
    }

    let selected_files: Vec<FileInfo> = files
        .into_par_iter()
        .filter(|f| selected_extensions.contains(&f.extension))
        .collect();

    if selected_files.is_empty() {
        println!("No matching files found.");
        timings.total = timings.find_gitignore + timings.collect_files;
        print_timings(&timings);
        return Ok(());
    }

    // Pre-sort files by path to avoid later sorting
    let mut sorted_files = selected_files;
    sorted_files.sort_by(|a, b| a.relative_path.cmp(&b.relative_path));

    if sorted_files.is_empty() {
        println!("No matching files found.");
        timings.total = timings.find_gitignore + timings.collect_files;
        print_timings(&timings);
        return Ok(());
    }

    // Generate tree structure (for display only)
    let stage_start_time = Instant::now();
    let tree_structure = generate_tree_structure_from_files(&sorted_files);
    timings.generate_tree = stage_start_time.elapsed().as_micros();

    // Create output file
    let current_time = Local::now();
    let timestamp_str = current_time.format("%Y%m%d_%H%M%S").to_string();
    let filename = format!("rosetree_{timestamp_str}.md");

    // Use streaming processing: read and write simultaneously
    let stage_start_time = Instant::now();
    write_files_streaming(&sorted_files, &tree_structure, &filename, &mut timings)?;
    timings.write_file = stage_start_time.elapsed().as_micros();

    println!("\nFile contents successfully extracted to: {filename}");

    timings.total = timings.find_gitignore
        + timings.collect_files
        + timings.read_contents
        + timings.generate_tree
        + timings.generate_output_string
        + timings.write_file;

    print_timings(&timings);

    Ok(())
}

fn print_timings(timings: &Timings) {
    println!("\nProgram Operation Execution Times (µs):");
    println!("-------------------------------------------");
    println!("Find .gitignore files:     {:>10}", timings.find_gitignore);
    println!("Collect files:             {:>10}", timings.collect_files);
    println!("Read selected contents:    {:>10}", timings.read_contents);
    println!("Generate tree structure:   {:>10}", timings.generate_tree);
    println!(
        "Generate output string:    {:>10}",
        timings.generate_output_string
    );
    println!("Write to file:             {:>10}", timings.write_file);
    println!("-------------------------------------------");
    println!("Total processing time:     {:>10} µs", timings.total);
    println!(
        "                           {:>10} ms (approx total)",
        timings.total / 1000
    );
    println!("-------------------------------------------");
}

fn find_gitignore_files(base_dir: &Path) -> Vec<GitIgnoreInfo> {
    let mut gitignore_files = Vec::new();
    let walker = WalkBuilder::new(base_dir)
        .standard_filters(false)
        .hidden(false)
        .parents(false)
        .ignore(false)
        .git_global(false)
        .git_exclude(false)
        .git_ignore(false)
        .filter_entry(|e| e.file_name() != std::ffi::OsStr::new(".git"))
        .build();

    for result in walker {
        match result {
            Ok(entry) => {
                if entry.file_type().is_some_and(|ft| ft.is_file())
                    && entry.file_name() == std::ffi::OsStr::new(".gitignore")
                    && !entry.path_is_symlink()
                {
                    let path = entry.path();
                    let relative_path = path
                        .strip_prefix(base_dir)
                        .unwrap_or(path)
                        .to_string_lossy()
                        .replace('\\', "/");
                    gitignore_files.push(GitIgnoreInfo { relative_path });
                }
            }
            Err(err) => {
                eprintln!("Warning: Error finding .gitignore files: {err}");
            }
        }
    }
    gitignore_files
}

fn collect_files_with_gitignore(base_dir: &Path) -> Vec<FileInfo> {
    let mut files = Vec::new();
    let walker = WalkBuilder::new(base_dir)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .parents(true)
        .ignore(true)
        .hidden(false)
        .follow_links(false)
        .build();

    for result in walker {
        match result {
            Ok(entry) => {
                let path = entry.path();
                if path.is_dir() || path.components().any(|c| c.as_os_str() == ".git") {
                    continue;
                }
                if !is_utf8_file(path) {
                    continue;
                }

                let relative_path = path
                    .strip_prefix(base_dir)
                    .unwrap_or(path)
                    .to_string_lossy()
                    .replace('\\', "/");
                let extension = path
                    .extension()
                    .and_then(|e| e.to_str())
                    .unwrap_or("")
                    .to_string();
                files.push(FileInfo {
                    path: path.to_path_buf(),
                    relative_path,
                    extension,
                });
            }
            Err(err) => {
                eprintln!("Warning: Error walking directory (with gitignore): {err}");
            }
        }
    }
    files
}

fn collect_files_without_gitignore(base_dir: &Path) -> Vec<FileInfo> {
    let files_map = Arc::new(DashMap::new());
    collect_files_recursive(base_dir, base_dir, &files_map);
    files_map
        .iter()
        .map(|entry| entry.value().clone())
        .collect()
}

fn collect_files_recursive(
    dir: &Path,
    base_dir: &Path,
    files_map: &Arc<DashMap<PathBuf, FileInfo>>,
) {
    let Ok(entries_result) = fs::read_dir(dir) else {
        eprintln!("Warning: Failed to read directory: {}", dir.display());
        return;
    };

    let entries: Vec<PathBuf> = entries_result
        .filter_map(Result::ok)
        .map(|e| e.path())
        .collect();

    entries.into_par_iter().for_each(|path| {
        if path.is_dir() {
            if path.file_name().and_then(|n| n.to_str()) == Some(".git") {
                return;
            }
            collect_files_recursive(&path, base_dir, files_map);
        } else if path.is_file() && is_utf8_file(&path) {
            let relative_path = path
                .strip_prefix(base_dir)
                .unwrap_or(&path)
                .to_string_lossy()
                .replace('\\', "/");
            let extension = path
                .extension()
                .and_then(|e| e.to_str())
                .unwrap_or("")
                .to_string();
            let file_info = FileInfo {
                path: path.clone(),
                relative_path,
                extension,
            };
            files_map.insert(path.clone(), file_info);
        }
    });
}

fn is_utf8_file(path: &Path) -> bool {
    // First check by file extension for known text file types
    if is_known_text_extension(path) {
        return true;
    }
    
    // For unknown extensions, perform content inspection
    match fs::File::open(path) {
        Ok(mut file) => {
            // Read up to 8KB for better detection accuracy
            let mut buffer = [0u8; 8192]; 
            match file.read(&mut buffer) {
                Ok(0) => true, // Empty files are considered text files
                Ok(bytes_read) => {
                    let sample = &buffer[..bytes_read];
                    
                    // Multiple checks for better accuracy:
                    // 1. Use content_inspector as primary check
                    if inspect(sample).is_text() {
                        return true;
                    }
                    
                    // 2. Check if it's valid UTF-8 and contains mostly printable chars
                    if let Ok(text) = std::str::from_utf8(sample) {
                        return is_mostly_printable_text(text);
                    }
                    
                    // 3. Final fallback: very small files with some text content
                    if bytes_read < 256 && has_some_text_chars(sample) {
                        return true;
                    }
                    
                    false
                }
                Err(_) => false,
            }
        }
        Err(_) => false,
    }
}

fn is_known_text_extension(path: &Path) -> bool {
    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
        let ext_lower = ext.to_lowercase();
        matches!(ext_lower.as_str(),
            // Programming languages
            "rs" | "py" | "js" | "ts" | "jsx" | "tsx" | "java" | "c" | "cpp" | "cc" | "cxx" | 
            "h" | "hpp" | "cs" | "php" | "rb" | "go" | "kt" | "swift" | "scala" | "clj" | 
            "hs" | "elm" | "erl" | "ex" | "exs" | "ml" | "fs" | "vb" | "pas" | "pl" | "pm" |
            "r" | "jl" | "m" | "mm" | "f" | "f90" | "f95" | "ada" | "d" | "nim" | "zig" |
            
            // Web and markup
            "html" | "htm" | "xml" | "xhtml" | "svg" | "css" | "scss" | "sass" | "less" |
            "vue" | "svelte" | "astro" | "ejs" | "handlebars" | "hbs" |
            
            // Data formats
            "json" | "yaml" | "yml" | "toml" | "ini" | "cfg" | "conf" | "properties" |
            "csv" | "tsv" | "txt" | "log" |
            
            // Documentation
            "md" | "markdown" | "rst" | "tex" | "latex" | "org" | "adoc" | "asciidoc" |
            
            // Scripts and config
            "sh" | "bash" | "zsh" | "fish" | "ps1" | "cmd" | "bat" | "dockerfile" |
            "makefile" | "mk" | "cmake" | "ninja" | "gradle" | "ant" |
            
            // Other common text files
            "gitignore" | "gitattributes" | "editorconfig" | "prettierrc" | "eslintrc" |
            "tsconfig" | "package" | "cargo" | "gemfile" | "pipfile" | "requirements" |
            "license" | "readme" | "changelog" | "authors" | "contributors" | "todo"
        )
    } else {
        // Files without extension that are commonly text files
        if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
            let filename_lower = filename.to_lowercase();
            matches!(filename_lower.as_str(),
                "makefile" | "dockerfile" | "cmakelists.txt" | "readme" | "license" | 
                "changelog" | "authors" | "contributors" | "todo" | "news" | "install" |
                "copying" | "notice" | "manifest" | "version" | "gemfile" | "rakefile" |
                "pipfile" | "procfile" | "vagrantfile" | "gruntfile" | "gulpfile" |
                ".gitignore" | ".gitattributes" | ".editorconfig" | ".prettierrc" |
                ".eslintrc" | ".babelrc" | ".npmrc" | ".yarnrc"
            )
        } else {
            false
        }
    }
}

fn is_mostly_printable_text(text: &str) -> bool {
    if text.is_empty() {
        return true;
    }
    
    let total_chars = text.chars().count();
    let printable_chars = text.chars()
        .filter(|&c| c.is_ascii_graphic() || c.is_ascii_whitespace() || !c.is_ascii())
        .count();
    
    // At least 85% of characters should be printable
    (printable_chars as f64 / total_chars as f64) >= 0.85
}

fn has_some_text_chars(data: &[u8]) -> bool {
    let text_chars = data.iter()
        .filter(|&&b| b.is_ascii_alphabetic() || b.is_ascii_digit() || b.is_ascii_whitespace())
        .count();
    
    // For very small files, if at least 50% are text characters, consider it text
    (text_chars as f64 / data.len() as f64) >= 0.5
}


fn write_files_streaming(
    files: &[FileInfo],
    tree_structure: &str,
    filename: &str,
    timings: &mut Timings,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("\nWriting file contents...");
    
    let output_file = fs::File::create(filename)
        .map_err(|e| format!("Failed to create output file: {e}"))?;
    let mut writer = BufWriter::new(output_file);
    
    // Write Markdown formatted project analysis report
    write!(writer, "# Project Analysis Report\n\n")?;
    write!(writer, "## File Structure\n\n```\n{tree_structure}```\n\n")?;
    write!(writer, "## File Contents\n\n")?;
    
    let stage_start_time = Instant::now();
    let mut files_processed = 0;
    let mut files_failed = 0;
    
    // Stream process each file
    for file_info in files {
        match read_and_write_file(&mut writer, file_info) {
            Ok(()) => {
                files_processed += 1;
            }
            Err(e) => {
                eprintln!("Warning: Failed to read {}: {}", file_info.relative_path, e);
                files_failed += 1;
            }
        }
    }
    
    writer.flush()?;
    timings.read_contents = stage_start_time.elapsed().as_micros();
    timings.generate_output_string = 0; // Already included in streaming process
    
    if files_processed == 0 && files_failed > 0 {
        return Err("All selected files failed to read.".into());
    }
    
    println!("Successfully processed {files_processed} files ({files_failed} failed)");
    Ok(())
}

fn read_and_write_file(
    writer: &mut BufWriter<fs::File>,
    file_info: &FileInfo,
) -> Result<(), Box<dyn std::error::Error>> {
    // Write Markdown file header
    write!(writer, "### `{}`\n\n", file_info.relative_path)?;
    
    // Determine syntax highlighting type based on extension
    let language = get_language_from_extension(&file_info.extension);
    writeln!(writer, "```{language}")?;
    
    // Stream read and write file content
    let file = fs::File::open(&file_info.path)?;
    let mut reader = BufReader::new(file);
    let mut line = String::new();
    
    while reader.read_line(&mut line)? > 0 {
        writer.write_all(line.as_bytes())?;
        line.clear();
    }
    
    write!(writer, "```\n\n")?;
    Ok(())
}

fn get_language_from_extension(extension: &str) -> &'static str {
    match extension {
        "rs" => "rust",
        "js" => "javascript",
        "ts" => "typescript",
        "py" => "python",
        "go" => "go",
        "java" => "java",
        "c" | "h" | "hpp" => "c",
        "cpp" | "cc" | "cxx" => "cpp",
        "html" => "html",
        "css" => "css",
        "json" => "json",
        "xml" => "xml",
        "yaml" | "yml" => "yaml",
        "toml" => "toml",
        "md" => "markdown",
        "sh" => "bash",
        "sql" => "sql",
        "dockerfile" => "dockerfile",
        "makefile" => "makefile",
        _ => "", // No syntax highlighting
    }
}

fn generate_tree_structure_from_files(files: &[FileInfo]) -> String {
    // Reuse existing logic but generate directly from FileInfo
    let file_tuples: Vec<(FileInfo, String)> = files.iter()
        .map(|f| (f.clone(), String::new()))
        .collect();
    generate_tree_structure(&file_tuples)
}

fn generate_tree_structure(files: &[(FileInfo, String)]) -> String {
    let mut path_map: HashMap<String, BTreeSet<String>> = HashMap::new();
    let mut all_distinct_paths: HashSet<String> = HashSet::new();

    for (file_info, _) in files {
        let path = Path::new(&file_info.relative_path);
        all_distinct_paths.insert(file_info.relative_path.clone());
        let mut current_accumulated_path = PathBuf::new();
        if let Some(parent_dir) = path.parent() {
            for component in parent_dir.components() {
                if let Some(comp_str) = component.as_os_str().to_str() {
                    if comp_str != "." && comp_str != "/" {
                        current_accumulated_path.push(comp_str);
                        if !current_accumulated_path.as_os_str().is_empty() {
                            all_distinct_paths.insert(
                                current_accumulated_path
                                    .to_string_lossy()
                                    .replace('\\', "/"),
                            );
                        }
                    }
                }
            }
        }
    }

    for path_str in all_distinct_paths {
        let p = Path::new(&path_str);
        let file_name_os = p.file_name().unwrap_or(p.as_os_str());
        let child_name = file_name_os.to_string_lossy().into_owned();

        if let Some(parent_path_os) = p.parent() {
            let parent_key = if parent_path_os.as_os_str().is_empty() {
                ".".to_string()
            } else {
                parent_path_os.to_string_lossy().replace('\\', "/")
            };
            path_map.entry(parent_key).or_default().insert(child_name);
        } else {
            path_map
                .entry(".".to_string())
                .or_default()
                .insert(child_name);
        }
    }
    if files.is_empty()
        && path_map
            .get(".")
            .is_none_or(std::collections::BTreeSet::is_empty)
    {
        return ".\n(No files or directories found to list)\n".to_string();
    } else if files.len() == 1
        && path_map.get(".").is_some_and(|s| s.len() == 1)
        && path_map.get(".").unwrap().iter().next().unwrap() == &files[0].0.relative_path
    {
    } else if !path_map.contains_key(".") && !files.is_empty() {
        for (file_info, _) in files {
            let p = Path::new(&file_info.relative_path);
            if p.parent().is_none_or(|par| par.as_os_str().is_empty()) {
                path_map
                    .entry(".".to_string())
                    .or_default()
                    .insert(p.file_name().unwrap().to_string_lossy().into_owned());
            }
        }
    }

    let mut output_tree_string = String::new();
    if path_map.contains_key(".") || !files.is_empty() {
        output_tree_string.push_str(".\n");
    }
    generate_tree_recursive(&path_map, ".", "", &mut output_tree_string, true);
    output_tree_string
}

fn generate_tree_recursive(
    path_map: &HashMap<String, BTreeSet<String>>,
    current_path_key: &str,
    prefix_for_children_lines: &str,
    output: &mut String,
    is_current_path_conceptual_root: bool,
) {
    if let Some(children_names) = path_map.get(current_path_key) {
        let num_children = children_names.len();
        for (i, child_name) in children_names.iter().enumerate() {
            let is_last = i == num_children - 1;

            output.push_str(prefix_for_children_lines);
            output.push_str(if is_last { "└── " } else { "├── " });
            output.push_str(child_name);
            output.push('\n');

            let child_full_key = if is_current_path_conceptual_root && current_path_key == "." {
                child_name.clone()
            } else {
                format!("{current_path_key}/{child_name}")
            };

            if path_map.contains_key(&child_full_key) {
                let mut new_prefix_for_grandchildren = prefix_for_children_lines.to_string();
                new_prefix_for_grandchildren.push_str(if is_last { "   " } else { "" });
                generate_tree_recursive(
                    path_map,
                    &child_full_key,
                    &new_prefix_for_grandchildren,
                    output,
                    false,
                );
            }
        }
    }
}