Skip to main content

broot/cli/
args.rs

1// Warning: this module can't import broot's stuff due to its use in build.rs
2use {
3    clap::{
4        Parser,
5        ValueEnum,
6    },
7    std::{
8        path::PathBuf,
9        str::FromStr,
10    },
11};
12
13/// Launch arguments
14#[derive(Debug, Parser)]
15#[command(about, version, disable_version_flag = true, disable_help_flag = true)]
16pub struct Args {
17    /// Print help information
18    #[arg(long)]
19    pub help: bool,
20
21    /// print the version
22    #[arg(long)]
23    pub version: bool,
24
25    /// Semicolon separated paths to specific config files
26    #[arg(long, value_name = "paths")]
27    pub conf: Option<String>,
28
29    /// Show the last modified date of files and directories
30    #[arg(short, long)]
31    pub dates: bool,
32
33    /// Don't show the last modified date
34    #[arg(short = 'D', long)]
35    pub no_dates: bool,
36
37    #[arg(short = 'f', long)]
38    /// Only show folders
39    pub only_folders: bool,
40
41    /// Show folders and files alike
42    #[arg(short = 'F', long)]
43    pub no_only_folders: bool,
44
45    /// Show filesystem info on top
46    #[arg(long)]
47    pub show_root_fs: bool,
48
49    /// Only show trees up to a certain depth
50    #[arg(long)]
51    pub max_depth: Option<u16>,
52
53    /// Show git statuses on files and stats on repo
54    #[arg(short = 'g', long)]
55    pub show_git_info: bool,
56
57    /// Don't show git statuses on files and stats on repo
58    #[arg(short = 'G', long)]
59    pub no_show_git_info: bool,
60
61    #[arg(long)]
62    /// Only show files having an interesting git status, including hidden ones
63    pub git_status: bool,
64
65    #[arg(short = 'h', long)]
66    /// Show hidden files
67    pub hidden: bool,
68
69    #[arg(short = 'H', long)]
70    /// Don't show hidden files
71    pub no_hidden: bool,
72
73    #[arg(short = 'i', long)]
74    /// Show git ignored files
75    pub git_ignored: bool,
76
77    #[arg(short = 'I', long)]
78    /// Don't show git ignored files
79    pub no_git_ignored: bool,
80
81    #[arg(short = 'p', long)]
82    /// Show permissions
83    pub permissions: bool,
84
85    #[arg(short = 'P', long)]
86    /// Don't show permissions
87    pub no_permissions: bool,
88
89    #[arg(short = 's', long)]
90    /// Show the size of files and directories
91    pub sizes: bool,
92
93    #[arg(short = 'S', long)]
94    /// Don't show sizes
95    pub no_sizes: bool,
96
97    #[arg(long)]
98    /// Sort by count (only show one level of the tree)
99    pub sort_by_count: bool,
100
101    #[arg(long)]
102    /// Sort by date (only show one level of the tree)
103    pub sort_by_date: bool,
104
105    #[arg(long)]
106    /// Sort by size (only show one level of the tree)
107    pub sort_by_size: bool,
108
109    #[arg(long)]
110    /// Same as sort-by-type-dirs-first
111    pub sort_by_type: bool,
112
113    #[arg(long)]
114    /// Do not show the tree, even if allowed by sorting mode.
115    pub no_tree: bool,
116
117    #[arg(long)]
118    /// Show the tree, when allowed by sorting mode.
119    pub tree: bool,
120
121    #[arg(long)]
122    /// Sort by type, directories first (only show one level of the tree)
123    pub sort_by_type_dirs_first: bool,
124
125    #[arg(long)]
126    /// Sort by type, directories last (only show one level of the tree)
127    pub sort_by_type_dirs_last: bool,
128
129    /// Don't sort
130    #[arg(long)]
131    pub no_sort: bool,
132
133    /// Sort by size, show ignored and hidden files
134    #[arg(short, long)]
135    pub whale_spotting: bool,
136
137    /// No sort, no show hidden, no show git ignored
138    #[arg(short = 'W', long)]
139    pub no_whale_spotting: bool,
140
141    /// Trim the root too and don't show a scrollbar
142    #[arg(short = 't', long)]
143    pub trim_root: bool,
144
145    /// Don't trim the root level, show a scrollbar
146    #[arg(short = 'T', long)]
147    pub no_trim_root: bool,
148
149    /// Where to write the produced cmd (if any)
150    #[arg(long, value_name = "path")]
151    pub outcmd: Option<PathBuf>,
152
153    /// Optional path for verbs using `:write_output`
154    #[arg(long, value_name = "verb-output")]
155    pub verb_output: Option<PathBuf>,
156
157    /// Semicolon separated commands to execute
158    #[arg(short, long, value_name = "cmd")]
159    pub cmd: Option<String>,
160
161    /// Whether to have styles and colors
162    #[arg(long, default_value = "auto", value_name = "color")]
163    pub color: TriBool,
164
165    /// Height (if you don't want to fill the screen or for file export)
166    #[arg(long, value_name = "height")]
167    pub height: Option<u16>,
168
169    /// Install or reinstall the br shell function
170    #[arg(long)]
171    pub install: bool,
172
173    /// Manually set installation state
174    #[arg(long, value_name = "state")]
175    pub set_install_state: Option<CliShellInstallState>,
176
177    /// Print to stdout the br function for a given shell
178    #[arg(long, value_name = "shell")]
179    pub print_shell_function: Option<String>,
180
181    /// A socket to listen to for commands
182    #[cfg(unix)]
183    #[arg(long, value_name = "socket")]
184    pub listen: Option<String>,
185
186    /// create a random socket to listen to for commands
187    #[cfg(unix)]
188    #[arg(long)]
189    pub listen_auto: bool,
190
191    /// Ask for the current root of the remote broot
192    #[cfg(unix)]
193    #[arg(long)]
194    pub get_root: bool,
195
196    /// Write default conf files in given directory
197    #[arg(long, value_name = "path")]
198    pub write_default_conf: Option<PathBuf>,
199
200    /// A socket to send commands to
201    #[cfg(unix)]
202    #[arg(long, value_name = "socket")]
203    pub send: Option<String>,
204
205    /// Root Directory
206    pub root: Option<PathBuf>,
207}
208
209/// This is an Option<bool> but I didn't find any way to configure
210/// clap to parse an Option<T> as I want
211#[derive(ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
212pub enum TriBool {
213    Auto,
214    Yes,
215    No,
216}
217impl TriBool {
218    pub fn unwrap_or_else<F>(
219        self,
220        f: F,
221    ) -> bool
222    where
223        F: FnOnce() -> bool,
224    {
225        match self {
226            Self::Auto => f(),
227            Self::Yes => true,
228            Self::No => false,
229        }
230    }
231}
232
233#[derive(Debug, Clone, Copy, clap::ValueEnum)]
234pub enum CliShellInstallState {
235    Undefined, // before any install, this is the initial state
236    Refused,
237    Installed,
238}
239impl FromStr for CliShellInstallState {
240    type Err = String;
241    fn from_str(state: &str) -> Result<Self, Self::Err> {
242        match state {
243            "undefined" => Ok(Self::Undefined),
244            "refused" => Ok(Self::Refused),
245            "installed" => Ok(Self::Installed),
246            _ => Err(format!("unexpected install state: {state:?}")),
247        }
248    }
249}