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
//! Structure command - Show code structure
//!
//! Extracts and displays functions, classes, and imports from source files.
//! Auto-routes through daemon when available for ~35x speedup.
use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use tldr_core::types::CodeStructure;
use tldr_core::{get_code_structure, IgnoreSpec, Language};
use crate::commands::daemon_router::{params_with_path_lang, try_daemon_route};
use crate::output::{format_structure_text, OutputFormat, OutputWriter};
/// Extract code structure (functions, classes, imports)
#[derive(Debug, Args)]
pub struct StructureArgs {
/// Directory to scan (default: current directory)
#[arg(default_value = ".")]
pub path: PathBuf,
/// Programming language (auto-detected if not specified)
#[arg(long, short = 'l')]
pub lang: Option<Language>,
/// Maximum number of files to process (0 = unlimited)
#[arg(long, short = 'm', default_value = "0")]
pub max_results: usize,
}
impl StructureArgs {
/// Run the structure command
pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
let writer = OutputWriter::new(format, quiet);
// Validate path exists BEFORE language detection / progress banner
// (lang-detect-default-v1: avoid printing misleading "(Python)" banner
// when the path doesn't exist and from_directory silently returns None.)
if !self.path.exists() {
anyhow::bail!("Path not found: {}", self.path.display());
}
// Determine language (auto-detect from directory, default to Python)
let language = self
.lang
.unwrap_or_else(|| Language::from_directory(&self.path).unwrap_or(Language::Python));
// Try daemon first for cached result
if let Some(structure) = try_daemon_route::<CodeStructure>(
&self.path,
"structure",
params_with_path_lang(&self.path, Some(language.as_str())),
) {
// Output based on format
if writer.is_text() {
let text = format_structure_text(&structure);
writer.write_text(&text)?;
return Ok(());
} else {
writer.write(&structure)?;
return Ok(());
}
}
// Fallback to direct compute
writer.progress(&format!(
"Extracting structure from {} ({:?})...",
self.path.display(),
language
));
// Get code structure
let structure = get_code_structure(
&self.path,
language,
self.max_results,
Some(&IgnoreSpec::default()),
)?;
// Output based on format
if writer.is_text() {
let text = format_structure_text(&structure);
writer.write_text(&text)?;
} else {
writer.write(&structure)?;
}
Ok(())
}
}