pthorpe92 / ansiterm-clone Goto Github PK
View Code? Open in Web Editor NEWRust library for ANSI terminal colours and styles (bold, underline)
License: MIT License
Rust library for ANSI terminal colours and styles (bold, underline)
License: MIT License
I'm trying to migrate my project from using colored to ansi_term, as one of my dependencies already uses the latter. I was expecting this to be a straightforward replacement, but faced an issue: the Display implementation in ansi_term seems to ignore the formatting settings, e.g. with the following code
const DIGITS: &'static str = "1234567890";
fn main() {
let s = Color::Red.paint(DIGITS);
println!("{:.5}", s);
}
the expected behavior is: the provided format string is used; red string 12345 is printed;
the actual behavior is: the formatting settings are ignored; red string 1234567890 is printed.
Is this intentional, or a bug?
rustc: 1.38.0
ansi_term: 0.12.1
This issue looks pretty hairy, although there does seem to be commits/ merged PR's in other forks that we can look into to see how it is solved.
Right now the
paint
function onStyle
andColor
only acceptsI where I: Into<Cow<'a, S>>, <S as ToOwned>::Owned: Debug,But in the documents it notes that the paint function doesn't do allocations and return the string with the color applied, instead it returns a
Display
type which wraps the original string. It seems like this restriction could be relaxed without making a breaking change.Ideally I'd like it to return a type that impls Display if the inner type impls display, and impls Debug if the inner type impls Debug, which would let me write code like this:
write!( f, "\n{}", Style::new().bold().paint(format_args!( "{:>8} > {}", cur_line_no, line.unwrap() )) )?;instead of what I have to write right now
write!( f, "\n{:>8}{}{}", bold.paint(cur_line_no.to_string()), bold.paint(" > "), bold.paint(line.unwrap()) )?;It would also make it easier to work with error reporting:
for (n, error) in errors { writeln!(f)?; write!(Indented::numbered(f, n), "{}", Red.paint(error))?; }instead of:
let mut buf = String::new(); for (n, error) in errors { writeln!(f)?; buf.clear(); write!(&mut buf, "{}", error).unwrap(); write!(Indented::numbered(f, n), "{}", Red.paint(&buf))?; }
I see many projects using this library, and I don't see simple way to disable colored output for those apps (without code modification). In production environments I like to grep easily logs and colors are making it super difficult.
More info about NO_COLOR environment variable at https://no-color.org/
This is a valid issue, and should be looked into
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.