Skip to content
This repository has been archived by the owner on May 20, 2020. It is now read-only.

Commit

Permalink
implement --verbose flag
Browse files Browse the repository at this point in the history
Fixes #128.
  • Loading branch information
euclio committed Aug 28, 2017
1 parent c4723b7 commit 6ab8685
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
18 changes: 13 additions & 5 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::process::{Command, Stdio};

use serde_json;

use Config;
use Verbosity;
use error::*;
use ui::Ui;

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

let target_dir = manifest_path
let target_dir = config
.manifest_path
.parent()
.ok_or("Expected manifest_path to point to Cargo.toml")?
.join("target/rls");

command
.arg("check")
.arg("--manifest-path")
.arg(manifest_path)
.arg(&config.manifest_path)
.env("RUSTFLAGS", "-Z save-analysis")
.env("CARGO_TARGET_DIR", target_dir)
.stderr(Stdio::piped())
.stdout(Stdio::null());

if let Verbosity::Verbose = *config.ui.verbosity() {
command.arg("--verbose");
}

match target.kind {
TargetKind::Library => {
command.arg("--lib");
Expand Down Expand Up @@ -130,7 +137,8 @@ where
// cargo messages. Alternatively, we could use the JSON message format to filter, but
// that is probably overkill.
if line.starts_with("Updating") || line.starts_with("Compiling") ||
line.starts_with("Finished")
line.starts_with("Finished") || line.starts_with("Running") ||
line.starts_with("Fresh") || line.starts_with("Downloading")
{
report_progress(line);
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,11 @@ pub fn build(config: &Config, artifacts: &[&str]) -> Result<()> {
/// `Cargo.toml` file
/// - `target`: The target to document
fn generate_and_load_analysis(config: &Config, target: &Target) -> Result<()> {
let manifest_path = &config.manifest_path;

let task = config.ui.start_task("Generating save analysis data");
task.report("In progress");

let analysis_result =
cargo::generate_analysis(manifest_path, target, |progress| { task.report(progress); });
cargo::generate_analysis(config, target, |progress| { task.report(progress); });

if analysis_result.is_err() {
task.error();
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ fn run() -> rustdoc::error::Result<()> {
.help("The path to the Cargo manifest of the project you are documenting."),
)
.arg(Arg::with_name("quiet").short("q").long("quiet").help(
"No output printed to stdout.",
"No output printed to stdout",
))
.arg(Arg::with_name("verbose").short("v").long("verbose").help(
"Use verbose output",
))
.subcommand(
SubCommand::with_name("build")
Expand Down Expand Up @@ -59,6 +62,8 @@ fn run() -> rustdoc::error::Result<()> {
let assets = include!(concat!(env!("OUT_DIR"), "/asset.in"));
let verbosity = if matches.is_present("quiet") {
Verbosity::Quiet
} else if matches.is_present("verbose") {
Verbosity::Verbose
} else {
Verbosity::Normal
};
Expand Down
47 changes: 36 additions & 11 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::cell::Cell;
use std::cmp;
use std::fmt::{self, Debug};

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

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

spinner.enable_steady_tick(50);
spinner.set_style(ProgressStyle::default_spinner().template(
"{spinner} {prefix}: {wide_msg}",
));
if let Some(ref spinner) = spinner {
spinner.enable_steady_tick(50);
spinner.set_style(ProgressStyle::default_spinner().template(
"{spinner} {prefix}: {wide_msg}",
));

spinner.set_prefix(name);
spinner.set_prefix(name);
}

Task {
ui: self,
name: name.to_owned(),
spinner,
is_error: Cell::new(false),
}
Expand All @@ -39,6 +42,10 @@ impl Ui {
eprintln!("warning: {}", message);
}
}

pub fn verbosity(&self) -> &Verbosity {
&self.verbosity
}
}

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

/// Normal output, with spinners.
Normal,

/// Verbose output. No spinners are displayed, and all intermediate output is printed.
Verbose,
}

impl Default for Verbosity {
Expand All @@ -59,7 +69,8 @@ impl Default for Verbosity {

pub struct Task<'a> {
ui: &'a Ui,
spinner: ProgressBar,
name: String,
spinner: Option<ProgressBar>,
is_error: Cell<bool>,
}

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

impl<'a> Task<'a> {
pub fn report(&self, message: &str) {
self.spinner.set_message(message);
if let Some(ref spinner) = self.spinner {
spinner.set_message(message);
} else {
println!("{}: {}", self.name, message);
}
}

pub fn report_verbose(&self, message: &str) {
if self.ui.verbosity > Verbosity::Normal {
println!("{}: {}", self.name, message);
}
}

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

self.spinner.finish_with_message(message);
if let Some(ref spinner) = self.spinner {
spinner.finish_with_message(message);
} else {
println!("{}: {}", self.name, message);
}
}
}

0 comments on commit 6ab8685

Please sign in to comment.