From a31933e4b58f3520a04b99ee60acbabf5695d91e Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Thu, 12 Sep 2019 11:45:33 +0300
Subject: add quiet mode to analysis-stats

---
 crates/ra_cli/src/analysis_stats.rs |  25 +++++--
 crates/ra_cli/src/help.rs           | 145 ++++++++++++++++++------------------
 crates/ra_cli/src/main.rs           |  28 ++++++-
 3 files changed, 116 insertions(+), 82 deletions(-)

(limited to 'crates/ra_cli')

diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs
index 1fad5b233..8f4ce42da 100644
--- a/crates/ra_cli/src/analysis_stats.rs
+++ b/crates/ra_cli/src/analysis_stats.rs
@@ -4,9 +4,14 @@ use ra_db::SourceDatabase;
 use ra_hir::{Crate, HasBodySource, HasSource, HirDisplay, ImplItem, ModuleDef, Ty, TypeWalk};
 use ra_syntax::AstNode;
 
-use crate::Result;
+use crate::{Result, Verbosity};
 
-pub fn run(verbose: bool, memory_usage: bool, path: &Path, only: Option<&str>) -> Result<()> {
+pub fn run(
+    verbosity: Verbosity,
+    memory_usage: bool,
+    path: &Path,
+    only: Option<&str>,
+) -> Result<()> {
     let db_load_time = Instant::now();
     let (mut host, roots) = ra_batch::load_cargo(path)?;
     let db = host.raw_database();
@@ -55,10 +60,14 @@ pub fn run(verbose: bool, memory_usage: bool, path: &Path, only: Option<&str>) -
     println!("Item Collection: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage());
 
     let inference_time = Instant::now();
-    let bar = indicatif::ProgressBar::with_draw_target(
-        funcs.len() as u64,
-        indicatif::ProgressDrawTarget::stderr_nohz(),
-    );
+    let bar = match verbosity {
+        Verbosity::Verbose | Verbosity::Normal => indicatif::ProgressBar::with_draw_target(
+            funcs.len() as u64,
+            indicatif::ProgressDrawTarget::stderr_nohz(),
+        ),
+        Verbosity::Quiet => indicatif::ProgressBar::hidden(),
+    };
+
     bar.set_style(
         indicatif::ProgressStyle::default_bar().template("{wide_bar} {pos}/{len}\n{msg}"),
     );
@@ -70,7 +79,7 @@ pub fn run(verbose: bool, memory_usage: bool, path: &Path, only: Option<&str>) -
     for f in funcs {
         let name = f.name(db);
         let mut msg = format!("processing: {}", name);
-        if verbose {
+        if verbosity.is_verbose() {
             let src = f.source(db);
             let original_file = src.file_id.original_file(db);
             let path = db.file_relative_path(original_file);
@@ -103,7 +112,7 @@ pub fn run(verbose: bool, memory_usage: bool, path: &Path, only: Option<&str>) -
             }
             if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
                 num_type_mismatches += 1;
-                if verbose {
+                if verbosity.is_verbose() {
                     let src = f.expr_source(db, expr_id);
                     if let Some(src) = src {
                         // FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
diff --git a/crates/ra_cli/src/help.rs b/crates/ra_cli/src/help.rs
index 5171578f0..2a74b8733 100644
--- a/crates/ra_cli/src/help.rs
+++ b/crates/ra_cli/src/help.rs
@@ -1,72 +1,73 @@
-pub const GLOBAL_HELP: &str = "ra-cli
-
-USAGE:
-    ra_cli <SUBCOMMAND>
-
-FLAGS:
-    -h, --help        Prints help information
-
-SUBCOMMANDS:
-    analysis-bench
-    analysis-stats
-    highlight
-    parse
-    symbols";
-
-pub const ANALYSIS_BENCH_HELP: &str = "ra_cli-analysis-bench
-
-USAGE:
-    ra_cli analysis-bench [FLAGS] [OPTIONS] [PATH]
-
-FLAGS:
-    -h, --help        Prints help information
-    -v, --verbose
-    
-OPTIONS:
-    --complete <PATH:LINE:COLUMN>    Compute completions at this location
-    --highlight <PATH>               Hightlight this file
-    
-ARGS:
-    <PATH>    Project to analyse";
-
-pub const ANALYSIS_STATS_HELP: &str = "ra-cli-analysis-stats
-
-USAGE:
-    ra_cli analysis-stats [FLAGS] [OPTIONS] [PATH]
-    
-FLAGS:
-    -h, --help            Prints help information
-        --memory-usage
-    -v, --verbose
-    
-OPTIONS:
-    -o <ONLY>
-    
-ARGS:
-    <PATH>";
-
-pub const HIGHLIGHT_HELP: &str = "ra-cli-highlight
-    
-USAGE:
-    ra_cli highlight [FLAGS]
-    
-FLAGS:
-    -h, --help       Prints help information
-    -r, --rainbow";
-
-pub const SYMBOLS_HELP: &str = "ra-cli-symbols
-    
-USAGE:
-    ra_cli highlight [FLAGS]
-    
-FLAGS:
-    -h, --help    Prints help inforamtion";
-
-pub const PARSE_HELP: &str = "ra-cli-parse
-    
-USAGE:
-    ra_cli parse [FLAGS]
-    
-FLAGS:
-    -h, --help       Prints help inforamtion
-        --no-dump";
+pub const GLOBAL_HELP: &str = "ra-cli
+
+USAGE:
+    ra_cli <SUBCOMMAND>
+
+FLAGS:
+    -h, --help        Prints help information
+
+SUBCOMMANDS:
+    analysis-bench
+    analysis-stats
+    highlight
+    parse
+    symbols";
+
+pub const ANALYSIS_BENCH_HELP: &str = "ra_cli-analysis-bench
+
+USAGE:
+    ra_cli analysis-bench [FLAGS] [OPTIONS] [PATH]
+
+FLAGS:
+    -h, --help        Prints help information
+    -v, --verbose
+
+OPTIONS:
+    --complete <PATH:LINE:COLUMN>    Compute completions at this location
+    --highlight <PATH>               Hightlight this file
+
+ARGS:
+    <PATH>    Project to analyse";
+
+pub const ANALYSIS_STATS_HELP: &str = "ra-cli-analysis-stats
+
+USAGE:
+    ra_cli analysis-stats [FLAGS] [OPTIONS] [PATH]
+
+FLAGS:
+    -h, --help            Prints help information
+        --memory-usage
+    -v, --verbose
+    -q, --quiet
+
+OPTIONS:
+    -o <ONLY>
+
+ARGS:
+    <PATH>";
+
+pub const HIGHLIGHT_HELP: &str = "ra-cli-highlight
+
+USAGE:
+    ra_cli highlight [FLAGS]
+
+FLAGS:
+    -h, --help       Prints help information
+    -r, --rainbow";
+
+pub const SYMBOLS_HELP: &str = "ra-cli-symbols
+
+USAGE:
+    ra_cli highlight [FLAGS]
+
+FLAGS:
+    -h, --help    Prints help inforamtion";
+
+pub const PARSE_HELP: &str = "ra-cli-parse
+
+USAGE:
+    ra_cli parse [FLAGS]
+
+FLAGS:
+    -h, --help       Prints help inforamtion
+        --no-dump";
diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs
index 23bb83ff1..ca9275cd4 100644
--- a/crates/ra_cli/src/main.rs
+++ b/crates/ra_cli/src/main.rs
@@ -12,6 +12,22 @@ use ra_syntax::{AstNode, SourceFile};
 
 type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
 
+#[derive(Clone, Copy)]
+pub enum Verbosity {
+    Verbose,
+    Normal,
+    Quiet,
+}
+
+impl Verbosity {
+    fn is_verbose(&self) -> bool {
+        match self {
+            Verbosity::Verbose => true,
+            _ => false,
+        }
+    }
+}
+
 fn main() -> Result<()> {
     Logger::with_env().start()?;
 
@@ -67,7 +83,15 @@ fn main() -> Result<()> {
                 eprintln!("{}", help::ANALYSIS_STATS_HELP);
                 return Ok(());
             }
-            let verbose = matches.contains(["-v", "--verbose"]);
+            let verbosity = match (
+                matches.contains(["-v", "--verbose"]),
+                matches.contains(["-q", "--quiet"]),
+            ) {
+                (false, false) => Verbosity::Normal,
+                (false, true) => Verbosity::Quiet,
+                (true, false) => Verbosity::Verbose,
+                (true, true) => Err("Invalid flags: -q conflicts with -v")?,
+            };
             let memory_usage = matches.contains("--memory-usage");
             let only = matches.value_from_str(["-o", "--only"])?.map(|v: String| v.to_owned());
             let path = {
@@ -79,7 +103,7 @@ fn main() -> Result<()> {
                 trailing.pop().unwrap()
             };
             analysis_stats::run(
-                verbose,
+                verbosity,
                 memory_usage,
                 path.as_ref(),
                 only.as_ref().map(String::as_ref),
-- 
cgit v1.2.3