use std::fmt::Debug;
use std::path::Path;
use nu_ansi_term::{AnsiString as ANSIString, Style};
use path_clean;
use unicode_width::UnicodeWidthStr;
use crate::fs::{File, FileTarget};
use crate::output::cell::TextCellContents;
use crate::output::escape;
use crate::output::icons::{icon_for_file, iconify_style};
use crate::output::render::FiletypeColours;
use crate::theme::FileNameStyle;
#[derive(Debug, Copy, Clone)]
pub struct Options {
pub classify: Classify,
pub show_icons: ShowIcons,
pub quote_style: QuoteStyle,
pub embed_hyperlinks: EmbedHyperlinks,
pub absolute: Absolute,
pub is_a_tty: bool,
}
impl Options {
pub fn for_file<'a, 'dir, C>(
self,
file: &'a File<'dir>,
colours: &'a C,
) -> FileName<'a, 'dir, C> {
FileName {
file,
colours,
link_style: LinkStyle::JustFilenames,
options: self,
target: if file.is_link() {
Some(file.link_target())
} else {
None
},
mount_style: MountStyle::JustDirectoryNames,
}
}
}
#[derive(PartialEq, Debug, Copy, Clone)]
enum LinkStyle {
JustFilenames,
FullLinkPaths,
}
#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)]
pub enum Classify {
#[default]
JustFilenames,
AddFileIndicators,
AutomaticAddFileIndicators,
}
#[derive(PartialEq, Debug, Copy, Clone)]
enum MountStyle {
JustDirectoryNames,
MountInfo,
}
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum ShowIcons {
Always(u32),
Automatic(u32),
Never,
}
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum EmbedHyperlinks {
Off,
On,
}
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum Absolute {
On,
Follow,
Off,
}
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum QuoteStyle {
NoQuotes,
QuoteSpaces,
}
pub struct FileName<'a, 'dir, C> {
file: &'a File<'dir>,
colours: &'a C,
target: Option<FileTarget<'dir>>,
link_style: LinkStyle,
pub options: Options,
mount_style: MountStyle,
}
impl<C> FileName<'_, '_, C> {
#[must_use]
pub fn with_link_paths(mut self) -> Self {
if !self.file.deref_links {
self.link_style = LinkStyle::FullLinkPaths;
}
self
}
#[must_use]
pub fn with_mount_details(mut self, enable: bool) -> Self {
self.mount_style = if enable {
MountStyle::MountInfo
} else {
MountStyle::JustDirectoryNames
};
self
}
}
impl<C: Colours> FileName<'_, '_, C> {
#[must_use]
pub fn paint(&self) -> TextCellContents {
let mut bits = Vec::new();
let (icon_override, filename_style_override) = match self.colours.style_override(self.file)
{
Some(FileNameStyle { icon, filename }) => (icon, filename),
None => (None, None),
};
let spaces_count_opt = match self.options.show_icons {
ShowIcons::Always(spaces_count) => Some(spaces_count),
ShowIcons::Automatic(spaces_count) if self.options.is_a_tty => Some(spaces_count),
_ => None,
};
let should_add_classify_char = match self.options.classify {
Classify::AddFileIndicators => true,
Classify::AutomaticAddFileIndicators if self.options.is_a_tty => true,
_ => false,
};
if let Some(spaces_count) = spaces_count_opt {
let (style, icon) = match icon_override {
Some(icon_override) => (
if let Some(style_override) = icon_override.style {
style_override
} else {
iconify_style(self.style())
},
icon_override
.glyph
.unwrap_or_else(|| icon_for_file(self.file))
.to_string(),
),
None => (
iconify_style(self.style()),
icon_for_file(self.file).to_string(),
),
};
bits.push(style.paint(icon));
bits.push(style.paint(" ".repeat(spaces_count as usize)));
}
if self.file.parent_dir.is_none() && self.options.absolute == Absolute::Off {
if let Some(parent) = self.file.path.parent() {
self.add_parent_bits(&mut bits, parent);
}
}
if !self.file.name.is_empty() {
for bit in self.escaped_file_name(filename_style_override) {
bits.push(bit);
}
}
if let (LinkStyle::FullLinkPaths, Some(target)) = (self.link_style, self.target.as_ref()) {
match target {
FileTarget::Ok(target) => {
bits.push(Style::default().paint(" "));
bits.push(self.colours.normal_arrow().paint("->"));
bits.push(Style::default().paint(" "));
if let Some(parent) = target.path.parent() {
self.add_parent_bits(&mut bits, parent);
}
if !target.name.is_empty() {
let target_options = Options {
classify: Classify::JustFilenames,
quote_style: QuoteStyle::QuoteSpaces,
show_icons: ShowIcons::Never,
embed_hyperlinks: EmbedHyperlinks::Off,
is_a_tty: self.options.is_a_tty,
absolute: Absolute::Off,
};
let target_name = FileName {
file: target,
colours: self.colours,
target: None,
link_style: LinkStyle::FullLinkPaths,
options: target_options,
mount_style: MountStyle::JustDirectoryNames,
};
for bit in target_name.escaped_file_name(filename_style_override) {
bits.push(bit);
}
if should_add_classify_char {
if let Some(class) = self.classify_char(target) {
bits.push(Style::default().paint(class));
}
}
}
}
FileTarget::Broken(broken_path) => {
bits.push(Style::default().paint(" "));
bits.push(self.colours.broken_symlink().paint("->"));
bits.push(Style::default().paint(" "));
escape(
broken_path.display().to_string(),
&mut bits,
self.colours.broken_filename(),
self.colours.broken_control_char(),
self.options.quote_style,
);
}
FileTarget::Err(_) => {
}
}
} else if should_add_classify_char {
if let Some(class) = self.classify_char(self.file) {
bits.push(Style::default().paint(class));
}
}
if self.mount_style == MountStyle::MountInfo {
if let Some(mount_details) = self.file.mount_point_info() {
bits.push(Style::default().paint(" ["));
bits.push(Style::default().paint(mount_details.source.clone()));
bits.push(Style::default().paint(" ("));
bits.push(Style::default().paint(mount_details.fstype.clone()));
bits.push(Style::default().paint(")]"));
}
}
bits.into()
}
fn add_parent_bits(&self, bits: &mut Vec<ANSIString<'_>>, parent: &Path) {
let coconut = parent.components().count();
if coconut == 1 && parent.has_root() {
bits.push(
self.colours
.symlink_path()
.paint(std::path::MAIN_SEPARATOR.to_string()),
);
} else if coconut >= 1 {
escape(
parent.to_string_lossy().to_string(),
bits,
self.colours.symlink_path(),
self.colours.control_char(),
self.options.quote_style,
);
bits.push(
self.colours
.symlink_path()
.paint(std::path::MAIN_SEPARATOR.to_string()),
);
}
}
#[cfg(unix)]
pub(crate) fn classify_char(&self, file: &File<'_>) -> Option<&'static str> {
if file.is_executable_file() {
Some("*")
} else if file.is_directory() {
Some("/")
} else if file.is_pipe() {
Some("|")
} else if file.is_link() {
Some("@")
} else if file.is_socket() {
Some("=")
} else {
None
}
}
#[cfg(windows)]
pub(crate) fn classify_char(&self, file: &File<'_>) -> Option<&'static str> {
if file.is_directory() {
Some("/")
} else if file.is_link() {
Some("@")
} else {
None
}
}
fn escaped_file_name<'unused>(
&self,
style_override: Option<Style>,
) -> Vec<ANSIString<'unused>> {
let file_style = style_override.unwrap_or(self.style());
let mut bits = Vec::new();
let mut display_hyperlink = false;
if self.options.embed_hyperlinks == EmbedHyperlinks::On {
if let Some(abs_path) = self
.file
.absolute_path()
.and_then(|p| p.as_os_str().to_str())
{
bits.push(ANSIString::from(escape::get_hyperlink_start_tag(abs_path)));
display_hyperlink = true;
}
}
escape(
self.display_name(),
&mut bits,
file_style,
self.colours.control_char(),
self.options.quote_style,
);
if display_hyperlink {
bits.push(ANSIString::from(escape::HYPERLINK_CLOSING));
}
bits
}
fn display_name(&self) -> String {
match self.options.absolute {
Absolute::On => std::env::current_dir().ok().and_then(|p| {
path_clean::clean(p.join(&self.file.path))
.to_str()
.map(std::borrow::ToOwned::to_owned)
}),
Absolute::Follow => self
.file
.absolute_path()
.and_then(|p| p.to_str())
.map(std::borrow::ToOwned::to_owned),
Absolute::Off => None,
}
.unwrap_or(self.file.name.clone())
}
#[must_use]
pub fn style(&self) -> Style {
if let LinkStyle::JustFilenames = self.link_style {
if let Some(ref target) = self.target {
if target.is_broken() {
return self.colours.broken_symlink();
}
}
}
#[rustfmt::skip]
return match self.file {
f if f.is_mount_point() => self.colours.mount_point(),
f if f.is_directory() => self.colours.directory(),
#[cfg(unix)]
f if f.is_executable_file() => self.colours.executable_file(),
f if f.is_link() => self.colours.symlink(),
#[cfg(unix)]
f if f.is_pipe() => self.colours.pipe(),
#[cfg(unix)]
f if f.is_block_device() => self.colours.block_device(),
#[cfg(unix)]
f if f.is_char_device() => self.colours.char_device(),
#[cfg(unix)]
f if f.is_socket() => self.colours.socket(),
f if ! f.is_file() => self.colours.special(),
_ => self.colours.colour_file(self.file),
};
}
#[must_use]
pub fn bare_utf8_width(&self) -> usize {
UnicodeWidthStr::width(self.file.name.as_str())
}
}
pub trait Colours: FiletypeColours {
fn symlink_path(&self) -> Style;
fn normal_arrow(&self) -> Style;
fn broken_symlink(&self) -> Style;
fn broken_filename(&self) -> Style;
fn control_char(&self) -> Style;
fn broken_control_char(&self) -> Style;
fn executable_file(&self) -> Style;
fn mount_point(&self) -> Style;
fn colour_file(&self, file: &File<'_>) -> Style;
fn style_override(&self, file: &File<'_>) -> Option<FileNameStyle>;
}