Skip to content

Commit 04129b7

Browse files
authored
parser: Switch from ansi_term to colored crate (rust-lang#100)
`ansi_term` is no longer maintained and for the simple needs here, `colored` is much better suited anyway. Fixes rust-lang#99.
2 parents c3b0287 + 91e300e commit 04129b7

File tree

2 files changed

+53
-61
lines changed

2 files changed

+53
-61
lines changed

varlink_parser/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ categories = ["parsing", "development-tools"]
1919
travis-ci = { repository = "varlink/rust" }
2020

2121
[dependencies]
22-
ansi_term = "0.12.1"
22+
colored = "2.1.0"
2323
chainerror = "0.8.0"
2424
peg = "0.6.3"

varlink_parser/src/format.rs

+52-60
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ansi_term::Colour;
1+
use colored::Colorize;
22
use std::fmt;
33

44
use crate::*;
@@ -49,41 +49,33 @@ impl<'a> Format for VTypeExt<'a> {
4949
impl<'a> FormatColored for VTypeExt<'a> {
5050
fn get_oneline_colored(&self) -> String {
5151
match *self {
52-
VTypeExt::Plain(VType::Bool) => Colour::Cyan.paint("bool").to_string(),
53-
VTypeExt::Plain(VType::Int) => Colour::Cyan.paint("int").to_string(),
54-
VTypeExt::Plain(VType::Float) => Colour::Cyan.paint("float").to_string(),
55-
VTypeExt::Plain(VType::String) => Colour::Cyan.paint("string").to_string(),
56-
VTypeExt::Plain(VType::Object) => Colour::Cyan.paint("object").to_string(),
57-
VTypeExt::Plain(VType::Typename(ref v)) => {
58-
Colour::Cyan.paint(v.to_string()).to_string()
59-
}
52+
VTypeExt::Plain(VType::Bool) => "bool".cyan().to_string(),
53+
VTypeExt::Plain(VType::Int) => "int".cyan().to_string(),
54+
VTypeExt::Plain(VType::Float) => "float".cyan().to_string(),
55+
VTypeExt::Plain(VType::String) => "string".cyan().to_string(),
56+
VTypeExt::Plain(VType::Object) => "object".cyan().to_string(),
57+
VTypeExt::Plain(VType::Typename(ref v)) => v.to_string().cyan().to_string(),
6058
VTypeExt::Plain(VType::Struct(ref v)) => v.get_oneline_colored(),
6159
VTypeExt::Plain(VType::Enum(ref v)) => v.get_oneline_colored(),
6260
VTypeExt::Array(ref v) => format!("[]{}", v.get_oneline_colored()),
63-
VTypeExt::Dict(ref v) => format!(
64-
"[{}]{}",
65-
Colour::Cyan.paint("string"),
66-
v.get_oneline_colored()
67-
),
61+
VTypeExt::Dict(ref v) => format!("[{}]{}", "string".cyan(), v.get_oneline_colored()),
6862
VTypeExt::Option(ref v) => format!("?{}", v.get_oneline_colored()),
6963
}
7064
}
7165
fn get_multiline_colored(&self, indent: usize, max: usize) -> String {
7266
match *self {
73-
VTypeExt::Plain(VType::Bool) => Colour::Cyan.paint("bool").to_string(),
74-
VTypeExt::Plain(VType::Int) => Colour::Cyan.paint("int").to_string(),
75-
VTypeExt::Plain(VType::Float) => Colour::Cyan.paint("float").to_string(),
76-
VTypeExt::Plain(VType::String) => Colour::Cyan.paint("string").to_string(),
77-
VTypeExt::Plain(VType::Object) => Colour::Cyan.paint("object").to_string(),
78-
VTypeExt::Plain(VType::Typename(ref v)) => {
79-
Colour::Cyan.paint(v.to_string()).to_string()
80-
}
67+
VTypeExt::Plain(VType::Bool) => "bool".cyan().to_string(),
68+
VTypeExt::Plain(VType::Int) => "int".cyan().to_string(),
69+
VTypeExt::Plain(VType::Float) => "float".cyan().to_string(),
70+
VTypeExt::Plain(VType::String) => "string".cyan().to_string(),
71+
VTypeExt::Plain(VType::Object) => "object".cyan().to_string(),
72+
VTypeExt::Plain(VType::Typename(ref v)) => v.to_string().cyan().to_string(),
8173
VTypeExt::Plain(VType::Struct(ref v)) => v.get_multiline_colored(indent, max),
8274
VTypeExt::Plain(VType::Enum(ref v)) => v.get_multiline_colored(indent, max),
8375
VTypeExt::Array(ref v) => format!("[]{}", v.get_multiline_colored(indent, max)),
8476
VTypeExt::Dict(ref v) => format!(
8577
"[{}]{}",
86-
Colour::Cyan.paint("string"),
78+
"string".cyan(),
8779
v.get_multiline_colored(indent, max)
8880
),
8981
VTypeExt::Option(ref v) => format!("?{}", v.get_multiline_colored(indent, max)),
@@ -555,53 +547,53 @@ impl<'a> FormatColored for IDL<'a> {
555547
let mut f = String::new();
556548

557549
if !self.doc.is_empty() {
558-
f += &Colour::Blue.paint(self.doc);
550+
f += &self.doc.blue();
559551
f += "\n";
560552
}
561-
f += &format!("{} {}\n", Colour::Purple.paint("interface"), self.name);
553+
f += &format!("{} {}\n", "interface", self.name.purple());
562554

563555
for t in self.typedef_keys.iter().map(|k| &self.typedefs[k]) {
564556
f += "\n";
565557
if !t.doc.is_empty() {
566-
f += &Colour::Blue.paint(t.doc);
558+
f += &t.doc.blue();
567559
f += "\n";
568560
}
569561

570562
f += &format!(
571563
"{} {} {}\n",
572-
Colour::Purple.paint("type"),
573-
Colour::Cyan.paint(t.name),
564+
"type".purple(),
565+
t.name.cyan(),
574566
t.elt.get_oneline_colored()
575567
);
576568
}
577569

578570
for m in self.method_keys.iter().map(|k| &self.methods[k]) {
579571
f += "\n";
580572
if !m.doc.is_empty() {
581-
f += &Colour::Blue.paint(m.doc);
573+
f += &m.doc.blue();
582574
f += "\n";
583575
}
584576

585577
f += &format!(
586578
"{} {}{} {} {}\n",
587-
Colour::Purple.paint("method"),
588-
Colour::Green.paint(m.name),
579+
"method".purple(),
580+
m.name.green(),
589581
m.input.get_oneline_colored(),
590-
Colour::Purple.paint("->"),
582+
"->".purple(),
591583
m.output.get_oneline_colored()
592584
);
593585
}
594586
for t in self.error_keys.iter().map(|k| &self.errors[k]) {
595587
f += "\n";
596588
if !t.doc.is_empty() {
597-
f += &Colour::Blue.paint(t.doc);
589+
f += &t.doc.blue();
598590
f += "\n";
599591
}
600592

601593
f += &format!(
602594
"{} {} {}\n",
603-
Colour::Purple.paint("error"),
604-
Colour::Cyan.paint(t.name),
595+
"error".purple(),
596+
t.name.cyan(),
605597
t.parm.get_oneline_colored()
606598
);
607599
}
@@ -615,15 +607,15 @@ impl<'a> FormatColored for IDL<'a> {
615607
f += &self
616608
.doc
617609
.split('\n')
618-
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
610+
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
619611
.collect::<Vec<String>>()
620612
.join("\n");
621613
f += "\n";
622614
}
623615
f += &format!(
624616
"{:indent$}{} {}\n",
625617
"",
626-
Colour::Purple.paint("interface"),
618+
"interface".purple(),
627619
self.name,
628620
indent = indent
629621
);
@@ -635,7 +627,7 @@ impl<'a> FormatColored for IDL<'a> {
635627
.doc
636628
.to_string()
637629
.split('\n')
638-
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
630+
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
639631
.collect::<Vec<String>>()
640632
.join("\n");
641633
f += "\n";
@@ -647,17 +639,17 @@ impl<'a> FormatColored for IDL<'a> {
647639
f += &format!(
648640
"{:indent$}{} {} {}\n",
649641
"",
650-
Colour::Purple.paint("type"),
651-
Colour::Cyan.paint(t.name),
642+
"type".purple(),
643+
t.name.cyan(),
652644
t.elt.get_oneline_colored(),
653645
indent = indent
654646
);
655647
} else {
656648
f += &format!(
657649
"{:indent$}{} {} {}\n",
658650
"",
659-
Colour::Purple.paint("type"),
660-
Colour::Cyan.paint(t.name),
651+
"type".purple(),
652+
t.name.cyan(),
661653
t.elt.get_multiline_colored(indent, max),
662654
indent = indent
663655
);
@@ -671,7 +663,7 @@ impl<'a> FormatColored for IDL<'a> {
671663
.doc
672664
.to_string()
673665
.split('\n')
674-
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
666+
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
675667
.collect::<Vec<String>>()
676668
.join("\n");
677669

@@ -687,43 +679,43 @@ impl<'a> FormatColored for IDL<'a> {
687679
f += &format!(
688680
"{:indent$}{} {}{} {} {}\n",
689681
"",
690-
Colour::Purple.paint("method"),
691-
Colour::Green.paint(m.name),
682+
"method".purple(),
683+
m.name.green(),
692684
m.input.get_oneline_colored(),
693-
Colour::Purple.paint("->"),
685+
"->".purple(),
694686
m.output.get_oneline_colored(),
695687
indent = indent
696688
);
697689
} else if (m_line.len() + m_input.len() + 6 <= max) || (m_input.len() == 2) {
698690
f += &format!(
699691
"{:indent$}{} {}{} {} {}\n",
700692
"",
701-
Colour::Purple.paint("method"),
702-
Colour::Green.paint(m.name),
693+
"method".purple(),
694+
m.name.green(),
703695
m.input.get_oneline_colored(),
704-
Colour::Purple.paint("->"),
696+
"->".purple(),
705697
m.output.get_multiline_colored(indent, max),
706698
indent = indent
707699
);
708700
} else if m_output.len() + 7 <= max {
709701
f += &format!(
710702
"{:indent$}{} {}{} {} {}\n",
711703
"",
712-
Colour::Purple.paint("method"),
713-
Colour::Green.paint(m.name),
704+
"method".purple(),
705+
m.name.green(),
714706
m.input.get_multiline_colored(indent, max),
715-
Colour::Purple.paint("->"),
707+
"->".purple(),
716708
m.output.get_oneline_colored(),
717709
indent = indent
718710
);
719711
} else {
720712
f += &format!(
721713
"{:indent$}{} {}{} {} {}\n",
722714
"",
723-
Colour::Purple.paint("method"),
724-
Colour::Green.paint(m.name),
715+
"method".purple(),
716+
m.name.green(),
725717
m.input.get_multiline_colored(indent, max),
726-
Colour::Purple.paint("->"),
718+
"->".purple(),
727719
m.output.get_multiline_colored(indent, max),
728720
indent = indent
729721
);
@@ -736,7 +728,7 @@ impl<'a> FormatColored for IDL<'a> {
736728
.doc
737729
.to_string()
738730
.split('\n')
739-
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
731+
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
740732
.collect::<Vec<String>>()
741733
.join("\n");
742734

@@ -749,17 +741,17 @@ impl<'a> FormatColored for IDL<'a> {
749741
f += &format!(
750742
"{:indent$}{} {} {}\n",
751743
"",
752-
Colour::Purple.paint("error"),
753-
Colour::Cyan.paint(t.name),
744+
"error".purple(),
745+
t.name.cyan(),
754746
t.parm.get_oneline_colored(),
755747
indent = indent
756748
);
757749
} else {
758750
f += &format!(
759751
"{:indent$}{} {} {}\n",
760752
"",
761-
Colour::Purple.paint("error"),
762-
Colour::Cyan.paint(t.name),
753+
"error".purple(),
754+
t.name.cyan(),
763755
t.parm.get_multiline_colored(indent, max),
764756
indent = indent
765757
);

0 commit comments

Comments
 (0)