From faff4bc9e83e795c5d94ce7cfee9893459abd439 Mon Sep 17 00:00:00 2001
From: Seo Sanghyeon <sanxiyn@gmail.com>
Date: Sun, 15 Nov 2015 15:41:41 +0900
Subject: [PATCH] Add verbose mode

---
 src/bin/rustfmt.rs | 29 ++++++++++++++++++-----------
 src/config.rs      |  1 +
 src/lib.rs         |  3 +++
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs
index b90cdb3d3ca..789f8773345 100644
--- a/src/bin/rustfmt.rs
+++ b/src/bin/rustfmt.rs
@@ -25,7 +25,7 @@ use std::fs::{self, File};
 use std::io::{self, Read, Write};
 use std::path::{Path, PathBuf};
 
-use getopts::Options;
+use getopts::{Matches, Options};
 
 /// Rustfmt operations.
 enum Operation {
@@ -75,9 +75,14 @@ fn lookup_and_read_project_file(input_file: &Path) -> io::Result<(PathBuf, Strin
     Ok((path, toml))
 }
 
+fn update_config(config: &mut Config, matches: &Matches) {
+    config.verbose = matches.opt_present("verbose");
+}
+
 fn execute() -> i32 {
     let mut opts = Options::new();
     opts.optflag("h", "help", "show this message");
+    opts.optflag("v", "verbose", "show progress");
     opts.optopt("",
                 "write-mode",
                 "mode to write in (not usable when piping from stdin)",
@@ -87,7 +92,15 @@ fn execute() -> i32 {
                  "config-help",
                  "show details of rustfmt configuration options");
 
-    let operation = determine_operation(&opts, env::args().skip(1));
+    let matches = match opts.parse(env::args().skip(1)) {
+        Ok(m) => m,
+        Err(e) => {
+            print_usage(&opts, &e.to_string());
+            return 1;
+        }
+    };
+
+    let operation = determine_operation(&matches);
 
     match operation {
         Operation::InvalidInput(reason) => {
@@ -116,7 +129,7 @@ fn execute() -> i32 {
         }
         Operation::Format(files, write_mode) => {
             for file in files {
-                let config = match lookup_and_read_project_file(&file) {
+                let mut config = match lookup_and_read_project_file(&file) {
                     Ok((path, toml)) => {
                         println!("Using rustfmt config file {} for {}",
                                  path.display(),
@@ -126,6 +139,7 @@ fn execute() -> i32 {
                     Err(_) => Default::default(),
                 };
 
+                update_config(&mut config, &matches);
                 run(&file, write_mode, &config);
             }
             0
@@ -154,14 +168,7 @@ fn print_usage(opts: &Options, reason: &str) {
     println!("{}", opts.usage(&reason));
 }
 
-fn determine_operation<I>(opts: &Options, args: I) -> Operation
-    where I: Iterator<Item = String>
-{
-    let matches = match opts.parse(args) {
-        Ok(m) => m,
-        Err(e) => return Operation::InvalidInput(e.to_string()),
-    };
-
+fn determine_operation(matches: &Matches) -> Operation {
     if matches.opt_present("h") {
         return Operation::Help;
     }
diff --git a/src/config.rs b/src/config.rs
index dcf0816335b..6d1c863bea8 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -257,6 +257,7 @@ macro_rules! create_config {
 }
 
 create_config! {
+    verbose: bool, false, "Use verbose output";
     max_width: usize, 100, "Maximum width of each line";
     ideal_width: usize, 80, "Ideal width of each line";
     tab_spaces: usize, 4, "Number of spaces per tab";
diff --git a/src/lib.rs b/src/lib.rs
index e73ed86599d..bf1a0164f68 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -300,6 +300,9 @@ fn fmt_ast(krate: &ast::Crate,
     let mut file_map = FileMap::new();
     for (path, module) in modules::list_files(krate, parse_session.codemap()) {
         let path = path.to_str().unwrap();
+        if config.verbose {
+            println!("Formatting {}", path);
+        }
         let mut visitor = FmtVisitor::from_codemap(parse_session, config, Some(mode));
         visitor.format_separate_mod(module, path);
         file_map.insert(path.to_owned(), visitor.buffer);