mirror of https://github.com/XAMPPRocky/tokei
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
This commit is contained in:
parent
8e37ba8e64
commit
a69e16b225
|
|
@ -1,6 +1,7 @@
|
||||||
use std::{env, fs, path::PathBuf};
|
use std::{env, fs, path::PathBuf};
|
||||||
|
|
||||||
use crate::language::LanguageType;
|
use crate::language::LanguageType;
|
||||||
|
use crate::sort::Sort;
|
||||||
|
|
||||||
/// A configuration struct for how [`Languages::get_statistics`] searches and
|
/// A configuration struct for how [`Languages::get_statistics`] searches and
|
||||||
/// counts languages.
|
/// counts languages.
|
||||||
|
|
@ -31,6 +32,8 @@ pub struct Config {
|
||||||
/// Whether to treat doc strings in languages as comments. *Default:*
|
/// Whether to treat doc strings in languages as comments. *Default:*
|
||||||
/// `false`.
|
/// `false`.
|
||||||
pub treat_doc_strings_as_comments: Option<bool>,
|
pub treat_doc_strings_as_comments: Option<bool>,
|
||||||
|
/// Sort languages. *Default:* `None`.
|
||||||
|
pub sort: Option<Sort>,
|
||||||
/// Filters languages searched to just those provided. E.g. A directory
|
/// Filters languages searched to just those provided. E.g. A directory
|
||||||
/// containing `C`, `Cpp`, and `Rust` with a `Config.types` of `[Cpp, Rust]`
|
/// containing `C`, `Cpp`, and `Rust` with a `Config.types` of `[Cpp, Rust]`
|
||||||
/// will count only `Cpp` and `Rust`. *Default:* `None`.
|
/// 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: current_dir.treat_doc_strings_as_comments.or(home_dir
|
||||||
.treat_doc_strings_as_comments
|
.treat_doc_strings_as_comments
|
||||||
.or(conf_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)),
|
types: current_dir.types.or(home_dir.types.or(conf_dir.types)),
|
||||||
no_ignore: current_dir
|
no_ignore: current_dir
|
||||||
.no_ignore
|
.no_ignore
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
print_header(&mut stdout, &row, columns)?;
|
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 {
|
for (_, ref mut language) in &mut languages {
|
||||||
language.sort_by(sort_category)
|
language.sort_by(sort_category)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
src/sort.rs
13
src/sort.rs
|
|
@ -1,5 +1,7 @@
|
||||||
use std::{borrow::Cow, str::FromStr};
|
use std::{borrow::Cow, str::FromStr};
|
||||||
|
|
||||||
|
use serde::de::{self, Deserialize, Deserializer};
|
||||||
|
|
||||||
/// Used for sorting languages.
|
/// Used for sorting languages.
|
||||||
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
|
||||||
pub enum Sort {
|
pub enum Sort {
|
||||||
|
|
@ -30,6 +32,17 @@ impl FromStr for Sort {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for Sort {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
String::deserialize(deserializer)?
|
||||||
|
.parse()
|
||||||
|
.map_err(de::Error::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> From<Sort> for Cow<'a, Sort> {
|
impl<'a> From<Sort> for Cow<'a, Sort> {
|
||||||
fn from(from: Sort) -> Self {
|
fn from(from: Sort) -> Self {
|
||||||
Cow::Owned(from)
|
Cow::Owned(from)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
# The width of the terminal output in columns.
|
# The width of the terminal output in columns.
|
||||||
columns = 80
|
columns = 80
|
||||||
|
# Sort languages based on the specified column.
|
||||||
|
sort = "lines"
|
||||||
# If set, tokei will only show the languages in `types`.
|
# If set, tokei will only show the languages in `types`.
|
||||||
types = ["Python"]
|
types = ["Python"]
|
||||||
# Any doc strings (e.g. `"""hello"""` in python) will be counted as comments.
|
# Any doc strings (e.g. `"""hello"""` in python) will be counted as comments.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue