tldr-cli 0.1.2

CLI binary for TLDR code analysis tool
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
//! Cache statistics command implementation
//!
//! CLI command: `tldr cache stats [--project PATH]`
//!
//! Displays cache statistics for a TLDR project:
//! - If daemon is running: queries cache stats via IPC
//! - If daemon is not running: reads cache files directly
//!
//! Statistics include:
//! - Salsa-style query cache: hits, misses, hit rate, invalidations
//! - Cache files: file count, total size on disk

use std::fs;
use std::path::{Path, PathBuf};

use clap::Args;
use serde::Serialize;

use crate::output::OutputFormat;

use super::error::{DaemonError, DaemonResult};
use super::ipc::send_command;
use super::salsa::QueryCache;
use super::types::{CacheFileInfo, DaemonCommand, DaemonResponse, SalsaCacheStats};

// =============================================================================
// CLI Arguments
// =============================================================================

/// Arguments for the `cache stats` command.
#[derive(Debug, Clone, Args)]
pub struct CacheStatsArgs {
    /// Project root directory (default: current directory)
    #[arg(long, short = 'p', default_value = ".")]
    pub project: PathBuf,
}

// =============================================================================
// Output Types
// =============================================================================

/// Output structure for cache stats command.
#[derive(Debug, Clone, Serialize)]
pub struct CacheStatsOutput {
    /// Salsa-style query cache statistics
    #[serde(skip_serializing_if = "Option::is_none")]
    pub salsa_stats: Option<SalsaCacheStats>,
    /// Cache file information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_files: Option<CacheFileInfo>,
    /// Optional message (e.g., "No cache statistics found")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

// =============================================================================
// Command Implementation
// =============================================================================

impl CacheStatsArgs {
    /// Run the cache stats command.
    pub fn run(&self, format: OutputFormat, quiet: bool) -> anyhow::Result<()> {
        // Create a new tokio runtime for the async operations
        let runtime = tokio::runtime::Runtime::new()?;
        runtime.block_on(self.run_async(format, quiet))
    }

    /// Async implementation of the cache stats command.
    async fn run_async(&self, format: OutputFormat, quiet: bool) -> anyhow::Result<()> {
        // Resolve project path to absolute
        let project = self.project.canonicalize().unwrap_or_else(|_| {
            std::env::current_dir()
                .unwrap_or_else(|_| PathBuf::from("."))
                .join(&self.project)
        });

        // Try to get stats from running daemon first
        let cmd = DaemonCommand::Status { session: None };

        match send_command(&project, &cmd).await {
            Ok(DaemonResponse::FullStatus { salsa_stats, .. }) => {
                // Daemon is running, use its stats
                let cache_files = scan_cache_files(&project)?;
                let output = CacheStatsOutput {
                    salsa_stats: Some(salsa_stats),
                    cache_files: Some(cache_files),
                    message: None,
                };
                self.print_output(&output, format, quiet)
            }
            Ok(_) | Err(DaemonError::NotRunning) | Err(DaemonError::ConnectionRefused) => {
                // Daemon not running, read from cache files directly
                self.read_cache_from_files(&project, format, quiet)
            }
            Err(e) => Err(anyhow::anyhow!("Failed to get cache stats: {}", e)),
        }
    }

    /// Read cache statistics from files when daemon is not running.
    fn read_cache_from_files(
        &self,
        project: &Path,
        format: OutputFormat,
        quiet: bool,
    ) -> anyhow::Result<()> {
        let cache_dir = project.join(".tldr").join("cache");

        // Check if cache directory exists
        if !cache_dir.exists() {
            let output = CacheStatsOutput {
                salsa_stats: None,
                cache_files: None,
                message: Some("No cache directory found".to_string()),
            };
            return self.print_output(&output, format, quiet);
        }

        // Try to load salsa stats from file
        let salsa_stats = self.load_salsa_stats(&cache_dir);

        // Scan cache files
        let cache_files = scan_cache_files(project)?;

        // Check if we have any cache data
        if salsa_stats.is_none() && cache_files.file_count == 0 {
            let output = CacheStatsOutput {
                salsa_stats: None,
                cache_files: Some(cache_files),
                message: Some("No cache statistics found".to_string()),
            };
            return self.print_output(&output, format, quiet);
        }

        let output = CacheStatsOutput {
            salsa_stats,
            cache_files: Some(cache_files),
            message: None,
        };

        self.print_output(&output, format, quiet)
    }

    /// Try to load salsa cache stats from persisted file.
    fn load_salsa_stats(&self, cache_dir: &Path) -> Option<SalsaCacheStats> {
        let salsa_cache_file = cache_dir.join("salsa_cache.bin");

        if !salsa_cache_file.exists() {
            return None;
        }

        // Try to load the cache and extract stats
        match QueryCache::load_from_file(&salsa_cache_file) {
            Ok(cache) => Some(cache.stats()),
            Err(_) => None,
        }
    }

    /// Print output in the requested format.
    fn print_output(
        &self,
        output: &CacheStatsOutput,
        format: OutputFormat,
        quiet: bool,
    ) -> anyhow::Result<()> {
        if quiet {
            return Ok(());
        }

        match format {
            OutputFormat::Json | OutputFormat::Compact => {
                println!("{}", serde_json::to_string_pretty(output)?);
            }
            OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
                if let Some(ref msg) = output.message {
                    println!("{}", msg);
                    return Ok(());
                }

                println!("Cache Statistics");
                println!("================");

                if let Some(ref stats) = output.salsa_stats {
                    println!();
                    println!("Salsa Cache:");
                    println!("  Hits:          {}", format_number(stats.hits));
                    println!("  Misses:        {}", format_number(stats.misses));
                    println!("  Hit Rate:      {:.2}%", stats.hit_rate());
                    println!("  Invalidations: {}", format_number(stats.invalidations));
                    println!("  Recomputations: {}", format_number(stats.recomputations));
                }

                if let Some(ref files) = output.cache_files {
                    println!();
                    println!("Cache Files:");
                    println!("  Count: {} files", files.file_count);
                    println!("  Size:  {}", files.total_size_human);
                }
            }
        }

        Ok(())
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Scan cache files in the project's .tldr/cache/ directory.
fn scan_cache_files(project: &Path) -> DaemonResult<CacheFileInfo> {
    let cache_dir = project.join(".tldr").join("cache");

    if !cache_dir.exists() {
        return Ok(CacheFileInfo {
            file_count: 0,
            total_bytes: 0,
            total_size_human: "0 B".to_string(),
        });
    }

    let mut file_count = 0;
    let mut total_bytes = 0u64;

    // Count all files in cache directory
    if let Ok(entries) = fs::read_dir(&cache_dir) {
        for entry in entries.flatten() {
            if let Ok(metadata) = entry.metadata() {
                if metadata.is_file() {
                    file_count += 1;
                    total_bytes += metadata.len();
                }
            }
        }
    }

    Ok(CacheFileInfo {
        file_count,
        total_bytes,
        total_size_human: format_bytes(total_bytes),
    })
}

/// Format bytes as human-readable string.
fn format_bytes(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

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

/// Format a number with thousands separators.
fn format_number(n: u64) -> String {
    let s = n.to_string();
    let bytes = s.as_bytes();
    let mut result = String::new();
    let len = bytes.len();

    for (i, &b) in bytes.iter().enumerate() {
        if i > 0 && (len - i).is_multiple_of(3) {
            result.push(',');
        }
        result.push(b as char);
    }

    result
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_cache_stats_args_default() {
        let args = CacheStatsArgs {
            project: PathBuf::from("."),
        };
        assert_eq!(args.project, PathBuf::from("."));
    }

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(0), "0 B");
        assert_eq!(format_bytes(500), "500 B");
        assert_eq!(format_bytes(1024), "1.0 KB");
        assert_eq!(format_bytes(1536), "1.5 KB");
        assert_eq!(format_bytes(1048576), "1.0 MB");
        assert_eq!(format_bytes(1572864), "1.5 MB");
        assert_eq!(format_bytes(1073741824), "1.0 GB");
    }

    #[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_scan_cache_files_no_cache_dir() {
        let temp = TempDir::new().unwrap();
        let result = scan_cache_files(temp.path()).unwrap();

        assert_eq!(result.file_count, 0);
        assert_eq!(result.total_bytes, 0);
        assert_eq!(result.total_size_human, "0 B");
    }

    #[test]
    fn test_scan_cache_files_with_files() {
        let temp = TempDir::new().unwrap();
        let cache_dir = temp.path().join(".tldr").join("cache");
        fs::create_dir_all(&cache_dir).unwrap();

        // Create some test files
        fs::write(cache_dir.join("file1.bin"), "hello").unwrap();
        fs::write(cache_dir.join("file2.json"), "world").unwrap();
        fs::write(cache_dir.join("call_graph.json"), r#"{"edges":[]}"#).unwrap();

        let result = scan_cache_files(temp.path()).unwrap();

        assert_eq!(result.file_count, 3);
        assert!(result.total_bytes > 0);
    }

    #[test]
    fn test_cache_stats_output_serialization() {
        let output = CacheStatsOutput {
            salsa_stats: Some(SalsaCacheStats {
                hits: 100,
                misses: 10,
                invalidations: 5,
                recomputations: 3,
            }),
            cache_files: Some(CacheFileInfo {
                file_count: 25,
                total_bytes: 1048576,
                total_size_human: "1.0 MB".to_string(),
            }),
            message: None,
        };

        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("hits"));
        assert!(json.contains("100"));
        assert!(json.contains("file_count"));
        assert!(json.contains("25"));
    }

    #[test]
    fn test_cache_stats_output_empty() {
        let output = CacheStatsOutput {
            salsa_stats: None,
            cache_files: None,
            message: Some("No cache statistics found".to_string()),
        };

        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("No cache statistics found"));
        assert!(!json.contains("salsa_stats"));
        assert!(!json.contains("cache_files"));
    }

    #[tokio::test]
    async fn test_cache_stats_no_cache() {
        let temp = TempDir::new().unwrap();
        let args = CacheStatsArgs {
            project: temp.path().to_path_buf(),
        };

        // Should succeed even with no cache
        let result = args.run_async(OutputFormat::Json, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_cache_stats_with_cache_dir() {
        let temp = TempDir::new().unwrap();
        let cache_dir = temp.path().join(".tldr").join("cache");
        fs::create_dir_all(&cache_dir).unwrap();
        fs::write(cache_dir.join("test.bin"), "test data").unwrap();

        let args = CacheStatsArgs {
            project: temp.path().to_path_buf(),
        };

        let result = args.run_async(OutputFormat::Json, true).await;
        assert!(result.is_ok());
    }
}