implemented file counting.

This commit is contained in:
Aaronepower 2016-05-01 18:33:46 +01:00
parent e0fa667f8f
commit c28bfa8e9e
4 changed files with 97 additions and 26 deletions

View file

@ -1,15 +1,15 @@
# Copyright (c) 2015 Aaron Power # Copyright (c) 2015 Aaron Power
# Use of this source code is governed by the MIT license that can be # Use of this source code is governed by the MIT/APACHE2.0 license that can be
# found in the LICENSE file. # found in the LICENCE-{APACHE, MIT} file.
[package] [package]
name = "tokei" name = "tokei"
version = "1.5.1" version = "1.6.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 lines of code within a directory, quickly." description = "Count your code, quickly."
[profile.dev] [profile.dev]
debug = true debug = true

View file

@ -2,9 +2,11 @@
// 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.
use std::cell::RefCell; use std::cell::{RefCell, RefMut};
use std::fmt; use std::fmt;
use std::path::PathBuf; use std::path::PathBuf;
use std::ops::AddAssign;
use stats::Stats;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Language<'a> { pub struct Language<'a> {
@ -105,3 +107,23 @@ impl<'a> fmt::Display for Language<'a> {
self.code) self.code)
} }
} }
// Adding languages to the raw total.
impl<'a, 'b> AddAssign<&'b Language<'a>> for Language<'a> {
fn add_assign(&mut self, rhs: &Self) {
self.total += rhs.files.len();
self.lines += rhs.lines;
self.comments += rhs.comments;
self.blanks += rhs.blanks;
self.code += rhs.code;
}
}
// Adding a file to the language.
impl<'a> AddAssign<Stats> for Language<'a> {
fn add_assign(&mut self, rhs: Stats) {
self.lines += rhs.lines;
self.code += rhs.code;
self.comments += rhs.comments;
self.blanks += rhs.blanks;
}
}

View file

@ -1,6 +1,6 @@
// Copyright (c) 2015 Aaron Power // Copyright (c) 2015 Aaron Power
// Use of this source code is governed by the MIT license that can be // Use of this source code is governed by the MIT/APACHE2.0 license that can be
// found in the LICENSE file. // found in the LICENCE-{APACHE, MIT} file.
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
@ -12,6 +12,7 @@ extern crate walkdir;
pub mod macros; pub mod macros;
pub mod language; pub mod language;
pub mod fsutil; pub mod fsutil;
pub mod stats;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -240,8 +241,8 @@ fn main() {
get_all_files(paths, &languages, ignored_directories); get_all_files(paths, &languages, ignored_directories);
let mut total = Language::new_raw("Total"); let mut total = Language::new_raw("Total");
for language in languages.values() { for language_ref in languages.values() {
let mut language = language.borrow_mut(); let mut language = language_ref.borrow_mut();
if language.printed { if language.printed {
continue; continue;
@ -256,14 +257,16 @@ fn main() {
for file in files { for file in files {
let mut contents = String::new(); let mut contents = String::new();
let is_fortran = language.name.contains("FORTRAN"); let is_fortran = language.name.contains("FORTRAN");
let mut stats = stats::Stats::new(unwrap_opt_cont!(file.to_str()));
let _ = unwrap_rs_cont!(unwrap_rs_cont!(File::open(file)) let _ = unwrap_rs_cont!(unwrap_rs_cont!(File::open(file))
.read_to_string(&mut contents)); .read_to_string(&mut contents));
let mut is_in_comments = false; let mut is_in_comments = false;
let lines = contents.lines(); let lines = contents.lines();
if is_blank_lang { if is_blank_lang {
language.code += lines.count(); stats.code += lines.count();
continue; continue;
} }
@ -273,10 +276,10 @@ fn main() {
} else { } else {
line.trim() line.trim()
}; };
language.lines += 1; stats.lines += 1;
if line.trim().is_empty() { if line.trim().is_empty() {
language.blanks += 1; stats.blanks += 1;
continue; continue;
} }
@ -286,7 +289,7 @@ fn main() {
if line.starts_with(multi_line) { if line.starts_with(multi_line) {
is_in_comments = true; is_in_comments = true;
} else if contains_comments(line, multi_line, multi_line_end) { } else if contains_comments(line, multi_line, multi_line_end) {
language.code += 1; stats.code += 1;
is_in_comments = true; is_in_comments = true;
} }
} }
@ -296,7 +299,7 @@ fn main() {
if line.contains(language.multi_line_end) { if line.contains(language.multi_line_end) {
is_in_comments = false; is_in_comments = false;
} }
language.comments += 1; stats.comments += 1;
continue; continue;
} }
@ -304,33 +307,34 @@ fn main() {
for single in single_comments { for single in single_comments {
if line.starts_with(single) { if line.starts_with(single) {
language.comments += 1; stats.comments += 1;
continue 'line; continue 'line;
} }
} }
language.code += 1; stats.code += 1;
} }
if matches.is_present(FILES) {
println!("{}", stats);
}
*language += stats;
} }
if !language.is_empty() { if !language.is_empty() {
language.printed = true; language.printed = true;
if let None = sort { if let None = sort {
println!("{}", *language);
if matches.is_present(FILES) { if matches.is_present(FILES) {
println!("{}", ROW); println!("{}", ROW);
for file in &language.files { println!("{}", *language);
println!("{}", unwrap_opt_cont!(file.to_str()));
}
println!("{}", ROW); println!("{}", ROW);
} else {
println!("{}", *language);
} }
} }
} }
total.total += language.files.len(); total += &*language;
total.lines += language.lines;
total.comments += language.comments;
total.blanks += language.blanks;
total.code += language.code;
} }
if let Some(sort_category) = sort { if let Some(sort_category) = sort {
@ -357,7 +361,9 @@ fn main() {
} }
} }
println!("{}", ROW); if !matches.is_present(FILES) {
println!("{}", ROW);
}
println!("{}", total); println!("{}", total);
println!("{}", ROW); println!("{}", ROW);
} }

43
src/stats.rs Normal file
View file

@ -0,0 +1,43 @@
use std::fmt;
#[derive(Debug)]
pub struct Stats {
pub name: String,
pub code: usize,
pub blanks: usize,
pub lines: usize,
pub comments: usize,
}
impl Stats {
pub fn new<S: Into<String>>(name: S) -> Self {
Stats {
name: name.into(),
code: 0,
blanks: 0,
lines: 0,
comments: 0,
}
}
}
impl fmt::Display for Stats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = if self.name.len() > 24 {
let mut name = String::from("|");
name.push_str(&self.name[self.name.len() - 24..]);
name
} else {
self.name.clone()
};
write!(f,
" {: <25} {:>12} {:>12} {:>12} {:>12}",
name,
self.lines,
self.blanks,
self.comments,
self.code)
}
}