2023-05-07 22:14:19 +00:00
|
|
|
// Generic output functions.
|
|
|
|
use crate::color::RgbColor;
|
2024-01-25 20:39:45 +00:00
|
|
|
use crate::common::{self, wcs2string_appending};
|
2023-06-11 18:44:36 +00:00
|
|
|
use crate::curses::{self, tparm1, Term};
|
2023-05-07 22:14:19 +00:00
|
|
|
use crate::env::EnvVar;
|
2024-02-28 19:06:45 +00:00
|
|
|
use crate::threads::MainThread;
|
2023-08-08 22:16:04 +00:00
|
|
|
use crate::wchar::prelude::*;
|
2023-05-16 19:46:16 +00:00
|
|
|
use bitflags::bitflags;
|
2024-03-02 01:42:43 +00:00
|
|
|
use std::cell::RefCell;
|
2023-11-25 18:40:31 +00:00
|
|
|
use std::ffi::CStr;
|
2023-06-11 21:47:54 +00:00
|
|
|
use std::io::{Result, Write};
|
2023-05-07 22:14:19 +00:00
|
|
|
use std::os::fd::RawFd;
|
|
|
|
use std::sync::atomic::{AtomicU8, Ordering};
|
2023-05-16 19:46:16 +00:00
|
|
|
|
|
|
|
bitflags! {
|
2023-09-15 09:56:03 +00:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2023-05-16 19:46:16 +00:00
|
|
|
pub struct ColorSupport: u8 {
|
|
|
|
const TERM_256COLOR = 1<<0;
|
|
|
|
const TERM_24BIT = 1<<1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 23:13:54 +00:00
|
|
|
/// Whether term256 and term24bit are supported.
|
|
|
|
static COLOR_SUPPORT: AtomicU8 = AtomicU8::new(0);
|
2023-05-16 19:46:16 +00:00
|
|
|
|
2023-05-07 22:14:19 +00:00
|
|
|
/// Returns true if we think tparm can handle outputting a color index.
|
2023-12-01 09:08:12 +00:00
|
|
|
fn term_supports_color_natively(term: &Term, c: u8) -> bool {
|
2023-05-07 22:14:19 +00:00
|
|
|
#[allow(clippy::int_plus_one)]
|
|
|
|
if let Some(max_colors) = term.max_colors {
|
2023-12-29 20:17:22 +00:00
|
|
|
max_colors >= usize::from(c) + 1
|
2023-05-07 22:14:19 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_color_support() -> ColorSupport {
|
|
|
|
let val = COLOR_SUPPORT.load(Ordering::Relaxed);
|
|
|
|
ColorSupport::from_bits_truncate(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_color_support(val: ColorSupport) {
|
|
|
|
COLOR_SUPPORT.store(val.bits(), Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index_for_color(c: RgbColor) -> u8 {
|
|
|
|
if c.is_named() || !(get_color_support().contains(ColorSupport::TERM_256COLOR)) {
|
|
|
|
return c.to_name_index();
|
|
|
|
}
|
|
|
|
c.to_term256_index()
|
|
|
|
}
|
|
|
|
|
2023-06-11 20:35:48 +00:00
|
|
|
fn write_color_escape(outp: &mut Outputter, term: &Term, todo: &CStr, mut idx: u8, is_fg: bool) {
|
2023-12-01 09:08:12 +00:00
|
|
|
if term_supports_color_natively(term, idx) {
|
2023-05-07 22:14:19 +00:00
|
|
|
// Use tparm to emit color escape.
|
2023-06-17 23:04:34 +00:00
|
|
|
outp.tputs_if_some(&tparm1(todo, idx.into()));
|
2023-05-07 22:14:19 +00:00
|
|
|
} else {
|
|
|
|
// We are attempting to bypass the term here. Generate the ANSI escape sequence ourself.
|
|
|
|
if idx < 16 {
|
|
|
|
// this allows the non-bright color to happen instead of no color working at all when a
|
|
|
|
// bright is attempted when only colors 0-7 are supported.
|
|
|
|
//
|
|
|
|
// TODO: enter bold mode in builtin_set_color in the same circumstance- doing that combined
|
|
|
|
// with what we do here, will make the brights actually work for virtual consoles/ancient
|
|
|
|
// emulators.
|
|
|
|
if term.max_colors == Some(8) && idx > 8 {
|
|
|
|
idx -= 8;
|
|
|
|
}
|
|
|
|
write!(
|
2023-06-11 21:47:54 +00:00
|
|
|
outp,
|
2023-05-07 22:14:19 +00:00
|
|
|
"\x1B[{}m",
|
|
|
|
(if idx > 7 { 82 } else { 30 }) + i32::from(idx) + ((i32::from(!is_fg)) * 10)
|
|
|
|
)
|
|
|
|
.expect("Writing to in-memory buffer should never fail");
|
|
|
|
} else {
|
2023-06-11 21:47:54 +00:00
|
|
|
write!(outp, "\x1B[{};5;{}m", if is_fg { 38 } else { 48 }, idx).unwrap();
|
2023-05-07 22:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_foreground_color(outp: &mut Outputter, idx: u8, term: &Term) -> bool {
|
2023-06-17 23:04:34 +00:00
|
|
|
if let Some(cap) = &term.set_a_foreground {
|
2023-06-11 20:35:48 +00:00
|
|
|
write_color_escape(outp, term, cap, idx, true);
|
|
|
|
true
|
2023-06-17 23:04:34 +00:00
|
|
|
} else if let Some(cap) = &term.set_foreground {
|
2023-06-11 20:35:48 +00:00
|
|
|
write_color_escape(outp, term, cap, idx, true);
|
|
|
|
true
|
2023-05-07 22:14:19 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_background_color(outp: &mut Outputter, idx: u8, term: &Term) -> bool {
|
2023-06-17 23:04:34 +00:00
|
|
|
if let Some(cap) = &term.set_a_background {
|
2023-06-11 20:35:48 +00:00
|
|
|
write_color_escape(outp, term, cap, idx, false);
|
|
|
|
true
|
2023-06-17 23:04:34 +00:00
|
|
|
} else if let Some(cap) = &term.set_background {
|
2023-06-11 20:35:48 +00:00
|
|
|
write_color_escape(outp, term, cap, idx, false);
|
|
|
|
true
|
2023-05-07 22:14:19 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Outputter {
|
|
|
|
/// Storage for buffered contents.
|
|
|
|
contents: Vec<u8>,
|
|
|
|
|
|
|
|
/// Count of how many outstanding begin_buffering() calls there are.
|
|
|
|
buffer_count: u32,
|
|
|
|
|
|
|
|
/// fd to output to, or -1 for none.
|
|
|
|
fd: RawFd,
|
|
|
|
|
|
|
|
/// Foreground.
|
|
|
|
last_color: RgbColor,
|
|
|
|
|
|
|
|
/// Background.
|
|
|
|
last_color2: RgbColor,
|
|
|
|
|
|
|
|
was_bold: bool,
|
|
|
|
was_underline: bool,
|
|
|
|
was_italics: bool,
|
|
|
|
was_dim: bool,
|
|
|
|
was_reverse: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Outputter {
|
|
|
|
/// Construct an outputter which outputs to a given fd.
|
|
|
|
/// If the fd is negative, the outputter will buffer its output.
|
|
|
|
const fn new_from_fd(fd: RawFd) -> Self {
|
|
|
|
Self {
|
|
|
|
contents: Vec::new(),
|
|
|
|
buffer_count: 0,
|
|
|
|
fd,
|
|
|
|
last_color: RgbColor::NORMAL,
|
|
|
|
last_color2: RgbColor::NORMAL,
|
|
|
|
was_bold: false,
|
|
|
|
was_underline: false,
|
|
|
|
was_italics: false,
|
|
|
|
was_dim: false,
|
|
|
|
was_reverse: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct an outputter which outputs to its string buffer.
|
|
|
|
pub fn new_buffering() -> Self {
|
|
|
|
Self::new_from_fd(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset_modes(&mut self) {
|
|
|
|
self.was_bold = false;
|
|
|
|
self.was_underline = false;
|
|
|
|
self.was_italics = false;
|
|
|
|
self.was_dim = false;
|
|
|
|
self.was_reverse = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_flush(&mut self) {
|
|
|
|
if self.fd >= 0 && self.buffer_count == 0 {
|
|
|
|
self.flush_to(self.fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unconditionally write the color string to the output.
|
|
|
|
/// Exported for builtin_set_color's usage only.
|
|
|
|
pub fn write_color(&mut self, color: RgbColor, is_fg: bool) -> bool {
|
|
|
|
let Some(term) = curses::term() else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
let term: &Term = &term;
|
|
|
|
let supports_term24bit = get_color_support().contains(ColorSupport::TERM_24BIT);
|
|
|
|
if !supports_term24bit || !color.is_rgb() {
|
|
|
|
// Indexed or non-24 bit color.
|
|
|
|
let idx = index_for_color(color);
|
|
|
|
if is_fg {
|
|
|
|
return write_foreground_color(self, idx, term);
|
|
|
|
} else {
|
|
|
|
return write_background_color(self, idx, term);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 24 bit! No tparm here, just ANSI escape sequences.
|
|
|
|
// Foreground: ^[38;2;<r>;<g>;<b>m
|
|
|
|
// Background: ^[48;2;<r>;<g>;<b>m
|
|
|
|
let rgb = color.to_color24();
|
|
|
|
write!(
|
2023-06-11 21:47:54 +00:00
|
|
|
self,
|
2023-05-07 22:14:19 +00:00
|
|
|
"\x1B[{};2;{};{};{}m",
|
|
|
|
if is_fg { 38 } else { 48 },
|
|
|
|
rgb.r,
|
|
|
|
rgb.g,
|
|
|
|
rgb.b
|
|
|
|
)
|
2023-06-11 21:47:54 +00:00
|
|
|
.expect("Outputter::write should never fail");
|
2023-05-07 22:14:19 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the fg and bg color. May be called as often as you like, since if the new color is the same
|
|
|
|
/// as the previous, nothing will be written. Negative values for set_color will also be ignored.
|
|
|
|
/// Since the terminfo string this function emits can potentially cause the screen to flicker, the
|
|
|
|
/// function takes care to write as little as possible.
|
|
|
|
///
|
|
|
|
/// Possible values for colors are RgbColor colors or special values like RgbColor::NORMAL
|
|
|
|
///
|
|
|
|
/// In order to set the color to normal, three terminfo strings may have to be written.
|
|
|
|
///
|
|
|
|
/// - First a string to set the color, such as set_a_foreground. This is needed because otherwise
|
|
|
|
/// the previous strings colors might be removed as well.
|
|
|
|
///
|
|
|
|
/// - After that we write the exit_attribute_mode string to reset all color attributes.
|
|
|
|
///
|
|
|
|
/// - Lastly we may need to write set_a_background or set_a_foreground to set the other half of the
|
|
|
|
/// color pair to what it should be.
|
2023-06-17 23:04:34 +00:00
|
|
|
#[allow(clippy::if_same_then_else)]
|
2023-05-07 22:14:19 +00:00
|
|
|
pub fn set_color(&mut self, mut fg: RgbColor, mut bg: RgbColor) {
|
|
|
|
// Test if we have at least basic support for setting fonts, colors and related bits - otherwise
|
|
|
|
// just give up...
|
|
|
|
let Some(term) = curses::term() else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let term: &Term = &term;
|
2023-06-17 23:04:34 +00:00
|
|
|
let Term {
|
|
|
|
enter_bold_mode,
|
|
|
|
enter_underline_mode,
|
|
|
|
exit_underline_mode,
|
|
|
|
enter_italics_mode,
|
|
|
|
exit_italics_mode,
|
|
|
|
enter_dim_mode,
|
|
|
|
enter_reverse_mode,
|
|
|
|
enter_standout_mode,
|
|
|
|
exit_attribute_mode,
|
|
|
|
..
|
|
|
|
} = term;
|
|
|
|
let Some(exit_attribute_mode) = exit_attribute_mode else {
|
2023-05-07 22:14:19 +00:00
|
|
|
return;
|
2023-06-17 23:04:34 +00:00
|
|
|
};
|
2023-05-07 22:14:19 +00:00
|
|
|
|
|
|
|
const normal: RgbColor = RgbColor::NORMAL;
|
|
|
|
let mut bg_set = false;
|
|
|
|
let mut last_bg_set = false;
|
|
|
|
let is_bold = fg.is_bold() || bg.is_bold();
|
|
|
|
let is_underline = fg.is_underline() || bg.is_underline();
|
|
|
|
let is_italics = fg.is_italics() || bg.is_italics();
|
|
|
|
let is_dim = fg.is_dim() || bg.is_dim();
|
|
|
|
let is_reverse = fg.is_reverse() || bg.is_reverse();
|
|
|
|
|
|
|
|
if fg.is_reset() || bg.is_reset() {
|
|
|
|
#[allow(unused_assignments)]
|
|
|
|
{
|
|
|
|
fg = normal;
|
|
|
|
bg = normal;
|
|
|
|
}
|
|
|
|
self.reset_modes();
|
|
|
|
// If we exit attribute mode, we must first set a color, or previously colored text might
|
|
|
|
// lose its color. Terminals are weird...
|
|
|
|
write_foreground_color(self, 0, term);
|
2023-06-17 23:04:34 +00:00
|
|
|
self.tputs(exit_attribute_mode);
|
2023-05-07 22:14:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (self.was_bold && !is_bold)
|
|
|
|
|| (self.was_dim && !is_dim)
|
|
|
|
|| (self.was_reverse && !is_reverse)
|
|
|
|
{
|
|
|
|
// Only way to exit bold/dim/reverse mode is a reset of all attributes.
|
2023-06-17 23:04:34 +00:00
|
|
|
self.tputs(exit_attribute_mode);
|
2023-05-07 22:14:19 +00:00
|
|
|
self.last_color = normal;
|
|
|
|
self.last_color2 = normal;
|
|
|
|
self.reset_modes();
|
|
|
|
}
|
|
|
|
if !self.last_color2.is_special() {
|
|
|
|
// Background was set.
|
|
|
|
// "Special" here refers to the special "normal", "reset" and "none" colors,
|
|
|
|
// that really just disable the background.
|
|
|
|
last_bg_set = true;
|
|
|
|
}
|
|
|
|
if !bg.is_special() {
|
|
|
|
// Background is set.
|
|
|
|
bg_set = true;
|
|
|
|
if fg == bg {
|
|
|
|
fg = if bg == RgbColor::WHITE {
|
|
|
|
RgbColor::BLACK
|
|
|
|
} else {
|
|
|
|
RgbColor::WHITE
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 23:04:34 +00:00
|
|
|
if let Some(enter_bold_mode) = &enter_bold_mode {
|
2023-05-07 22:14:19 +00:00
|
|
|
if bg_set && !last_bg_set {
|
|
|
|
// Background color changed and is set, so we enter bold mode to make reading easier.
|
|
|
|
// This means bold mode is _always_ on when the background color is set.
|
2023-06-17 23:04:34 +00:00
|
|
|
self.tputs(enter_bold_mode);
|
2023-05-07 22:14:19 +00:00
|
|
|
}
|
|
|
|
if !bg_set && last_bg_set {
|
|
|
|
// Background color changed and is no longer set, so we exit bold mode.
|
2023-06-17 23:04:34 +00:00
|
|
|
self.tputs(exit_attribute_mode);
|
2023-05-07 22:14:19 +00:00
|
|
|
self.reset_modes();
|
|
|
|
// We don't know if exit_attribute_mode resets colors, so we set it to something known.
|
|
|
|
if write_foreground_color(self, 0, term) {
|
|
|
|
self.last_color = RgbColor::BLACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.last_color != fg {
|
|
|
|
if fg.is_normal() {
|
|
|
|
write_foreground_color(self, 0, term);
|
2023-06-17 23:04:34 +00:00
|
|
|
self.tputs(exit_attribute_mode);
|
2023-05-07 22:14:19 +00:00
|
|
|
|
|
|
|
self.last_color2 = RgbColor::NORMAL;
|
|
|
|
self.reset_modes();
|
|
|
|
} else if !fg.is_special() {
|
|
|
|
self.write_color(fg, true /* foreground */);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.last_color = fg;
|
|
|
|
|
|
|
|
if self.last_color2 != bg {
|
|
|
|
if bg.is_normal() {
|
|
|
|
write_background_color(self, 0, term);
|
|
|
|
|
2023-06-17 23:04:34 +00:00
|
|
|
self.tputs(exit_attribute_mode);
|
2023-05-07 22:14:19 +00:00
|
|
|
if !self.last_color.is_normal() {
|
|
|
|
self.write_color(self.last_color, true /* foreground */);
|
|
|
|
}
|
|
|
|
self.reset_modes();
|
|
|
|
self.last_color2 = bg;
|
|
|
|
} else if !bg.is_special() {
|
|
|
|
self.write_color(bg, false /* not foreground */);
|
|
|
|
self.last_color2 = bg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lastly, we set bold, underline, italics, dim, and reverse modes correctly.
|
2023-06-17 23:04:34 +00:00
|
|
|
if is_bold && !self.was_bold && !bg_set && self.tputs_if_some(enter_bold_mode) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.was_bold = is_bold;
|
|
|
|
}
|
|
|
|
|
2023-06-17 23:04:34 +00:00
|
|
|
if !self.was_underline && is_underline && self.tputs_if_some(enter_underline_mode) {
|
|
|
|
self.was_underline = is_underline;
|
|
|
|
} else if self.was_underline && !is_underline && self.tputs_if_some(exit_underline_mode) {
|
|
|
|
self.was_underline = is_underline;
|
2023-05-07 22:14:19 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 23:04:34 +00:00
|
|
|
if self.was_italics && !is_italics && self.tputs_if_some(exit_italics_mode) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.was_italics = is_italics;
|
2023-06-17 23:04:34 +00:00
|
|
|
} else if !self.was_italics && is_italics && self.tputs_if_some(enter_italics_mode) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.was_italics = is_italics;
|
|
|
|
}
|
|
|
|
|
2023-06-17 23:04:34 +00:00
|
|
|
if is_dim && !self.was_dim && self.tputs_if_some(enter_dim_mode) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.was_dim = is_dim;
|
|
|
|
}
|
|
|
|
// N.B. there is no exit_dim_mode in curses, it's handled by exit_attribute_mode above.
|
|
|
|
|
|
|
|
if is_reverse && !self.was_reverse {
|
|
|
|
// Some terms do not have a reverse mode set, so standout mode is a fallback.
|
2023-06-17 23:04:34 +00:00
|
|
|
if self.tputs_if_some(enter_reverse_mode) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.was_reverse = is_reverse;
|
2023-06-17 23:04:34 +00:00
|
|
|
} else if self.tputs_if_some(enter_standout_mode) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.was_reverse = is_reverse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write a wide character to the receiver.
|
2023-05-29 23:39:44 +00:00
|
|
|
pub fn writech(&mut self, ch: char) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.write_wstr(wstr::from_char_slice(&[ch]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write a narrow character to the receiver.
|
2023-05-29 23:39:44 +00:00
|
|
|
pub fn push(&mut self, ch: u8) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.contents.push(ch);
|
|
|
|
self.maybe_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write a wide string.
|
|
|
|
pub fn write_wstr(&mut self, str: &wstr) {
|
|
|
|
wcs2string_appending(&mut self.contents, str);
|
|
|
|
self.maybe_flush();
|
|
|
|
}
|
|
|
|
|
2024-05-06 19:58:10 +00:00
|
|
|
/// Return the "output" contents.
|
2023-05-07 22:14:19 +00:00
|
|
|
pub fn contents(&self) -> &[u8] {
|
|
|
|
&self.contents
|
|
|
|
}
|
|
|
|
|
2024-05-06 19:52:36 +00:00
|
|
|
/// Output any buffered data to the given `fd`.
|
2023-05-07 22:14:19 +00:00
|
|
|
fn flush_to(&mut self, fd: RawFd) {
|
|
|
|
if fd >= 0 && !self.contents.is_empty() {
|
|
|
|
let _ = common::write_loop(&fd, &self.contents);
|
|
|
|
self.contents.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Begins buffering. Output will not be automatically flushed until a corresponding
|
|
|
|
/// end_buffering() call.
|
2023-11-25 18:40:31 +00:00
|
|
|
pub fn begin_buffering(&mut self) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.buffer_count += 1;
|
|
|
|
assert!(self.buffer_count > 0, "buffer_count overflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Balance a begin_buffering() call.
|
2023-11-25 18:40:31 +00:00
|
|
|
pub fn end_buffering(&mut self) {
|
2023-05-07 22:14:19 +00:00
|
|
|
assert!(self.buffer_count > 0, "buffer_count underflow");
|
|
|
|
self.buffer_count -= 1;
|
|
|
|
self.maybe_flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-11 21:47:54 +00:00
|
|
|
/// Outputter implements Write, so it may be used as the receiver of the write! macro.
|
|
|
|
/// Only ASCII data should be written this way: do NOT assume that the receiver is UTF-8.
|
|
|
|
impl Write for Outputter {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
self.contents.extend_from_slice(buf);
|
|
|
|
self.maybe_flush();
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<()> {
|
|
|
|
self.flush_to(self.fd);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-07 22:14:19 +00:00
|
|
|
impl Outputter {
|
2023-06-17 23:04:34 +00:00
|
|
|
/// Emit a terminfo string, like tputs.
|
|
|
|
/// affcnt (number of lines affected) is assumed to be 1, i.e. not applicable.
|
|
|
|
pub fn tputs(&mut self, str: &CStr) {
|
2024-04-06 19:34:38 +00:00
|
|
|
self.tputs_bytes(str.to_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tputs_bytes(&mut self, str: &[u8]) {
|
2023-05-07 22:14:19 +00:00
|
|
|
self.begin_buffering();
|
2024-04-06 19:34:38 +00:00
|
|
|
let _ = self.write(str);
|
2023-05-07 22:14:19 +00:00
|
|
|
self.end_buffering();
|
2023-06-17 23:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience cover over tputs, in recognition of the fact that our Term has Optional fields.
|
|
|
|
/// If `str` is Some, write it with tputs and return true. Otherwise, return false.
|
2023-11-25 18:40:31 +00:00
|
|
|
pub fn tputs_if_some(&mut self, str: &Option<impl AsRef<CStr>>) -> bool {
|
2023-06-17 23:04:34 +00:00
|
|
|
if let Some(str) = str {
|
2023-11-25 18:40:31 +00:00
|
|
|
self.tputs(str.as_ref());
|
2023-06-17 23:04:34 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2023-05-07 22:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the outputter for stdout.
|
|
|
|
/// This should only be used from the main thread.
|
2024-03-02 01:42:43 +00:00
|
|
|
pub fn stdoutput() -> &'static RefCell<Outputter> {
|
|
|
|
static STDOUTPUT: MainThread<RefCell<Outputter>> =
|
|
|
|
MainThread::new(RefCell::new(Outputter::new_from_fd(libc::STDOUT_FILENO)));
|
|
|
|
STDOUTPUT.get()
|
2023-05-07 22:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a list of RgbColor, pick the "best" one, as determined by the color support. Returns
|
|
|
|
/// RgbColor::NONE if empty.
|
2023-05-29 23:39:44 +00:00
|
|
|
pub fn best_color(candidates: &[RgbColor], support: ColorSupport) -> RgbColor {
|
2023-05-07 22:14:19 +00:00
|
|
|
if candidates.is_empty() {
|
|
|
|
return RgbColor::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut first_rgb = RgbColor::NONE;
|
|
|
|
let mut first_named = RgbColor::NONE;
|
|
|
|
for color in candidates {
|
|
|
|
if first_rgb.is_none() && color.is_rgb() {
|
|
|
|
first_rgb = *color;
|
|
|
|
}
|
|
|
|
if first_named.is_none() && color.is_named() {
|
|
|
|
first_named = *color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have both RGB and named colors, then prefer rgb if term256 is supported.
|
|
|
|
let mut result;
|
|
|
|
let has_term256 = support.contains(ColorSupport::TERM_256COLOR);
|
|
|
|
if (!first_rgb.is_none() && has_term256) || first_named.is_none() {
|
|
|
|
result = first_rgb;
|
|
|
|
} else {
|
|
|
|
result = first_named;
|
|
|
|
}
|
|
|
|
if result.is_none() {
|
|
|
|
result = candidates[0];
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the internal color code representing the specified color.
|
|
|
|
/// TODO: This code should be refactored to enable sharing with builtin_set_color.
|
|
|
|
/// In particular, the argument parsing still isn't fully capable.
|
|
|
|
#[allow(clippy::collapsible_else_if)]
|
2023-10-08 21:22:27 +00:00
|
|
|
pub fn parse_color(var: &EnvVar, is_background: bool) -> RgbColor {
|
2023-05-07 22:14:19 +00:00
|
|
|
let mut is_bold = false;
|
|
|
|
let mut is_underline = false;
|
|
|
|
let mut is_italics = false;
|
|
|
|
let mut is_dim = false;
|
|
|
|
let mut is_reverse = false;
|
|
|
|
|
|
|
|
let mut candidates: Vec<RgbColor> = Vec::new();
|
|
|
|
|
|
|
|
let prefix = L!("--background=");
|
|
|
|
|
|
|
|
let mut next_is_background = false;
|
|
|
|
let mut color_name = WString::new();
|
|
|
|
for next in var.as_list() {
|
|
|
|
color_name.clear();
|
|
|
|
if is_background {
|
|
|
|
if color_name.is_empty() && next_is_background {
|
|
|
|
color_name = next.to_owned();
|
|
|
|
next_is_background = false;
|
|
|
|
} else if next.starts_with(prefix) {
|
|
|
|
// Look for something like "--background=red".
|
|
|
|
color_name = next.slice_from(prefix.char_count()).to_owned();
|
|
|
|
} else if next == "--background" || next == "-b" {
|
|
|
|
// Without argument attached the next token is the color
|
|
|
|
// - if it's another option it's an error.
|
|
|
|
next_is_background = true;
|
|
|
|
} else if next == "--reverse" || next == "-r" {
|
|
|
|
// Reverse should be meaningful in either context
|
|
|
|
is_reverse = true;
|
|
|
|
} else if next.starts_with("-b") {
|
|
|
|
// Look for something like "-bred".
|
|
|
|
// Yes, that length is hardcoded.
|
|
|
|
color_name = next.slice_from(2).to_owned();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if next == "--bold" || next == "-o" {
|
|
|
|
is_bold = true;
|
|
|
|
} else if next == "--underline" || next == "-u" {
|
|
|
|
is_underline = true;
|
|
|
|
} else if next == "--italics" || next == "-i" {
|
|
|
|
is_italics = true;
|
|
|
|
} else if next == "--dim" || next == "-d" {
|
|
|
|
is_dim = true;
|
|
|
|
} else if next == "--reverse" || next == "-r" {
|
|
|
|
is_reverse = true;
|
|
|
|
} else {
|
|
|
|
color_name = next.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !color_name.is_empty() {
|
|
|
|
let color: Option<RgbColor> = RgbColor::from_wstr(&color_name);
|
|
|
|
if let Some(color) = color {
|
|
|
|
candidates.push(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut result = best_color(&candidates, get_color_support());
|
|
|
|
if result.is_none() {
|
|
|
|
result = RgbColor::NORMAL;
|
|
|
|
}
|
|
|
|
result.set_bold(is_bold);
|
|
|
|
result.set_underline(is_underline);
|
|
|
|
result.set_italics(is_italics);
|
|
|
|
result.set_dim(is_dim);
|
|
|
|
result.set_reverse(is_reverse);
|
|
|
|
result
|
|
|
|
}
|