2017-10-21 10:16:03 +02:00
|
|
|
// Copyright (c) 2017 fd developers
|
|
|
|
// Licensed under the Apache License, Version 2.0
|
|
|
|
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
|
|
|
|
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
|
|
|
// at your option. All files in the project carrying such
|
|
|
|
// notice may not be copied, modified, or distributed except
|
|
|
|
// according to those terms.
|
|
|
|
|
2019-01-05 18:17:22 +01:00
|
|
|
use crate::exit_codes::ExitCode;
|
|
|
|
use crate::internal::opts::FdOptions;
|
2018-12-09 14:56:05 +01:00
|
|
|
use lscolors::{LsColors, Style};
|
2017-10-10 08:01:17 +02:00
|
|
|
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::ops::Deref;
|
2018-01-01 12:16:43 +01:00
|
|
|
use std::path::{self, Component, Path, PathBuf};
|
2018-04-13 22:46:17 +02:00
|
|
|
use std::process;
|
2017-11-22 23:05:09 +01:00
|
|
|
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.
|
|
|
|
fn strip_current_dir<'a>(pathbuf: &'a PathBuf) -> &'a Path {
|
|
|
|
let mut iter = pathbuf.components();
|
|
|
|
let mut iter_next = iter.clone();
|
|
|
|
if iter_next.next() == Some(Component::CurDir) {
|
|
|
|
iter.next();
|
|
|
|
}
|
|
|
|
iter.as_path()
|
|
|
|
}
|
|
|
|
|
2017-11-22 23:05:09 +01:00
|
|
|
pub fn print_entry(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
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
let r = if let Some(ref ls_colors) = config.ls_colors {
|
2017-11-22 23:05:09 +01:00
|
|
|
print_entry_colorized(path, config, ls_colors, &wants_to_quit)
|
2017-10-14 02:30:19 +02:00
|
|
|
} else {
|
2017-10-18 20:04:34 +02:00
|
|
|
print_entry_uncolorized(path, config)
|
2017-10-14 02:30:19 +02:00
|
|
|
};
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
if r.is_err() {
|
|
|
|
// Probably a broken pipe. Exit gracefully.
|
2018-10-03 15:27:53 +02:00
|
|
|
process::exit(ExitCode::GeneralError.into());
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-11-22 23:05:09 +01:00
|
|
|
fn print_entry_colorized(
|
|
|
|
path: &Path,
|
|
|
|
config: &FdOptions,
|
|
|
|
ls_colors: &LsColors,
|
|
|
|
wants_to_quit: &Arc<AtomicBool>,
|
|
|
|
) -> io::Result<()> {
|
2017-10-14 02:30:19 +02:00
|
|
|
let default_style = ansi_term::Style::default();
|
2017-10-10 08:01:17 +02:00
|
|
|
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
// Separator to use before the current component.
|
|
|
|
let mut separator = String::new();
|
2017-10-14 02:30:19 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
// Full path to the current component.
|
2017-10-18 20:04:34 +02:00
|
|
|
let mut component_path = PathBuf::new();
|
2017-10-14 02:30:19 +02:00
|
|
|
|
|
|
|
// Traverse the path and colorize each component
|
2017-10-14 03:44:24 +02:00
|
|
|
for component in path.components() {
|
2017-10-14 02:30:19 +02:00
|
|
|
let comp_str = component.as_os_str().to_string_lossy();
|
|
|
|
component_path.push(Path::new(comp_str.deref()));
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2018-12-09 14:56:05 +01:00
|
|
|
let style = ls_colors.style_for_path(&component_path);
|
|
|
|
let style = style
|
|
|
|
.map(Style::to_ansi_term_style)
|
|
|
|
.unwrap_or(default_style);
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
write!(handle, "{}{}", separator, style.paint(comp_str))?;
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
// Determine separator to print before next component.
|
|
|
|
separator = match component {
|
|
|
|
// Prefix needs no separator, as it is always followed by RootDir.
|
|
|
|
Component::Prefix(_) => String::new(),
|
|
|
|
// RootDir is already a separator.
|
|
|
|
Component::RootDir => String::new(),
|
|
|
|
// Everything else uses a separator that is painted the same way as the component.
|
|
|
|
_ => style.paint(path::MAIN_SEPARATOR.to_string()).to_string(),
|
|
|
|
};
|
2017-11-22 23:05:09 +01:00
|
|
|
|
|
|
|
if wants_to_quit.load(Ordering::Relaxed) {
|
|
|
|
write!(handle, "\n")?;
|
2018-10-03 15:27:53 +02:00
|
|
|
process::exit(ExitCode::KilledBySigint.into());
|
2017-11-22 23:05:09 +01:00
|
|
|
}
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
if config.null_separator {
|
|
|
|
write!(handle, "\0")
|
2017-10-10 08:01:17 +02:00
|
|
|
} else {
|
2017-10-14 02:30:19 +02:00
|
|
|
writeln!(handle, "")
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {
|
2017-10-14 02:30:19 +02:00
|
|
|
let separator = if config.null_separator { "\0" } else { "\n" };
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
let path_str = path.to_string_lossy();
|
|
|
|
write!(&mut io::stdout(), "{}{}", path_str, separator)
|
|
|
|
}
|