summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorBenedict <benedict@0xb8000.de>2016-11-12 14:52:16 +0100
committerBenedict <benedict@0xb8000.de>2016-11-12 19:29:59 +0100
commit2aaff9278c5011468fa5b0e7981dda4e9244377c (patch)
tree3a9b0819279eb3bf8dcd9ef11cb2531ca65a7e99 /src/main.rs
parent30b11e15698e7341d3a5986767cdd1d5a804587c (diff)
tui: introduce a command line option -f
It is now possible to scan from the current working directory recursivly all childrens. This this also the new default behaivour when no command line arguments are given. For scanning indivual files, which was the default behaviour before, a new command line option -f (--file) is introduced.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs68
1 files changed, 59 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 57a8196..a19e43d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,10 @@
+#[macro_use]
+extern crate clap;
use std::fs::File;
-use std::env;
use std::path::Path;
use std::io::*;
+use std::path::PathBuf;
+use clap::{App, Arg};
mod config;
@@ -59,7 +62,52 @@ fn count_lines(filename: &str, config: &mut Vec<config::CommentTokens>) -> Resul
Ok(sum)
}
+fn count_files_for_dir(dir: PathBuf, mut sum: &mut i32, mut config: &mut Vec<config::CommentTokens>) {
+ match dir.read_dir() {
+ Ok(iter) => {
+ for entry in iter {
+ match entry {
+ Ok(i) => {
+ if i.path().is_dir() {
+ count_files_for_dir(i.path(), &mut sum,
+ &mut config);
+ }
+ else {
+ count_lines_for_file(i.path().to_str().unwrap(),
+ &mut sum, &mut config);
+ } },
+ Err(e) => println!("error: {}", e),
+ }
+ }
+ },
+ Err(e) => println!("error: {}", e),
+ }
+}
+
+fn count_lines_for_file(filename: &str, sum: &mut i32, mut config: &mut Vec<config::CommentTokens>) {
+ match count_lines(filename, &mut config) {
+ Ok(i) => {
+ println!("{}\t: {}", i, filename);
+ *sum += i; },
+ Err(e) => println!("error for file {}: {}", filename, e),
+ }
+}
+
fn main() {
+
+ let m = App::new("loc").about("Count lines of code. Without parameter read recursive all
+ files from current directory and count lines of code\
+ for each")
+ .version(crate_version!())
+ .author("Benedict Börger <benedict@0xb8000.de>")
+ .arg(Arg::with_name("file")
+ .help("files to count lines of code")
+ .short("f")
+ .long("file")
+ .takes_value(true)
+ .multiple(true))
+ .get_matches();
+
let mut config = match config::read_config() {
Err(e) => panic!("error reading config file(s): {}", e),
Ok(i) => i,
@@ -67,15 +115,17 @@ fn main() {
let mut sum = 0;
- // TODO read files from standard input option
- // TODO read all file below this dictionary
- for argument in env::args().skip(1) {
- match count_lines(argument.as_str(), &mut config) {
- Ok(i) => {
- println!("{}\t: {}", i, argument);
- sum += i; },
- Err(e) => println!("error for file {}: {}", argument, e),
+ if let Some(files) = m.values_of("file") {
+ for file in files {
+ count_lines_for_file(file, &mut sum, &mut config)
}
}
+ else {
+ match std::env::current_dir() {
+ Ok(i) => count_files_for_dir(i, &mut sum, &mut config),
+ Err(e) => panic!("could not determine current dir: {}", e),
+ }
+ }
+
println!("{}\t: total", sum);
}