Skip to content
Permalink
Browse files

Added arguments to allow for disabling ignoring

  • Loading branch information...
Aaronepower committed Mar 11, 2019
1 parent fb15e3c commit 1035614b9a8cb729d1c7e94b86d23780a796aed4
Showing with 533 additions and 53 deletions.
  1. +257 −10 Cargo.lock
  2. +4 −2 Cargo.toml
  3. +70 −7 src/cli.rs
  4. +21 −6 src/config.rs
  5. +0 −9 src/language/language_type.hbs.rs
  6. +9 −0 src/language/language_type.rs
  7. +1 −1 src/language/mod.rs
  8. +3 −4 src/main.rs
  9. +1 −1 src/stats.rs
  10. +166 −12 src/utils/fs.rs
  11. +1 −1 tests/accuracy.rs

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -3,6 +3,7 @@ authors = ["Aaronepower <theaaronepower@gmail.com>"]
build = "build.rs"
categories = ["command-line-utilities", "development-tools", "visualization"]
description = "A utility that allows you to count code, quickly."
edition = "2018"
homepage = "https://tokei.rs"
include = ["Cargo.lock", "Cargo.toml", "LICENCE-APACHE", "LICENCE-MIT", "build.rs", "languages.json", "src/**/*"]
keywords = ["utility", "cli", "cloc", "lines", "statistics"]
@@ -11,7 +12,6 @@ name = "tokei"
readme = "README.md"
repository = "https://github.com/Aaronepower/tokei.git"
version = "9.0.0"
edition = "2018"

[badges]
[badges.appveyor]
@@ -61,7 +61,8 @@ version = "0.8"
[dev-dependencies]
lazy_static = "1"
regex = "1.0"
tempdir = "0.3"
tempfile = "3.0.7"
git2 = "0.8.0"

[features]
all = ["json", "cbor", "yaml"]
@@ -70,6 +71,7 @@ default = []
json = ["serde_json"]
yaml = ["serde_yaml"]

[profile]
[profile.release]
lto = true
panic = "abort"
@@ -1,16 +1,23 @@
use std::mem;

use clap::{clap_app, crate_description, ArgMatches};
use tokei::{Config, LanguageType, Sort};

use crate::{cli_utils::*, input::Format};
use tokei::{LanguageType, Sort};

#[derive(Debug)]
pub struct Cli<'a> {
matches: ArgMatches<'a>,
pub output: Option<Format>,
pub columns: Option<usize>,
pub files: bool,
pub hidden: bool,
pub no_ignore: bool,
pub no_ignore_parent: bool,
pub no_ignore_vcs: bool,
pub output: Option<Format>,
pub print_languages: bool,
pub sort: Option<Sort>,
pub types: Option<Vec<LanguageType>>,
pub columns: Option<usize>,
pub verbose: u64,
}

@@ -35,12 +42,19 @@ impl<'a> Cli<'a> {
+takes_value
"Gives statistics from a previous tokei run. Can be given a file path, \
or \"stdin\" to read from stdin.")
(@arg hidden: --hidden "Count hidden files.")
(@arg input:
conflicts_with[languages] ...
"The input file(s)/directory(ies) to be counted.")
(@arg languages: -l --languages
conflicts_with[input]
"Prints out supported languages and their extensions.")
(@arg no_ignore: --("no-ignore")
"Don't respect ignore files.")
(@arg no_ignore_parent: --("no-ignore-parent")
"Don't respect ignore files in parent directories.")
(@arg no_ignore_vcs: --("no-ignore-vcs")
"Don't respect VCS ignore files.")
(@arg output: -o --output
// `all` is used so to fail later with a better error
possible_values(Format::all())
@@ -64,6 +78,10 @@ impl<'a> Cli<'a> {

let columns = matches.value_of("columns").map(parse_or_exit::<usize>);
let files = matches.is_present("files");
let hidden = matches.is_present("hidden");
let no_ignore = matches.is_present("no_ignore");
let no_ignore_parent = matches.is_present("no_ignore_parent");
let no_ignore_vcs = matches.is_present("no_ignore_vcs");
let print_languages = matches.is_present("languages");
let verbose = matches.occurrences_of("verbose");
let types = matches.value_of("types").map(|e| {
@@ -82,16 +100,26 @@ impl<'a> Cli<'a> {
let output = matches.value_of("output")
.map(parse_or_exit::<Format>);

Cli {
crate::cli_utils::setup_logger(verbose);

let cli = Cli {
columns,
files,
hidden,
matches,
no_ignore,
no_ignore_parent,
no_ignore_vcs,
output,
files,
print_languages,
sort,
types,
verbose,
columns,
}
};

debug!("CLI Config: {:#?}", cli);

cli
}

pub fn file_input(&self) -> Option<&str> {
@@ -119,6 +147,41 @@ impl<'a> Cli<'a> {
}
}

/// Overrides the shared options (See `tokei::Config` for option
/// descriptions) between the CLI and the config files. CLI flags have
/// higher precedence than options present in config files.
///
/// #### Shared options
/// * `no_ignore`
/// * `no_ignore_parent`
/// * `no_ignore_vcs`
/// * `types`
pub fn override_config(&mut self, mut config: Config) -> Config {
config.hidden = match self.hidden {
true => Some(true),
false => config.hidden,
};

config.no_ignore = match self.no_ignore {
true => Some(true),
false => config.no_ignore
};

config.no_ignore_parent = match self.no_ignore_parent {
true => Some(true),
false => config.no_ignore_parent
};

config.no_ignore_vcs = match self.no_ignore_vcs {
true => Some(true),
false => config.no_ignore_vcs
};

config.types = mem::replace(&mut self.types, None).or(config.types);

config
}

pub fn print_input_parse_failure(input_filename: &str) {
eprintln!("Error:\n Failed to parse input file: {}", input_filename);

@@ -19,21 +19,33 @@ use crate::language::LanguageType;
/// [`Languages::get_statistics`]: struct.Languages.html#method.get_statistics
#[derive(Debug, Default, Deserialize)]
pub struct Config {
/// Number columns to print. (Only used in the CLI).
/// Width of columns to be printed to the terminal. _This option is ignored
/// in the library._ *Default:* Auto detected width of the terminal.
pub columns: Option<usize>,
// /// A map of individual language configuration.
// pub languages: Option<HashMap<LanguageType, LanguageConfig>>,
/// Whether to treat doc strings in languages as comments. By default this
/// will be `false`.
/// Count hidden files and directories. *Default:* `false`.
pub hidden: Option<bool>,
/// Don't respect ignore files. *Default:* `false`.
pub no_ignore: Option<bool>,
/// Don't respect ignore files in parent directories. *Default:* `false`.
pub no_ignore_parent: Option<bool>,
/// Don't respect VCS ignore files. *Default:* `false`.
pub no_ignore_vcs: Option<bool>,
/// Whether to treat doc strings in languages as comments. *Default:*
/// `false`.
pub treat_doc_strings_as_comments: Option<bool>,
/// 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`.
/// will count only `Cpp` and `Rust`. *Default:* `None`.
pub types: Option<Vec<LanguageType>>,
// /// A map of individual language configuration.
// pub languages: Option<HashMap<LanguageType, LanguageConfig>>,
}

impl Config {

/// Get either `tokei.toml` or `.tokeirc`. `tokei.toml` takes precedence
/// over `.tokeirc` as the latter is a hidden file on Unix and not idiomatic
/// on Windows.
fn get_config(base: PathBuf) -> Option<Self> {
fs::read_to_string(base.join("tokei.toml"))
.ok()
@@ -79,6 +91,9 @@ impl Config {
treat_doc_strings_as_comments: current_dir.treat_doc_strings_as_comments
.or(conf_dir.treat_doc_strings_as_comments),
types: current_dir.types.or(conf_dir.types),
no_ignore: current_dir.no_ignore.or(conf_dir.no_ignore),
no_ignore_parent: current_dir.no_ignore_parent.or(conf_dir.no_ignore_parent),
no_ignore_vcs: current_dir.no_ignore_vcs.or(conf_dir.no_ignore_vcs),
..Self::default()
}
}
@@ -498,12 +498,3 @@ pub fn get_filetype_from_shebang(file: &Path) -> Option<&'static str>
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn rust() {
assert_eq!(LanguageType::Rust.allows_nested(), true);
}
}
@@ -201,3 +201,12 @@ impl LanguageType {
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn rust_allows_nested() {
assert!(LanguageType::Rust.allows_nested());
}
}
@@ -12,7 +12,7 @@ use crate::sort::Sort::{self, *};
use crate::stats::Stats;

/// A struct representing statistics about a single Language.
#[derive(Clone, Debug, Deserialize, Default, Serialize)]
#[derive(Clone, Debug, Deserialize, Default, PartialEq, Serialize)]
pub struct Language {
/// The total number of blank lines.
pub blanks: usize,
@@ -1,3 +1,5 @@
#[macro_use] extern crate log;

mod cli;
mod cli_utils;
mod input;
@@ -11,16 +13,13 @@ use tokei::{Language, Languages, Sort, Config};

fn main() -> Result<(), Box<Error>> {
let mut cli = Cli::from_args();
let mut config = Config::from_config_files();

config.types = ::std::mem::replace(&mut cli.types, None).or(config.types);

if cli.print_languages {
Cli::print_supported_languages();
process::exit(0);
}

setup_logger(cli.verbose);
let config = cli.override_config(Config::from_config_files());
let mut languages = Languages::new();

if let Some(input) = cli.file_input() {
@@ -3,7 +3,7 @@ use std::path::PathBuf;

/// A struct representing the statistics of a file.
#[derive(Deserialize, Serialize)]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct Stats {
/// Number of blank lines within the file.
pub blanks: usize,
Oops, something went wrong.

0 comments on commit 1035614

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.