summaryrefslogtreecommitdiff
path: root/src/ansi.rs
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-02-07 20:07:20 -0500
committerGitHub <noreply@github.com>2019-02-07 20:07:20 -0500
commit46804e50ed3941cba9a7c6b12c77c73c988ddf62 (patch)
tree31ffe677d8c8cc73ff95e0bc4dcf3db0f0672b4e /src/ansi.rs
parentf22e0d72c52e89994a81265d35a023d85f230fd7 (diff)
Color exceptions (#1698)
Diffstat (limited to 'src/ansi.rs')
-rw-r--r--src/ansi.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/ansi.rs b/src/ansi.rs
new file mode 100644
index 000000000..47bf6c941
--- /dev/null
+++ b/src/ansi.rs
@@ -0,0 +1,69 @@
+use ansi_term::Color::Cyan;
+use ansi_term::Color::Red;
+use ansi_term::Color::Yellow;
+use ansi_term::Style;
+use regex::Regex;
+use std::borrow::Cow;
+use std::env;
+use std::fmt;
+
+lazy_static! {
+ // STRIP_ANSI_RE and strip_ansi_codes are lifted from the "console" crate.
+ // Copyright 2017 Armin Ronacher <armin.ronacher@active-4.com>. MIT License.
+ static ref STRIP_ANSI_RE: Regex = Regex::new(
+ r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]"
+ ).unwrap();
+ static ref NO_COLOR: bool = {
+ env::var_os("NO_COLOR").is_some()
+ };
+}
+
+/// Helper function to strip ansi codes.
+#[allow(dead_code)]
+pub fn strip_ansi_codes(s: &str) -> Cow<str> {
+ STRIP_ANSI_RE.replace_all(s, "")
+}
+
+pub fn use_color() -> bool {
+ *NO_COLOR == false
+}
+
+pub fn red_bold(s: String) -> impl fmt::Display {
+ let mut style = Style::new();
+ if use_color() {
+ style = style.bold().fg(Red);
+ }
+ style.paint(s)
+}
+
+pub fn italic_bold(s: String) -> impl fmt::Display {
+ let mut style = Style::new();
+ if use_color() {
+ style = style.italic().bold();
+ }
+ style.paint(s)
+}
+
+pub fn yellow(s: String) -> impl fmt::Display {
+ let mut style = Style::new();
+ if use_color() {
+ style = style.fg(Yellow);
+ }
+ style.paint(s)
+}
+
+pub fn cyan(s: String) -> impl fmt::Display {
+ let mut style = Style::new();
+ if use_color() {
+ style = style.fg(Cyan);
+ }
+ style.paint(s)
+}
+
+pub fn bold(s: String) -> impl fmt::Display {
+ let mut style = Style::new();
+ if use_color() {
+ style = style.bold();
+ }
+ style.paint(s)
+}