summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock95
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs68
3 files changed, 155 insertions, 9 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0d14a1a..50957fc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,4 +1,99 @@
[root]
name = "loc"
version = "0.1.0"
+dependencies = [
+ "clap 2.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+[[package]]
+name = "ansi_term"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "bitflags"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "clap"
+version = "2.18.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
+ "strsim 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "term_size 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-segmentation 0.1.2 (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)",
+]
+
+[[package]]
+name = "kernel32-sys"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "strsim"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "term_size"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unicode-segmentation"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "unicode-width"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "vec_map"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-build"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
+"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
+"checksum clap 2.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "40046b8a004bf3ba43b9078bf4b9b6d1708406a234848f925dbd7160a374c8a8"
+"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
+"checksum libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)" = "044d1360593a78f5c8e5e710beccdc24ab71d1f01bc19a29bcacdba22e8475d8"
+"checksum strsim 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "50c069df92e4b01425a8bf3576d5d417943a6a7272fbabaf5bd80b1aaa76442e"
+"checksum term_size 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f7f5f3f71b0040cecc71af239414c23fd3c73570f5ff54cf50e03cef637f2a0"
+"checksum unicode-segmentation 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b905d0fc2a1f0befd86b0e72e31d1787944efef9d38b9358a9e92a69757f7e3b"
+"checksum unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2d6722facc10989f63ee0e20a83cd4e1714a9ae11529403ac7e0afd069abc39e"
+"checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f"
+"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
+"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
diff --git a/Cargo.toml b/Cargo.toml
index 3916615..3ab30ca 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["Benedict Boerger <benedict.boerger@cs.tu-dortmund.de>"]
[dependencies]
+clap = "2.7.0"
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);
}