Skip to content

Commit 6ac5df9

Browse files
authored
refactor: replace ansi_term with nu_ansi_term (starship#4339)
1 parent 020759e commit 6ac5df9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+149
-142
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
180180
mod tests {
181181
use super::*;
182182
use crate::test::ModuleRenderer;
183-
use ansi_term::Color;
183+
use nu_ansi_term::Color;
184184
use std::fs::File;
185185
use std::io;
186186

Cargo.lock

+17-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ config-schema = ["schemars"]
3535
notify = ["notify-rust"]
3636

3737
[dependencies]
38-
ansi_term = "0.12.1"
3938
chrono = { version = "0.4.22", features = ["clock", "std"] }
4039
clap = { version = "=3.2.20", features = ["derive", "cargo", "unicode", "unstable-v4"] }
4140
clap_complete = "3.2.4"
@@ -51,6 +50,7 @@ log = { version = "0.4.16", features = ["std"] }
5150
# nofity-rust is optional (on by default) because the crate doesn't currently build for darwin with nix
5251
# see: https://github.com/NixOS/nixpkgs/issues/160876
5352
notify-rust = { version = "4.5.8", optional = true }
53+
nu-ansi-term = "0.46.0"
5454
once_cell = "1.13.1"
5555
open = "3.0.2"
5656
os_info = "3.5.0"

src/config.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::serde_utils::ValueDeserializer;
22
use crate::utils;
3-
use ansi_term::Color;
3+
use nu_ansi_term::Color;
44
use serde::{
55
de::value::Error as ValueError, de::Error as SerdeError, Deserialize, Deserializer, Serialize,
66
};
@@ -251,7 +251,7 @@ impl StarshipConfig {
251251
}
252252

253253
/// Deserialize a style string in the starship format with serde
254-
pub fn deserialize_style<'de, D>(de: D) -> Result<ansi_term::Style, D::Error>
254+
pub fn deserialize_style<'de, D>(de: D) -> Result<nu_ansi_term::Style, D::Error>
255255
where
256256
D: Deserializer<'de>,
257257
{
@@ -271,10 +271,10 @@ where
271271
- 'blink'
272272
- '<color>' (see the `parse_color_string` doc for valid color strings)
273273
*/
274-
pub fn parse_style_string(style_string: &str) -> Option<ansi_term::Style> {
274+
pub fn parse_style_string(style_string: &str) -> Option<nu_ansi_term::Style> {
275275
style_string
276276
.split_whitespace()
277-
.fold(Some(ansi_term::Style::new()), |maybe_style, token| {
277+
.fold(Some(nu_ansi_term::Style::new()), |maybe_style, token| {
278278
maybe_style.and_then(|style| {
279279
let token = token.to_lowercase();
280280

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

355355
// Parse a u8 (ansi color)
@@ -369,14 +369,14 @@ fn parse_color_string(color_string: &str) -> Option<ansi_term::Color> {
369369
"purple" => Some(Color::Purple),
370370
"cyan" => Some(Color::Cyan),
371371
"white" => Some(Color::White),
372-
"bright-black" => Some(Color::Fixed(8)), // "bright-black" is dark grey
373-
"bright-red" => Some(Color::Fixed(9)),
374-
"bright-green" => Some(Color::Fixed(10)),
375-
"bright-yellow" => Some(Color::Fixed(11)),
376-
"bright-blue" => Some(Color::Fixed(12)),
377-
"bright-purple" => Some(Color::Fixed(13)),
378-
"bright-cyan" => Some(Color::Fixed(14)),
379-
"bright-white" => Some(Color::Fixed(15)),
372+
"bright-black" => Some(Color::DarkGray), // "bright-black" is dark grey
373+
"bright-red" => Some(Color::LightRed),
374+
"bright-green" => Some(Color::LightGreen),
375+
"bright-yellow" => Some(Color::LightYellow),
376+
"bright-blue" => Some(Color::LightBlue),
377+
"bright-purple" => Some(Color::LightPurple),
378+
"bright-cyan" => Some(Color::LightCyan),
379+
"bright-white" => Some(Color::LightGray),
380380
_ => None,
381381
};
382382

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

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

@@ -600,7 +600,7 @@ mod tests {
600600
assert!(mystyle.is_dimmed);
601601
assert_eq!(
602602
mystyle,
603-
ansi_term::Style::new()
603+
nu_ansi_term::Style::new()
604604
.bold()
605605
.italic()
606606
.underline()
@@ -620,7 +620,7 @@ mod tests {
620620
assert!(mystyle.is_reverse);
621621
assert_eq!(
622622
mystyle,
623-
ansi_term::Style::new()
623+
nu_ansi_term::Style::new()
624624
.bold()
625625
.italic()
626626
.underline()
@@ -641,7 +641,7 @@ mod tests {
641641
assert!(mystyle.is_blink);
642642
assert_eq!(
643643
mystyle,
644-
ansi_term::Style::new()
644+
nu_ansi_term::Style::new()
645645
.bold()
646646
.italic()
647647
.underline()
@@ -662,7 +662,7 @@ mod tests {
662662
assert!(mystyle.is_hidden);
663663
assert_eq!(
664664
mystyle,
665-
ansi_term::Style::new()
665+
nu_ansi_term::Style::new()
666666
.bold()
667667
.italic()
668668
.underline()
@@ -683,7 +683,7 @@ mod tests {
683683
assert!(mystyle.is_strikethrough);
684684
assert_eq!(
685685
mystyle,
686-
ansi_term::Style::new()
686+
nu_ansi_term::Style::new()
687687
.bold()
688688
.italic()
689689
.underline()
@@ -698,7 +698,7 @@ mod tests {
698698
// Test a "plain" style with no formatting
699699
let config = Value::from("");
700700
let plain_style = <StyleWrapper>::from_config(&config).unwrap().0;
701-
assert_eq!(plain_style, ansi_term::Style::new());
701+
assert_eq!(plain_style, nu_ansi_term::Style::new());
702702

703703
// Test a string that's clearly broken
704704
let config = Value::from("djklgfhjkldhlhk;j");
@@ -763,7 +763,7 @@ mod tests {
763763
Style::new()
764764
.underline()
765765
.fg(Color::Fixed(120))
766-
.on(Color::RGB(5, 5, 5))
766+
.on(Color::Rgb(5, 5, 5))
767767
);
768768

769769
// Test that the last color style is always the one used

src/formatter/string_formatter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ansi_term::Style;
1+
use nu_ansi_term::Style;
22
use pest::error::Error as PestError;
33
use rayon::prelude::*;
44
use std::borrow::Cow;
@@ -465,7 +465,7 @@ where
465465
#[cfg(test)]
466466
mod tests {
467467
use super::*;
468-
use ansi_term::Color;
468+
use nu_ansi_term::Color;
469469

470470
// match_next(result: IterMut<Segment>, value, style)
471471
macro_rules! match_next {

src/logger.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils;
2-
use ansi_term::Color;
32
use log::{Level, LevelFilter, Metadata, Record};
3+
use nu_ansi_term::Color;
44
use once_cell::sync::OnceCell;
55
use std::{
66
collections::HashSet,

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ enum Commands {
114114
fn main() {
115115
// Configure the current terminal on windows to support ANSI escape sequences.
116116
#[cfg(windows)]
117-
let _ = ansi_term::enable_ansi_support();
117+
let _ = nu_ansi_term::enable_ansi_support();
118118
logger::init();
119119
init_global_threadpool();
120120

src/module.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::context::Shell;
22
use crate::segment;
33
use crate::segment::{FillSegment, Segment};
44
use crate::utils::wrap_colorseq_for_shell;
5-
use ansi_term::{ANSIString, ANSIStrings};
5+
use nu_ansi_term::{AnsiString, AnsiStrings};
66
use std::fmt;
77
use std::time::Duration;
88

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

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

155-
pub fn ansi_strings_for_shell(&self, shell: Shell, width: Option<usize>) -> Vec<ANSIString> {
155+
pub fn ansi_strings_for_shell(&self, shell: Shell, width: Option<usize>) -> Vec<AnsiString> {
156156
let mut iter = self.segments.iter().peekable();
157-
let mut ansi_strings: Vec<ANSIString> = Vec::new();
157+
let mut ansi_strings: Vec<AnsiString> = Vec::new();
158158
while iter.peek().is_some() {
159159
ansi_strings.extend(ansi_line(&mut iter, width));
160160
}
@@ -171,27 +171,27 @@ impl<'a> Module<'a> {
171171
impl<'a> fmt::Display for Module<'a> {
172172
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173173
let ansi_strings = self.ansi_strings();
174-
write!(f, "{}", ANSIStrings(&ansi_strings))
174+
write!(f, "{}", AnsiStrings(&ansi_strings))
175175
}
176176
}
177177

178-
fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: Shell) -> Vec<ANSIString> {
178+
fn ansi_strings_modified(ansi_strings: Vec<AnsiString>, shell: Shell) -> Vec<AnsiString> {
179179
ansi_strings
180180
.into_iter()
181181
.map(|ansi| {
182182
let wrapped = wrap_colorseq_for_shell(ansi.to_string(), shell);
183-
ANSIString::from(wrapped)
183+
AnsiString::from(wrapped)
184184
})
185-
.collect::<Vec<ANSIString>>()
185+
.collect::<Vec<AnsiString>>()
186186
}
187187

188-
fn ansi_line<'a, I>(segments: &mut I, term_width: Option<usize>) -> Vec<ANSIString<'a>>
188+
fn ansi_line<'a, I>(segments: &mut I, term_width: Option<usize>) -> Vec<AnsiString<'a>>
189189
where
190190
I: Iterator<Item = &'a Segment>,
191191
{
192192
let mut used = 0usize;
193-
let mut current: Vec<ANSIString> = Vec::new();
194-
let mut chunks: Vec<(Vec<ANSIString>, &FillSegment)> = Vec::new();
193+
let mut current: Vec<AnsiString> = Vec::new();
194+
let mut chunks: Vec<(Vec<AnsiString>, &FillSegment)> = Vec::new();
195195

196196
for segment in segments {
197197
match segment {
@@ -223,7 +223,7 @@ where
223223
.chain(std::iter::once(fill.ansi_string(fill_size)))
224224
})
225225
.chain(current.into_iter())
226-
.collect::<Vec<ANSIString>>()
226+
.collect::<Vec<AnsiString>>()
227227
}
228228
}
229229

src/modules/aws.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
248248
#[cfg(test)]
249249
mod tests {
250250
use crate::test::ModuleRenderer;
251-
use ansi_term::Color;
251+
use nu_ansi_term::Color;
252252
use std::fs::{create_dir, File};
253253
use std::io::{self, Write};
254254

src/modules/azure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ fn parse_json(json_file_path: &Path) -> Option<JValue> {
108108
mod tests {
109109
use crate::modules::azure::parse_json;
110110
use crate::test::ModuleRenderer;
111-
use ansi_term::Color;
112111
use ini::Ini;
112+
use nu_ansi_term::Color;
113113
use std::fs::File;
114114
use std::io::{self, Write};
115115
use std::path::PathBuf;

src/modules/battery.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl BatteryInfoProvider for BatteryInfoProviderImpl {
172172
mod tests {
173173
use super::*;
174174
use crate::test::ModuleRenderer;
175-
use ansi_term::Color;
175+
use nu_ansi_term::Color;
176176

177177
#[test]
178178
fn no_battery_status() {

src/modules/buf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn parse_buf_version(buf_version: &str) -> Option<String> {
6464
mod tests {
6565
use super::parse_buf_version;
6666
use crate::test::ModuleRenderer;
67-
use ansi_term::Color;
67+
use nu_ansi_term::Color;
6868
use std::fs::File;
6969
use std::io;
7070

src/modules/bun.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn parse_bun_version(bun_version: String) -> String {
7171
#[cfg(test)]
7272
mod tests {
7373
use crate::test::ModuleRenderer;
74-
use ansi_term::Color;
74+
use nu_ansi_term::Color;
7575
use std::fs::File;
7676
use std::io;
7777

src/modules/c.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
9292
#[cfg(test)]
9393
mod tests {
9494
use crate::{test::ModuleRenderer, utils::CommandOutput};
95-
use ansi_term::Color;
95+
use nu_ansi_term::Color;
9696
use std::fs::File;
9797
use std::io;
9898

0 commit comments

Comments
 (0)