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
89use std::env;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use lscolors::{LsColors, Style};
#[cfg(all(
not(feature = "nu-ansi-term"),
not(feature = "gnu_legacy"),
not(feature = "ansi_term"),
not(feature = "crossterm"),
not(feature = "owo-colors")
))]
compile_error!(
"one feature must be enabled: ansi_term, nu-ansi-term, crossterm, gnu_legacy, owo-colors"
);
fn print_path(handle: &mut dyn Write, ls_colors: &LsColors, path: &str) -> io::Result<()> {
for (component, style) in ls_colors.style_for_path_components(Path::new(path)) {
#[cfg(any(feature = "nu-ansi-term", feature = "gnu_legacy"))]
{
let ansi_style = style.map(Style::to_nu_ansi_term_style).unwrap_or_default();
write!(handle, "{}", ansi_style.paint(component.to_string_lossy()))?;
}
#[cfg(feature = "ansi_term")]
{
let ansi_style = style.map(Style::to_ansi_term_style).unwrap_or_default();
write!(handle, "{}", ansi_style.paint(component.to_string_lossy()))?;
}
#[cfg(feature = "crossterm")]
{
let ansi_style = style.map(Style::to_crossterm_style).unwrap_or_default();
write!(handle, "{}", ansi_style.apply(component.to_string_lossy()))?;
}
#[cfg(feature = "owo-colors")]
{
use owo_colors::OwoColorize;
let ansi_style = style.map(Style::to_owo_colors_style).unwrap_or_default();
write!(handle, "{}", component.to_string_lossy().style(ansi_style))?;
}
}
writeln!(handle)?;
Ok(())
}
fn run() -> io::Result<()> {
let ls_colors = LsColors::from_env().unwrap_or_default();
let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut args = env::args();
if args.len() >= 2 {
// Skip program name
args.next();
for arg in args {
print_path(&mut stdout, &ls_colors, &arg)?;
}
} else {
let stdin = io::stdin();
let mut buf = vec![];
while let Ok(size) = stdin.lock().read_until(b'\n', &mut buf) {
if size == 0 {
break;
}
let path_str = String::from_utf8_lossy(&buf[..(buf.len() - 1)]);
#[cfg(windows)]
let path_str = path_str.trim_end_matches('\r');
print_path(&mut stdout, &ls_colors, path_str.as_ref())?;
buf.clear();
}
}
Ok(())
}
fn main() {
run().ok();
}