Merge pull request #605 from sanxiyn/verbose

Add verbose mode
This commit is contained in:
Nick Cameron 2015-11-16 07:33:39 +13:00
commit 605bcdae0a
3 changed files with 22 additions and 11 deletions

View file

@ -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;
}

View file

@ -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";

View file

@ -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);