diff --git a/src/bin/main.rs b/src/bin/main.rs index b7bb92c858f4..9577d041388f 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -25,10 +25,9 @@ use failure::err_msg; use getopts::{Matches, Options}; use rustfmt::{ - emit_post_matter, emit_pre_matter, load_config, CliOptions, Config, FmtResult, WriteMode, - WRITE_MODE_LIST, + emit_post_matter, emit_pre_matter, format_and_emit_report, load_config, CliOptions, Config, + FileName, FmtResult, Input, Summary, Verbosity, WriteMode, WRITE_MODE_LIST, }; -use rustfmt::{format_and_emit_report, FileName, Input, Summary}; fn main() { env_logger::init(); @@ -144,6 +143,7 @@ fn make_opts() -> Options { "Enables unstable features. Only available on nightly channel", ); opts.optflag("v", "verbose", "Print verbose output"); + opts.optflag("q", "quiet", "Print less output"); opts.optflag("V", "version", "Show version information"); opts.optopt( "", @@ -187,8 +187,9 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> { let options = CliOptions::from_matches(&matches)?; let (mut config, _) = load_config(Some(Path::new(".")), Some(&options))?; - // write_mode is always Plain for Stdin. - config.set().write_mode(WriteMode::Plain); + // write_mode is always Display for Stdin. + config.set().write_mode(WriteMode::Display); + config.set().verbose(Verbosity::Quiet); // parse file_lines if let Some(ref file_lines) = matches.opt_str("file-lines") { @@ -211,7 +212,7 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> { } emit_post_matter(&config)?; - Ok((WriteMode::Plain, error_summary)) + Ok((WriteMode::Display, error_summary)) } Operation::Format { files, @@ -231,7 +232,7 @@ fn format( options.verify_file_lines(&files); let (config, config_path) = load_config(None, Some(&options))?; - if config.verbose() { + if config.verbose() == Verbosity::Verbose { if let Some(path) = config_path.as_ref() { println!("Using rustfmt config file {}", path.display()); } @@ -252,7 +253,7 @@ fn format( let local_config = if config_path.is_none() { let (local_config, config_path) = load_config(Some(file.parent().unwrap()), Some(&options))?; - if local_config.verbose() { + if local_config.verbose() == Verbosity::Verbose { if let Some(path) = config_path { println!( "Using rustfmt config file {} for {}", diff --git a/src/config/mod.rs b/src/config/mod.rs index 382e7e7cc924..5f6d9082ce87 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -143,7 +143,7 @@ create_config! { "Skip formatting the specified files and directories."; // Not user-facing - verbose: bool, false, false, "Use verbose output"; + verbose: Verbosity, Verbosity::Normal, false, "How much to information to emit to the user"; verbose_diff: bool, false, false, "Emit verbose diffs"; file_lines: FileLines, FileLines::all(), false, "Lines to format; this is not supported in rustfmt.toml, and can only be specified \ @@ -230,7 +230,7 @@ fn config_path(options: &CliOptions) -> Result, Error> { #[cfg(test)] mod test { - use super::Config; + use super::*; use std::str; #[allow(dead_code)] @@ -249,7 +249,8 @@ mod test { "Require a specific version of rustfmt."; ignore: IgnoreList, IgnoreList::default(), false, "Skip formatting the specified files and directories."; - verbose: bool, false, false, "Use verbose output"; + verbose: Verbosity, Verbosity::Normal, false, + "How much to information to emit to the user"; file_lines: FileLines, FileLines::all(), false, "Lines to format; this is not supported in rustfmt.toml, and can only be specified \ via the --file-lines option"; @@ -265,10 +266,10 @@ mod test { #[test] fn test_config_set() { let mut config = Config::default(); - config.set().verbose(false); - assert_eq!(config.verbose(), false); - config.set().verbose(true); - assert_eq!(config.verbose(), true); + config.set().verbose(Verbosity::Quiet); + assert_eq!(config.verbose(), Verbosity::Quiet); + config.set().verbose(Verbosity::Normal); + assert_eq!(config.verbose(), Verbosity::Normal); } #[test] diff --git a/src/config/options.rs b/src/config/options.rs index bd8823f9ce41..e18c88c30273 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -185,8 +185,6 @@ configuration_option_enum! { WriteMode: // Displays how much of the input file was processed Coverage, // Unfancy stdout - Plain, - // Outputs a checkstyle XML file. Checkstyle, // Output the changed lines (for internal value only) Modified, @@ -207,6 +205,14 @@ configuration_option_enum! { Color: Auto, } +configuration_option_enum! { Verbosity: + // Emit more. + Verbose, + Normal, + // Emit as little as possible. + Quiet, +} + #[derive(Deserialize, Serialize, Clone, Debug)] pub struct WidthHeuristics { // Maximum width of the args of a function call before falling back @@ -322,6 +328,7 @@ impl ::std::str::FromStr for IgnoreList { #[derive(Clone, Debug, Default)] pub struct CliOptions { skip_children: Option, + quiet: bool, verbose: bool, verbose_diff: bool, pub(super) config_path: Option, @@ -336,6 +343,10 @@ impl CliOptions { pub fn from_matches(matches: &Matches) -> FmtResult { let mut options = CliOptions::default(); options.verbose = matches.opt_present("verbose"); + options.quiet = matches.opt_present("quiet"); + if options.verbose && options.quiet { + return Err(format_err!("Can't use both `--verbose` and `--quiet`")); + } options.verbose_diff = matches.opt_present("verbose-diff"); let unstable_features = matches.opt_present("unstable-features"); @@ -386,7 +397,13 @@ impl CliOptions { } pub fn apply_to(self, config: &mut Config) { - config.set().verbose(self.verbose); + if self.verbose { + config.set().verbose(Verbosity::Verbose); + } else if self.quiet { + config.set().verbose(Verbosity::Quiet); + } else { + config.set().verbose(Verbosity::Normal); + } config.set().verbose_diff(self.verbose_diff); config.set().file_lines(self.file_lines); config.set().unstable_features(self.unstable_features); diff --git a/src/filemap.rs b/src/filemap.rs index f74918935129..fc86beeede46 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -15,7 +15,7 @@ use std::io::{self, BufWriter, Read, Write}; use std::path::Path; use checkstyle::output_checkstyle_file; -use config::{Config, NewlineStyle, WriteMode}; +use config::{Config, NewlineStyle, Verbosity, WriteMode}; use rustfmt_diff::{make_diff, output_modified, print_diff, Mismatch}; use syntax::codemap::FileName; @@ -150,11 +150,10 @@ where write_system_newlines(file, text, config)?; } } - WriteMode::Plain => { - write_system_newlines(out, text, config)?; - } WriteMode::Display | WriteMode::Coverage => { - println!("{}:\n", filename); + if config.verbose() != Verbosity::Quiet { + println!("{}:\n", filename); + } write_system_newlines(out, text, config)?; } WriteMode::Diff => { diff --git a/src/lib.rs b/src/lib.rs index 48c9295c4f9f..115b21f4b11f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ use visitor::{FmtVisitor, SnippetProvider}; pub use config::options::CliOptions; pub use config::summary::Summary; -pub use config::{file_lines, load_config, Config, WriteMode}; +pub use config::{file_lines, load_config, Config, Verbosity, WriteMode}; pub type FmtResult = std::result::Result; @@ -331,7 +331,7 @@ fn should_emit_verbose(path: &FileName, config: &Config, f: F) where F: Fn(), { - if config.verbose() && path.to_string() != STDIN { + if config.verbose() == Verbosity::Verbose && path.to_string() != STDIN { f(); } } @@ -351,9 +351,7 @@ where // diff mode: check if any files are differing let mut has_diff = false; - // We always skip children for the "Plain" write mode, since there is - // nothing to distinguish the nested module contents. - let skip_children = config.skip_children() || config.write_mode() == config::WriteMode::Plain; + let skip_children = config.skip_children(); for (path, module) in modules::list_files(krate, parse_session.codemap())? { if (skip_children && path != *main_file) || config.ignore().skip_file(&path) { continue; @@ -603,7 +601,8 @@ pub fn format_snippet(snippet: &str, config: &Config) -> Option { let mut out: Vec = Vec::with_capacity(snippet.len() * 2); let input = Input::Text(snippet.into()); let mut config = config.clone(); - config.set().write_mode(config::WriteMode::Plain); + config.set().write_mode(config::WriteMode::Display); + config.set().verbose(Verbosity::Quiet); config.set().hide_parse_errors(true); match format_input(input, &config, Some(&mut out)) { // `format_input()` returns an empty string on parsing error.