pith 0.1.0

Generate optimized codebase context for LLMs
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
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
//! File tree representation and rendering.
//!
//! Provides types for representing directory structures and
//! functions for rendering them with box-drawing characters.

use std::cmp::Ordering;
use std::collections::HashSet;
use std::path::{Path, PathBuf};

use crate::filter::Language;

/// The type of a filesystem node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeKind {
    Directory,
    File {
        extension: Option<String>,
        size: u64,
        lines: Option<usize>,
    },
}

impl NodeKind {
    /// Check if this is a directory.
    pub fn is_directory(&self) -> bool {
        matches!(self, NodeKind::Directory)
    }

    /// Check if this is a file.
    pub fn is_file(&self) -> bool {
        matches!(self, NodeKind::File { .. })
    }
}

/// A node in the file tree.
#[derive(Debug, Clone)]
pub struct FileNode {
    /// File or directory name (not full path).
    pub name: String,
    /// Full path from root.
    pub path: PathBuf,
    /// Type of node (file or directory).
    pub kind: NodeKind,
    /// Child nodes (empty for files). Vec needed for recursive type.
    children: Vec<FileNode>,
}

impl FileNode {
    /// Create a new directory node.
    pub fn directory(name: impl Into<String>, path: impl Into<PathBuf>) -> Self {
        Self {
            name: name.into(),
            path: path.into(),
            kind: NodeKind::Directory,
            children: Vec::new(),
        }
    }

    /// Create a new file node.
    pub fn file(
        name: impl Into<String>,
        path: impl Into<PathBuf>,
        extension: Option<String>,
        size: u64,
        lines: Option<usize>,
    ) -> Self {
        Self {
            name: name.into(),
            path: path.into(),
            kind: NodeKind::File {
                extension,
                size,
                lines,
            },
            children: Vec::new(),
        }
    }

    /// Check if this is a directory.
    pub fn is_directory(&self) -> bool {
        self.kind.is_directory()
    }

    /// Check if this is a file.
    pub fn is_file(&self) -> bool {
        self.kind.is_file()
    }

    /// Add a child node. Only valid for directories.
    pub fn add_child(&mut self, child: FileNode) {
        self.children.push(child);
    }

    /// Get child nodes.
    pub fn children(&self) -> &[FileNode] {
        &self.children
    }

    /// Sort children: directories first, then alphabetically.
    pub fn sort_children(&mut self) {
        self.children.sort_by(|a, b| {
            match (&a.kind, &b.kind) {
                (NodeKind::Directory, NodeKind::File { .. }) => Ordering::Less,
                (NodeKind::File { .. }, NodeKind::Directory) => Ordering::Greater,
                _ => a.name.to_lowercase().cmp(&b.name.to_lowercase()),
            }
        });

        // Recursively sort children's children
        for child in &mut self.children {
            child.sort_children();
        }
    }

    /// Get file extension if this is a file.
    pub fn extension(&self) -> Option<&str> {
        match &self.kind {
            NodeKind::File { extension, .. } => extension.as_deref(),
            NodeKind::Directory => None,
        }
    }

    /// Get file size if this is a file.
    pub fn size(&self) -> Option<u64> {
        match &self.kind {
            NodeKind::File { size, .. } => Some(*size),
            NodeKind::Directory => None,
        }
    }

    /// Count total files in this tree.
    pub fn file_count(&self) -> usize {
        match &self.kind {
            NodeKind::File { .. } => 1,
            NodeKind::Directory => self.children.iter().map(|c| c.file_count()).sum(),
        }
    }

    /// Count total directories in this tree.
    pub fn directory_count(&self) -> usize {
        match &self.kind {
            NodeKind::File { .. } => 0,
            NodeKind::Directory => {
                1 + self.children.iter().map(|c| c.directory_count()).sum::<usize>()
            }
        }
    }
}

/// Options for rendering the tree.
#[derive(Debug, Clone, Default)]
pub struct RenderOptions<'a> {
    /// Show file sizes.
    pub show_size: bool,
    /// Show line counts.
    pub show_lines: bool,
    /// Show detected language.
    pub show_language: bool,
    /// Paths that are selected (marked with *).
    pub selected: HashSet<&'a PathBuf>,
    /// Paths that have codemaps (marked with +).
    pub has_codemap: HashSet<&'a PathBuf>,
}

impl<'a> RenderOptions<'a> {
    /// Create options with all metadata enabled.
    pub fn with_metadata() -> Self {
        Self {
            show_size: true,
            show_lines: true,
            show_language: true,
            ..Default::default()
        }
    }

    /// Create minimal options (no metadata).
    pub fn minimal() -> Self {
        Self::default()
    }
}

/// Box-drawing characters for tree rendering.
const BRANCH: &str = "├── ";
const LAST_BRANCH: &str = "└── ";
const VERTICAL: &str = "";
const SPACE: &str = "    ";

/// Render a file tree to a string with box-drawing characters.
///
/// # Examples
///
/// ```
/// use pith::tree::{FileNode, NodeKind, RenderOptions, render_tree};
///
/// let mut root = FileNode::directory("project", "project");
/// root.add_child(FileNode::file("main.rs", "project/main.rs", Some("rs".into()), 1024, Some(50)));
/// root.sort_children();
///
/// let output = render_tree(&root, &RenderOptions::minimal());
/// assert!(output.contains("main.rs"));
/// ```
pub fn render_tree(root: &FileNode, options: &RenderOptions<'_>) -> String {
    // Pre-allocate for typical tree size
    let mut output = String::with_capacity(4096);
    render_node(&mut output, root, "", true, true, options);
    output
}

fn render_node(
    output: &mut String,
    node: &FileNode,
    prefix: &str,
    is_last: bool,
    is_root: bool,
    options: &RenderOptions<'_>,
) {
    // Render this node
    let branch = if is_root {
        "" // Root node has no branch
    } else if is_last {
        LAST_BRANCH
    } else {
        BRANCH
    };

    output.push_str(prefix);
    output.push_str(branch);
    output.push_str(&node.name);

    // Add trailing slash for directories
    if node.is_directory() {
        output.push('/');
    }

    // Add metadata for files
    if let NodeKind::File {
        extension,
        size,
        lines,
    } = &node.kind
    {
        let mut metadata = Vec::new();

        if options.show_language {
            if let Some(ext) = extension {
                if let Ok(lang) = ext.parse::<Language>() {
                    metadata.push(lang.to_string());
                }
            }
        }

        if options.show_lines {
            if let Some(line_count) = lines {
                metadata.push(format!("{} lines", line_count));
            }
        }

        if options.show_size {
            metadata.push(format_size(*size));
        }

        if !metadata.is_empty() {
            output.push_str(" [");
            output.push_str(&metadata.join(", "));
            output.push(']');
        }
    }

    // Add markers
    let is_selected = options.selected.contains(&node.path);
    let has_codemap = options.has_codemap.contains(&node.path);

    if is_selected || has_codemap {
        output.push(' ');
        if is_selected {
            output.push('*');
        }
        if has_codemap {
            output.push('+');
        }
    }

    output.push('\n');

    // Render children
    let child_count = node.children.len();
    for (i, child) in node.children.iter().enumerate() {
        let is_last_child = i == child_count - 1;

        // Build new prefix for children
        let new_prefix = if is_root {
            // Root's children have no prefix before their branch
            String::new()
        } else {
            // Non-root: add continuation (vertical line or space) based on whether this node is last
            let continuation = if is_last { SPACE } else { VERTICAL };
            format!("{}{}", prefix, continuation)
        };

        render_node(output, child, &new_prefix, is_last_child, false, options);
    }
}

/// Format file size for display.
fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;

    if bytes < KB {
        format!("{}B", bytes)
    } else if bytes < MB {
        format!("{:.1}KB", bytes as f64 / KB as f64)
    } else {
        format!("{:.1}MB", bytes as f64 / MB as f64)
    }
}

/// Format number with thousands separators.
pub fn format_number(n: usize) -> String {
    if n == 0 {
        return "0".to_string();
    }

    // Build digits in reverse, then reverse once at the end
    let mut result = String::with_capacity(16);
    let mut n = n;
    let mut digits = 0;

    while n > 0 {
        if digits > 0 && digits % 3 == 0 {
            result.push(',');
        }
        result.push((b'0' + (n % 10) as u8) as char);
        n /= 10;
        digits += 1;
    }

    // Single reversal instead of double
    result.chars().rev().collect()
}

/// Detect language from a path (convenience wrapper).
pub fn detect_language_from_path(path: &Path) -> Option<Language> {
    crate::filter::detect_language(path)
}

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

    #[test]
    fn test_directory_node() {
        let node = FileNode::directory("src", "project/src");
        assert!(node.is_directory());
        assert!(!node.is_file());
        assert_eq!(node.name, "src");
    }

    #[test]
    fn test_file_node() {
        let node = FileNode::file("main.rs", "project/main.rs", Some("rs".into()), 1024, Some(50));
        assert!(node.is_file());
        assert!(!node.is_directory());
        assert_eq!(node.extension(), Some("rs"));
        assert_eq!(node.size(), Some(1024));
    }

    #[test]
    fn test_add_child() {
        let mut dir = FileNode::directory("src", "src");
        dir.add_child(FileNode::file("lib.rs", "src/lib.rs", Some("rs".into()), 512, Some(25)));
        assert_eq!(dir.children.len(), 1);
    }

    #[test]
    fn test_sort_children() {
        let mut dir = FileNode::directory("src", "src");
        dir.add_child(FileNode::file("z.rs", "src/z.rs", Some("rs".into()), 100, Some(5)));
        dir.add_child(FileNode::directory("utils", "src/utils"));
        dir.add_child(FileNode::file("a.rs", "src/a.rs", Some("rs".into()), 100, Some(5)));

        dir.sort_children();

        // Directory should come first
        assert!(dir.children[0].is_directory());
        assert_eq!(dir.children[0].name, "utils");
        // Then files alphabetically
        assert_eq!(dir.children[1].name, "a.rs");
        assert_eq!(dir.children[2].name, "z.rs");
    }

    #[test]
    fn test_file_count() {
        let mut root = FileNode::directory("root", "root");
        root.add_child(FileNode::file("a.rs", "root/a.rs", Some("rs".into()), 100, Some(5)));

        let mut sub = FileNode::directory("sub", "root/sub");
        sub.add_child(FileNode::file("b.rs", "root/sub/b.rs", Some("rs".into()), 100, Some(5)));
        sub.add_child(FileNode::file("c.rs", "root/sub/c.rs", Some("rs".into()), 100, Some(5)));
        root.add_child(sub);

        assert_eq!(root.file_count(), 3);
    }

    #[test]
    fn test_render_simple() {
        let mut root = FileNode::directory("project", "project");
        root.add_child(FileNode::file(
            "main.rs",
            "project/main.rs",
            Some("rs".into()),
            1024,
            Some(50),
        ));
        root.sort_children();

        let output = render_tree(&root, &RenderOptions::minimal());
        assert!(output.contains("project/"));
        assert!(output.contains("main.rs"));
    }

    #[test]
    fn test_render_with_metadata() {
        let mut root = FileNode::directory("project", "project");
        root.add_child(FileNode::file(
            "main.rs",
            "project/main.rs",
            Some("rs".into()),
            2048,
            Some(100),
        ));
        root.sort_children();

        let options = RenderOptions {
            show_size: true,
            show_language: false,
            show_lines: false,
            ..Default::default()
        };

        let output = render_tree(&root, &options);
        assert!(output.contains("2.0KB"));
    }

    #[test]
    fn test_render_with_markers() {
        let mut root = FileNode::directory("project", "project");
        root.add_child(FileNode::file(
            "main.rs",
            "project/main.rs",
            Some("rs".into()),
            1024,
            Some(50),
        ));
        root.sort_children();

        let main_path = PathBuf::from("project/main.rs");
        let options = RenderOptions {
            selected: [&main_path].into_iter().collect(),
            has_codemap: [&main_path].into_iter().collect(),
            ..Default::default()
        };

        let output = render_tree(&root, &options);
        assert!(output.contains("*+"));
    }

    #[test]
    fn test_format_size() {
        assert_eq!(format_size(0), "0B");
        assert_eq!(format_size(512), "512B");
        assert_eq!(format_size(1024), "1.0KB");
        assert_eq!(format_size(1536), "1.5KB");
        assert_eq!(format_size(1024 * 1024), "1.0MB");
    }

    #[test]
    fn test_format_number() {
        assert_eq!(format_number(0), "0");
        assert_eq!(format_number(999), "999");
        assert_eq!(format_number(1000), "1,000");
        assert_eq!(format_number(1234567), "1,234,567");
    }

    #[test]
    fn test_render_nested() {
        let mut root = FileNode::directory("project", "project");

        let mut src = FileNode::directory("src", "project/src");
        src.add_child(FileNode::file(
            "main.rs",
            "project/src/main.rs",
            Some("rs".into()),
            100,
            Some(5),
        ));
        src.add_child(FileNode::file(
            "lib.rs",
            "project/src/lib.rs",
            Some("rs".into()),
            200,
            Some(10),
        ));

        root.add_child(src);
        root.add_child(FileNode::file(
            "Cargo.toml",
            "project/Cargo.toml",
            Some("toml".into()),
            50,
            Some(3),
        ));
        root.sort_children();

        let output = render_tree(&root, &RenderOptions::minimal());

        // Verify structure
        assert!(output.contains("project/"));
        assert!(output.contains("src/"));
        assert!(output.contains("main.rs"));
        assert!(output.contains("lib.rs"));
        assert!(output.contains("Cargo.toml"));

        // Verify box drawing characters
        assert!(output.contains("├──") || output.contains("└──"));
    }
}