Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


use clap::ArgMatches;

use error::{CliError, CliResult};
Expand Down Expand Up @@ -34,9 +32,11 @@ impl<'a> Config<'a> {
if let Some(ext_vec) = m.values_of("language") {
for e in ext_vec {
if let None = Language::from_ext(e) {
return Err(CliError::UnknownExt(format!("unsupported source code extension \
'{}'",
e.to_owned())));
return Err(CliError::UnknownExt(format!(
"unsupported source code extension \
'{}'",
e.to_owned()
)));
}
}
}
Expand Down
153 changes: 89 additions & 64 deletions src/count/counts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


use comment::Comment;
use config::{Config, Utf8Rule};
use count::Count;
Expand Down Expand Up @@ -54,11 +52,13 @@ impl<'c> Counts<'c> {
for path in &self.cfg.to_count {
debugln!("iter; path={:?};", path);
let mut files = vec![];
fsutil::get_all_files(&mut files,
path,
&self.cfg.exclude,
self.cfg.follow_links,
&gitignore);
fsutil::get_all_files(
&mut files,
path,
&self.cfg.exclude,
self.cfg.follow_links,
&gitignore,
);

for file in files {
debugln!("iter; file={:?};", file);
Expand Down Expand Up @@ -200,15 +200,19 @@ impl<'c> Counts<'c> {
debugln!("There is a keyword");
debugln!("line={:?}", line);
if is_in_unsafe {
debugln!("It didn't contain the keyword, but we are still in \
unsafe");
debugln!(
"It didn't contain the keyword, but we are still in \
unsafe"
);
count.usafe += 1;
bracket_count = Counts::count_brackets(line, Some(bracket_count));
is_in_unsafe = bracket_count > 0;
debugln!("after counting brackets; is_in_unsafe={:?}; \
bracket_count={:?}",
is_in_unsafe,
bracket_count);
debugln!(
"after counting brackets; is_in_unsafe={:?}; \
bracket_count={:?}",
is_in_unsafe,
bracket_count
);
} else if let Some(caps) = re.captures(line) {
let mut should_count = true;
if let Some(before) = caps.at(1) {
Expand All @@ -221,8 +225,9 @@ impl<'c> Counts<'c> {
}
}
if let Some(multi) = count.lang.multi_start() {
if before.contains(multi) &&
!before.contains(count.lang.multi_end().unwrap()) {
if before.contains(multi)
&& !before.contains(count.lang.multi_end().unwrap())
{
should_count = false;
}
}
Expand All @@ -234,10 +239,12 @@ impl<'c> Counts<'c> {
debugln!("after_usafe={:?}", after);
bracket_count = Counts::count_brackets(after, None);
is_in_unsafe = bracket_count > 0;
debugln!("after counting brackets; is_in_unsafe={:?}; \
bracket_count={:?}",
is_in_unsafe,
bracket_count);
debugln!(
"after counting brackets; is_in_unsafe={:?}; \
bracket_count={:?}",
is_in_unsafe,
bracket_count
);
}
}
} else {
Expand Down Expand Up @@ -269,66 +276,84 @@ impl<'c> Counts<'c> {

pub fn write_results(&mut self) -> CliResult<()> {
let mut w = TabWriter::new(vec![]);
cli_try!(write!(w,
"\tLanguage\tFiles\tLines\tBlanks\tComments\tCode{}\n",
if self.cfg.usafe { "\tUnsafe (%)" } else { "" }));
cli_try!(write!(w,
"\t--------\t-----\t-----\t------\t--------\t----{}\n",
if self.cfg.usafe { "\t----------" } else { "" }));
cli_try!(write!(
w,
"\tLanguage\tFiles\tLines\tBlanks\tComments\tCode{}\n",
if self.cfg.usafe { "\tUnsafe (%)" } else { "" }
));
cli_try!(write!(
w,
"\t--------\t-----\t-----\t------\t--------\t----{}\n",
if self.cfg.usafe { "\t----------" } else { "" }
));
for count in &self.counts {
if self.cfg.usafe {
let usafe_per = if count.code != 0 {
(count.usafe as f64 / count.code as f64) * 100.00f64
} else {
0f64
};
cli_try!(write!(w,
"\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
count.lang.name(),
count.total_files(),
count.lines(),
count.blanks(),
count.comments(),
count.code(),
if (usafe_per - 00f64).abs() < f64::EPSILON {
"".to_owned()
} else {
format!("{} ({:.2}%)", count.usafe(), usafe_per)
}));
cli_try!(write!(
w,
"\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
count.lang.name(),
count.total_files(),
count.lines(),
count.blanks(),
count.comments(),
count.code(),
if (usafe_per - 00f64).abs() < f64::EPSILON {
"".to_owned()
} else {
format!("{} ({:.2}%)", count.usafe(), usafe_per)
}
));
} else {
cli_try!(write!(w, "\t{}\n", count));
}
}
cli_try!(write!(w,
"\t--------\t-----\t-----\t------\t--------\t----{}\n",
if self.cfg.usafe { "\t----------" } else { "" }));
cli_try!(write!(w,
"{}\t\t{}\t{}\t{}\t{}\t{}{}\n",
"Totals:",
fmt::format_number(self.tot as u64, self.cfg.thousands),
fmt::format_number(self.tot_lines, self.cfg.thousands),
fmt::format_number(self.tot_blanks, self.cfg.thousands),
fmt::format_number(self.tot_comments, self.cfg.thousands),
fmt::format_number(self.tot_code, self.cfg.thousands),
if self.cfg.usafe {
format!("\t{} ({:.2}%)",
fmt::format_number(self.tot_usafe, self.cfg.thousands),
(self.tot_usafe as f64 / self.tot_code as f64) * 100.00f64)
} else {
"".to_owned()
}));
cli_try!(write!(
w,
"\t--------\t-----\t-----\t------\t--------\t----{}\n",
if self.cfg.usafe { "\t----------" } else { "" }
));
cli_try!(write!(
w,
"{}\t\t{}\t{}\t{}\t{}\t{}{}\n",
"Totals:",
fmt::format_number(self.tot as u64, self.cfg.thousands),
fmt::format_number(self.tot_lines, self.cfg.thousands),
fmt::format_number(self.tot_blanks, self.cfg.thousands),
fmt::format_number(self.tot_comments, self.cfg.thousands),
fmt::format_number(self.tot_code, self.cfg.thousands),
if self.cfg.usafe {
format!(
"\t{} ({:.2}%)",
fmt::format_number(self.tot_usafe, self.cfg.thousands),
(self.tot_usafe as f64 / self.tot_code as f64) * 100.00f64
)
} else {
"".to_owned()
}
));

cli_try!(w.flush());

verboseln!(self.cfg,
"{} {}",
Format::Good("Displaying"),
"the results:");
verboseln!(
self.cfg,
"{} {}",
Format::Good("Displaying"),
"the results:"
);
if self.tot > 0 {
write!(io::stdout(),
"{}",
String::from_utf8(w.unwrap()).ok().expect("failed to get valid UTF-8 String"))
.expect("failed to write output");
write!(
io::stdout(),
"{}",
String::from_utf8(w.unwrap())
.ok()
.expect("failed to get valid UTF-8 String")
)
.expect("failed to write output");
} else {
println!("\n\tNo source files were found matching the specified criteria");
}
Expand Down
21 changes: 11 additions & 10 deletions src/count/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
mod counts;


pub use self::counts::Counts;
use fmt;
use language::Language;
pub use self::counts::Counts;

use std::fmt as StdFmt;
use std::ops::Deref;
Expand Down Expand Up @@ -73,13 +72,15 @@ impl Deref for Count {

impl StdFmt::Display for Count {
fn fmt(&self, f: &mut StdFmt::Formatter) -> StdFmt::Result {
write!(f,
"{}\t{}\t{}\t{}\t{}\t{}",
self.lang,
self.total_files(),
self.lines(),
self.blanks(),
self.comments(),
self.code())
write!(
f,
"{}\t{}\t{}\t{}\t{}\t{}",
self.lang,
self.total_files(),
self.lines(),
self.blanks(),
self.comments(),
self.code()
)
}
}
11 changes: 6 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@


use fmt::Format;use std::error::Error;
use std::fmt::{Display, Formatter};
use fmt::Format;
use std::error::Error;
use std::fmt::Result as FmtResult;
use std::fmt::{Display, Formatter};

pub type CliResult<T> = Result<T, CliError>;

Expand Down Expand Up @@ -51,7 +50,9 @@ impl Error for CliError {
match *self {
CliError::Generic(ref d) => &*d,
CliError::UnknownExt(ref d) => &*d,
CliError::Unknown => "An unknown fatal error has occurred, please consider filing a bug-report!",
CliError::Unknown => {
"An unknown fatal error has occurred, please consider filing a bug-report!"
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

#[cfg(all(feature = "color", not(target_os = "windows")))]
use ansi_term::ANSIString;

#[cfg(all(feature = "color", not(target_os = "windows")))]
use ansi_term::Colour::{Green, Red, Yellow};use std::fmt;
use ansi_term::Colour::{Green, Red, Yellow};
use std::fmt;

#[allow(dead_code)]
pub enum Format<T> {
Expand Down
55 changes: 32 additions & 23 deletions src/fsutil.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@

use gitignore::File;

use glob;use std::fs;
use glob;
use std::fs;
use std::io::Result;
use std::path::PathBuf;

pub fn get_all_files(v: &mut Vec<PathBuf>,
path: &PathBuf,
exclude: &[PathBuf],
follow_links: bool,
gitignore: &Option<File>) {
debugln!("executing; get_all_files; path={:?}; exclude={:?}; all={:?}",
path,
exclude,
all);
pub fn get_all_files(
v: &mut Vec<PathBuf>,
path: &PathBuf,
exclude: &[PathBuf],
follow_links: bool,
gitignore: &Option<File>,
) {
debugln!(
"executing; get_all_files; path={:?}; exclude={:?}; all={:?}",
path,
exclude,
all
);
if exclude.contains(path) {
return;
}
Expand All @@ -33,19 +37,22 @@ pub fn get_all_files(v: &mut Vec<PathBuf>,
for entry in dir {
let entry = entry.unwrap();
let file_path = entry.path();
get_all_files(v,
&file_path.to_path_buf(),
exclude,
follow_links,
gitignore);
get_all_files(
v,
&file_path.to_path_buf(),
exclude,
follow_links,
gitignore,
);
}
} else {
debugln!("It's a file");
v.push(path.clone());
}
} else {
for path_buf in glob::glob(path.to_str().unwrap_or(""))
.expect("failed to get files from glob") {
for path_buf in
glob::glob(path.to_str().unwrap_or("")).expect("failed to get files from glob")
{
if let Ok(file_path) = path_buf {
if let Ok(result) = get_metadata(&file_path, follow_links) {
if result.is_dir() {
Expand All @@ -54,11 +61,13 @@ pub fn get_all_files(v: &mut Vec<PathBuf>,
for entry in dir {
let entry = entry.unwrap();
let file_path = entry.path();
get_all_files(v,
&file_path.to_path_buf(),
exclude,
follow_links,
gitignore);
get_all_files(
v,
&file_path.to_path_buf(),
exclude,
follow_links,
gitignore,
);
}
} else {
debugln!("It's a file");
Expand Down
Loading