Skip to content

Commit

Permalink
refactor: replace ansi_term with nu_ansi_term (starship#4339)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkna authored Sep 4, 2022
1 parent 020759e commit 6ac5df9
Show file tree
Hide file tree
Showing 81 changed files with 149 additions and 142 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
mod tests {
use super::*;
use crate::test::ModuleRenderer;
use ansi_term::Color;
use nu_ansi_term::Color;
use std::fs::File;
use std::io;

Expand Down
27 changes: 17 additions & 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 @@ -35,7 +35,6 @@ config-schema = ["schemars"]
notify = ["notify-rust"]

[dependencies]
ansi_term = "0.12.1"
chrono = { version = "0.4.22", features = ["clock", "std"] }
clap = { version = "=3.2.20", features = ["derive", "cargo", "unicode", "unstable-v4"] }
clap_complete = "3.2.4"
Expand All @@ -51,6 +50,7 @@ log = { version = "0.4.16", features = ["std"] }
# nofity-rust is optional (on by default) because the crate doesn't currently build for darwin with nix
# see: https://github.com/NixOS/nixpkgs/issues/160876
notify-rust = { version = "4.5.8", optional = true }
nu-ansi-term = "0.46.0"
once_cell = "1.13.1"
open = "3.0.2"
os_info = "3.5.0"
Expand Down
46 changes: 23 additions & 23 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::serde_utils::ValueDeserializer;
use crate::utils;
use ansi_term::Color;
use nu_ansi_term::Color;
use serde::{
de::value::Error as ValueError, de::Error as SerdeError, Deserialize, Deserializer, Serialize,
};
Expand Down Expand Up @@ -251,7 +251,7 @@ impl StarshipConfig {
}

/// Deserialize a style string in the starship format with serde
pub fn deserialize_style<'de, D>(de: D) -> Result<ansi_term::Style, D::Error>
pub fn deserialize_style<'de, D>(de: D) -> Result<nu_ansi_term::Style, D::Error>
where
D: Deserializer<'de>,
{
Expand All @@ -271,10 +271,10 @@ where
- 'blink'
- '<color>' (see the `parse_color_string` doc for valid color strings)
*/
pub fn parse_style_string(style_string: &str) -> Option<ansi_term::Style> {
pub fn parse_style_string(style_string: &str) -> Option<nu_ansi_term::Style> {
style_string
.split_whitespace()
.fold(Some(ansi_term::Style::new()), |maybe_style, token| {
.fold(Some(nu_ansi_term::Style::new()), |maybe_style, token| {
maybe_style.and_then(|style| {
let token = token.to_lowercase();

Expand Down Expand Up @@ -333,7 +333,7 @@ pub fn parse_style_string(style_string: &str) -> Option<ansi_term::Style> {
- u8 (a number from 0-255, representing an ANSI color)
- colstring (one of the 16 predefined color strings)
*/
fn parse_color_string(color_string: &str) -> Option<ansi_term::Color> {
fn parse_color_string(color_string: &str) -> Option<nu_ansi_term::Color> {
// Parse RGB hex values
log::trace!("Parsing color_string: {}", color_string);
if color_string.starts_with('#') {
Expand All @@ -349,7 +349,7 @@ fn parse_color_string(color_string: &str) -> Option<ansi_term::Color> {
let g: u8 = u8::from_str_radix(&color_string[3..5], 16).ok()?;
let b: u8 = u8::from_str_radix(&color_string[5..7], 16).ok()?;
log::trace!("Read RGB color string: {},{},{}", r, g, b);
return Some(Color::RGB(r, g, b));
return Some(Color::Rgb(r, g, b));
}

// Parse a u8 (ansi color)
Expand All @@ -369,14 +369,14 @@ fn parse_color_string(color_string: &str) -> Option<ansi_term::Color> {
"purple" => Some(Color::Purple),
"cyan" => Some(Color::Cyan),
"white" => Some(Color::White),
"bright-black" => Some(Color::Fixed(8)), // "bright-black" is dark grey
"bright-red" => Some(Color::Fixed(9)),
"bright-green" => Some(Color::Fixed(10)),
"bright-yellow" => Some(Color::Fixed(11)),
"bright-blue" => Some(Color::Fixed(12)),
"bright-purple" => Some(Color::Fixed(13)),
"bright-cyan" => Some(Color::Fixed(14)),
"bright-white" => Some(Color::Fixed(15)),
"bright-black" => Some(Color::DarkGray), // "bright-black" is dark grey
"bright-red" => Some(Color::LightRed),
"bright-green" => Some(Color::LightGreen),
"bright-yellow" => Some(Color::LightYellow),
"bright-blue" => Some(Color::LightBlue),
"bright-purple" => Some(Color::LightPurple),
"bright-cyan" => Some(Color::LightCyan),
"bright-white" => Some(Color::LightGray),
_ => None,
};

Expand All @@ -391,7 +391,7 @@ fn parse_color_string(color_string: &str) -> Option<ansi_term::Color> {
#[cfg(test)]
mod tests {
use super::*;
use ansi_term::Style;
use nu_ansi_term::Style;

// Small wrapper to allow deserializing Style without a struct with #[serde(deserialize_with=)]
#[derive(Default, Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -574,7 +574,7 @@ mod tests {
let config = Value::from("#a12BcD");
assert_eq!(
<StyleWrapper>::from_config(&config).unwrap().0,
Color::RGB(0xA1, 0x2B, 0xCD).into()
Color::Rgb(0xA1, 0x2B, 0xCD).into()
);
}

Expand All @@ -600,7 +600,7 @@ mod tests {
assert!(mystyle.is_dimmed);
assert_eq!(
mystyle,
ansi_term::Style::new()
nu_ansi_term::Style::new()
.bold()
.italic()
.underline()
Expand All @@ -620,7 +620,7 @@ mod tests {
assert!(mystyle.is_reverse);
assert_eq!(
mystyle,
ansi_term::Style::new()
nu_ansi_term::Style::new()
.bold()
.italic()
.underline()
Expand All @@ -641,7 +641,7 @@ mod tests {
assert!(mystyle.is_blink);
assert_eq!(
mystyle,
ansi_term::Style::new()
nu_ansi_term::Style::new()
.bold()
.italic()
.underline()
Expand All @@ -662,7 +662,7 @@ mod tests {
assert!(mystyle.is_hidden);
assert_eq!(
mystyle,
ansi_term::Style::new()
nu_ansi_term::Style::new()
.bold()
.italic()
.underline()
Expand All @@ -683,7 +683,7 @@ mod tests {
assert!(mystyle.is_strikethrough);
assert_eq!(
mystyle,
ansi_term::Style::new()
nu_ansi_term::Style::new()
.bold()
.italic()
.underline()
Expand All @@ -698,7 +698,7 @@ mod tests {
// Test a "plain" style with no formatting
let config = Value::from("");
let plain_style = <StyleWrapper>::from_config(&config).unwrap().0;
assert_eq!(plain_style, ansi_term::Style::new());
assert_eq!(plain_style, nu_ansi_term::Style::new());

// Test a string that's clearly broken
let config = Value::from("djklgfhjkldhlhk;j");
Expand Down Expand Up @@ -763,7 +763,7 @@ mod tests {
Style::new()
.underline()
.fg(Color::Fixed(120))
.on(Color::RGB(5, 5, 5))
.on(Color::Rgb(5, 5, 5))
);

// Test that the last color style is always the one used
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/string_formatter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansi_term::Style;
use nu_ansi_term::Style;
use pest::error::Error as PestError;
use rayon::prelude::*;
use std::borrow::Cow;
Expand Down Expand Up @@ -465,7 +465,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use ansi_term::Color;
use nu_ansi_term::Color;

// match_next(result: IterMut<Segment>, value, style)
macro_rules! match_next {
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils;
use ansi_term::Color;
use log::{Level, LevelFilter, Metadata, Record};
use nu_ansi_term::Color;
use once_cell::sync::OnceCell;
use std::{
collections::HashSet,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ enum Commands {
fn main() {
// Configure the current terminal on windows to support ANSI escape sequences.
#[cfg(windows)]
let _ = ansi_term::enable_ansi_support();
let _ = nu_ansi_term::enable_ansi_support();
logger::init();
init_global_threadpool();

Expand Down
28 changes: 14 additions & 14 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::context::Shell;
use crate::segment;
use crate::segment::{FillSegment, Segment};
use crate::utils::wrap_colorseq_for_shell;
use ansi_term::{ANSIString, ANSIStrings};
use nu_ansi_term::{AnsiString, AnsiStrings};
use std::fmt;
use std::time::Duration;

Expand Down Expand Up @@ -146,15 +146,15 @@ impl<'a> Module<'a> {
self.segments.iter().map(segment::Segment::value).collect()
}

/// Returns a vector of colored `ANSIString` elements to be later used with
/// `ANSIStrings()` to optimize ANSI codes
pub fn ansi_strings(&self) -> Vec<ANSIString> {
/// Returns a vector of colored `AnsiString` elements to be later used with
/// `AnsiStrings()` to optimize ANSI codes
pub fn ansi_strings(&self) -> Vec<AnsiString> {
self.ansi_strings_for_shell(Shell::Unknown, None)
}

pub fn ansi_strings_for_shell(&self, shell: Shell, width: Option<usize>) -> Vec<ANSIString> {
pub fn ansi_strings_for_shell(&self, shell: Shell, width: Option<usize>) -> Vec<AnsiString> {
let mut iter = self.segments.iter().peekable();
let mut ansi_strings: Vec<ANSIString> = Vec::new();
let mut ansi_strings: Vec<AnsiString> = Vec::new();
while iter.peek().is_some() {
ansi_strings.extend(ansi_line(&mut iter, width));
}
Expand All @@ -171,27 +171,27 @@ impl<'a> Module<'a> {
impl<'a> fmt::Display for Module<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ansi_strings = self.ansi_strings();
write!(f, "{}", ANSIStrings(&ansi_strings))
write!(f, "{}", AnsiStrings(&ansi_strings))
}
}

fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: Shell) -> Vec<ANSIString> {
fn ansi_strings_modified(ansi_strings: Vec<AnsiString>, shell: Shell) -> Vec<AnsiString> {
ansi_strings
.into_iter()
.map(|ansi| {
let wrapped = wrap_colorseq_for_shell(ansi.to_string(), shell);
ANSIString::from(wrapped)
AnsiString::from(wrapped)
})
.collect::<Vec<ANSIString>>()
.collect::<Vec<AnsiString>>()
}

fn ansi_line<'a, I>(segments: &mut I, term_width: Option<usize>) -> Vec<ANSIString<'a>>
fn ansi_line<'a, I>(segments: &mut I, term_width: Option<usize>) -> Vec<AnsiString<'a>>
where
I: Iterator<Item = &'a Segment>,
{
let mut used = 0usize;
let mut current: Vec<ANSIString> = Vec::new();
let mut chunks: Vec<(Vec<ANSIString>, &FillSegment)> = Vec::new();
let mut current: Vec<AnsiString> = Vec::new();
let mut chunks: Vec<(Vec<AnsiString>, &FillSegment)> = Vec::new();

for segment in segments {
match segment {
Expand Down Expand Up @@ -223,7 +223,7 @@ where
.chain(std::iter::once(fill.ansi_string(fill_size)))
})
.chain(current.into_iter())
.collect::<Vec<ANSIString>>()
.collect::<Vec<AnsiString>>()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use ansi_term::Color;
use nu_ansi_term::Color;
use std::fs::{create_dir, File};
use std::io::{self, Write};

Expand Down
2 changes: 1 addition & 1 deletion src/modules/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ fn parse_json(json_file_path: &Path) -> Option<JValue> {
mod tests {
use crate::modules::azure::parse_json;
use crate::test::ModuleRenderer;
use ansi_term::Color;
use ini::Ini;
use nu_ansi_term::Color;
use std::fs::File;
use std::io::{self, Write};
use std::path::PathBuf;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl BatteryInfoProvider for BatteryInfoProviderImpl {
mod tests {
use super::*;
use crate::test::ModuleRenderer;
use ansi_term::Color;
use nu_ansi_term::Color;

#[test]
fn no_battery_status() {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn parse_buf_version(buf_version: &str) -> Option<String> {
mod tests {
use super::parse_buf_version;
use crate::test::ModuleRenderer;
use ansi_term::Color;
use nu_ansi_term::Color;
use std::fs::File;
use std::io;

Expand Down
2 changes: 1 addition & 1 deletion src/modules/bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn parse_bun_version(bun_version: String) -> String {
#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use ansi_term::Color;
use nu_ansi_term::Color;
use std::fs::File;
use std::io;

Expand Down
2 changes: 1 addition & 1 deletion src/modules/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
#[cfg(test)]
mod tests {
use crate::{test::ModuleRenderer, utils::CommandOutput};
use ansi_term::Color;
use nu_ansi_term::Color;
use std::fs::File;
use std::io;

Expand Down
Loading

0 comments on commit 6ac5df9

Please sign in to comment.