Skip to content

Commit c148020

Browse files
authored
Allow config to set level (#46)
2 parents 32e1118 + d88cd4b commit c148020

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/cli.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ pub struct Cli {
2424
#[arg(short = 'L', long)]
2525
pub level: Option<usize>,
2626

27+
/// Force this tool to have no upper limit for level.
28+
///
29+
/// Useful for overriding a level set by the configuration file.
30+
#[arg(long, alias = "unset-level", conflicts_with = "level")]
31+
pub max_level: bool,
32+
2733
/// Edit the main configuration file and exit.
2834
#[arg(long, num_args = 0..=1, default_missing_value = "config")]
2935
pub edit_config: Option<EditConfig>,
@@ -105,6 +111,8 @@ impl Cli {
105111

106112
if let Some(level) = self.level {
107113
builder = builder.max_level(level);
114+
} else if self.max_level {
115+
builder = builder.unset_level();
108116
}
109117

110118
let tree = builder.build();

src/config/main/config.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ return {
1313
---@type Sorting|nil
1414
-- When this is nil, the default sorting algorithm will be used.
1515
sorting = nil,
16+
---@type integer|nil
17+
-- When this is not nil, it will set how many levels deep this tool should search in
18+
-- the directory tree.
19+
level = nil,
1620
}

src/config/main/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub struct Main {
2424
skip: Option<mlua::Function>,
2525
/// Determines how to sort files in a directory.
2626
sorting: Sorting,
27+
/// How many levels deep to search before stopping.
28+
level: Option<usize>,
2729
}
2830

2931
impl Main {
@@ -81,6 +83,11 @@ impl Main {
8183
1.. => Ordering::Greater,
8284
}
8385
}
86+
87+
/// How many levels deep to search before stopping.
88+
pub fn level(&self) -> Option<usize> {
89+
self.level
90+
}
8491
}
8592

8693
impl Default for Main {
@@ -89,6 +96,7 @@ impl Default for Main {
8996
color: Default::default(),
9097
skip: None,
9198
sorting: Self::default_sorting(),
99+
level: None,
92100
}
93101
}
94102
}
@@ -116,10 +124,12 @@ impl FromLua for Main {
116124
let sorting = table
117125
.get::<Option<Sorting>>("sorting")?
118126
.unwrap_or_else(Self::default_sorting);
127+
let level = table.get("level")?;
119128
let main = Main {
120129
color,
121130
skip,
122131
sorting,
132+
level,
123133
};
124134
Ok(main)
125135
}

src/tree/builder.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct Builder<'git, 'charset, P: AsRef<Path>> {
1414
color_choice: Option<ColorChoice>,
1515
charset: Option<Charset<'charset>>,
1616
max_level: Option<usize>,
17+
/// Override the level limit that may be set by the configuration.
18+
unset_level: bool,
1719
config: Option<config::Main>,
1820
icons: Option<config::Icons>,
1921
colors: Option<config::Colors>,
@@ -30,6 +32,7 @@ where
3032
root,
3133
git: None,
3234
max_level: None,
35+
unset_level: false,
3336
charset: None,
3437
color_choice: None,
3538
config: None,
@@ -58,6 +61,19 @@ where
5861
}
5962
}
6063

64+
/// Unsets the maximum depth level for the [`Tree`], returning to the default
65+
/// behavior of searching infinitely deep.
66+
///
67+
/// This helps override a maximum level that may have been set by the configuration.
68+
#[inline]
69+
#[must_use]
70+
pub fn unset_level(self) -> Self {
71+
Self {
72+
unset_level: true,
73+
..self
74+
}
75+
}
76+
6177
/// Sets the [`Charset`] for the [`Tree`].
6278
#[inline]
6379
#[must_use]
@@ -110,11 +126,25 @@ where
110126
}
111127

112128
/// Creates the [`Tree`].
129+
///
130+
/// # Panics
131+
///
132+
/// - Panics if `max_level` and `unset_level` were both called.
113133
pub fn build(self) -> Tree<'git, 'charset, P> {
134+
assert!(
135+
!(self.unset_level && self.max_level.is_some()),
136+
"max_level cannot be set when unset_level is true"
137+
);
138+
let max_level = if self.unset_level {
139+
None
140+
} else {
141+
self.max_level
142+
.or(self.config.as_ref().and_then(|config| config.level()))
143+
};
114144
Tree {
115145
root: self.root,
116146
git: self.git,
117-
max_level: self.max_level,
147+
max_level,
118148
charset: self.charset.unwrap_or_default(),
119149
color_choice: self.color_choice,
120150
config: self.config.unwrap_or_default(),
@@ -123,3 +153,14 @@ where
123153
}
124154
}
125155
}
156+
157+
#[cfg(test)]
158+
mod tests {
159+
use super::*;
160+
161+
#[test]
162+
#[should_panic]
163+
fn test_cannot_build_unset_level_with_max_level() {
164+
Builder::new(".").max_level(1).unset_level().build();
165+
}
166+
}

0 commit comments

Comments
 (0)