Skip to content
Open
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
11 changes: 1 addition & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ test = ["dep:snapbox", "dep:walkdir"]

# Sorted by alphabetic order
[dependencies]
anstream = "0.6.20"
anstyle = "1.0.11"
anyhow = "1.0.69"
cfg-if = "1.0"
Expand Down Expand Up @@ -86,7 +87,6 @@ sharded-slab = "0.1.1"
strsim = "0.11"
tar = "0.4.26"
tempfile = "3.8"
termcolor = "1.2"
thiserror = "2"
threadpool = "1"
tokio = { version = "1.26.0", default-features = false, features = ["macros", "rt-multi-thread", "sync"] }
Expand Down
42 changes: 21 additions & 21 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::{cmp, env};

use anstyle::{AnsiColor, Style};
use anyhow::{Context, Result, anyhow};
use git_testament::{git_testament, render_testament};
use termcolor::Color;
use tracing::{error, info, warn};
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};

Expand All @@ -18,7 +18,7 @@ use crate::{
dist::{TargetTriple, ToolchainDesc},
errors::RustupError,
install::UpdateStatus,
process::{Attr, Process},
process::Process,
toolchain::{LocalToolchainName, Toolchain, ToolchainName},
utils,
};
Expand Down Expand Up @@ -152,10 +152,10 @@ fn show_channel_updates(
) -> Result<()> {
let data = updates.into_iter().map(|(pkg, result)| {
let (banner, color) = match &result {
Ok(UpdateStatus::Installed) => ("installed", Some(Color::Green)),
Ok(UpdateStatus::Updated(_)) => ("updated", Some(Color::Green)),
Ok(UpdateStatus::Installed) => ("installed", Some(AnsiColor::Green)),
Ok(UpdateStatus::Updated(_)) => ("updated", Some(AnsiColor::Green)),
Ok(UpdateStatus::Unchanged) => ("unchanged", None),
Err(_) => ("update failed", Some(Color::Red)),
Err(_) => ("update failed", Some(AnsiColor::Red)),
};

let (previous_version, version) = match &pkg {
Expand Down Expand Up @@ -193,7 +193,8 @@ fn show_channel_updates(
Ok((pkg, banner, width, color, version, previous_version))
});

let mut t = cfg.process.stdout();
let t = cfg.process.stdout();
let mut t = t.lock();

let data: Vec<_> = data.collect::<Result<_>>()?;
let max_width = data
Expand All @@ -203,20 +204,19 @@ fn show_channel_updates(
for (pkg, banner, width, color, version, previous_version) in data {
let padding = max_width - width;
let padding: String = " ".repeat(padding);
let _ = write!(t.lock(), " {padding}");
let _ = t.attr(Attr::Bold);
if let Some(color) = color {
let _ = t.fg(color);
let style = if let Some(color) = color {
color.on_default()
} else {
Style::new()
}
let _ = write!(t.lock(), "{pkg} {banner}");
let _ = t.reset();
let _ = write!(t.lock(), " - {version}");
.bold();
let _ = write!(t, " {padding}{style}{pkg} {banner}{style:#} - {version}");
if let Some(previous_version) = previous_version {
let _ = write!(t.lock(), " (from {previous_version})");
let _ = write!(t, " (from {previous_version})");
}
let _ = writeln!(t.lock());
let _ = writeln!(t);
}
let _ = writeln!(t.lock());
let _ = writeln!(t);

Ok(())
}
Expand Down Expand Up @@ -256,14 +256,14 @@ pub(super) fn list_items(
quiet: bool,
process: &Process,
) -> Result<utils::ExitCode> {
let mut t = process.stdout();
let t = process.stdout();
let mut t = t.lock();
let bold = Style::new().bold();
for (name, installed) in items {
if installed && !installed_only && !quiet {
t.attr(Attr::Bold)?;
writeln!(t.lock(), "{name} (installed)")?;
t.reset()?;
writeln!(t, "{bold}{name} (installed){bold:#}")?;
} else if installed || !installed_only {
writeln!(t.lock(), "{name}")?;
writeln!(t, "{name}")?;
}
}
Ok(utils::ExitCode(0))
Expand Down
31 changes: 25 additions & 6 deletions src/cli/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Write Markdown to the terminal
use std::io::Write;

use anstyle::{AnsiColor, Reset, Style};
use pulldown_cmark::{Event, Tag, TagEnd};
use termcolor::Color;

use crate::process::{Attr, ColorableTerminal};
use crate::process::ColorableTerminal;

// Handles the wrapping of text written to the console
struct LineWrapper<'a> {
Expand Down Expand Up @@ -96,6 +96,7 @@ struct LineFormatter<'a> {
is_code_block: bool,
wrapper: LineWrapper<'a>,
attrs: Vec<Attr>,
style: Style,
}

impl<'a> LineFormatter<'a> {
Expand All @@ -104,18 +105,21 @@ impl<'a> LineFormatter<'a> {
is_code_block: false,
wrapper: LineWrapper::new(w, indent, margin),
attrs: Vec::new(),
style: Style::new(),
}
}
fn push_attr(&mut self, attr: Attr) {
self.attrs.push(attr);
let _ = self.wrapper.w.attr(attr);
attr.apply_to(&mut self.style);
let _ = write!(self.wrapper.w.lock(), "{Reset}{}", self.style);
}
fn pop_attr(&mut self) {
self.attrs.pop();
let _ = self.wrapper.w.reset();
self.style = Style::new();
for attr in &self.attrs {
let _ = self.wrapper.w.attr(*attr);
attr.apply_to(&mut self.style);
}
let _ = write!(self.wrapper.w.lock(), "{Reset}{}", self.style);
}

fn start_tag(&mut self, tag: Tag<'a>) {
Expand Down Expand Up @@ -151,7 +155,7 @@ impl<'a> LineFormatter<'a> {
self.wrapper.write_line();
}
Tag::Emphasis => {
self.push_attr(Attr::ForegroundColor(Color::Red));
self.push_attr(Attr::ForegroundColor(AnsiColor::Red));
}
Tag::Strong => {}
Tag::Strikethrough => {}
Expand Down Expand Up @@ -240,6 +244,21 @@ impl<'a> LineFormatter<'a> {
}
}

#[derive(Copy, Clone, Debug)]
pub enum Attr {
Bold,
ForegroundColor(AnsiColor),
}

impl Attr {
fn apply_to(&self, style: &mut Style) {
match self {
Self::Bold => *style = style.bold(),
Self::ForegroundColor(color) => *style = style.fg_color(Some((*color).into())),
}
}
}

pub(crate) fn md<S: AsRef<str>>(t: &mut ColorableTerminal, content: S) {
let mut f = LineFormatter::new(t, 0, 79);
let parser = pulldown_cmark::Parser::new(content.as_ref());
Expand Down
47 changes: 26 additions & 21 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use std::{
time::Duration,
};

use anstream::ColorChoice;
use anstyle::Style;
use anyhow::{Context, Error, Result, anyhow};
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum, builder::PossibleValue};
use clap_complete::Shell;
use console::style;
use futures_util::stream::StreamExt;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use itertools::Itertools;
use termcolor::ColorChoice;
use tokio::sync::Semaphore;
use tracing::{info, trace, warn};
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
Expand All @@ -39,7 +40,7 @@ use crate::{
},
errors::RustupError,
install::{InstallMethod, UpdateStatus},
process::{Attr, ColorableTerminal, Process},
process::{ColorableTerminal, Process},
toolchain::{
CustomToolchainName, DistributableToolchain, LocalToolchainName,
MaybeResolvableToolchainName, ResolvableLocalToolchainName, ResolvableToolchainName,
Expand Down Expand Up @@ -1072,23 +1073,29 @@ async fn which(
async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated(cfg.process);

let bold = Style::new().bold();

// Print host triple
{
let mut t = cfg.process.stdout();
t.attr(Attr::Bold)?;
write!(t.lock(), "Default host: ")?;
t.reset()?;
writeln!(t.lock(), "{}", cfg.get_default_host_triple()?)?;
let t = cfg.process.stdout();
let mut t = t.lock();
writeln!(
t,
"{bold}Default host: {bold:#}{}",
cfg.get_default_host_triple()?
)?;
}

// Print rustup home directory
{
let mut t = cfg.process.stdout();
t.attr(Attr::Bold)?;
write!(t.lock(), "rustup home: ")?;
t.reset()?;
writeln!(t.lock(), "{}", cfg.rustup_dir.display())?;
writeln!(t.lock())?;
let t = cfg.process.stdout();
let mut t = t.lock();
writeln!(
t,
"{bold}rustup home: {bold:#}{}",
cfg.rustup_dir.display()
)?;
writeln!(t)?;
}

let installed_toolchains = cfg.list_toolchains()?;
Expand Down Expand Up @@ -1192,14 +1199,12 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
}
}

fn print_header(t: &mut ColorableTerminal, s: &str) -> Result<(), Error> {
t.attr(Attr::Bold)?;
{
let mut term_lock = t.lock();
writeln!(term_lock, "{s}")?;
writeln!(term_lock, "{}", "-".repeat(s.len()))?;
} // drop the term_lock
t.reset()?;
fn print_header(t: &mut ColorableTerminal, text: &str) -> Result<(), Error> {
let bold = Style::new().bold();
let divider = "-".repeat(text.len());
let mut term_lock = t.lock();
writeln!(term_lock, "{bold}{text}{bold:#}")?;
writeln!(term_lock, "{bold}{divider}{bold:#}")?;
Ok(())
}

Expand Down
27 changes: 14 additions & 13 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ use std::process::Command;
use std::str::FromStr;
use std::{fmt, fs};

use anstyle::{AnsiColor, Style};
use anyhow::{Context, Result, anyhow};
use cfg_if::cfg_if;
use clap::ValueEnum;
use clap::builder::PossibleValue;
use itertools::Itertools;
use same_file::Handle;
use serde::{Deserialize, Serialize};
use termcolor::Color;
use tracing::{error, info, trace, warn};

use crate::dist::download::DownloadCfg;
Expand All @@ -63,7 +63,7 @@ use crate::{
download::download_file,
errors::RustupError,
install::UpdateStatus,
process::{Attr, Process},
process::Process,
toolchain::{
DistributableToolchain, MaybeOfficialToolchainName, ResolvableToolchainName, Toolchain,
ToolchainName,
Expand Down Expand Up @@ -1357,27 +1357,28 @@ impl fmt::Display for SchemaVersion {

/// Returns whether an update was available
pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg<'_>) -> anyhow::Result<bool> {
let mut t = dl_cfg.process.stdout();
let t = dl_cfg.process.stdout();
let mut t = t.lock();
// Get current rustup version
let current_version = env!("CARGO_PKG_VERSION");

// Get available rustup version
let available_version = get_available_rustup_version(dl_cfg).await?;

let _ = t.attr(Attr::Bold);
write!(t.lock(), "rustup - ")?;
let bold = Style::new().bold();
let yellow = AnsiColor::Yellow.on_default().bold();
let green = AnsiColor::Green.on_default().bold();

write!(t, "{bold}rustup - {bold:#}")?;

Ok(if current_version != available_version {
let _ = t.fg(Color::Yellow);
write!(t.lock(), "Update available")?;
let _ = t.reset();
writeln!(t.lock(), " : {current_version} -> {available_version}")?;
writeln!(
t,
"{yellow}Update available{yellow:#} : {current_version} -> {available_version}"
)?;
true
} else {
let _ = t.fg(Color::Green);
write!(t.lock(), "Up to date")?;
let _ = t.reset();
writeln!(t.lock(), " : {current_version}")?;
writeln!(t, "{green}Up to date{green:#} : {current_version}")?;
false
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::cli::log;

mod file_source;
mod terminal_source;
pub use terminal_source::{Attr, ColorableTerminal};
pub use terminal_source::ColorableTerminal;

/// Allows concrete types for the process abstraction.
#[derive(Clone, Debug)]
Expand Down
Loading
Loading