From 45aa2068c3724cfbdae6295aa2a18ca0d69998e5 Mon Sep 17 00:00:00 2001 From: l00556901 Date: Wed, 24 Nov 2021 09:02:46 +0800 Subject: [PATCH 1/9] search color --- src/cargo/ops/registry.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index fe0a9469272..8d512a88db2 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -13,6 +13,7 @@ use crates_io::{self, NewCrate, NewCrateDependency, Registry}; use curl::easy::{Easy, InfoType, SslOpt, SslVersion}; use log::{log, Level}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; +use termcolor::Color::Green; use crate::core::dependency::DepKind; use crate::core::manifest::ManifestMetadata; @@ -57,6 +58,12 @@ pub struct PublishOpts<'cfg> { pub cli_features: CliFeatures, } +struct MessageInfo { + name: String, + space: String, + desc: String, +} + pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { let specs = opts.to_publish.to_package_id_specs(ws)?; let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; @@ -953,16 +960,27 @@ pub fn search( }); for (name, description) in names.into_iter().zip(descriptions) { - let line = match description { + let message = match description { Some(desc) => { let space = repeat(' ') .take(description_margin - name.len()) .collect::(); - name + &space + "# " + &desc + MessageInfo { + name: name, + space: space, + desc: desc, + } + } + None => { + MessageInfo { + name: name, + space: String::new(), + desc: String::new(), + } } - None => name, }; - drop_println!(config, "{}", line); + let _ = config.shell().print(&message.name, Some(&(message.space + "# " + &message.desc)), Green, true); + } let search_max_limit = 100; From 877b339b551f4c612721a76d1d05aed63cab339c Mon Sep 17 00:00:00 2001 From: l00556901 Date: Wed, 24 Nov 2021 09:12:28 +0800 Subject: [PATCH 2/9] fmt --- src/cargo/ops/registry.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 8d512a88db2..cf9e8c6ad15 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -971,16 +971,18 @@ pub fn search( desc: desc, } } - None => { - MessageInfo { - name: name, - space: String::new(), - desc: String::new(), - } - } + None => MessageInfo { + name: name, + space: String::new(), + desc: String::new(), + }, }; - let _ = config.shell().print(&message.name, Some(&(message.space + "# " + &message.desc)), Green, true); - + let _ = config.shell().print( + &message.name, + Some(&(message.space + "# " + &message.desc)), + Green, + true, + ); } let search_max_limit = 100; From aa668b42e19df5fc641e6d99d4300f78def6af30 Mon Sep 17 00:00:00 2001 From: l00556901 Date: Wed, 24 Nov 2021 09:17:58 +0800 Subject: [PATCH 3/9] fix private to public --- src/cargo/core/shell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 887b8967d25..15f6c741e99 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -124,7 +124,7 @@ impl Shell { /// Prints a message, where the status will have `color` color, and can be justified. The /// messages follows without color. - fn print( + pub fn print( &mut self, status: &dyn fmt::Display, message: Option<&dyn fmt::Display>, From 971618ec240f7d752a0782f30bd0314b286b4936 Mon Sep 17 00:00:00 2001 From: l00556901 Date: Wed, 24 Nov 2021 10:50:38 +0800 Subject: [PATCH 4/9] fix test --- src/cargo/core/shell.rs | 2 +- src/cargo/ops/registry.rs | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 15f6c741e99..887b8967d25 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -124,7 +124,7 @@ impl Shell { /// Prints a message, where the status will have `color` color, and can be justified. The /// messages follows without color. - pub fn print( + fn print( &mut self, status: &dyn fmt::Display, message: Option<&dyn fmt::Display>, diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index cf9e8c6ad15..0280904f863 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -13,7 +13,6 @@ use crates_io::{self, NewCrate, NewCrateDependency, Registry}; use curl::easy::{Easy, InfoType, SslOpt, SslVersion}; use log::{log, Level}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; -use termcolor::Color::Green; use crate::core::dependency::DepKind; use crate::core::manifest::ManifestMetadata; @@ -977,12 +976,9 @@ pub fn search( desc: String::new(), }, }; - let _ = config.shell().print( - &message.name, - Some(&(message.space + "# " + &message.desc)), - Green, - true, - ); + let _ = config + .shell() + .status(&message.name, message.space + "# " + &message.desc); } let search_max_limit = 100; From 742710c419b18acf7f1bf8428935bee01cade3dd Mon Sep 17 00:00:00 2001 From: l00556901 Date: Thu, 25 Nov 2021 14:51:03 +0800 Subject: [PATCH 5/9] fix test --- tests/testsuite/search.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/testsuite/search.rs b/tests/testsuite/search.rs index 0d239b3b455..b8652ba07b3 100644 --- a/tests/testsuite/search.rs +++ b/tests/testsuite/search.rs @@ -93,8 +93,8 @@ fn write_crates(dest: &Path) { } const SEARCH_RESULTS: &str = "\ -hoare = \"0.1.1\" # Design by contract style assertions for Rust -postgres = \"0.17.3\" # A native, synchronous PostgreSQL client +hoare = \"0.1.1\" # Design by contract style assertions for Rust +postgres = \"0.17.3\" # A native, synchronous PostgreSQL client "; fn setup() { @@ -155,8 +155,8 @@ fn not_update() { drop(lock); cargo_process("search postgres") - .with_stdout_contains(SEARCH_RESULTS) - .with_stderr("") // without "Updating ... index" + .with_stderr_contains(SEARCH_RESULTS) + .with_stderr_does_not_contain("[..]Updating [..] index") .run(); } @@ -166,7 +166,7 @@ fn replace_default() { set_cargo_config(); cargo_process("search postgres") - .with_stdout_contains(SEARCH_RESULTS) + .with_stderr_contains(SEARCH_RESULTS) .with_stderr_contains("[..]Updating [..] index") .run(); } @@ -177,7 +177,7 @@ fn simple() { cargo_process("search postgres --index") .arg(registry_url().to_string()) - .with_stdout_contains(SEARCH_RESULTS) + .with_stderr_contains(SEARCH_RESULTS) .run(); } @@ -189,7 +189,7 @@ fn simple_with_host() { cargo_process("search postgres --host") .arg(registry_url().to_string()) - .with_stderr( + .with_stderr_contains( "\ [WARNING] The flag '--host' is no longer valid. @@ -203,7 +203,7 @@ about this warning. [UPDATING] `[CWD]/registry` index ", ) - .with_stdout_contains(SEARCH_RESULTS) + .with_stderr_contains(SEARCH_RESULTS) .run(); } @@ -217,7 +217,7 @@ fn simple_with_index_and_host() { .arg(registry_url().to_string()) .arg("--host") .arg(registry_url().to_string()) - .with_stderr( + .with_stderr_contains( "\ [WARNING] The flag '--host' is no longer valid. @@ -231,7 +231,7 @@ about this warning. [UPDATING] `[CWD]/registry` index ", ) - .with_stdout_contains(SEARCH_RESULTS) + .with_stderr_contains(SEARCH_RESULTS) .run(); } @@ -241,6 +241,6 @@ fn multiple_query_params() { cargo_process("search postgres sql --index") .arg(registry_url().to_string()) - .with_stdout_contains(SEARCH_RESULTS) + .with_stderr_contains(SEARCH_RESULTS) .run(); } From 8682ec6bc8d10deb6f99e6effd40f4d8cc22fd24 Mon Sep 17 00:00:00 2001 From: l00556901 Date: Fri, 26 Nov 2021 09:49:18 +0800 Subject: [PATCH 6/9] chang code to use stdout --- src/cargo/core/shell.rs | 74 ++++++++++++++++++++++++++++++++++++++- src/cargo/ops/registry.rs | 2 +- tests/testsuite/search.rs | 18 +++++----- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 887b8967d25..bd7246cc7ac 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -143,6 +143,27 @@ impl Shell { } } + /// Prints a message to stdout, where the status will have `color` color, and can be justified. The + /// messages follows without color. + pub fn print_stdout( + &mut self, + status: &dyn fmt::Display, + message: Option<&dyn fmt::Display>, + color: Color, + justified: bool, + ) -> CargoResult<()> { + match self.verbosity { + Verbosity::Quiet => Ok(()), + _ => { + if self.needs_clear { + self.err_erase_line(); + } + self.output + .message_stdout(status, message, color, justified) + } + } + } + /// Sets whether the next print should clear the current line. pub fn set_needs_clear(&mut self, needs_clear: bool) { self.needs_clear = needs_clear; @@ -195,7 +216,7 @@ impl Shell { } } - /// Shortcut to right-align and color green a status message. + /// Shortcut to right-align and color green a status message in stderr. pub fn status(&mut self, status: T, message: U) -> CargoResult<()> where T: fmt::Display, @@ -204,6 +225,15 @@ impl Shell { self.print(&status, Some(&message), Green, true) } + /// Shortcut to right-align and color green a status message in stdout. + pub fn status_stdout(&mut self, status: T, message: U) -> CargoResult<()> + where + T: fmt::Display, + U: fmt::Display, + { + self.print_stdout(&status, Some(&message), Green, true) + } + pub fn status_header(&mut self, status: T) -> CargoResult<()> where T: fmt::Display, @@ -423,6 +453,48 @@ impl ShellOut { Ok(()) } + /// Prints out a message with a status to stdout. The status comes first, and is bold plus the given + /// color. The status can be justified, in which case the max width that will right align is + /// 12 chars. + fn message_stdout( + &mut self, + status: &dyn fmt::Display, + message: Option<&dyn fmt::Display>, + color: Color, + justified: bool, + ) -> CargoResult<()> { + match *self { + ShellOut::Stream { ref mut stdout, .. } => { + stdout.reset()?; + stdout.set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)))?; + if justified { + write!(stdout, "{:>12}", status)?; + } else { + write!(stdout, "{}", status)?; + stdout.set_color(ColorSpec::new().set_bold(true))?; + write!(stdout, ":")?; + } + stdout.reset()?; + match message { + Some(message) => writeln!(stdout, " {}", message)?, + None => write!(stdout, " ")?, + } + } + ShellOut::Write(ref mut w) => { + if justified { + write!(w, "{:>12}", status)?; + } else { + write!(w, "{}:", status)?; + } + match message { + Some(message) => writeln!(w, " {}", message)?, + None => write!(w, " ")?, + } + } + } + Ok(()) + } + /// Gets stdout as a `io::Write`. fn stdout(&mut self) -> &mut dyn Write { match *self { diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 0280904f863..4bb922cc252 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -978,7 +978,7 @@ pub fn search( }; let _ = config .shell() - .status(&message.name, message.space + "# " + &message.desc); + .status_stdout(&message.name, message.space + "# " + &message.desc); } let search_max_limit = 100; diff --git a/tests/testsuite/search.rs b/tests/testsuite/search.rs index b8652ba07b3..9c7ca28c8d1 100644 --- a/tests/testsuite/search.rs +++ b/tests/testsuite/search.rs @@ -155,8 +155,8 @@ fn not_update() { drop(lock); cargo_process("search postgres") - .with_stderr_contains(SEARCH_RESULTS) - .with_stderr_does_not_contain("[..]Updating [..] index") + .with_stdout_contains(SEARCH_RESULTS) + .with_stderr("") // without "Updating ... index" .run(); } @@ -166,7 +166,7 @@ fn replace_default() { set_cargo_config(); cargo_process("search postgres") - .with_stderr_contains(SEARCH_RESULTS) + .with_stdout_contains(SEARCH_RESULTS) .with_stderr_contains("[..]Updating [..] index") .run(); } @@ -177,7 +177,7 @@ fn simple() { cargo_process("search postgres --index") .arg(registry_url().to_string()) - .with_stderr_contains(SEARCH_RESULTS) + .with_stdout_contains(SEARCH_RESULTS) .run(); } @@ -189,7 +189,7 @@ fn simple_with_host() { cargo_process("search postgres --host") .arg(registry_url().to_string()) - .with_stderr_contains( + .with_stderr( "\ [WARNING] The flag '--host' is no longer valid. @@ -203,7 +203,7 @@ about this warning. [UPDATING] `[CWD]/registry` index ", ) - .with_stderr_contains(SEARCH_RESULTS) + .with_stdout_contains(SEARCH_RESULTS) .run(); } @@ -217,7 +217,7 @@ fn simple_with_index_and_host() { .arg(registry_url().to_string()) .arg("--host") .arg(registry_url().to_string()) - .with_stderr_contains( + .with_stderr( "\ [WARNING] The flag '--host' is no longer valid. @@ -231,7 +231,7 @@ about this warning. [UPDATING] `[CWD]/registry` index ", ) - .with_stderr_contains(SEARCH_RESULTS) + .with_stdout_contains(SEARCH_RESULTS) .run(); } @@ -241,6 +241,6 @@ fn multiple_query_params() { cargo_process("search postgres sql --index") .arg(registry_url().to_string()) - .with_stderr_contains(SEARCH_RESULTS) + .with_stdout_contains(SEARCH_RESULTS) .run(); } From bd8215356ca2cf1586f4c2949390e53b4dbf8d54 Mon Sep 17 00:00:00 2001 From: l00556901 Date: Mon, 20 Dec 2021 16:22:44 +0800 Subject: [PATCH 7/9] Color the query content --- src/cargo/core/shell.rs | 84 +++++++++++++++++++++++---------------- src/cargo/ops/registry.rs | 7 +++- 2 files changed, 55 insertions(+), 36 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index bd7246cc7ac..483f8b1354e 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -143,12 +143,12 @@ impl Shell { } } - /// Prints a message to stdout, where the status will have `color` color, and can be justified. The - /// messages follows without color. - pub fn print_stdout( + /// Print the message to standard output, where the query will have the color `color`. + pub fn print_stdout_with_part_color( &mut self, - status: &dyn fmt::Display, - message: Option<&dyn fmt::Display>, + name_no_query: Vec<&str>, + desc_no_query: Vec<&str>, + query: &str, color: Color, justified: bool, ) -> CargoResult<()> { @@ -159,7 +159,7 @@ impl Shell { self.err_erase_line(); } self.output - .message_stdout(status, message, color, justified) + .message_stdout(name_no_query, desc_no_query, query, color, justified) } } } @@ -225,13 +225,14 @@ impl Shell { self.print(&status, Some(&message), Green, true) } - /// Shortcut to right-align and color green a status message in stdout. - pub fn status_stdout(&mut self, status: T, message: U) -> CargoResult<()> - where - T: fmt::Display, - U: fmt::Display, - { - self.print_stdout(&status, Some(&message), Green, true) + /// Shortcut to right-align and paint the query part of the output message as a green in stdout. + pub fn status_stdout_part_green( + &mut self, + name_no_query: Vec<&str>, + desc_no_query: Vec<&str>, + query: &str, + ) -> CargoResult<()> { + self.print_stdout_with_part_color(name_no_query, desc_no_query, query, Green, true) } pub fn status_header(&mut self, status: T) -> CargoResult<()> @@ -453,42 +454,57 @@ impl ShellOut { Ok(()) } - /// Prints out a message with a status to stdout. The status comes first, and is bold plus the given - /// color. The status can be justified, in which case the max width that will right align is - /// 12 chars. + /// Prints out a message with a status to stdout. Output the specified color and bold for the query content. fn message_stdout( &mut self, - status: &dyn fmt::Display, - message: Option<&dyn fmt::Display>, + name_no_query: Vec<&str>, + desc_no_query: Vec<&str>, + query: &str, color: Color, justified: bool, ) -> CargoResult<()> { match *self { ShellOut::Stream { ref mut stdout, .. } => { - stdout.reset()?; - stdout.set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)))?; - if justified { - write!(stdout, "{:>12}", status)?; + let mut count_name = 0; + for message in name_no_query.iter() { + count_name += 1; + stdout.reset()?; + write!(stdout, "{}", message)?; + stdout.set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)))?; + if count_name != name_no_query.len() { + write!(stdout, "{}", query)?; + } + } + + if !desc_no_query.is_empty() { + let mut count_desc = 0; + for message in desc_no_query.iter() { + count_desc += 1; + stdout.reset()?; + write!(stdout, "{}", message)?; + stdout.set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)))?; + if count_desc != desc_no_query.len() { + write!(stdout, "{}", query)?; + } + } } else { - write!(stdout, "{}", status)?; - stdout.set_color(ColorSpec::new().set_bold(true))?; - write!(stdout, ":")?; + write!(stdout, " ")?; } + stdout.reset()?; - match message { - Some(message) => writeln!(stdout, " {}", message)?, - None => write!(stdout, " ")?, - } + write!(stdout, "{}", "\n")?; } + ShellOut::Write(ref mut w) => { if justified { - write!(w, "{:>12}", status)?; + write!(w, "{:>12}", name_no_query.join(query))?; } else { - write!(w, "{}:", status)?; + write!(w, "{}:", name_no_query.join(query))?; } - match message { - Some(message) => writeln!(w, " {}", message)?, - None => write!(w, " ")?, + if !desc_no_query.is_empty() { + writeln!(w, " {}", desc_no_query.join(query))? + } else { + writeln!(w, " ")?; } } } diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 4bb922cc252..b0117405284 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -59,7 +59,6 @@ pub struct PublishOpts<'cfg> { struct MessageInfo { name: String, - space: String, desc: String, } @@ -976,9 +975,13 @@ pub fn search( desc: String::new(), }, }; + + // The query part is displayed in green in the output result. + let name_vec: Vec<&str> = message.name.split(query).collect(); + let desc_vec: Vec<&str> = message.desc.split(query).collect(); let _ = config .shell() - .status_stdout(&message.name, message.space + "# " + &message.desc); + .status_stdout_part_green(name_vec, desc_vec, &query); } let search_max_limit = 100; From 1c8354cbfa3bbdceadaf2f396064a8ff7f10a2dc Mon Sep 17 00:00:00 2001 From: l00556901 Date: Mon, 20 Dec 2021 16:43:33 +0800 Subject: [PATCH 8/9] fix test --- src/cargo/ops/registry.rs | 4 +--- tests/testsuite/search.rs | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index b0117405284..66bc8afa956 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -965,13 +965,11 @@ pub fn search( .collect::(); MessageInfo { name: name, - space: space, - desc: desc, + desc: space + "# " + &desc, } } None => MessageInfo { name: name, - space: String::new(), desc: String::new(), }, }; diff --git a/tests/testsuite/search.rs b/tests/testsuite/search.rs index 9c7ca28c8d1..0d239b3b455 100644 --- a/tests/testsuite/search.rs +++ b/tests/testsuite/search.rs @@ -93,8 +93,8 @@ fn write_crates(dest: &Path) { } const SEARCH_RESULTS: &str = "\ -hoare = \"0.1.1\" # Design by contract style assertions for Rust -postgres = \"0.17.3\" # A native, synchronous PostgreSQL client +hoare = \"0.1.1\" # Design by contract style assertions for Rust +postgres = \"0.17.3\" # A native, synchronous PostgreSQL client "; fn setup() { From ce0b6ef265c21e463165049523c50d478c3ea8dd Mon Sep 17 00:00:00 2001 From: l00556901 Date: Thu, 23 Dec 2021 14:55:53 +0800 Subject: [PATCH 9/9] rebuild code --- src/cargo/core/shell.rs | 71 ++++++++++++++++++--------------------- src/cargo/ops/registry.rs | 6 ++-- tests/testsuite/search.rs | 10 ++++++ 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 483f8b1354e..7d1a8d1f841 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -152,16 +152,8 @@ impl Shell { color: Color, justified: bool, ) -> CargoResult<()> { - match self.verbosity { - Verbosity::Quiet => Ok(()), - _ => { - if self.needs_clear { - self.err_erase_line(); - } - self.output - .message_stdout(name_no_query, desc_no_query, query, color, justified) - } - } + self.output + .message_stdout(name_no_query, desc_no_query, query, color, justified) } /// Sets whether the next print should clear the current line. @@ -455,38 +447,41 @@ impl ShellOut { } /// Prints out a message with a status to stdout. Output the specified color and bold for the query content. + /// name_no_specific:This refers to the string array after the crate name separated by characters matched by a specific string. + /// desc_no_specific:This refers to the string array after the crate description separated by characters matched by a specific string. fn message_stdout( &mut self, - name_no_query: Vec<&str>, - desc_no_query: Vec<&str>, - query: &str, + name_no_specific: Vec<&str>, + desc_no_specific: Vec<&str>, + specific: &str, color: Color, justified: bool, ) -> CargoResult<()> { + fn print_message( + stdout: &mut StandardStream, + messages_vec: Vec<&str>, + specific: &str, + color: Color, + ) -> CargoResult<()> { + let mut count = 0; + for message in messages_vec.iter() { + count += 1; + stdout.reset()?; + write!(stdout, "{}", message)?; + stdout.set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)))?; + if count != messages_vec.len() { + write!(stdout, "{}", specific)?; + } + } + Ok(()) + } + match *self { ShellOut::Stream { ref mut stdout, .. } => { - let mut count_name = 0; - for message in name_no_query.iter() { - count_name += 1; - stdout.reset()?; - write!(stdout, "{}", message)?; - stdout.set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)))?; - if count_name != name_no_query.len() { - write!(stdout, "{}", query)?; - } - } + print_message(stdout, name_no_specific, specific, color)?; - if !desc_no_query.is_empty() { - let mut count_desc = 0; - for message in desc_no_query.iter() { - count_desc += 1; - stdout.reset()?; - write!(stdout, "{}", message)?; - stdout.set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)))?; - if count_desc != desc_no_query.len() { - write!(stdout, "{}", query)?; - } - } + if !desc_no_specific.is_empty() { + print_message(stdout, desc_no_specific, specific, color)?; } else { write!(stdout, " ")?; } @@ -497,12 +492,12 @@ impl ShellOut { ShellOut::Write(ref mut w) => { if justified { - write!(w, "{:>12}", name_no_query.join(query))?; + write!(w, "{:>12}", name_no_specific.join(specific))?; } else { - write!(w, "{}:", name_no_query.join(query))?; + write!(w, "{}:", name_no_specific.join(specific))?; } - if !desc_no_query.is_empty() { - writeln!(w, " {}", desc_no_query.join(query))? + if !desc_no_specific.is_empty() { + writeln!(w, " {}", desc_no_specific.join(specific))? } else { writeln!(w, " ")?; } diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 66bc8afa956..122c1694dbd 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -13,6 +13,7 @@ use crates_io::{self, NewCrate, NewCrateDependency, Registry}; use curl::easy::{Easy, InfoType, SslOpt, SslVersion}; use log::{log, Level}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; +use termcolor::Color::Green; use crate::core::dependency::DepKind; use crate::core::manifest::ManifestMetadata; @@ -977,9 +978,10 @@ pub fn search( // The query part is displayed in green in the output result. let name_vec: Vec<&str> = message.name.split(query).collect(); let desc_vec: Vec<&str> = message.desc.split(query).collect(); - let _ = config + + config .shell() - .status_stdout_part_green(name_vec, desc_vec, &query); + .print_stdout_with_part_color(name_vec, desc_vec, query, Green, true)?; } let search_max_limit = 100; diff --git a/tests/testsuite/search.rs b/tests/testsuite/search.rs index 0d239b3b455..71f4f1a63c5 100644 --- a/tests/testsuite/search.rs +++ b/tests/testsuite/search.rs @@ -244,3 +244,13 @@ fn multiple_query_params() { .with_stdout_contains(SEARCH_RESULTS) .run(); } + +#[cargo_test] +fn search_quiet() { + setup(); + set_cargo_config(); + + cargo_process("search -q postgres") + .with_stdout_contains(SEARCH_RESULTS) + .run(); +}