Skip to content

Increase the output color of search results #10116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ impl Shell {
}
}

/// Print the message to standard output, where the query will have the color `color`.
pub fn print_stdout_with_part_color(
&mut self,
name_no_query: Vec<&str>,
desc_no_query: Vec<&str>,
query: &str,
color: Color,
justified: bool,
) -> CargoResult<()> {
self.output
.message_stdout(name_no_query, desc_no_query, query, 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;
Expand Down Expand Up @@ -195,7 +208,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<T, U>(&mut self, status: T, message: U) -> CargoResult<()>
where
T: fmt::Display,
Expand All @@ -204,6 +217,16 @@ impl Shell {
self.print(&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>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document what these arguments are? I can't figure out what these are from the signature/name of the method...

Copy link
Contributor Author

@heisen-li heisen-li Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added explanation and changed parameter naming.

query: &str,
) -> CargoResult<()> {
self.print_stdout_with_part_color(name_no_query, desc_no_query, query, Green, true)
}

pub fn status_header<T>(&mut self, status: T) -> CargoResult<()>
where
T: fmt::Display,
Expand Down Expand Up @@ -423,6 +446,66 @@ impl ShellOut {
Ok(())
}

/// 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_specific: Vec<&str>,
desc_no_specific: Vec<&str>,
specific: &str,
color: Color,
justified: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always true right now so does this need to be a parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there are cases where color output is not supported.
image

I learned the way of the message_stderr()function.

) -> 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, .. } => {
print_message(stdout, name_no_specific, specific, color)?;

if !desc_no_specific.is_empty() {
print_message(stdout, desc_no_specific, specific, color)?;
} else {
write!(stdout, " ")?;
}

stdout.reset()?;
write!(stdout, "{}", "\n")?;
}

ShellOut::Write(ref mut w) => {
if justified {
write!(w, "{:>12}", name_no_specific.join(specific))?;
} else {
write!(w, "{}:", name_no_specific.join(specific))?;
}
if !desc_no_specific.is_empty() {
writeln!(w, " {}", desc_no_specific.join(specific))?
} else {
writeln!(w, " ")?;
}
}
}
Ok(())
}

/// Gets stdout as a `io::Write`.
fn stdout(&mut self) -> &mut dyn Write {
match *self {
Expand Down
27 changes: 23 additions & 4 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,6 +58,11 @@ pub struct PublishOpts<'cfg> {
pub cli_features: CliFeatures,
}

struct MessageInfo {
name: 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)?;
Expand Down Expand Up @@ -953,16 +959,29 @@ 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::<String>();
name + &space + "# " + &desc
MessageInfo {
name: name,
desc: space + "# " + &desc,
}
}
None => name,
None => MessageInfo {
name: name,
desc: String::new(),
},
};
drop_println!(config, "{}", line);

// 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();

config
.shell()
.print_stdout_with_part_color(name_vec, desc_vec, query, Green, true)?;
}

let search_max_limit = 100;
Expand Down
10 changes: 10 additions & 0 deletions tests/testsuite/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}