fd/src/output.rs

100 lines
2.6 KiB
Rust
Raw Normal View History

use crate::exit_codes::ExitCode;
use crate::internal::opts::FdOptions;
use lscolors::{LsColors, Style};
2017-10-10 08:01:17 +02:00
use std::borrow::Cow;
2019-01-01 22:52:08 +01:00
use std::io::{self, StdoutLock, Write};
2019-01-01 22:23:05 +01:00
use std::path::{Component, Path, PathBuf};
2018-04-13 22:46:17 +02:00
use std::process;
use std::sync::atomic::{AtomicBool, Ordering};
2018-04-13 22:46:17 +02:00
use std::sync::Arc;
2017-10-10 08:01:17 +02:00
use ansi_term;
2018-02-24 17:02:14 +01:00
/// Remove the `./` prefix from a path.
2019-01-26 02:13:16 +01:00
fn strip_current_dir(pathbuf: &PathBuf) -> &Path {
2018-02-24 17:02:14 +01:00
let mut iter = pathbuf.components();
let mut iter_next = iter.clone();
if iter_next.next() == Some(Component::CurDir) {
iter.next();
}
iter.as_path()
}
2019-01-01 22:52:08 +01:00
pub fn print_entry(
stdout: &mut StdoutLock,
entry: &PathBuf,
config: &FdOptions,
wants_to_quit: &Arc<AtomicBool>,
) {
2018-02-24 17:02:14 +01:00
let path = if entry.is_absolute() {
entry.as_path()
} else {
strip_current_dir(entry)
};
2017-10-14 03:44:24 +02:00
let r = if let Some(ref ls_colors) = config.ls_colors {
2019-01-01 22:52:08 +01:00
print_entry_colorized(stdout, path, config, ls_colors, &wants_to_quit)
} else {
2019-01-01 22:52:08 +01:00
print_entry_uncolorized(stdout, path, config)
};
2017-10-10 08:01:17 +02:00
if r.is_err() {
// Probably a broken pipe. Exit gracefully.
process::exit(ExitCode::GeneralError.into());
}
}
2017-10-10 08:01:17 +02:00
2019-09-15 10:36:40 +02:00
fn replace_path_separator<'a>(config: &FdOptions, path: &mut Cow<'a, str>) {
match &config.path_separator {
2019-09-15 10:36:40 +02:00
None => {}
Some(sep) => {
*path.to_mut() = path.replace(std::path::MAIN_SEPARATOR, &sep);
}
}
}
fn print_entry_colorized(
2019-01-01 22:52:08 +01:00
stdout: &mut StdoutLock,
path: &Path,
config: &FdOptions,
ls_colors: &LsColors,
wants_to_quit: &Arc<AtomicBool>,
) -> io::Result<()> {
let default_style = ansi_term::Style::default();
2017-10-10 08:01:17 +02:00
// Traverse the path and colorize each component
2019-01-01 22:23:05 +01:00
for (component, style) in ls_colors.style_for_path_components(path) {
let style = style
.map(Style::to_ansi_term_style)
.unwrap_or(default_style);
2017-10-10 08:01:17 +02:00
2019-09-15 10:36:40 +02:00
let mut path_string = component.to_string_lossy();
replace_path_separator(&config, &mut path_string);
write!(stdout, "{}", style.paint(path_string))?;
if wants_to_quit.load(Ordering::Relaxed) {
2019-01-26 02:13:16 +01:00
writeln!(stdout)?;
process::exit(ExitCode::KilledBySigint.into());
}
}
2017-10-10 08:01:17 +02:00
if config.null_separator {
2019-01-01 22:52:08 +01:00
write!(stdout, "\0")
2017-10-10 08:01:17 +02:00
} else {
2019-01-26 02:13:16 +01:00
writeln!(stdout)
}
}
2017-10-10 08:01:17 +02:00
2019-01-01 22:52:08 +01:00
fn print_entry_uncolorized(
stdout: &mut StdoutLock,
path: &Path,
config: &FdOptions,
) -> io::Result<()> {
let separator = if config.null_separator { "\0" } else { "\n" };
2017-10-10 08:01:17 +02:00
2019-09-15 10:36:40 +02:00
let mut path_str = path.to_string_lossy();
replace_path_separator(&config, &mut path_str);
2019-01-01 22:52:08 +01:00
write!(stdout, "{}{}", path_str, separator)
2017-10-14 03:44:24 +02:00
}