Skip to content

Commit

Permalink
Merge pull request #27
Browse files Browse the repository at this point in the history
Move short flag into OptionsCli struct
  • Loading branch information
simeg authored Aug 5, 2018
2 parents e5287f7 + 9ea81eb commit dd39f59
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
50 changes: 25 additions & 25 deletions src/bin/commentective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,46 @@ use std::path::Path;
use std::process;

fn main() {
let arg_short = Arg::with_name("short").short("s").long("short").help(
"Formats output with \"file.ext:line\" without colors. Only outputs files with comments.",
);
let arg_extension = Arg::with_name("extension")
.short("e")
.long("extension")
.help("Only analyze files with this extension")
.takes_value(true);
let arg_files = Arg::with_name("FILES")
.help("Files to analyze")
.required(true)
.multiple(true)
.validator_os(exists_on_filesystem)
.index(1);

let matches = App::new("commentective")
.version("0.3.0")
.author("Simon Egersand <[email protected]>")
.about("CLI tool to find comments and commented out code")
.arg(
Arg::with_name("FILES")
.help("Files to analyze")
.required(true)
.multiple(true)
.validator_os(exists_on_filesystem)
.index(1),
)
.arg(
Arg::with_name("extension")
.short("e")
.long("extension")
.help("Only analyze files with this extension")
.takes_value(true),
)
.arg(
Arg::with_name("short")
.short("s")
.long("short")
.help("Formats output with \"file.ext:line\" without colors"),
)
.arg(arg_files)
.arg(arg_extension)
.arg(arg_short)
.get_matches();

let values: Values = matches.values_of("FILES").unwrap();
let paths: Vec<&Path> = values.map(|file| Path::new(file)).collect::<Vec<&Path>>();

let extension: Option<String> = matches.value_of("extension").map(str); // Convert &str -> String

let mut printer = Printer {
writer: io::stdout(),
let opts_cli = OptionsCli {
extension,
short: matches.is_present("short"),
};

let opts_cli = OptionsCli { extension };
let mut printer = Printer {
writer: io::stdout(),
options: &opts_cli,
};

let successful = commentective::run(paths, opts_cli)
let successful = commentective::run(paths, &opts_cli)
.into_iter()
.map(|result| printer.terminal(result))
.filter(|result| result.is_err())
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ pub mod language;
pub mod printer;
pub mod utils;

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct OptionsCli {
pub extension: Option<String>,
pub short: bool,
}

pub fn run(paths: Vec<&Path>, opts: OptionsCli) -> Vec<Result<FindResult, Error>> {
pub fn run(paths: Vec<&Path>, opts: &OptionsCli) -> Vec<Result<FindResult, Error>> {
paths
.into_iter()
.map(|path| resolve_type_and_run(path, &opts))
Expand Down
17 changes: 9 additions & 8 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use language::FindResult;
use printer::Color::*;
use std::io::Error;
use std::io::Write;
use OptionsCli;

enum Color {
Green,
Expand All @@ -15,12 +16,12 @@ enum Color {
Yellow,
}

pub struct Printer<W> {
pub struct Printer<'a, W> {
pub writer: W,
pub short: bool,
pub options: &'a OptionsCli,
}

impl<W> Printer<W>
impl<'a, W> Printer<'a, W>
where
W: Write,
{
Expand All @@ -32,7 +33,7 @@ where
}
match result.lines.len() {
0 => {
if !self.short {
if !self.options.short {
self.print_file_name(&result.file_name);
self.print_colored(String::from("> No comments found"), White);
}
Expand All @@ -52,19 +53,19 @@ where
}

fn error(&mut self, err: &Error) {
if !self.short {
if !self.options.short {
self.print_colored_line(Red);
}
let msg = format!("Error: {}", err.to_string());
if !self.short {
if !self.options.short {
self.print_colored(msg, Red);
self.print_colored_line(Red);
}
}

fn result(&mut self, result: FindResult) {
let file_name = result.file_name;
if !self.short {
if !self.options.short {
self.print_file_name(&file_name);
}
result
Expand All @@ -82,7 +83,7 @@ where
}

fn print_comments(&mut self, line_number: u32, file_name: &String) {
if !self.short {
if !self.options.short {
let msg = format!("L{}", line_number.to_string());
self.print_colored(msg, Green);
} else {
Expand Down
11 changes: 6 additions & 5 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ mod flags {
let path_python = Path::new("tests/resources/python/with-comments.py");
let paths = vec![path_javascript, path_python];

let opts_no_ext = OptionsCli { extension: None };
for (i, result) in commentective::run(paths.clone(), opts_no_ext)
let opts_no_ext = OptionsCli { extension: None, short: false };
for (i, result) in commentective::run(paths.clone(), &opts_no_ext)
.iter()
.enumerate()
{
Expand All @@ -450,8 +450,9 @@ mod flags {

let opts_with_ext = OptionsCli {
extension: Some(str("js")),
short: false,
};
for (i, result) in commentective::run(paths, opts_with_ext).iter().enumerate() {
for (i, result) in commentective::run(paths, &opts_with_ext).iter().enumerate() {
assert!(result.is_ok());

if i == 0 {
Expand Down Expand Up @@ -489,14 +490,14 @@ mod utils {
#[test]
fn resolve_type_with_value() {
let path = Path::new(EXISTING_FILE);
let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None });
let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None, short: false });
assert!(result.is_ok());
}

#[test]
fn resolve_type_with_err() {
let path = Path::new(UNSUPPORTED_FILE);
let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None });
let result = commentective::resolve_type_and_run(path, &OptionsCli { extension: None, short: false });
assert!(result.is_err());
}

Expand Down

0 comments on commit dd39f59

Please sign in to comment.