2018-03-27 14:58:26 +00:00
|
|
|
use ansi_term::Style;
|
2018-12-07 23:43:31 +00:00
|
|
|
|
|
|
|
use crate::fs::File;
|
|
|
|
use crate::info::filetype::FileExtensions;
|
Massive theming and view options refactor
This commit significantly refactors the way that options are parsed. It introduces the Theme type which contains both styling and extension configuration, converts the option-parsing process into a being a pure function, and removes some rather gnarly old code.
The main purpose of the refactoring is to fix GH-318, "Tests fail when not connected to a terminal". Even though exa was compiling fine on my machine and on Travis, it was failing for automated build scripts. This was because of what the option-parsing code was trying to accomplish: it wasn't just providing a struct of the user's settings, it was also checking the terminal, providing a View directly.
This has been changed so that the options module now _only_ looks at the command-line arguments and environment variables. Instead of returning a View, it returns the user's _preference_, and it's then up to the 'main' module to examine the terminal width and figure out if the view is doable, downgrading it if necessary.
The code that used to determine the view was horrible and I'm pleased it can be cut out. Also, the terminal width used to be in a lazy_static because it was queried multiple times, and now it's not in one because it's only queried once, which is a good sign for things going in the right direction.
There are also some naming and organisational changes around themes. The blanket terms "Colours" and "Styles" have been yeeted in favour of "Theme", which handles both extensions and UI colours. The FileStyle struct has been replaced with file_name::Options, making it similar to the views in how it has an Options struct and a Render struct.
Finally, eight unit tests have been removed because they turned out to be redundant (testing --colour and --color) after examining the tangled code, and the default theme has been put in its own file in preparation for more themes.
2020-10-22 21:34:00 +00:00
|
|
|
use crate::theme::Theme;
|
2018-03-22 23:13:02 +00:00
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2018-03-27 17:00:38 +00:00
|
|
|
pub trait FileIcon {
|
2020-10-13 00:36:41 +00:00
|
|
|
fn icon_file(&self, file: &File<'_>) -> Option<char>;
|
2018-03-27 17:00:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2020-10-10 14:30:19 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2018-03-27 17:00:38 +00:00
|
|
|
pub enum Icons {
|
|
|
|
Audio,
|
|
|
|
Image,
|
|
|
|
Video,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Icons {
|
2020-10-10 14:30:19 +00:00
|
|
|
pub fn value(self) -> char {
|
|
|
|
match self {
|
2020-10-10 18:49:46 +00:00
|
|
|
Self::Audio => '\u{f001}',
|
|
|
|
Self::Image => '\u{f1c5}',
|
|
|
|
Self::Video => '\u{f03d}',
|
2018-03-27 17:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
Massive theming and view options refactor
This commit significantly refactors the way that options are parsed. It introduces the Theme type which contains both styling and extension configuration, converts the option-parsing process into a being a pure function, and removes some rather gnarly old code.
The main purpose of the refactoring is to fix GH-318, "Tests fail when not connected to a terminal". Even though exa was compiling fine on my machine and on Travis, it was failing for automated build scripts. This was because of what the option-parsing code was trying to accomplish: it wasn't just providing a struct of the user's settings, it was also checking the terminal, providing a View directly.
This has been changed so that the options module now _only_ looks at the command-line arguments and environment variables. Instead of returning a View, it returns the user's _preference_, and it's then up to the 'main' module to examine the terminal width and figure out if the view is doable, downgrading it if necessary.
The code that used to determine the view was horrible and I'm pleased it can be cut out. Also, the terminal width used to be in a lazy_static because it was queried multiple times, and now it's not in one because it's only queried once, which is a good sign for things going in the right direction.
There are also some naming and organisational changes around themes. The blanket terms "Colours" and "Styles" have been yeeted in favour of "Theme", which handles both extensions and UI colours. The FileStyle struct has been replaced with file_name::Options, making it similar to the views in how it has an Options struct and a Render struct.
Finally, eight unit tests have been removed because they turned out to be redundant (testing --colour and --color) after examining the tangled code, and the default theme has been put in its own file in preparation for more themes.
2020-10-22 21:34:00 +00:00
|
|
|
pub fn painted_icon(file: &File<'_>, theme: &Theme) -> String {
|
|
|
|
use crate::output::file_name::Colours;
|
|
|
|
|
2020-10-10 14:30:19 +00:00
|
|
|
let file_icon = icon(file).to_string();
|
Massive theming and view options refactor
This commit significantly refactors the way that options are parsed. It introduces the Theme type which contains both styling and extension configuration, converts the option-parsing process into a being a pure function, and removes some rather gnarly old code.
The main purpose of the refactoring is to fix GH-318, "Tests fail when not connected to a terminal". Even though exa was compiling fine on my machine and on Travis, it was failing for automated build scripts. This was because of what the option-parsing code was trying to accomplish: it wasn't just providing a struct of the user's settings, it was also checking the terminal, providing a View directly.
This has been changed so that the options module now _only_ looks at the command-line arguments and environment variables. Instead of returning a View, it returns the user's _preference_, and it's then up to the 'main' module to examine the terminal width and figure out if the view is doable, downgrading it if necessary.
The code that used to determine the view was horrible and I'm pleased it can be cut out. Also, the terminal width used to be in a lazy_static because it was queried multiple times, and now it's not in one because it's only queried once, which is a good sign for things going in the right direction.
There are also some naming and organisational changes around themes. The blanket terms "Colours" and "Styles" have been yeeted in favour of "Theme", which handles both extensions and UI colours. The FileStyle struct has been replaced with file_name::Options, making it similar to the views in how it has an Options struct and a Render struct.
Finally, eight unit tests have been removed because they turned out to be redundant (testing --colour and --color) after examining the tangled code, and the default theme has been put in its own file in preparation for more themes.
2020-10-22 21:34:00 +00:00
|
|
|
let c = theme.colour_file(file);
|
|
|
|
|
|
|
|
// Remove underline from icon
|
|
|
|
let painted =
|
|
|
|
if c.is_underline {
|
|
|
|
match c.foreground {
|
|
|
|
Some(color) => {
|
|
|
|
Style::from(color).paint(file_icon).to_string()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
Style::default().paint(file_icon).to_string()
|
2018-03-27 14:58:26 +00:00
|
|
|
}
|
2020-10-10 18:49:46 +00:00
|
|
|
}
|
Massive theming and view options refactor
This commit significantly refactors the way that options are parsed. It introduces the Theme type which contains both styling and extension configuration, converts the option-parsing process into a being a pure function, and removes some rather gnarly old code.
The main purpose of the refactoring is to fix GH-318, "Tests fail when not connected to a terminal". Even though exa was compiling fine on my machine and on Travis, it was failing for automated build scripts. This was because of what the option-parsing code was trying to accomplish: it wasn't just providing a struct of the user's settings, it was also checking the terminal, providing a View directly.
This has been changed so that the options module now _only_ looks at the command-line arguments and environment variables. Instead of returning a View, it returns the user's _preference_, and it's then up to the 'main' module to examine the terminal width and figure out if the view is doable, downgrading it if necessary.
The code that used to determine the view was horrible and I'm pleased it can be cut out. Also, the terminal width used to be in a lazy_static because it was queried multiple times, and now it's not in one because it's only queried once, which is a good sign for things going in the right direction.
There are also some naming and organisational changes around themes. The blanket terms "Colours" and "Styles" have been yeeted in favour of "Theme", which handles both extensions and UI colours. The FileStyle struct has been replaced with file_name::Options, making it similar to the views in how it has an Options struct and a Render struct.
Finally, eight unit tests have been removed because they turned out to be redundant (testing --colour and --color) after examining the tangled code, and the default theme has been put in its own file in preparation for more themes.
2020-10-22 21:34:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
c.paint(file_icon).to_string()
|
|
|
|
};
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2019-12-29 01:52:18 +00:00
|
|
|
format!("{} ", painted)
|
2018-03-22 23:13:02 +00:00
|
|
|
}
|
2018-03-25 01:18:49 +00:00
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2020-10-13 00:36:41 +00:00
|
|
|
fn icon(file: &File<'_>) -> char {
|
2018-03-27 17:00:38 +00:00
|
|
|
let extensions = Box::new(FileExtensions);
|
2020-10-10 18:49:46 +00:00
|
|
|
|
|
|
|
if file.points_to_directory() {
|
|
|
|
'\u{f115}'
|
|
|
|
}
|
|
|
|
else if let Some(icon) = extensions.icon_file(file) {
|
|
|
|
icon
|
|
|
|
}
|
2020-04-19 04:33:42 +00:00
|
|
|
else if let Some(ext) = file.ext.as_ref() {
|
|
|
|
match ext.as_str() {
|
|
|
|
"ai" => '\u{e7b4}',
|
|
|
|
"android" => '\u{e70e}',
|
|
|
|
"apple" => '\u{f179}',
|
|
|
|
"avro" => '\u{e60b}',
|
|
|
|
"clj" => '\u{e768}',
|
|
|
|
"coffee" => '\u{f0f4}',
|
|
|
|
"cpp" => '\u{e61d}',
|
|
|
|
"hpp" => '\u{e61d}',
|
|
|
|
"c" => '\u{e61e}',
|
|
|
|
"h" => '\u{e61e}',
|
|
|
|
"cs" => '\u{f81a}',
|
|
|
|
"css" => '\u{e749}',
|
|
|
|
"d" => '\u{e7af}',
|
|
|
|
"dart" => '\u{e798}',
|
|
|
|
"db" => '\u{f1c0}',
|
|
|
|
"diff" => '\u{f440}',
|
|
|
|
"patch" => '\u{f440}',
|
|
|
|
"rtf" => '\u{f1c2}',
|
|
|
|
"doc" => '\u{f1c2}',
|
|
|
|
"docx" => '\u{f1c2}',
|
|
|
|
"odt" => '\u{f1c2}',
|
|
|
|
"ebook" => '\u{e28b}',
|
|
|
|
"env" => '\u{f462}',
|
|
|
|
"epub" => '\u{e28a}',
|
|
|
|
"erl" => '\u{e7b1}',
|
|
|
|
"font" => '\u{f031}',
|
|
|
|
"gform" => '\u{f298}',
|
|
|
|
"git" => '\u{f1d3}',
|
|
|
|
"go" => '\u{e626}',
|
|
|
|
"hs" => '\u{e777}',
|
|
|
|
"htm" => '\u{f13b}',
|
|
|
|
"html" => '\u{f13b}',
|
|
|
|
"xhtml" => '\u{f13b}',
|
|
|
|
"iml" => '\u{e7b5}',
|
|
|
|
"java" => '\u{e204}',
|
|
|
|
"js" => '\u{e74e}',
|
|
|
|
"mjs" => '\u{e74e}',
|
|
|
|
"json" => '\u{e60b}',
|
|
|
|
"jsx" => '\u{e7ba}',
|
|
|
|
"vue" => '\u{fd42}',
|
|
|
|
"node" => '\u{f898}',
|
|
|
|
"less" => '\u{e758}',
|
|
|
|
"log" => '\u{f18d}',
|
|
|
|
"lua" => '\u{e620}',
|
|
|
|
"md" => '\u{f48a}',
|
|
|
|
"markdown" => '\u{f48a}',
|
|
|
|
"mustache" => '\u{e60f}',
|
|
|
|
"npmignore" => '\u{e71e}',
|
|
|
|
"pdf" => '\u{f1c1}',
|
|
|
|
"djvu" => '\u{f02d}',
|
|
|
|
"mobi" => '\u{f02d}',
|
|
|
|
"php" => '\u{e73d}',
|
|
|
|
"pl" => '\u{e769}',
|
|
|
|
"ppt" => '\u{f1c4}',
|
|
|
|
"pptx" => '\u{f1c4}',
|
|
|
|
"odp" => '\u{f1c4}',
|
|
|
|
"psd" => '\u{e7b8}',
|
|
|
|
"py" => '\u{e606}',
|
|
|
|
"r" => '\u{f25d}',
|
|
|
|
"rb" => '\u{e21e}',
|
|
|
|
"ru" => '\u{e21e}',
|
|
|
|
"erb" => '\u{e21e}',
|
|
|
|
"gem" => '\u{e21e}',
|
|
|
|
"rdb" => '\u{e76d}',
|
|
|
|
"rs" => '\u{e7a8}',
|
|
|
|
"rss" => '\u{f09e}',
|
|
|
|
"rubydoc" => '\u{e73b}',
|
|
|
|
"sass" => '\u{e74b}',
|
|
|
|
"stylus" => '\u{e759}',
|
|
|
|
"scala" => '\u{e737}',
|
|
|
|
"shell" => '\u{f489}',
|
|
|
|
"sqlite3" => '\u{e7c4}',
|
|
|
|
"styl" => '\u{e600}',
|
|
|
|
"latex" => '\u{e600}',
|
|
|
|
"tex" => '\u{e600}',
|
|
|
|
"ts" => '\u{e628}',
|
|
|
|
"tsx" => '\u{e628}',
|
|
|
|
"twig" => '\u{e61c}',
|
|
|
|
"txt" => '\u{f15c}',
|
|
|
|
"video" => '\u{f03d}',
|
|
|
|
"vim" => '\u{e62b}',
|
|
|
|
"xml" => '\u{e619}',
|
|
|
|
"yml" => '\u{f481}',
|
|
|
|
"yaml" => '\u{f481}',
|
|
|
|
"rar" => '\u{f410}',
|
|
|
|
"zip" => '\u{f410}',
|
|
|
|
"bz" => '\u{f410}',
|
|
|
|
"bz2" => '\u{f410}',
|
|
|
|
"xz" => '\u{f410}',
|
|
|
|
"taz" => '\u{f410}',
|
|
|
|
"tbz" => '\u{f410}',
|
|
|
|
"tbz2" => '\u{f410}',
|
|
|
|
"tz" => '\u{f410}',
|
|
|
|
"tar" => '\u{f410}',
|
|
|
|
"tzo" => '\u{f410}',
|
|
|
|
"lz" => '\u{f410}',
|
|
|
|
"lzh" => '\u{f410}',
|
|
|
|
"lzma" => '\u{f410}',
|
|
|
|
"lzo" => '\u{f410}',
|
|
|
|
"gz" => '\u{f410}',
|
|
|
|
"deb" => '\u{e77d}',
|
|
|
|
"rpm" => '\u{e7bb}',
|
|
|
|
"exe" => '\u{e70f}',
|
|
|
|
"msi" => '\u{e70f}',
|
|
|
|
"dll" => '\u{e70f}',
|
|
|
|
"cab" => '\u{e70f}',
|
|
|
|
"bat" => '\u{e70f}',
|
|
|
|
"cmd" => '\u{e70f}',
|
2020-10-08 21:30:20 +00:00
|
|
|
"sh" => '\u{f489}',
|
|
|
|
"bash" => '\u{f489}',
|
|
|
|
"zsh" => '\u{f489}',
|
|
|
|
"fish" => '\u{f489}',
|
|
|
|
"csh" => '\u{f489}',
|
2020-04-19 04:33:42 +00:00
|
|
|
"ini" => '\u{e615}',
|
|
|
|
"toml" => '\u{e615}',
|
|
|
|
"cfg" => '\u{e615}',
|
|
|
|
"conf" => '\u{e615}',
|
|
|
|
"apk" => '\u{e70e}',
|
|
|
|
"ttf" => '\u{f031}',
|
|
|
|
"woff" => '\u{f031}',
|
|
|
|
"woff2" => '\u{f031}',
|
|
|
|
"otf" => '\u{f031}',
|
|
|
|
"csv" => '\u{f1c3}',
|
|
|
|
"tsv" => '\u{f1c3}',
|
|
|
|
"xls" => '\u{f1c3}',
|
|
|
|
"xlsx" => '\u{f1c3}',
|
|
|
|
"ods" => '\u{f1c3}',
|
|
|
|
"so" => '\u{f17c}',
|
|
|
|
"sql" => '\u{f1c0}',
|
|
|
|
"jar" => '\u{e256}',
|
|
|
|
"jad" => '\u{e256}',
|
|
|
|
"class" => '\u{e256}',
|
|
|
|
"war" => '\u{e256}',
|
|
|
|
"groovy" => '\u{e775}',
|
|
|
|
"iso" => '\u{e271}',
|
|
|
|
"lock" => '\u{f023}',
|
2020-10-08 21:23:18 +00:00
|
|
|
"swift" => '\u{e755}',
|
2020-10-08 21:56:16 +00:00
|
|
|
"nix" => '\u{f313}',
|
2020-10-08 21:00:36 +00:00
|
|
|
_ => '\u{f016}'
|
2018-03-25 01:18:49 +00:00
|
|
|
}
|
2020-10-10 18:49:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-10-08 21:00:36 +00:00
|
|
|
'\u{f016}'
|
2018-03-25 01:18:49 +00:00
|
|
|
}
|
|
|
|
}
|