Skip to content

Commit c0d0062

Browse files
CopilotKSXGitHub
andcommitted
refactor: single LazyLock<AnsiPrefixes> + format_args! with row destructuring
Co-authored-by: KSXGitHub <11488886+KSXGitHub@users.noreply.github.com>
1 parent f622673 commit c0d0062

1 file changed

Lines changed: 42 additions & 42 deletions

File tree

src/visualizer/methods.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,39 @@ use std::{
1919
cmp::min,
2020
fmt::{self, Display},
2121
hash::Hash,
22-
sync::OnceLock,
22+
sync::LazyLock,
2323
};
2424
use zero_copy_pads::{align_left, align_right, Width};
2525

26-
fn ls_colors() -> &'static LsColors {
27-
static LS_COLORS: OnceLock<LsColors> = OnceLock::new();
28-
LS_COLORS.get_or_init(|| LsColors::from_env().unwrap_or_default())
26+
struct AnsiPrefixes {
27+
directory: String,
28+
normal: String,
29+
executable: String,
30+
symlink: String,
2931
}
3032

31-
fn color_ansi_prefix(color: Color) -> &'static str {
32-
fn compute_prefix(indicator: Indicator) -> String {
33-
ls_colors()
33+
static ANSI_PREFIXES: LazyLock<AnsiPrefixes> = LazyLock::new(|| {
34+
let ls_colors = LsColors::from_env().unwrap_or_default();
35+
let compute = |indicator: Indicator| {
36+
ls_colors
3437
.style_for_indicator(indicator)
3538
.map(|s| s.to_nu_ansi_term_style().prefix().to_string())
3639
.unwrap_or_default()
40+
};
41+
AnsiPrefixes {
42+
directory: compute(Indicator::Directory),
43+
normal: compute(Indicator::RegularFile),
44+
executable: compute(Indicator::ExecutableFile),
45+
symlink: compute(Indicator::SymbolicLink),
3746
}
47+
});
48+
49+
fn color_ansi_prefix(color: Color) -> &'static str {
3850
match color {
39-
Color::Directory => {
40-
static PREFIX: OnceLock<String> = OnceLock::new();
41-
PREFIX.get_or_init(|| compute_prefix(Indicator::Directory))
42-
}
43-
Color::Normal => {
44-
static PREFIX: OnceLock<String> = OnceLock::new();
45-
PREFIX.get_or_init(|| compute_prefix(Indicator::RegularFile))
46-
}
47-
Color::Executable => {
48-
static PREFIX: OnceLock<String> = OnceLock::new();
49-
PREFIX.get_or_init(|| compute_prefix(Indicator::ExecutableFile))
50-
}
51-
Color::Symlink => {
52-
static PREFIX: OnceLock<String> = OnceLock::new();
53-
PREFIX.get_or_init(|| compute_prefix(Indicator::SymbolicLink))
54-
}
51+
Color::Directory => &ANSI_PREFIXES.directory,
52+
Color::Normal => &ANSI_PREFIXES.normal,
53+
Color::Executable => &ANSI_PREFIXES.executable,
54+
Color::Symlink => &ANSI_PREFIXES.symlink,
5555
}
5656
}
5757

@@ -151,33 +151,33 @@ where
151151
bar_table
152152
.into_iter()
153153
.map(|row| {
154-
let slice = &row.tree_horizontal_slice;
154+
let BarRow { tree_row, proportion_bar } = row;
155+
let TreeRow { initial_row, tree_horizontal_slice: slice } = tree_row;
155156

156157
let node_color = self.coloring.and_then(|coloring| {
157-
if row.node_info.children_count > 0 {
158+
if initial_row.node_info.children_count > 0 {
158159
Some(Color::Directory)
159160
} else {
160-
coloring.get(row.node_info.name).copied()
161+
coloring.get(initial_row.node_info.name).copied()
161162
}
162163
});
163164

164-
macro_rules! render_row {
165-
($tree:expr) => {
166-
format!(
167-
"{size} {tree}│{bar}│{ratio}",
168-
size = align_right(&row.size, size_width),
169-
tree = $tree,
170-
bar = row.proportion_bar.display(self.bar_alignment),
171-
ratio = align_right(&row.percentage, PERCENTAGE_COLUMN_MAX_WIDTH),
172-
)
173-
};
174-
}
175-
176-
if let Some(color) = node_color {
177-
render_row!(align_left(ColoredSlice { slice, color }, tree_width))
165+
let aligned_colored;
166+
let aligned_normal;
167+
let tree = if let Some(color) = node_color {
168+
aligned_colored = align_left(ColoredSlice { slice: &slice, color }, tree_width);
169+
format_args!("{aligned_colored}")
178170
} else {
179-
render_row!(align_left(slice, tree_width))
180-
}
171+
aligned_normal = align_left(&slice, tree_width);
172+
format_args!("{aligned_normal}")
173+
};
174+
175+
format!(
176+
"{size} {tree}│{bar}│{ratio}",
177+
size = align_right(&initial_row.size, size_width),
178+
bar = proportion_bar.display(self.bar_alignment),
179+
ratio = align_right(&initial_row.percentage, PERCENTAGE_COLUMN_MAX_WIDTH),
180+
)
181181
})
182182
.collect()
183183
}

0 commit comments

Comments
 (0)