summaryrefslogtreecommitdiffstats
path: root/vendor/color-print/src/terminfo
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
commitc23a457e72abe608715ac76f076f47dc42af07a5 (patch)
tree2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /vendor/color-print/src/terminfo
parentReleasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz
rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/color-print/src/terminfo')
-rw-r--r--vendor/color-print/src/terminfo/color.rs82
-rw-r--r--vendor/color-print/src/terminfo/mod.rs13
-rw-r--r--vendor/color-print/src/terminfo/style.rs40
3 files changed, 135 insertions, 0 deletions
diff --git a/vendor/color-print/src/terminfo/color.rs b/vendor/color-print/src/terminfo/color.rs
new file mode 100644
index 000000000..03e1a272b
--- /dev/null
+++ b/vendor/color-print/src/terminfo/color.rs
@@ -0,0 +1,82 @@
+//! Lazy constants representing the ANSI codes for setting terminal colors.
+//!
+//! Terminfo is used internally to guess the right codes.
+
+use lazy_static::lazy_static;
+use terminfo_crate::{capability as cap, expand, Capability};
+
+use crate::terminfo::TERMINFO;
+
+#[rustfmt::skip]
+lazy_static! {
+ pub static ref BLACK: String = foreground(0);
+ pub static ref RED: String = foreground(1);
+ pub static ref GREEN: String = foreground(2);
+ pub static ref YELLOW: String = foreground(3);
+ pub static ref BLUE: String = foreground(4);
+ pub static ref MAGENTA: String = foreground(5);
+ pub static ref CYAN: String = foreground(6);
+ pub static ref WHITE: String = foreground(7);
+
+ pub static ref BRIGHT_BLACK: String = foreground(8);
+ pub static ref BRIGHT_RED: String = foreground(9);
+ pub static ref BRIGHT_GREEN: String = foreground(10);
+ pub static ref BRIGHT_YELLOW: String = foreground(11);
+ pub static ref BRIGHT_BLUE: String = foreground(12);
+ pub static ref BRIGHT_MAGENTA: String = foreground(13);
+ pub static ref BRIGHT_CYAN: String = foreground(14);
+ pub static ref BRIGHT_WHITE: String = foreground(15);
+
+ pub static ref BG_BLACK: String = background(0);
+ pub static ref BG_RED: String = background(1);
+ pub static ref BG_GREEN: String = background(2);
+ pub static ref BG_YELLOW: String = background(3);
+ pub static ref BG_BLUE: String = background(4);
+ pub static ref BG_MAGENTA: String = background(5);
+ pub static ref BG_CYAN: String = background(6);
+ pub static ref BG_WHITE: String = background(7);
+
+ pub static ref BG_BRIGHT_BLACK: String = background(8);
+ pub static ref BG_BRIGHT_RED: String = background(9);
+ pub static ref BG_BRIGHT_GREEN: String = background(10);
+ pub static ref BG_BRIGHT_YELLOW: String = background(11);
+ pub static ref BG_BRIGHT_BLUE: String = background(12);
+ pub static ref BG_BRIGHT_MAGENTA: String = background(13);
+ pub static ref BG_BRIGHT_CYAN: String = background(14);
+ pub static ref BG_BRIGHT_WHITE: String = background(15);
+}
+
+/// Gets the ANSI code which sets the foreground color to the given color (0 to 15 included).
+fn foreground(v: u8) -> String {
+ #[cfg(debug_assertions)]
+ assert!(v < 16);
+
+ expand1_string::<cap::SetAForeground>(v)
+}
+
+/// Gets the ANSI code which sets the background color to the given color (0 to 15 included).
+fn background(v: u8) -> String {
+ #[cfg(debug_assertions)]
+ assert!(v < 16);
+
+ expand1_string::<cap::SetABackground>(v)
+}
+
+/// Shortcut function for the `foreground()` and `background()` functions.
+fn expand1_string<'a, T>(v: u8) -> String
+where
+ T: Capability<'a> + AsRef<[u8]>,
+{
+ expand1::<'a, T>(v).unwrap_or_else(|| String::new())
+}
+
+/// Shortcut function for the `foreground()` and `background()` functions.
+fn expand1<'a, T>(v: u8) -> Option<String>
+where
+ T: Capability<'a> + AsRef<[u8]>,
+{
+ let info = (*TERMINFO).as_ref()?;
+ let e = expand!(info.get::<T>()?.as_ref(); v).ok()?;
+ let s = std::str::from_utf8(&e).ok()?;
+ Some(s.to_owned())
+}
diff --git a/vendor/color-print/src/terminfo/mod.rs b/vendor/color-print/src/terminfo/mod.rs
new file mode 100644
index 000000000..2ba3a6f37
--- /dev/null
+++ b/vendor/color-print/src/terminfo/mod.rs
@@ -0,0 +1,13 @@
+mod color;
+mod style;
+
+pub use color::*;
+pub use style::*;
+
+use lazy_static::lazy_static;
+use terminfo_crate::Database;
+
+lazy_static! {
+ /// The terminfo database.
+ static ref TERMINFO: Option<Database> = Database::from_env().ok();
+}
diff --git a/vendor/color-print/src/terminfo/style.rs b/vendor/color-print/src/terminfo/style.rs
new file mode 100644
index 000000000..5f14e2bac
--- /dev/null
+++ b/vendor/color-print/src/terminfo/style.rs
@@ -0,0 +1,40 @@
+//! Lazy constants representing the ANSI codes for setting terminal styles, like bold, underline,
+//! etc...
+//!
+//! Terminfo is used internally to guess the right codes.
+
+use lazy_static::lazy_static;
+use terminfo_crate::{capability as cap, expand, Capability};
+
+use crate::terminfo::TERMINFO;
+
+lazy_static! {
+ pub static ref CLEAR: String = style::<cap::ExitAttributeMode>();
+ pub static ref BOLD: String = style::<cap::EnterBoldMode>();
+ pub static ref DIM: String = style::<cap::EnterDimMode>();
+ pub static ref BLINK: String = style::<cap::EnterBlinkMode>();
+ pub static ref ITALICS: String = style::<cap::EnterItalicsMode>();
+ pub static ref REVERSE: String = style::<cap::EnterReverseMode>();
+ pub static ref UNDERLINE: String = style::<cap::EnterUnderlineMode>();
+ pub static ref NO_ITALICS: String = style::<cap::ExitItalicsMode>();
+ pub static ref NO_UNDERLINE: String = style::<cap::ExitUnderlineMode>();
+}
+
+/// Gets the ANSI code which sets the given style `T`.
+fn style<'a, T>() -> String
+where
+ T: Capability<'a> + AsRef<[u8]>,
+{
+ expand0::<'a, T>().unwrap_or_else(|| String::new())
+}
+
+/// Shortcut function for the `style()` function.
+fn expand0<'a, T>() -> Option<String>
+where
+ T: Capability<'a> + AsRef<[u8]>,
+{
+ let info = (*TERMINFO).as_ref()?;
+ let e = expand!(info.get::<T>()?.as_ref()).ok()?;
+ let s = std::str::from_utf8(&e).ok()?;
+ Some(s.to_owned())
+}