log: 📝 unique error output and update comment to fit config

This commit is contained in:
zwPapEr 2020-11-28 23:19:50 +08:00 committed by Abin Simon
parent cf9030cdf7
commit 9b237bbf9e
19 changed files with 36 additions and 67 deletions

View file

@ -134,8 +134,7 @@ The [release page](https://github.com/Peltoche/lsd/releases) includes precompile
## Configuration ## Configuration
`lsd` can be configured with a configuration file to set the default options. `lsd` can be configured with a configuration file to set the default options.
Right now this only supports setting options that can be passed via the command Check [Config file content](#config-file-content) for details.
line options as well.
### Config file location ### Config file location
@ -145,19 +144,20 @@ On non-Windows systems `lsd` follows the
[XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
convention for the location of the configuration file. The configuration dir convention for the location of the configuration file. The configuration dir
`lsd` uses is itself named `lsd`. In that directory it looks first for a file `lsd` uses is itself named `lsd`. In that directory it looks first for a file
called `config.yaml` and if it can't find one, a file named `config.yml`. called `config.yaml`.
For most people it should be enough to put their config file at For most people it should be enough to put their config file at
`~/.config/lsd/config.yaml`. `~/.config/lsd/config.yaml`.
#### Windows #### Windows
On Windows systems `lsd` only looks for the two files in one location: On Windows systems `lsd` only looks for the `config.yaml` files in one location:
`%APPDATA%\lsd\` `%APPDATA%\lsd\`
### Config file content ### Config file content
This is an example config file with the default values and some additional This is an example config file with the default values and some additional
remarks. remarks.
```yaml ```yaml
# == Classic == # == Classic ==
# This is a shorthand to override some of the options to be backwards compatible # This is a shorthand to override some of the options to be backwards compatible

View file

@ -97,7 +97,7 @@ impl Config {
Err(e) => { Err(e) => {
match e.kind() { match e.kind() {
std::io::ErrorKind::NotFound => {} std::io::ErrorKind::NotFound => {}
_ => print_error!("bad config file: {}, {}\n", &file, e), _ => print_error!("Can not open config file {}: {}.", &file, e),
}; };
None None
} }
@ -109,7 +109,7 @@ impl Config {
match serde_yaml::from_str::<Self>(yaml) { match serde_yaml::from_str::<Self>(yaml) {
Ok(c) => Some(c), Ok(c) => Some(c),
Err(e) => { Err(e) => {
print_error!("configuration file format error, {}\n\n", e); print_error!("Configuration file format error, {}.", e);
None None
} }
} }
@ -126,7 +126,7 @@ impl Config {
return Some(p); return Some(p);
} }
} }
Err(e) => print_error!("can not open config file: {}", e), Err(e) => print_error!("Can not open config file: {}.", e),
} }
None None
} }

View file

@ -89,7 +89,7 @@ impl Core {
let mut meta = match Meta::from_path(&path, self.flags.dereference.0) { let mut meta = match Meta::from_path(&path, self.flags.dereference.0) {
Ok(meta) => meta, Ok(meta) => meta,
Err(err) => { Err(err) => {
print_error!("lsd: {}: {}\n", path.display(), err); print_error!("{}: {}.", path.display(), err);
continue; continue;
} }
}; };

View file

@ -24,11 +24,6 @@ impl Blocks {
/// `Blocks` does not contain a [Block] of variant [INode](Block::INode) yet, one is prepended /// `Blocks` does not contain a [Block] of variant [INode](Block::INode) yet, one is prepended
/// to the returned value. /// to the returned value.
/// ///
/// # Note
///
/// The configuration file's Yaml is read in any case, to be able to check for errors and print
/// out warnings.
///
/// # Errors /// # Errors
/// ///
/// This errors if any of the [ArgMatches] parameter arguments causes [Block]'s implementation /// This errors if any of the [ArgMatches] parameter arguments causes [Block]'s implementation
@ -119,26 +114,6 @@ impl Blocks {
} }
} }
/// Get a [Blocks] from a [Yaml] array. The [Config] is used to log warnings about wrong values
/// in a Yaml.
// fn from_yaml_array(values: &[Yaml], config: &Config) -> Option<Self> {
// let mut blocks: Vec<Block> = vec![];
// for array_el in values.iter() {
// match array_el {
// Yaml::String(value) => match Block::try_from(value.as_str()) {
// Ok(block) => blocks.push(block),
// Err(err) => config.print_warning(&err),
// },
// _ => config.print_warning("The blocks config values have to be strings."),
// }
// }
// if blocks.is_empty() {
// None
// } else {
// Some(Self(blocks))
// }
// }
/// This returns a Blocks struct for the long format. /// This returns a Blocks struct for the long format.
/// ///
/// It contains the [Block]s [Permission](Block::Permission), [User](Block::User), /// It contains the [Block]s [Permission](Block::Permission), [User](Block::User),

View file

@ -36,8 +36,7 @@ pub enum ColorOption {
} }
impl ColorOption { impl ColorOption {
/// Get a Color value from a [Yaml] string. The [Config] is used to log warnings about wrong /// Get a Color value from a [String].
/// values in a Yaml.
fn from_str(value: &str) -> Option<Self> { fn from_str(value: &str) -> Option<Self> {
match value { match value {
"always" => Some(Self::Always), "always" => Some(Self::Always),
@ -45,7 +44,7 @@ impl ColorOption {
"never" => Some(Self::Never), "never" => Some(Self::Never),
_ => { _ => {
print_error!( print_error!(
"color/when could only be one of auto, always and never, got {}", "Config color.when could only be one of auto, always and never, got {}.",
&value &value
); );
None None
@ -67,7 +66,7 @@ impl Configurable<Self> for ColorOption {
if let Some(color) = matches.value_of("color") { if let Some(color) = matches.value_of("color") {
Self::from_str(&color) Self::from_str(&color)
} else { } else {
print_error!("bad color, should never happen"); print_error!("Bad color args. This should not be reachable!");
None None
} }
} else { } else {

View file

@ -1,4 +1,4 @@
//! This module defines the [DateFlag]. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [DateFlag]. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use its [configure_from](Configurable::configure_from) method. //! [Default] value, use its [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;
@ -23,7 +23,7 @@ impl DateFlag {
match app::validate_time_format(&value) { match app::validate_time_format(&value) {
Ok(()) => Some(Self::Formatted(value[1..].to_string())), Ok(()) => Some(Self::Formatted(value[1..].to_string())),
_ => { _ => {
print_error!("Not a valid date format: {}", value); print_error!("Not a valid date format: {}.", value);
None None
} }
} }
@ -36,7 +36,7 @@ impl DateFlag {
"relative" => Some(Self::Relative), "relative" => Some(Self::Relative),
_ if value.starts_with('+') => Self::from_format_string(&value), _ if value.starts_with('+') => Self::from_format_string(&value),
_ => { _ => {
print_error!("Not a valid date value: {}", value); print_error!("Not a valid date value: {}.", value);
None None
} }
} }

View file

@ -1,4 +1,4 @@
//! This module defines the [Dereference] flag. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [Dereference] flag. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use the [configure_from](Configurable::configure_from) method. //! [Default] value, use the [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;

View file

@ -1,4 +1,4 @@
//! This module defines the [Display] flag. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [Display] flag. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use its [configure_from](Configurable::configure_from) method. //! [Default] value, use its [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;

View file

@ -1,4 +1,4 @@
//! This module defines the [IconOption]. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [IconOption]. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use its [configure_from](Configurable::configure_from) method. //! [Default] value, use its [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;
@ -94,14 +94,13 @@ pub enum IconTheme {
} }
impl IconTheme { impl IconTheme {
/// Get a value from a [Yaml] string. The [Config] is used to log warnings about wrong values /// Get a value from a string.
/// in a Yaml.
fn from_str(value: &str) -> Option<Self> { fn from_str(value: &str) -> Option<Self> {
match value { match value {
"fancy" => Some(Self::Fancy), "fancy" => Some(Self::Fancy),
"unicode" => Some(Self::Unicode), "unicode" => Some(Self::Unicode),
_ => { _ => {
print_error!("icons->theme: {}", &value); print_error!("Bad icons.theme config, {}", &value);
None None
} }
} }

View file

@ -1,4 +1,4 @@
//! This module defines the [IgnoreGlobs]. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [IgnoreGlobs]. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use the [configure_from](IgnoreGlobs::configure_from) method. //! [Default] value, use the [configure_from](IgnoreGlobs::configure_from) method.
use crate::config_file::Config; use crate::config_file::Config;
@ -17,11 +17,6 @@ impl IgnoreGlobs {
/// - [from_config](IgnoreGlobs::from_config) /// - [from_config](IgnoreGlobs::from_config)
/// - [Default::default] /// - [Default::default]
/// ///
/// # Note
///
/// The configuration file's Yaml is read in any case, to be able to check for errors and print
/// out warnings.
///
/// # Errors /// # Errors
/// ///
/// If either of the [Glob::new] or [GlobSetBuilder.build] methods return an [Err]. /// If either of the [Glob::new] or [GlobSetBuilder.build] methods return an [Err].

View file

@ -1,4 +1,4 @@
//! This module defines the [Indicators] flag. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [Indicators] flag. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use the [configure_from](Configurable::configure_from) method. //! [Default] value, use the [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;

View file

@ -1,4 +1,4 @@
//! This module defines the [Layout] flag. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [Layout] flag. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use its [configure_from](Configurable::configure_from) method. //! [Default] value, use its [configure_from](Configurable::configure_from) method.
use crate::config_file::Config; use crate::config_file::Config;

View file

@ -1,4 +1,4 @@
//! This module defines the [Recursion] options. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [Recursion] options. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use the [configure_from](Recursion::configure_from) method. //! [Default] value, use the [configure_from](Recursion::configure_from) method.
use crate::config_file::Config; use crate::config_file::Config;

View file

@ -1,4 +1,4 @@
//! This module defines the [SizeFlag]. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [SizeFlag]. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use its [configure_from](Configurable::configure_from) method. //! [Default] value, use its [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;
@ -29,7 +29,7 @@ impl SizeFlag {
"bytes" => Some(Self::Bytes), "bytes" => Some(Self::Bytes),
_ => { _ => {
print_error!( print_error!(
"size can only be one of default, short or bytes, but got {}", "Size can only be one of default, short or bytes, but got {}.",
value value
); );
None None

View file

@ -1,4 +1,4 @@
//! This module defines the [Sorting] options. To set it up from [ArgMatches], a [Yaml] //! This module defines the [Sorting] options. To set it up from [ArgMatches], a [Config]
//! and its [Default] value, use the [configure_from](Sorting::configure_from) method. //! and its [Default] value, use the [configure_from](Sorting::configure_from) method.
use super::Configurable; use super::Configurable;

View file

@ -1,4 +1,4 @@
//! This module defines the [NoSymlink] flag. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [NoSymlink] flag. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use the [configure_from](Configurable::configure_from) method. //! [Default] value, use the [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;

View file

@ -1,4 +1,4 @@
//! This module defines the [TotalSize] flag. To set it up from [ArgMatches], a [Yaml] and its //! This module defines the [TotalSize] flag. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use the [configure_from](Configurable::configure_from) method. //! [Default] value, use the [configure_from](Configurable::configure_from) method.
use super::Configurable; use super::Configurable;

View file

@ -57,7 +57,8 @@ macro_rules! print_error {
let mut handle = stderr.lock(); let mut handle = stderr.lock();
// We can write on stderr, so we simply ignore the error and don't print // We can write on stderr, so we simply ignore the error and don't print
// and stop with success. // and stop with success.
let res = handle.write_all(std::format!($($arg)*).as_bytes()); let res = handle.write_all(std::format!("lsd: {}\n\n",
std::format!($($arg)*)).as_bytes());
if res.is_err() { if res.is_err() {
std::process::exit(0); std::process::exit(0);
} }

View file

@ -71,7 +71,7 @@ impl Meta {
let entries = match self.path.read_dir() { let entries = match self.path.read_dir() {
Ok(entries) => entries, Ok(entries) => entries,
Err(err) => { Err(err) => {
print_error!("lsd: {}: {}\n", self.path.display(), err); print_error!("{}: {}.", self.path.display(), err);
return Ok(None); return Ok(None);
} }
}; };
@ -112,7 +112,7 @@ impl Meta {
let mut entry_meta = match Self::from_path(&path, flags.dereference.0) { let mut entry_meta = match Self::from_path(&path, flags.dereference.0) {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(err) => {
print_error!("lsd: {}: {}\n", path.display(), err); print_error!("{}: {}.", path.display(), err);
continue; continue;
} }
}; };
@ -129,7 +129,7 @@ impl Meta {
match entry_meta.recurse_into(depth - 1, &flags) { match entry_meta.recurse_into(depth - 1, &flags) {
Ok(content) => entry_meta.content = content, Ok(content) => entry_meta.content = content,
Err(err) => { Err(err) => {
print_error!("lsd: {}: {}\n", path.display(), err); print_error!("{}: {}.", path.display(), err);
continue; continue;
} }
}; };
@ -167,7 +167,7 @@ impl Meta {
let metadata = match metadata { let metadata = match metadata {
Ok(meta) => meta, Ok(meta) => meta,
Err(err) => { Err(err) => {
print_error!("lsd: {}: {}\n", path.display(), err); print_error!("{}: {}.", path.display(), err);
return 0; return 0;
} }
}; };
@ -180,7 +180,7 @@ impl Meta {
let entries = match path.read_dir() { let entries = match path.read_dir() {
Ok(entries) => entries, Ok(entries) => entries,
Err(err) => { Err(err) => {
print_error!("lsd: {}: {}\n", path.display(), err); print_error!("{}: {}.", path.display(), err);
return size; return size;
} }
}; };
@ -188,7 +188,7 @@ impl Meta {
let path = match entry { let path = match entry {
Ok(entry) => entry.path(), Ok(entry) => entry.path(),
Err(err) => { Err(err) => {
print_error!("lsd: {}: {}\n", path.display(), err); print_error!("{}: {}.", path.display(), err);
continue; continue;
} }
}; };