Skip to content

Commit 55ac062

Browse files
committed
Add --quiet flag, remove Plain write mode
cc #1976
1 parent 5dba81b commit 55ac062

File tree

5 files changed

+46
-29
lines changed

5 files changed

+46
-29
lines changed

src/bin/main.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ use failure::err_msg;
2525
use getopts::{Matches, Options};
2626

2727
use rustfmt::{
28-
emit_post_matter, emit_pre_matter, load_config, CliOptions, Config, FmtResult, WriteMode,
29-
WRITE_MODE_LIST,
28+
emit_post_matter, emit_pre_matter, format_and_emit_report, load_config, CliOptions, Config,
29+
FileName, FmtResult, Input, Summary, Verbosity, WriteMode, WRITE_MODE_LIST,
3030
};
31-
use rustfmt::{format_and_emit_report, FileName, Input, Summary};
3231

3332
fn main() {
3433
env_logger::init();
@@ -144,6 +143,7 @@ fn make_opts() -> Options {
144143
"Enables unstable features. Only available on nightly channel",
145144
);
146145
opts.optflag("v", "verbose", "Print verbose output");
146+
opts.optflag("q", "quiet", "Print less output");
147147
opts.optflag("V", "version", "Show version information");
148148
opts.optopt(
149149
"",
@@ -187,8 +187,9 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> {
187187
let options = CliOptions::from_matches(&matches)?;
188188
let (mut config, _) = load_config(Some(Path::new(".")), Some(&options))?;
189189

190-
// write_mode is always Plain for Stdin.
191-
config.set().write_mode(WriteMode::Plain);
190+
// write_mode is always Display for Stdin.
191+
config.set().write_mode(WriteMode::Display);
192+
config.set().verbose(Verbosity::Quiet);
192193

193194
// parse file_lines
194195
if let Some(ref file_lines) = matches.opt_str("file-lines") {
@@ -211,7 +212,7 @@ fn execute(opts: &Options) -> FmtResult<(WriteMode, Summary)> {
211212
}
212213
emit_post_matter(&config)?;
213214

214-
Ok((WriteMode::Plain, error_summary))
215+
Ok((WriteMode::Display, error_summary))
215216
}
216217
Operation::Format {
217218
files,
@@ -231,7 +232,7 @@ fn format(
231232
options.verify_file_lines(&files);
232233
let (config, config_path) = load_config(None, Some(&options))?;
233234

234-
if config.verbose() {
235+
if config.verbose() == Verbosity::Verbose {
235236
if let Some(path) = config_path.as_ref() {
236237
println!("Using rustfmt config file {}", path.display());
237238
}
@@ -252,7 +253,7 @@ fn format(
252253
let local_config = if config_path.is_none() {
253254
let (local_config, config_path) =
254255
load_config(Some(file.parent().unwrap()), Some(&options))?;
255-
if local_config.verbose() {
256+
if local_config.verbose() == Verbosity::Verbose {
256257
if let Some(path) = config_path {
257258
println!(
258259
"Using rustfmt config file {} for {}",

src/config/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ create_config! {
143143
"Skip formatting the specified files and directories.";
144144

145145
// Not user-facing
146-
verbose: bool, false, false, "Use verbose output";
146+
verbose: Verbosity, Verbosity::Normal, false, "How much to information to emit to the user";
147147
verbose_diff: bool, false, false, "Emit verbose diffs";
148148
file_lines: FileLines, FileLines::all(), false,
149149
"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<Option<PathBuf>, Error> {
230230

231231
#[cfg(test)]
232232
mod test {
233-
use super::Config;
233+
use super::*;
234234
use std::str;
235235

236236
#[allow(dead_code)]
@@ -249,7 +249,8 @@ mod test {
249249
"Require a specific version of rustfmt.";
250250
ignore: IgnoreList, IgnoreList::default(), false,
251251
"Skip formatting the specified files and directories.";
252-
verbose: bool, false, false, "Use verbose output";
252+
verbose: Verbosity, Verbosity::Normal, false,
253+
"How much to information to emit to the user";
253254
file_lines: FileLines, FileLines::all(), false,
254255
"Lines to format; this is not supported in rustfmt.toml, and can only be specified \
255256
via the --file-lines option";
@@ -265,10 +266,10 @@ mod test {
265266
#[test]
266267
fn test_config_set() {
267268
let mut config = Config::default();
268-
config.set().verbose(false);
269-
assert_eq!(config.verbose(), false);
270-
config.set().verbose(true);
271-
assert_eq!(config.verbose(), true);
269+
config.set().verbose(Verbosity::Quiet);
270+
assert_eq!(config.verbose(), Verbosity::Quiet);
271+
config.set().verbose(Verbosity::Normal);
272+
assert_eq!(config.verbose(), Verbosity::Normal);
272273
}
273274

274275
#[test]

src/config/options.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ configuration_option_enum! { WriteMode:
185185
// Displays how much of the input file was processed
186186
Coverage,
187187
// Unfancy stdout
188-
Plain,
189-
// Outputs a checkstyle XML file.
190188
Checkstyle,
191189
// Output the changed lines (for internal value only)
192190
Modified,
@@ -207,6 +205,14 @@ configuration_option_enum! { Color:
207205
Auto,
208206
}
209207

208+
configuration_option_enum! { Verbosity:
209+
// Emit more.
210+
Verbose,
211+
Normal,
212+
// Emit as little as possible.
213+
Quiet,
214+
}
215+
210216
#[derive(Deserialize, Serialize, Clone, Debug)]
211217
pub struct WidthHeuristics {
212218
// Maximum width of the args of a function call before falling back
@@ -322,6 +328,7 @@ impl ::std::str::FromStr for IgnoreList {
322328
#[derive(Clone, Debug, Default)]
323329
pub struct CliOptions {
324330
skip_children: Option<bool>,
331+
quiet: bool,
325332
verbose: bool,
326333
verbose_diff: bool,
327334
pub(super) config_path: Option<PathBuf>,
@@ -336,6 +343,10 @@ impl CliOptions {
336343
pub fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
337344
let mut options = CliOptions::default();
338345
options.verbose = matches.opt_present("verbose");
346+
options.quiet = matches.opt_present("quiet");
347+
if options.verbose && options.quiet {
348+
return Err(format_err!("Can't use both `--verbose` and `--quiet`"));
349+
}
339350
options.verbose_diff = matches.opt_present("verbose-diff");
340351

341352
let unstable_features = matches.opt_present("unstable-features");
@@ -386,7 +397,13 @@ impl CliOptions {
386397
}
387398

388399
pub fn apply_to(self, config: &mut Config) {
389-
config.set().verbose(self.verbose);
400+
if self.verbose {
401+
config.set().verbose(Verbosity::Verbose);
402+
} else if self.quiet {
403+
config.set().verbose(Verbosity::Quiet);
404+
} else {
405+
config.set().verbose(Verbosity::Normal);
406+
}
390407
config.set().verbose_diff(self.verbose_diff);
391408
config.set().file_lines(self.file_lines);
392409
config.set().unstable_features(self.unstable_features);

src/filemap.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::io::{self, BufWriter, Read, Write};
1515
use std::path::Path;
1616

1717
use checkstyle::output_checkstyle_file;
18-
use config::{Config, NewlineStyle, WriteMode};
18+
use config::{Config, NewlineStyle, Verbosity, WriteMode};
1919
use rustfmt_diff::{make_diff, output_modified, print_diff, Mismatch};
2020
use syntax::codemap::FileName;
2121

@@ -150,11 +150,10 @@ where
150150
write_system_newlines(file, text, config)?;
151151
}
152152
}
153-
WriteMode::Plain => {
154-
write_system_newlines(out, text, config)?;
155-
}
156153
WriteMode::Display | WriteMode::Coverage => {
157-
println!("{}:\n", filename);
154+
if config.verbose() != Verbosity::Quiet {
155+
println!("{}:\n", filename);
156+
}
158157
write_system_newlines(out, text, config)?;
159158
}
160159
WriteMode::Diff => {

src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use visitor::{FmtVisitor, SnippetProvider};
6464

6565
pub use config::options::CliOptions;
6666
pub use config::summary::Summary;
67-
pub use config::{file_lines, load_config, Config, WriteMode};
67+
pub use config::{file_lines, load_config, Config, Verbosity, WriteMode};
6868

6969
pub type FmtResult<T> = std::result::Result<T, failure::Error>;
7070

@@ -331,7 +331,7 @@ fn should_emit_verbose<F>(path: &FileName, config: &Config, f: F)
331331
where
332332
F: Fn(),
333333
{
334-
if config.verbose() && path.to_string() != STDIN {
334+
if config.verbose() == Verbosity::Verbose && path.to_string() != STDIN {
335335
f();
336336
}
337337
}
@@ -351,9 +351,7 @@ where
351351
// diff mode: check if any files are differing
352352
let mut has_diff = false;
353353

354-
// We always skip children for the "Plain" write mode, since there is
355-
// nothing to distinguish the nested module contents.
356-
let skip_children = config.skip_children() || config.write_mode() == config::WriteMode::Plain;
354+
let skip_children = config.skip_children();
357355
for (path, module) in modules::list_files(krate, parse_session.codemap())? {
358356
if (skip_children && path != *main_file) || config.ignore().skip_file(&path) {
359357
continue;
@@ -603,7 +601,8 @@ pub fn format_snippet(snippet: &str, config: &Config) -> Option<String> {
603601
let mut out: Vec<u8> = Vec::with_capacity(snippet.len() * 2);
604602
let input = Input::Text(snippet.into());
605603
let mut config = config.clone();
606-
config.set().write_mode(config::WriteMode::Plain);
604+
config.set().write_mode(config::WriteMode::Display);
605+
config.set().verbose(Verbosity::Quiet);
607606
config.set().hide_parse_errors(true);
608607
match format_input(input, &config, Some(&mut out)) {
609608
// `format_input()` returns an empty string on parsing error.

0 commit comments

Comments
 (0)