Skip to content

Commit

Permalink
parser: Switch from ansi_term to colored crate
Browse files Browse the repository at this point in the history
`ansi_term` is no longer maintained and for the simple needs here,
`colored` is much better suited anyway.

Fixes varlink#99.

Signed-off-by: Zeeshan Ali Khan <zeenix@gmail.com>
  • Loading branch information
zeenix committed Mar 14, 2024
1 parent c3b0287 commit 91e300e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 61 deletions.
2 changes: 1 addition & 1 deletion varlink_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ categories = ["parsing", "development-tools"]
travis-ci = { repository = "varlink/rust" }

[dependencies]
ansi_term = "0.12.1"
colored = "2.1.0"
chainerror = "0.8.0"
peg = "0.6.3"
112 changes: 52 additions & 60 deletions varlink_parser/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansi_term::Colour;
use colored::Colorize;
use std::fmt;

use crate::*;
Expand Down Expand Up @@ -49,41 +49,33 @@ impl<'a> Format for VTypeExt<'a> {
impl<'a> FormatColored for VTypeExt<'a> {
fn get_oneline_colored(&self) -> String {
match *self {
VTypeExt::Plain(VType::Bool) => Colour::Cyan.paint("bool").to_string(),
VTypeExt::Plain(VType::Int) => Colour::Cyan.paint("int").to_string(),
VTypeExt::Plain(VType::Float) => Colour::Cyan.paint("float").to_string(),
VTypeExt::Plain(VType::String) => Colour::Cyan.paint("string").to_string(),
VTypeExt::Plain(VType::Object) => Colour::Cyan.paint("object").to_string(),
VTypeExt::Plain(VType::Typename(ref v)) => {
Colour::Cyan.paint(v.to_string()).to_string()
}
VTypeExt::Plain(VType::Bool) => "bool".cyan().to_string(),
VTypeExt::Plain(VType::Int) => "int".cyan().to_string(),
VTypeExt::Plain(VType::Float) => "float".cyan().to_string(),
VTypeExt::Plain(VType::String) => "string".cyan().to_string(),
VTypeExt::Plain(VType::Object) => "object".cyan().to_string(),
VTypeExt::Plain(VType::Typename(ref v)) => v.to_string().cyan().to_string(),
VTypeExt::Plain(VType::Struct(ref v)) => v.get_oneline_colored(),
VTypeExt::Plain(VType::Enum(ref v)) => v.get_oneline_colored(),
VTypeExt::Array(ref v) => format!("[]{}", v.get_oneline_colored()),
VTypeExt::Dict(ref v) => format!(
"[{}]{}",
Colour::Cyan.paint("string"),
v.get_oneline_colored()
),
VTypeExt::Dict(ref v) => format!("[{}]{}", "string".cyan(), v.get_oneline_colored()),
VTypeExt::Option(ref v) => format!("?{}", v.get_oneline_colored()),
}
}
fn get_multiline_colored(&self, indent: usize, max: usize) -> String {
match *self {
VTypeExt::Plain(VType::Bool) => Colour::Cyan.paint("bool").to_string(),
VTypeExt::Plain(VType::Int) => Colour::Cyan.paint("int").to_string(),
VTypeExt::Plain(VType::Float) => Colour::Cyan.paint("float").to_string(),
VTypeExt::Plain(VType::String) => Colour::Cyan.paint("string").to_string(),
VTypeExt::Plain(VType::Object) => Colour::Cyan.paint("object").to_string(),
VTypeExt::Plain(VType::Typename(ref v)) => {
Colour::Cyan.paint(v.to_string()).to_string()
}
VTypeExt::Plain(VType::Bool) => "bool".cyan().to_string(),
VTypeExt::Plain(VType::Int) => "int".cyan().to_string(),
VTypeExt::Plain(VType::Float) => "float".cyan().to_string(),
VTypeExt::Plain(VType::String) => "string".cyan().to_string(),
VTypeExt::Plain(VType::Object) => "object".cyan().to_string(),
VTypeExt::Plain(VType::Typename(ref v)) => v.to_string().cyan().to_string(),
VTypeExt::Plain(VType::Struct(ref v)) => v.get_multiline_colored(indent, max),
VTypeExt::Plain(VType::Enum(ref v)) => v.get_multiline_colored(indent, max),
VTypeExt::Array(ref v) => format!("[]{}", v.get_multiline_colored(indent, max)),
VTypeExt::Dict(ref v) => format!(
"[{}]{}",
Colour::Cyan.paint("string"),
"string".cyan(),
v.get_multiline_colored(indent, max)
),
VTypeExt::Option(ref v) => format!("?{}", v.get_multiline_colored(indent, max)),
Expand Down Expand Up @@ -555,53 +547,53 @@ impl<'a> FormatColored for IDL<'a> {
let mut f = String::new();

if !self.doc.is_empty() {
f += &Colour::Blue.paint(self.doc);
f += &self.doc.blue();
f += "\n";
}
f += &format!("{} {}\n", Colour::Purple.paint("interface"), self.name);
f += &format!("{} {}\n", "interface", self.name.purple());

for t in self.typedef_keys.iter().map(|k| &self.typedefs[k]) {
f += "\n";
if !t.doc.is_empty() {
f += &Colour::Blue.paint(t.doc);
f += &t.doc.blue();
f += "\n";
}

f += &format!(
"{} {} {}\n",
Colour::Purple.paint("type"),
Colour::Cyan.paint(t.name),
"type".purple(),
t.name.cyan(),
t.elt.get_oneline_colored()
);
}

for m in self.method_keys.iter().map(|k| &self.methods[k]) {
f += "\n";
if !m.doc.is_empty() {
f += &Colour::Blue.paint(m.doc);
f += &m.doc.blue();
f += "\n";
}

f += &format!(
"{} {}{} {} {}\n",
Colour::Purple.paint("method"),
Colour::Green.paint(m.name),
"method".purple(),
m.name.green(),
m.input.get_oneline_colored(),
Colour::Purple.paint("->"),
"->".purple(),
m.output.get_oneline_colored()
);
}
for t in self.error_keys.iter().map(|k| &self.errors[k]) {
f += "\n";
if !t.doc.is_empty() {
f += &Colour::Blue.paint(t.doc);
f += &t.doc.blue();
f += "\n";
}

f += &format!(
"{} {} {}\n",
Colour::Purple.paint("error"),
Colour::Cyan.paint(t.name),
"error".purple(),
t.name.cyan(),
t.parm.get_oneline_colored()
);
}
Expand All @@ -615,15 +607,15 @@ impl<'a> FormatColored for IDL<'a> {
f += &self
.doc
.split('\n')
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
.collect::<Vec<String>>()
.join("\n");
f += "\n";
}
f += &format!(
"{:indent$}{} {}\n",
"",
Colour::Purple.paint("interface"),
"interface".purple(),
self.name,
indent = indent
);
Expand All @@ -635,7 +627,7 @@ impl<'a> FormatColored for IDL<'a> {
.doc
.to_string()
.split('\n')
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
.collect::<Vec<String>>()
.join("\n");
f += "\n";
Expand All @@ -647,17 +639,17 @@ impl<'a> FormatColored for IDL<'a> {
f += &format!(
"{:indent$}{} {} {}\n",
"",
Colour::Purple.paint("type"),
Colour::Cyan.paint(t.name),
"type".purple(),
t.name.cyan(),
t.elt.get_oneline_colored(),
indent = indent
);
} else {
f += &format!(
"{:indent$}{} {} {}\n",
"",
Colour::Purple.paint("type"),
Colour::Cyan.paint(t.name),
"type".purple(),
t.name.cyan(),
t.elt.get_multiline_colored(indent, max),
indent = indent
);
Expand All @@ -671,7 +663,7 @@ impl<'a> FormatColored for IDL<'a> {
.doc
.to_string()
.split('\n')
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
.collect::<Vec<String>>()
.join("\n");

Expand All @@ -687,43 +679,43 @@ impl<'a> FormatColored for IDL<'a> {
f += &format!(
"{:indent$}{} {}{} {} {}\n",
"",
Colour::Purple.paint("method"),
Colour::Green.paint(m.name),
"method".purple(),
m.name.green(),
m.input.get_oneline_colored(),
Colour::Purple.paint("->"),
"->".purple(),
m.output.get_oneline_colored(),
indent = indent
);
} else if (m_line.len() + m_input.len() + 6 <= max) || (m_input.len() == 2) {
f += &format!(
"{:indent$}{} {}{} {} {}\n",
"",
Colour::Purple.paint("method"),
Colour::Green.paint(m.name),
"method".purple(),
m.name.green(),
m.input.get_oneline_colored(),
Colour::Purple.paint("->"),
"->".purple(),
m.output.get_multiline_colored(indent, max),
indent = indent
);
} else if m_output.len() + 7 <= max {
f += &format!(
"{:indent$}{} {}{} {} {}\n",
"",
Colour::Purple.paint("method"),
Colour::Green.paint(m.name),
"method".purple(),
m.name.green(),
m.input.get_multiline_colored(indent, max),
Colour::Purple.paint("->"),
"->".purple(),
m.output.get_oneline_colored(),
indent = indent
);
} else {
f += &format!(
"{:indent$}{} {}{} {} {}\n",
"",
Colour::Purple.paint("method"),
Colour::Green.paint(m.name),
"method".purple(),
m.name.green(),
m.input.get_multiline_colored(indent, max),
Colour::Purple.paint("->"),
"->".purple(),
m.output.get_multiline_colored(indent, max),
indent = indent
);
Expand All @@ -736,7 +728,7 @@ impl<'a> FormatColored for IDL<'a> {
.doc
.to_string()
.split('\n')
.map(|s| format!("{:indent$}{}", "", Colour::Blue.paint(s), indent = indent))
.map(|s| format!("{:indent$}{}", "", s.blue(), indent = indent))
.collect::<Vec<String>>()
.join("\n");

Expand All @@ -749,17 +741,17 @@ impl<'a> FormatColored for IDL<'a> {
f += &format!(
"{:indent$}{} {} {}\n",
"",
Colour::Purple.paint("error"),
Colour::Cyan.paint(t.name),
"error".purple(),
t.name.cyan(),
t.parm.get_oneline_colored(),
indent = indent
);
} else {
f += &format!(
"{:indent$}{} {} {}\n",
"",
Colour::Purple.paint("error"),
Colour::Cyan.paint(t.name),
"error".purple(),
t.name.cyan(),
t.parm.get_multiline_colored(indent, max),
indent = indent
);
Expand Down

0 comments on commit 91e300e

Please sign in to comment.