Fixed tests

This commit is contained in:
Aaronepower 2016-06-10 22:33:27 +01:00
parent 9a3dbf1a4a
commit b46bf60ab8
14 changed files with 632 additions and 477 deletions

565
Cargo.lock generated
View file

@ -1,283 +1,282 @@
[root] [root]
name = "tokei" name = "tokei"
version = "2.1.3" version = "2.1.3"
dependencies = [ dependencies = [
"clap 2.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"maplit 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "maplit 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_cbor 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde_cbor 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_codegen 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde_codegen 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"serializable_enum 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "serializable_enum 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.7.2" version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "aster" name = "aster"
version = "0.17.0" version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "0.5.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "0.3.13" version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.5.2" version = "2.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "deque" name = "deque"
version = "0.3.1" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "glob" name = "glob"
version = "0.2.11" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "kernel32-sys" name = "kernel32-sys"
version = "0.2.2" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.11" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "log" name = "log"
version = "0.3.6" version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "maplit" name = "maplit"
version = "0.1.3" version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.1.32" version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "0.2.12" version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "quasi" name = "quasi"
version = "0.11.0" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "quasi_codegen" name = "quasi_codegen"
version = "0.11.0" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", "aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.3.14" version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "0.3.1" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "rustc-serialize" name = "rustc-serialize"
version = "0.3.19" version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "serde" name = "serde"
version = "0.7.7" version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "serde_cbor" name = "serde_cbor"
version = "0.3.3" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"byteorder 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "serde_codegen" name = "serde_codegen"
version = "0.7.7" version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", "aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "quasi 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi_codegen 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "quasi_codegen 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "0.7.1" version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "serde_yaml" name = "serde_yaml"
version = "0.2.4" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "serializable_enum" name = "serializable_enum"
version = "0.3.0" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.4.1" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "syntex" name = "syntex"
version = "0.33.0" version = "0.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "syntex_syntax" name = "syntex_syntax"
version = "0.33.0" version = "0.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "term" name = "term"
version = "0.2.14" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.1.30" version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", ]
]
[[package]]
[[package]] name = "unicode-width"
name = "unicode-width" version = "0.1.3"
version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
[[package]] name = "unicode-xid"
name = "unicode-xid" version = "0.0.3"
version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
[[package]] name = "vec_map"
name = "vec_map" version = "0.6.0"
version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
[[package]] name = "walkdir"
name = "walkdir" version = "0.1.5"
version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index"
source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [
dependencies = [ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", ]
]
[[package]]
[[package]] name = "winapi"
name = "winapi" version = "0.2.7"
version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
[[package]] name = "winapi-build"
name = "winapi-build" version = "0.1.1"
version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
[[package]] name = "yaml-rust"
name = "yaml-rust" version = "0.3.3"
version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index"
source = "registry+https://github.com/rust-lang/crates.io-index"

View file

@ -1,54 +1,58 @@
# Copyright (c) 2015 Aaron Power # Copyright (c) 2015 Aaron Power
# Use of this source code is governed by the MIT/APACHE2.0 license that can be # Use of this source code is governed by the MIT/APACHE2.0 license that can be
# found in the LICENCE-{APACHE, MIT} file. # found in the LICENCE-{APACHE, MIT} file.
[package] [package]
name = "tokei" name = "tokei"
version = "2.1.3" version = "3.0.0"
authors = ["Aaronepower <theaaronepower@gmail.com>"] authors = ["Aaronepower <theaaronepower@gmail.com>"]
repository = "https://github.com/Aaronepower/tokei.git" repository = "https://github.com/Aaronepower/tokei.git"
homepage = "https://aaronepower.github.io/tokei/" homepage = "https://aaronepower.github.io/tokei/"
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
readme = "README.md" readme = "README.md"
description = "Count code, quickly." description = "Count code, quickly."
build = "src/lib/build.rs" build = "src/lib/build.rs"
[lib] [lib]
name = "tokei" name = "tokei"
path = "src/lib/lib.rs" path = "src/lib/lib.rs"
[[bin]] [[bin]]
name = "tokei" name = "tokei"
path = "src/main.rs" path = "src/main.rs"
doc = false doc = false
# For building serde in stable. # For building serde in stable.
[build-dependencies] [build-dependencies]
serde_codegen = "0.7.7" serde_codegen = "0.7.7"
syntex = "0.33.0" syntex = "0.33.0"
# Dependencies, and why they are used. # Dependencies, and why they are used.
# - Clap: For CLI argument parsing. # - Clap: For CLI argument parsing.
# - Glob: When a user passes in a ./*.rs path. # - Glob: When a user passes in a ./*.rs path.
# - Maplit: Cleaner initialization of the language map. # - Maplit: Cleaner initialization of the language map.
# - Rayon: Parallelization of language analysation. # - Rayon: Parallelization of language analysation.
# - Rustc-serialize: Converting String to hex, for CBOR. # - Rustc-serialize: Converting String to hex, for CBOR.
# - Serde: Converting Language struct to other formats. # - Serde: Converting Language struct to other formats.
# - Serde_cbor: To CBOR. http://cbor.io/ # - Serde_cbor: To CBOR. http://cbor.io/
# - Serde_json: To JSON. http://json.org/ # - Serde_json: To JSON. http://json.org/
# - Serde_yaml: To YAML. http://yaml.org/ # - Serde_yaml: To YAML. http://yaml.org/
# - serializable_enum: To fix a dumb change in behaviour in serde, hopefully to be removed next release. https://github.com/serde-rs/serde/issues/251 # - serializable_enum: To fix a dumb change in behaviour in serde, hopefully to be removed next release. https://github.com/serde-rs/serde/issues/251
# - Toml: To TOML https://github.com/toml-lang/toml # - Toml: To TOML https://github.com/toml-lang/toml
# - Walkdir: Handling recursively reading directories across Windows, OSX, and UNIX. # - Walkdir: Handling recursively reading directories across Windows, OSX, and UNIX.
[dependencies] [dependencies]
clap = {version = "2.5.1", features = ["yaml"]} clap = {version = "2.5.1", features = ["yaml"]}
glob = "0.2.11" glob = "0.2.11"
maplit = "0.1.3" maplit = "0.1.3"
rayon = "0.3.1" rayon = "0.3.1"
rustc-serialize = "0.3.19" rustc-serialize = "0.3.19"
serde = "0.7.5" serde = "0.7.5"
serde_cbor = "0.3.3" serde_cbor = "0.3.3"
serde_json = "0.7.0" serde_json = "0.7.0"
serde_yaml = "0.2.4" serde_yaml = "0.2.4"
serializable_enum = "0.3.0" serializable_enum = "0.3.0"
toml = {version = "0.1.30", features = ["serde"]} walkdir = "0.1.5"
walkdir = "0.1.5"
[dependencies.toml]
version = "0.1.30"
default-features = false
features = ["serde"]

View file

@ -1,7 +1,8 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::ops::AddAssign; use std::ops::AddAssign;
use utils::*; use sort::Sort;
use sort::Sort::*;
use stats::Stats; use stats::Stats;
/// Struct representing a single Language. /// Struct representing a single Language.
@ -29,14 +30,13 @@ pub struct Language {
/// Whether the language supports nested multi line comments or not. /// Whether the language supports nested multi line comments or not.
#[serde(skip_deserializing, skip_serializing)] #[serde(skip_deserializing, skip_serializing)]
pub nested: bool, pub nested: bool,
/// The total number of files from `stats`.
pub total_files: usize,
} }
impl Language { impl Language {
/// Constructs a new empty Language with the comments provided. /// Constructs a new empty Language with the comments provided.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let mut rust = Language::new(vec!["//"], vec![("/*", "*/")]); /// let mut rust = Language::new(vec!["//"], vec![("/*", "*/")]);
/// ``` /// ```
pub fn new(line_comment: Vec<&'static str>, pub fn new(line_comment: Vec<&'static str>,
@ -53,9 +53,10 @@ impl Language {
/// Convience constructor for creating a language that has no commenting syntax. /// Convience constructor for creating a language that has no commenting syntax.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let json = Language::new_blank(); /// let json = Language::new_blank();
/// /// let blank_vec: Vec<&str> = vec![];
/// assert_eq!(json.line_comment, vec![]); /// assert_eq!(json.line_comment, blank_vec);
/// ``` /// ```
pub fn new_blank() -> Self { pub fn new_blank() -> Self {
Self::default() Self::default()
@ -64,6 +65,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as C like languages. /// Convience constructor for creating a language that has the same commenting syntax as C like languages.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let rust = Language::new(vec!["//"], vec![("/*", "*/")]); /// let rust = Language::new(vec!["//"], vec![("/*", "*/")]);
/// let c = Language::new_c(); /// let c = Language::new_c();
/// ///
@ -81,6 +83,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as ML like languages. /// Convience constructor for creating a language that has the same commenting syntax as ML like languages.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let ocaml = Language::new_multi(vec![("(*", "*)")]); /// let ocaml = Language::new_multi(vec![("(*", "*)")]);
/// let coq = Language::new_func(); /// let coq = Language::new_func();
/// ///
@ -94,6 +97,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as HTML like languages. /// Convience constructor for creating a language that has the same commenting syntax as HTML like languages.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let xml = Language::new_multi(vec![("<!--", "-->")]); /// let xml = Language::new_multi(vec![("<!--", "-->")]);
/// let html = Language::new_html(); /// let html = Language::new_html();
/// ///
@ -107,6 +111,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as Bash. /// Convience constructor for creating a language that has the same commenting syntax as Bash.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let bash = Language::new_single(vec!["#"]); /// let bash = Language::new_single(vec!["#"]);
/// let yaml = Language::new_hash(); /// let yaml = Language::new_hash();
/// ///
@ -120,6 +125,7 @@ impl Language {
/// Convience constructor for creating a language that only has multi line comments. /// Convience constructor for creating a language that only has multi line comments.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let mustache = Language::new_multi(vec![("{{!", "}}")]); /// let mustache = Language::new_multi(vec![("{{!", "}}")]);
/// ``` /// ```
pub fn new_multi(multi_line: Vec<(&'static str, &'static str)>) -> Self { pub fn new_multi(multi_line: Vec<(&'static str, &'static str)>) -> Self {
@ -129,6 +135,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as Prolog. /// Convience constructor for creating a language that has the same commenting syntax as Prolog.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let prolog = Language::new(vec!["%"], vec![("/*", "*/")]); /// let prolog = Language::new(vec!["%"], vec![("/*", "*/")]);
/// let oz = Language::new_pro(); /// let oz = Language::new_pro();
/// ///
@ -146,6 +153,7 @@ impl Language {
/// Convience constructor for creating a language that only has single line comments. /// Convience constructor for creating a language that only has single line comments.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let haskell = Language::new_single(vec!["--"]); /// let haskell = Language::new_single(vec!["--"]);
/// ``` /// ```
pub fn new_single(line_comment: Vec<&'static str>) -> Self { pub fn new_single(line_comment: Vec<&'static str>) -> Self {
@ -155,6 +163,7 @@ impl Language {
/// Checks if the language is empty. Empty meaning it doesn't have any statistics. /// Checks if the language is empty. Empty meaning it doesn't have any statistics.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let rust = Language::new_c(); /// let rust = Language::new_c();
/// ///
/// assert!(rust.is_empty()); /// assert!(rust.is_empty());
@ -166,6 +175,7 @@ impl Language {
/// Checks if the language doesn't contain any comments. /// Checks if the language doesn't contain any comments.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let json = Language::new_blank(); /// let json = Language::new_blank();
/// ///
/// assert!(json.is_blank()); /// assert!(json.is_blank());
@ -177,6 +187,7 @@ impl Language {
/// Specify if the the language supports nested multi line comments. /// Specify if the the language supports nested multi line comments.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let mut rust = Language::new(vec!["//"], vec![("/*", "*/")]).nested(); /// let mut rust = Language::new(vec!["//"], vec![("/*", "*/")]).nested();
/// assert!(rust.nested); /// assert!(rust.nested);
/// ``` /// ```
@ -189,9 +200,10 @@ impl Language {
/// panic!'s if given the wrong category. /// panic!'s if given the wrong category.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let mut rust = Language::new_c(); /// let mut rust = Language::new_c();
/// let foo_stats = Stats::new(); /// let mut foo_stats = Stats::new("foo");
/// let bar_stats = Stats::new(); /// let mut bar_stats = Stats::new("bar");
/// ///
/// foo_stats.code += 20; /// foo_stats.code += 20;
/// bar_stats.code += 10; /// bar_stats.code += 10;
@ -201,26 +213,24 @@ impl Language {
/// ///
/// assert_eq!(rust.stats, vec![bar_stats.clone(), foo_stats.clone()]); /// assert_eq!(rust.stats, vec![bar_stats.clone(), foo_stats.clone()]);
/// ///
/// rust.sort_by(CODE); /// rust.sort_by(Sort::Code);
/// ///
/// assert_eq!(rust.stats, vec![foo_stats, bar_stats]); /// assert_eq!(rust.stats, vec![foo_stats, bar_stats]);
/// ///
/// ``` /// ```
pub fn sort_by(&mut self, category: &str) { pub fn sort_by(&mut self, category: Sort) {
match category { match category {
BLANKS => self.stats.sort_by(|a, b| b.blanks.cmp(&a.blanks)), Blanks => self.stats.sort_by(|a, b| b.blanks.cmp(&a.blanks)),
COMMENTS => self.stats.sort_by(|a, b| b.comments.cmp(&a.comments)), Comments => self.stats.sort_by(|a, b| b.comments.cmp(&a.comments)),
CODE => self.stats.sort_by(|a, b| b.code.cmp(&a.code)), Code => self.stats.sort_by(|a, b| b.code.cmp(&a.code)),
FILES => self.stats.sort_by(|a, b| a.name.cmp(&b.name)), Files => self.stats.sort_by(|a, b| a.name.cmp(&b.name)),
LINES => self.stats.sort_by(|a, b| b.lines.cmp(&a.lines)), Lines => self.stats.sort_by(|a, b| b.lines.cmp(&a.lines)),
_ => unreachable!(),
} }
} }
} }
impl AddAssign for Language { impl AddAssign for Language {
fn add_assign(&mut self, rhs: Self) { fn add_assign(&mut self, rhs: Self) {
self.total_files += rhs.total_files;
self.lines += rhs.lines; self.lines += rhs.lines;
self.comments += rhs.comments; self.comments += rhs.comments;
self.blanks += rhs.blanks; self.blanks += rhs.blanks;
@ -231,7 +241,6 @@ impl AddAssign for Language {
impl<'a> AddAssign<&'a Language> for Language { impl<'a> AddAssign<&'a Language> for Language {
fn add_assign(&mut self, rhs: &'a Self) { fn add_assign(&mut self, rhs: &'a Self) {
self.total_files += rhs.total_files;
self.lines += rhs.lines; self.lines += rhs.lines;
self.comments += rhs.comments; self.comments += rhs.comments;
self.blanks += rhs.blanks; self.blanks += rhs.blanks;
@ -242,7 +251,6 @@ impl<'a> AddAssign<&'a Language> for Language {
impl<'a> AddAssign<&'a mut Language> for Language { impl<'a> AddAssign<&'a mut Language> for Language {
fn add_assign(&mut self, rhs: &mut Self) { fn add_assign(&mut self, rhs: &mut Self) {
self.total_files += rhs.total_files;
self.lines += rhs.lines; self.lines += rhs.lines;
self.comments += rhs.comments; self.comments += rhs.comments;
self.blanks += rhs.blanks; self.blanks += rhs.blanks;

View file

@ -161,8 +161,6 @@ serializable_enum! {
Yaml, Yaml,
/// Zsh /// Zsh
Zsh, Zsh,
#[doc(hidden)]
__Total,
} }
LanguageTypeVisitor LanguageTypeVisitor
} }
@ -254,7 +252,6 @@ impl_as_ref_from_str! {
Xml => "XML", Xml => "XML",
Yaml => "YAML", Yaml => "YAML",
Zsh => "Zsh", Zsh => "Zsh",
__Total => "Total",
} }
Error::Parse Error::Parse
} }
@ -264,6 +261,7 @@ impl LanguageType {
/// Returns the display name of a language. /// Returns the display name of a language.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let bash = LanguageType::Bash; /// let bash = LanguageType::Bash;
/// ///
/// assert_eq!(bash.name(), "BASH"); /// assert_eq!(bash.name(), "BASH");
@ -344,16 +342,16 @@ impl LanguageType {
Xml => "XML", Xml => "XML",
Yaml => "YAML", Yaml => "YAML",
Zsh => "Zsh", Zsh => "Zsh",
__Total => "Total",
} }
} }
/// Get language from it's file extension. /// Get language from it's file extension.
/// ///
/// ``` /// ```no_run
/// let rust = LanguageType::from_extension("rs"); /// # use tokei::*;
/// let rust = LanguageType::from_extension("./main.rs");
/// ///
/// assert_eq!(rust, LanguageType::Rust); /// assert_eq!(rust, Some(LanguageType::Rust));
/// ``` /// ```
pub fn from_extension<P: AsRef<Path>>(entry: P) -> Option<Self> { pub fn from_extension<P: AsRef<Path>>(entry: P) -> Option<Self> {
if let Some(extension) = get_extension(entry) { if let Some(extension) = get_extension(entry) {

View file

@ -12,6 +12,7 @@ use std::ops::{AddAssign, Deref, DerefMut};
use serde_cbor; use serde_cbor;
use serde_json; use serde_json;
use serde_yaml; use serde_yaml;
use toml;
use rayon::prelude::*; use rayon::prelude::*;
use utils::*; use utils::*;
@ -28,6 +29,22 @@ pub struct Languages {
impl Languages { impl Languages {
/// Creates a `Languages` struct from cbor. /// Creates a `Languages` struct from cbor.
///
/// ```
/// # extern crate tokei;
/// # use tokei::*;
/// # extern crate rustc_serialize;
/// # use rustc_serialize::hex::FromHex;
/// # fn main () {
/// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e7473\
/// 0065737461747381a566626c616e6b730564636f64650c68636f6d6d656e74730065\
/// 6c696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e\
/// 6573116b746f74616c5f66696c657301";
///
/// let mut languages = Languages::from_cbor(&*cbor.from_hex().unwrap()).unwrap();
/// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
/// # }
/// ```
pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> serde_cbor::Result<Self> { pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> serde_cbor::Result<Self> {
let map = try!(serde_cbor::from_slice(cbor.into())); let map = try!(serde_cbor::from_slice(cbor.into()));
@ -38,9 +55,26 @@ impl Languages {
/// Creates a `Languages` struct from json. /// Creates a `Languages` struct from json.
/// ///
/// ``` /// ```
/// let json = r#"{"Rust":{"blanks":5,"code":12,"comments":0,"stats":[{"blanks":5,"code":12,"comments":0,"lines":17,"name":".\\src\\lib\\build.rs"}],"lines":17,"total_files":1}}"#; /// # use tokei::*;
/// let languages = Languages::from_json(&json); /// let json = r#"{
/// assert!(languages.get_mut(&LanguageType::Rust).unwrap().code == 12) /// "Rust": {
/// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "stats": [
/// {
/// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "lines": 17,
/// "name": ".\\src\\lib\\build.rs"
/// }
/// ],
/// "lines": 17
/// }
/// }"#;
/// let mut languages = Languages::from_json(json.as_bytes()).unwrap();
/// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
/// ``` /// ```
pub fn from_json<'a, I: Into<&'a [u8]>>(json: I) -> serde_json::Result<Self> { pub fn from_json<'a, I: Into<&'a [u8]>>(json: I) -> serde_json::Result<Self> {
let map = try!(serde_json::from_slice(json.into())); let map = try!(serde_json::from_slice(json.into()));
@ -50,27 +84,27 @@ impl Languages {
/// Creates a `Languages` struct from json. /// Creates a `Languages` struct from json.
/// ///
/// ``` /// ```no_run
/// let yaml = r#" /// # use tokei::*;
/// --- /// let yaml = r#"\
/// "Rust": /// ---
/// "blanks": 5 /// Rust:
/// "code": 12 /// blanks: 5
/// "comments": 0 /// code: 12
/// "lines": 17 /// comments: 0
/// "stats": /// lines: 17
/// - /// stats:
/// "blanks": 5 /// -
/// "code": 12 /// blanks: 5
/// "comments": 0 /// code: 12
/// "lines": 17 /// comments: 0
/// "name": ".\\src\\lib\\build.rs" /// lines: 17
/// "total_files": 1 /// name: .\src\lib\build.rs
/// "#; /// "#;
/// ///
/// let languages = Languages::from_yaml(&yaml); /// let mut languages = Languages::from_yaml(yaml.as_bytes()).unwrap();
/// ///
/// assert!(languages.get_mut(&LanguageType::Rust).unwrap().code == 12) /// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
/// ``` /// ```
pub fn from_yaml<'a, I: Into<&'a [u8]>>(yaml: I) -> serde_yaml::Result<Self> { pub fn from_yaml<'a, I: Into<&'a [u8]>>(yaml: I) -> serde_yaml::Result<Self> {
let map = try!(serde_yaml::from_slice(yaml.into())); let map = try!(serde_yaml::from_slice(yaml.into()));
@ -93,8 +127,9 @@ impl Languages {
/// to ignore paths containing them. /// to ignore paths containing them.
/// ///
/// ```no_run /// ```no_run
/// let languages = Languages::new(); /// # use tokei::*;
/// languages.get_statistics(&vec!["."], &vec![".git", "target"]); /// let mut languages = Languages::new();
/// languages.get_statistics(&*vec!["."], &*vec![".git", "target"]);
/// ///
/// println!("{:?}", languages); /// println!("{:?}", languages);
/// ``` /// ```
@ -111,7 +146,6 @@ impl Languages {
return; return;
} }
language.total_files = language.files.len();
let is_fortran = name == &FortranModern || name == &FortranLegacy; let is_fortran = name == &FortranModern || name == &FortranLegacy;
let files: Vec<_> = language.files.drain(..).collect(); let files: Vec<_> = language.files.drain(..).collect();
@ -202,6 +236,7 @@ impl Languages {
/// Constructs a new, blank `Languages`. /// Constructs a new, blank `Languages`.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let languages = Languages::new(); /// let languages = Languages::new();
/// ``` /// ```
pub fn new() -> Self { pub fn new() -> Self {
@ -293,7 +328,21 @@ impl Languages {
Languages { inner: map } Languages { inner: map }
} }
fn remove_empty(&self) -> BTreeMap<LanguageType, Language> { /// Creates a new map that only contains non empty languages.
///
/// ```
/// use tokei::*;
/// use std::collections::BTreeMap;
///
/// let mut languages = Languages::new();
/// languages.get_statistics(vec!["doesnt/exist"], vec![".git"]);
///
/// let empty_map = languages.remove_empty();
/// let new_map: BTreeMap<LanguageType, Language> = BTreeMap::new();
///
/// assert_eq!(empty_map, new_map);
/// ```
pub fn remove_empty(&self) -> BTreeMap<LanguageType, Language> {
let mut map = BTreeMap::new(); let mut map = BTreeMap::new();
for (name, language) in &self.inner { for (name, language) in &self.inner {
@ -307,18 +356,22 @@ impl Languages {
/// Converts `Languages` to CBOR. /// Converts `Languages` to CBOR.
/// ///
/// ```no_run /// ```no_run
/// extern crate tokei;
/// # use tokei::*;
/// extern crate rustc_serialize; /// extern crate rustc_serialize;
/// use rustc_serialize::ToHex; /// use rustc_serialize::hex::ToHex;
/// ///
/// # fn main () {
/// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e74730\ /// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e74730\
/// 065737461747381a566626c616e6b730564636f64650c68636f6d6d656e747300656c\ /// 065737461747381a566626c616e6b730564636f64650c68636f6d6d656e747300656c\
/// 696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e657\ /// 696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e657\
/// 3116b746f74616c5f66696c657301"; /// 3116b746f74616c5f66696c657301";
/// ///
/// let languages = Languages::new(); /// let mut languages = Languages::new();
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]); /// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
/// ///
/// assert_eq!(cbor, languages.to_cbor().to_hex()); /// assert_eq!(cbor, languages.to_cbor().unwrap().to_hex());
/// # }
/// ``` /// ```
pub fn to_cbor(&self) -> Result<Vec<u8>, serde_cbor::Error> { pub fn to_cbor(&self) -> Result<Vec<u8>, serde_cbor::Error> {
serde_cbor::to_vec(&self.remove_empty()) serde_cbor::to_vec(&self.remove_empty())
@ -327,20 +380,42 @@ impl Languages {
/// Converts `Languages` to JSON. /// Converts `Languages` to JSON.
/// ///
/// ```no_run /// ```no_run
/// # use tokei::*;
/// ///
/// let json = r#"{"Rust":{"blanks":5,"code":12,"comments":0,"stats":[{"blanks":5,"code":12,"comments":0,"lines":17,"name":".\\src\\lib\\build.rs"}],"lines":17,"total_files":1}}"#; /// let json = r#"{
/// let languages = Languages::new(); /// "Rust": {
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]); /// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "stats": [
/// {
/// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "lines": 17,
/// "name": ".\\src\\lib\\build.rs"
/// }
/// ],
/// "lines": 17
/// }
/// }"#;
/// let mut languages = Languages::new();
/// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
/// ///
/// assert_eq!(json, languages.to_json()); /// assert_eq!(json, languages.to_json().unwrap());
/// ``` /// ```
pub fn to_json(&self) -> Result<String, serde_json::Error> { pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(&self.remove_empty()) serde_json::to_string(&self.remove_empty())
} }
pub fn to_toml(&self) -> String {
toml::encode_str(&self.remove_empty())
}
/// Converts `Languages` to YAML. /// Converts `Languages` to YAML.
/// ///
/// ```no_run /// ```no_run
/// /// # use tokei::*;
/// let yaml = r#" /// let yaml = r#"
/// --- /// ---
/// "Rust": /// "Rust":
@ -354,12 +429,11 @@ impl Languages {
/// "code": 12 /// "code": 12
/// "comments": 0 /// "comments": 0
/// "lines": 17 /// "lines": 17
/// "name": ".\\src\\lib\\build.rs" /// "name": ".\\src\\lib\\build.rs"#;
/// "total_files": 1"#; /// let mut languages = Languages::new();
/// let languages = Languages::new(); /// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]);
/// ///
/// assert_eq!(yaml, languages.to_yaml()); /// assert_eq!(yaml, languages.to_yaml().unwrap());
pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> { pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.remove_empty()) serde_yaml::to_string(&self.remove_empty())
} }

View file

@ -4,9 +4,47 @@
unstable_features, unstable_features,
unused_import_braces)] unused_import_braces)]
//! # Tokei: Code Analysis Library([For the binary](https://github.com/Aaronepower/tokei/)) //! # Tokei: Code Analysis Library
//! //!
//! Tokei is a code analysis library powering the application of the same name. //! A simple, effcient library for analysing code in directories.[_For the binary_](https://github.com/Aaronepower/tokei/)
//!
//! ## How to use
//!
//! Tokei provides both `Languages` a map of existing programming languages and `Language` for creating custom languages.
//!
//! ### Example(Get total lines of code from all rust files in current directory, and all subdirectories)
//!
//! ```no_run
//! extern crate tokei;
//!
//! use std::collections::BTreeMap;
//! use std::fs::File;
//! use std::io::Read;
//!
//! use tokei::{Languages, LanguageType};
//!
//! fn main() {
//! // The paths to search. Accepts absolute, relative, and glob paths.
//! let paths = vec!["**/*.rs"];
//! // Exclude any path that contains any of these strings.
//! let excluded = vec!["target", ".git"];
//!
//! // Create new Languages
//! let mut languages = Languages::new();
//!
//! // Get statistics
//! languages.get_statistics(&*paths, &*excluded);
//!
//! // Remove empty languages
//! let language_map = languages.remove_empty();
//!
//! // Get Rust from statistics
//! let rust = language_map.get(&LanguageType::Rust).unwrap();
//!
//! // Print the number of lines that were code.
//! println!("Lines of code: {}", rust.code);
//! }
//! ```
#[macro_use] #[macro_use]
extern crate maplit; extern crate maplit;

View file

@ -1,10 +1,9 @@
#[macro_use]
#[macro_use] mod utils;
#![allow(missing_docs)] mod language;
mod utils; mod stats;
mod language; mod sort;
mod stats;
pub use language::{LanguageType, Languages, Language};
pub use language::{LanguageType, Languages, Language}; pub use stats::Stats;
pub use stats::Stats; pub use sort::Sort;
pub use utils::consts;

28
src/lib/sort.rs Normal file
View file

@ -0,0 +1,28 @@
use std::borrow::Cow;
/// Used for sorting languages.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Sort {
/// Sort by number blank lines.
Blanks,
/// Sort by number comments lines.
Comments,
/// Sort by number code lines.
Code,
/// Sort by number files lines.
Files,
/// Sort by number of lines.
Lines,
}
impl<'a> From<Sort> for Cow<'a, Sort> {
fn from(from: Sort) -> Self {
Cow::Owned(from)
}
}
impl<'a> From<&'a Sort> for Cow<'a, Sort> {
fn from(from: &'a Sort) -> Self {
Cow::Borrowed(from)
}
}

View file

@ -20,6 +20,7 @@ impl Stats {
/// Create a new `Stats` from a file path. /// Create a new `Stats` from a file path.
/// ///
/// ``` /// ```
/// # use tokei::*;
/// let stats = Stats::new("src/main.rs"); /// let stats = Stats::new("src/main.rs");
/// ``` /// ```
pub fn new<S: Into<String>>(name: S) -> Self { pub fn new<S: Into<String>>(name: S) -> Self {

View file

@ -1,6 +0,0 @@
pub const BLANKS: &'static str = "blanks";
pub const COMMENTS: &'static str = "comments";
pub const CODE: &'static str = "code";
pub const FILES: &'static str = "files";
pub const LINES: &'static str = "lines";

View file

@ -68,7 +68,7 @@ pub fn get_all_files<'a>(paths: Cow<'a, [&'a str]>,
let mut language = if opt_or_cont!(path.to_str()).contains("Makefile") { let mut language = if opt_or_cont!(path.to_str()).contains("Makefile") {
languages.get_mut(&Makefile).unwrap() languages.get_mut(&Makefile).unwrap()
} else { } else {
opt_or_cont!( opt_or_cont!(
languages.get_mut(&opt_or_cont!(LanguageType::from_extension(&path)))) languages.get_mut(&opt_or_cont!(LanguageType::from_extension(&path))))
}; };
@ -91,8 +91,8 @@ pub fn get_all_files<'a>(paths: Cow<'a, [&'a str]>,
let mut language = if opt_or_cont!(entry.path().to_str()).contains("Makefile") { let mut language = if opt_or_cont!(entry.path().to_str()).contains("Makefile") {
languages.get_mut(&Makefile).unwrap() languages.get_mut(&Makefile).unwrap()
} else { } else {
opt_or_cont!( opt_or_cont!(
languages.get_mut(&opt_or_cont!(LanguageType::from_extension(entry.path()))) languages.get_mut(&opt_or_cont!(LanguageType::from_extension(entry.path())))
) )
}; };
@ -104,47 +104,47 @@ pub fn get_all_files<'a>(paths: Cow<'a, [&'a str]>,
pub fn get_extension<P: AsRef<Path>>(path: P) -> Option<String> { pub fn get_extension<P: AsRef<Path>>(path: P) -> Option<String> {
let path = path.as_ref(); let path = path.as_ref();
match path.extension() { match path.extension() {
Some(extension_os) => { Some(extension_os) => {
match extension_os.to_str() { match extension_os.to_str() {
Some(extension) => Some(extension.to_lowercase()), Some(extension) => Some(extension.to_lowercase()),
None => None, None => None,
} }
} }
None => { None => {
match get_filetype_from_shebang(path) { match get_filetype_from_shebang(path) {
// Using String::from here because all file extensions from // Using String::from here because all file extensions from
// get_filetype_from_shebang are guaranteed to be lowercase. // get_filetype_from_shebang are guaranteed to be lowercase.
Some(extension) => Some(String::from(extension)), Some(extension) => Some(String::from(extension)),
None => None, None => None,
} }
} }
} }
} }
/// This is for getting the file type from the first line of a file /// This is for getting the file type from the first line of a file
pub fn get_filetype_from_shebang<P: AsRef<Path>>(file: P) -> Option<&'static str> { pub fn get_filetype_from_shebang<P: AsRef<Path>>(file: P) -> Option<&'static str> {
let file = match File::open(file) { let file = match File::open(file) {
Ok(file) => file, Ok(file) => file,
_ => return None, _ => return None,
}; };
let mut buf = BufReader::new(file); let mut buf = BufReader::new(file);
let mut line = String::new(); let mut line = String::new();
let _ = buf.read_line(&mut line); let _ = buf.read_line(&mut line);
let mut words = line.split_whitespace(); let mut words = line.split_whitespace();
match words.next() { match words.next() {
Some("#!/bin/sh") => Some("sh"), Some("#!/bin/sh") => Some("sh"),
Some("#!/bin/csh") => Some("csh"), Some("#!/bin/csh") => Some("csh"),
Some("#!/usr/bin/perl") => Some("pl"), Some("#!/usr/bin/perl") => Some("pl"),
Some("#!/usr/bin/env") => { Some("#!/usr/bin/env") => {
match words.next() { match words.next() {
Some("python") | Some("python2") | Some("python3") => Some("py"), Some("python") | Some("python2") | Some("python3") => Some("py"),
Some("sh") => Some("sh"), Some("sh") => Some("sh"),
_ => None, _ => None,
} }
} }
_ => None, _ => None,
} }
} }

View file

@ -2,26 +2,26 @@
// Use of this source code is governed by the MIT license that can be // Use of this source code is governed by the MIT license that can be
// found in the LICENSE file. // found in the LICENSE file.
macro_rules! opt_or_cont { macro_rules! opt_or_cont {
($option:expr) => { ($option:expr) => {
match $option { match $option {
Some(result) => result, Some(result) => result,
None => continue, None => continue,
} }
} }
} }
macro_rules! rs_or_cont { macro_rules! rs_or_cont {
($result:expr) => { ($result:expr) => {
match $result { match $result {
Ok(result) => result, Ok(result) => result,
Err(_) => continue, Err(_) => continue,
} }
} }
} }
macro_rules! debug { macro_rules! debug {
($fmt:expr) => (if cfg!(debug_assertions) {println!($fmt)}); ($fmt:expr) => (if cfg!(debug_assertions) {println!($fmt)});
($fmt:expr, $($arg:tt)*) => (if cfg!(debug_assertions) {println!($fmt, $($arg)*)}); ($fmt:expr, $($arg:tt)*) => (if cfg!(debug_assertions) {println!($fmt, $($arg)*)});
} }

View file

@ -1,7 +1,5 @@
#[macro_use] #[macro_use]
mod macros; mod macros;
pub mod consts;
pub mod fs; pub mod fs;
pub use self::consts::*;
pub use self::fs::*; pub use self::fs::*;

View file

@ -7,6 +7,7 @@ extern crate clap;
extern crate serde_cbor; extern crate serde_cbor;
extern crate serde_json; extern crate serde_json;
extern crate serde_yaml; extern crate serde_yaml;
extern crate toml;
extern crate rustc_serialize; extern crate rustc_serialize;
extern crate tokei; extern crate tokei;
@ -23,10 +24,15 @@ use clap::App;
use rustc_serialize::hex::FromHex; use rustc_serialize::hex::FromHex;
use tokei::{Languages, Language, LanguageType}; use tokei::{Languages, Language, LanguageType};
use tokei::consts::*; use tokei::Sort::*;
pub const ROW: &'static str = "-------------------------------------------------------------------\ pub const ROW: &'static str = "-------------------------------------------------------------------\
------------"; ------------";
pub const BLANKS: &'static str = "blanks";
pub const COMMENTS: &'static str = "comments";
pub const CODE: &'static str = "code";
pub const FILES: &'static str = "files";
pub const LINES: &'static str = "lines";
fn main() { fn main() {
// Get options at the beginning, so the program doesn't have to make any extra calls to get the // Get options at the beginning, so the program doesn't have to make any extra calls to get the
@ -163,11 +169,7 @@ fn main() {
} }
} }
"json" => print!("{}", languages.to_json().unwrap()), "json" => print!("{}", languages.to_json().unwrap()),
// "toml" => print!("{}", { "toml" => print!("{}", languages.to_toml()),
// let encoder = toml::Encoder::new();
// lang_map.encode(&mut encoder).unwrap();
// encoder.toml
// }),
"yaml" => print!("{}", languages.to_yaml().unwrap()), "yaml" => print!("{}", languages.to_yaml().unwrap()),
_ => unreachable!(), _ => unreachable!(),
} }
@ -175,11 +177,11 @@ fn main() {
for (_, ref mut language) in &mut languages { for (_, ref mut language) in &mut languages {
match &*sort_category { match &*sort_category {
BLANKS => language.sort_by(BLANKS), BLANKS => language.sort_by(Blanks),
COMMENTS => language.sort_by(COMMENTS), COMMENTS => language.sort_by(Comments),
CODE => language.sort_by(CODE), CODE => language.sort_by(Code),
FILES => language.sort_by(FILES), FILES => language.sort_by(Files),
LINES => language.sort_by(LINES), LINES => language.sort_by(Lines),
_ => unreachable!(), _ => unreachable!(),
} }
} }
@ -215,7 +217,13 @@ fn main() {
if !files_option { if !files_option {
println!("{}", ROW); println!("{}", ROW);
} }
print_language(&total, LanguageType::__Total); println!(" {: <18} {: >6} {:>12} {:>12} {:>12} {:>12}",
"Total",
total.stats.len(),
total.lines,
total.code,
total.comments,
total.blanks);
println!("{}", ROW); println!("{}", ROW);
} }
} }
@ -231,7 +239,13 @@ pub fn convert_input(contents: String) -> Option<BTreeMap<LanguageType, Language
Some(result) Some(result)
} else if let Ok(result) = serde_yaml::from_str(&*contents) { } else if let Ok(result) = serde_yaml::from_str(&*contents) {
Some(result) Some(result)
} else if let Ok(result) = serde_cbor::from_slice(&*contents.from_hex().unwrap()) { } else if let Ok(hex) = contents.from_hex() {
if let Ok(result) = serde_cbor::from_slice(&*hex) {
Some(result)
} else {
None
}
} else if let Some(result) = toml::decode_str(&*contents) {
Some(result) Some(result)
} else { } else {
None None
@ -243,7 +257,7 @@ fn print_language<'a, C>(language: &'a Language, name: C)
{ {
println!(" {: <18} {: >6} {:>12} {:>12} {:>12} {:>12}", println!(" {: <18} {: >6} {:>12} {:>12} {:>12} {:>12}",
name.into().name(), name.into().name(),
language.total_files, language.stats.len(),
language.lines, language.lines,
language.code, language.code,
language.comments, language.comments,