From a69e16b225676b08607b7ec4c731f3fb61c29b36 Mon Sep 17 00:00:00 2001 From: Theo Date: Tue, 2 Jun 2020 14:03:54 +0200 Subject: [PATCH] Specify the --sort option in the configuration files (#550) * Specify the --sort option in the configuration files * Let --sort option be case insensitive in configuration files * style: inline sort variable * Add an example of using sort to the tokei.example.toml --- src/config.rs | 4 ++++ src/main.rs | 2 +- src/sort.rs | 13 +++++++++++++ tokei.example.toml | 2 ++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index a8e0aa4..a5a4053 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use std::{env, fs, path::PathBuf}; use crate::language::LanguageType; +use crate::sort::Sort; /// A configuration struct for how [`Languages::get_statistics`] searches and /// counts languages. @@ -31,6 +32,8 @@ pub struct Config { /// Whether to treat doc strings in languages as comments. *Default:* /// `false`. pub treat_doc_strings_as_comments: Option, + /// Sort languages. *Default:* `None`. + pub sort: Option, /// Filters languages searched to just those provided. E.g. A directory /// containing `C`, `Cpp`, and `Rust` with a `Config.types` of `[Cpp, Rust]` /// will count only `Cpp` and `Rust`. *Default:* `None`. @@ -96,6 +99,7 @@ impl Config { treat_doc_strings_as_comments: current_dir.treat_doc_strings_as_comments.or(home_dir .treat_doc_strings_as_comments .or(conf_dir.treat_doc_strings_as_comments)), + sort: current_dir.sort.or(home_dir.sort.or(conf_dir.sort)), types: current_dir.types.or(home_dir.types.or(conf_dir.types)), no_ignore: current_dir .no_ignore diff --git a/src/main.rs b/src/main.rs index 14544c0..c7b26fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ fn main() -> Result<(), Box> { print_header(&mut stdout, &row, columns)?; - if let Some(sort_category) = cli.sort { + if let Some(sort_category) = cli.sort.or(config.sort) { for (_, ref mut language) in &mut languages { language.sort_by(sort_category) } diff --git a/src/sort.rs b/src/sort.rs index 4ad548c..a18b194 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -1,5 +1,7 @@ use std::{borrow::Cow, str::FromStr}; +use serde::de::{self, Deserialize, Deserializer}; + /// Used for sorting languages. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] pub enum Sort { @@ -30,6 +32,17 @@ impl FromStr for Sort { } } +impl<'de> Deserialize<'de> for Sort { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + String::deserialize(deserializer)? + .parse() + .map_err(de::Error::custom) + } +} + impl<'a> From for Cow<'a, Sort> { fn from(from: Sort) -> Self { Cow::Owned(from) diff --git a/tokei.example.toml b/tokei.example.toml index 69d034e..16d268c 100644 --- a/tokei.example.toml +++ b/tokei.example.toml @@ -1,5 +1,7 @@ # The width of the terminal output in columns. columns = 80 +# Sort languages based on the specified column. +sort = "lines" # If set, tokei will only show the languages in `types`. types = ["Python"] # Any doc strings (e.g. `"""hello"""` in python) will be counted as comments.