Skip to content

Commit 6ab8685

Browse files
committed
implement --verbose flag
Fixes steveklabnik#128.
1 parent c4723b7 commit 6ab8685

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

src/cargo.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::process::{Command, Stdio};
77

88
use serde_json;
99

10+
use Config;
11+
use Verbosity;
1012
use error::*;
1113
use ui::Ui;
1214

@@ -75,29 +77,34 @@ pub fn retrieve_metadata(manifest_path: &Path) -> Result<serde_json::Value> {
7577
///
7678
/// ## Arguments
7779
///
78-
/// - `manifest_path`: The path to the crate's Cargo.toml
80+
/// - `config`: Rustdoc configuration
7981
/// - `target`: The target that we should generate the analysis data for
8082
/// - `report_progress`: A closure that should be called to report a progress message
81-
pub fn generate_analysis<F>(manifest_path: &Path, target: &Target, report_progress: F) -> Result<()>
83+
pub fn generate_analysis<F>(config: &Config, target: &Target, report_progress: F) -> Result<()>
8284
where
8385
F: Fn(&str) -> (),
8486
{
8587
let mut command = Command::new("cargo");
8688

87-
let target_dir = manifest_path
89+
let target_dir = config
90+
.manifest_path
8891
.parent()
8992
.ok_or("Expected manifest_path to point to Cargo.toml")?
9093
.join("target/rls");
9194

9295
command
9396
.arg("check")
9497
.arg("--manifest-path")
95-
.arg(manifest_path)
98+
.arg(&config.manifest_path)
9699
.env("RUSTFLAGS", "-Z save-analysis")
97100
.env("CARGO_TARGET_DIR", target_dir)
98101
.stderr(Stdio::piped())
99102
.stdout(Stdio::null());
100103

104+
if let Verbosity::Verbose = *config.ui.verbosity() {
105+
command.arg("--verbose");
106+
}
107+
101108
match target.kind {
102109
TargetKind::Library => {
103110
command.arg("--lib");
@@ -130,7 +137,8 @@ where
130137
// cargo messages. Alternatively, we could use the JSON message format to filter, but
131138
// that is probably overkill.
132139
if line.starts_with("Updating") || line.starts_with("Compiling") ||
133-
line.starts_with("Finished")
140+
line.starts_with("Finished") || line.starts_with("Running") ||
141+
line.starts_with("Fresh") || line.starts_with("Downloading")
134142
{
135143
report_progress(line);
136144
}

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,11 @@ pub fn build(config: &Config, artifacts: &[&str]) -> Result<()> {
145145
/// `Cargo.toml` file
146146
/// - `target`: The target to document
147147
fn generate_and_load_analysis(config: &Config, target: &Target) -> Result<()> {
148-
let manifest_path = &config.manifest_path;
149-
150148
let task = config.ui.start_task("Generating save analysis data");
151149
task.report("In progress");
152150

153151
let analysis_result =
154-
cargo::generate_analysis(manifest_path, target, |progress| { task.report(progress); });
152+
cargo::generate_analysis(config, target, |progress| { task.report(progress); });
155153

156154
if analysis_result.is_err() {
157155
task.error();

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ fn run() -> rustdoc::error::Result<()> {
3131
.help("The path to the Cargo manifest of the project you are documenting."),
3232
)
3333
.arg(Arg::with_name("quiet").short("q").long("quiet").help(
34-
"No output printed to stdout.",
34+
"No output printed to stdout",
35+
))
36+
.arg(Arg::with_name("verbose").short("v").long("verbose").help(
37+
"Use verbose output",
3538
))
3639
.subcommand(
3740
SubCommand::with_name("build")
@@ -59,6 +62,8 @@ fn run() -> rustdoc::error::Result<()> {
5962
let assets = include!(concat!(env!("OUT_DIR"), "/asset.in"));
6063
let verbosity = if matches.is_present("quiet") {
6164
Verbosity::Quiet
65+
} else if matches.is_present("verbose") {
66+
Verbosity::Verbose
6267
} else {
6368
Verbosity::Normal
6469
};

src/ui.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::cell::Cell;
2-
use std::cmp;
32
use std::fmt::{self, Debug};
43

54
use indicatif::{ProgressBar, ProgressStyle};
@@ -16,19 +15,23 @@ impl Ui {
1615

1716
pub fn start_task(&self, name: &str) -> Task {
1817
let spinner = match self.verbosity {
19-
Verbosity::Normal => ProgressBar::new_spinner(),
20-
Verbosity::Quiet => ProgressBar::hidden(),
18+
Verbosity::Normal => Some(ProgressBar::new_spinner()),
19+
Verbosity::Quiet => Some(ProgressBar::hidden()),
20+
Verbosity::Verbose => None,
2121
};
2222

23-
spinner.enable_steady_tick(50);
24-
spinner.set_style(ProgressStyle::default_spinner().template(
25-
"{spinner} {prefix}: {wide_msg}",
26-
));
23+
if let Some(ref spinner) = spinner {
24+
spinner.enable_steady_tick(50);
25+
spinner.set_style(ProgressStyle::default_spinner().template(
26+
"{spinner} {prefix}: {wide_msg}",
27+
));
2728

28-
spinner.set_prefix(name);
29+
spinner.set_prefix(name);
30+
}
2931

3032
Task {
3133
ui: self,
34+
name: name.to_owned(),
3235
spinner,
3336
is_error: Cell::new(false),
3437
}
@@ -39,6 +42,10 @@ impl Ui {
3942
eprintln!("warning: {}", message);
4043
}
4144
}
45+
46+
pub fn verbosity(&self) -> &Verbosity {
47+
&self.verbosity
48+
}
4249
}
4350

4451
/// The verbosity of the output displayed to the user.
@@ -49,6 +56,9 @@ pub enum Verbosity {
4956

5057
/// Normal output, with spinners.
5158
Normal,
59+
60+
/// Verbose output. No spinners are displayed, and all intermediate output is printed.
61+
Verbose,
5262
}
5363

5464
impl Default for Verbosity {
@@ -59,7 +69,8 @@ impl Default for Verbosity {
5969

6070
pub struct Task<'a> {
6171
ui: &'a Ui,
62-
spinner: ProgressBar,
72+
name: String,
73+
spinner: Option<ProgressBar>,
6374
is_error: Cell<bool>,
6475
}
6576

@@ -75,7 +86,17 @@ impl<'a> Debug for Task<'a> {
7586

7687
impl<'a> Task<'a> {
7788
pub fn report(&self, message: &str) {
78-
self.spinner.set_message(message);
89+
if let Some(ref spinner) = self.spinner {
90+
spinner.set_message(message);
91+
} else {
92+
println!("{}: {}", self.name, message);
93+
}
94+
}
95+
96+
pub fn report_verbose(&self, message: &str) {
97+
if self.ui.verbosity > Verbosity::Normal {
98+
println!("{}: {}", self.name, message);
99+
}
79100
}
80101

81102
pub fn error(&self) {
@@ -91,6 +112,10 @@ impl<'a> Drop for Task<'a> {
91112
"Done"
92113
};
93114

94-
self.spinner.finish_with_message(message);
115+
if let Some(ref spinner) = self.spinner {
116+
spinner.finish_with_message(message);
117+
} else {
118+
println!("{}: {}", self.name, message);
119+
}
95120
}
96121
}

0 commit comments

Comments
 (0)