mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-28 13:53:10 +00:00
restyle output module to match project style
Reduces lint errors from 34 to 31 (-9%). Line count from 712 to 535 (-25%). Another step in resolving issue #2902.
This commit is contained in:
parent
4f619c966b
commit
ed8d1040ba
2 changed files with 197 additions and 374 deletions
434
src/output.cpp
434
src/output.cpp
|
@ -1,10 +1,8 @@
|
||||||
/** \file output.c
|
// Generic output functions.
|
||||||
Generic output functions
|
|
||||||
*/
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#if HAVE_NCURSES_H
|
#if HAVE_NCURSES_H
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
@ -18,87 +16,61 @@
|
||||||
#elif HAVE_NCURSES_TERM_H
|
#elif HAVE_NCURSES_TERM_H
|
||||||
#include <ncurses/term.h>
|
#include <ncurses/term.h>
|
||||||
#endif
|
#endif
|
||||||
#include <wchar.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "fallback.h" // IWYU pragma: keep
|
|
||||||
#include "wutil.h" // IWYU pragma: keep
|
|
||||||
#include "common.h"
|
|
||||||
#include "output.h"
|
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
|
#include "common.h"
|
||||||
|
#include "fallback.h" // IWYU pragma: keep
|
||||||
|
#include "output.h"
|
||||||
|
#include "wutil.h" // IWYU pragma: keep
|
||||||
|
|
||||||
static int writeb_internal(char c);
|
static int writeb_internal(char c);
|
||||||
|
|
||||||
/**
|
/// The function used for output.
|
||||||
The function used for output
|
|
||||||
*/
|
|
||||||
|
|
||||||
static int (*out)(char c) = &writeb_internal;
|
static int (*out)(char c) = &writeb_internal;
|
||||||
|
|
||||||
/**
|
/// Name of terminal.
|
||||||
Name of terminal
|
|
||||||
*/
|
|
||||||
static wcstring current_term;
|
static wcstring current_term;
|
||||||
|
|
||||||
/* Whether term256 and term24bit are supported */
|
/// Whether term256 and term24bit are supported.
|
||||||
static color_support_t color_support = 0;
|
static color_support_t color_support = 0;
|
||||||
|
|
||||||
|
void output_set_writer(int (*writer)(char)) {
|
||||||
void output_set_writer(int (*writer)(char))
|
CHECK(writer, );
|
||||||
{
|
|
||||||
CHECK(writer,);
|
|
||||||
out = writer;
|
out = writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
int (*output_get_writer())(char)
|
int (*output_get_writer())(char) { return out; }
|
||||||
{
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool term256_support_is_native(void)
|
static bool term256_support_is_native(void) {
|
||||||
{
|
// Return YES if we think the term256 support is "native" as opposed to forced.
|
||||||
/* Return YES if we think the term256 support is "native" as opposed to forced. */
|
|
||||||
return max_colors >= 256;
|
return max_colors >= 256;
|
||||||
}
|
}
|
||||||
|
|
||||||
color_support_t output_get_color_support(void)
|
color_support_t output_get_color_support(void) { return color_support; }
|
||||||
{
|
|
||||||
return color_support;
|
|
||||||
}
|
|
||||||
|
|
||||||
void output_set_color_support(color_support_t val)
|
void output_set_color_support(color_support_t val) { color_support = val; }
|
||||||
{
|
|
||||||
color_support = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char index_for_color(rgb_color_t c)
|
unsigned char index_for_color(rgb_color_t c) {
|
||||||
{
|
if (c.is_named() || !(output_get_color_support() & color_support_term256)) {
|
||||||
if (c.is_named() || ! (output_get_color_support() & color_support_term256))
|
|
||||||
{
|
|
||||||
return c.to_name_index();
|
return c.to_name_index();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return c.to_term256_index();
|
return c.to_term256_index();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool write_color_escape(char *todo, unsigned char idx, bool is_fg) {
|
||||||
static bool write_color_escape(char *todo, unsigned char idx, bool is_fg)
|
|
||||||
{
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
if (idx < 16 || term256_support_is_native())
|
if (idx < 16 || term256_support_is_native()) {
|
||||||
{
|
// Use tparm.
|
||||||
/* Use tparm */
|
|
||||||
writembs(tparm(todo, idx));
|
writembs(tparm(todo, idx));
|
||||||
result = true;
|
result = true;
|
||||||
}
|
} else {
|
||||||
else
|
// We are attempting to bypass the term here. Generate the ANSI escape sequence ourself.
|
||||||
{
|
|
||||||
/* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */
|
|
||||||
char stridx[128];
|
char stridx[128];
|
||||||
format_long_safe(stridx, idx);
|
format_long_safe(stridx, idx);
|
||||||
char buff[128] = "\x1b[";
|
char buff[128] = "\x1b[";
|
||||||
|
@ -107,10 +79,8 @@ static bool write_color_escape(char *todo, unsigned char idx, bool is_fg)
|
||||||
strcat(buff, "m");
|
strcat(buff, "m");
|
||||||
|
|
||||||
int (*writer)(char) = output_get_writer();
|
int (*writer)(char) = output_get_writer();
|
||||||
if (writer)
|
if (writer) {
|
||||||
{
|
for (size_t i = 0; buff[i]; i++) {
|
||||||
for (size_t i=0; buff[i]; i++)
|
|
||||||
{
|
|
||||||
writer(buff[i]);
|
writer(buff[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,70 +90,50 @@ static bool write_color_escape(char *todo, unsigned char idx, bool is_fg)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool write_foreground_color(unsigned char idx)
|
static bool write_foreground_color(unsigned char idx) {
|
||||||
{
|
if (set_a_foreground && set_a_foreground[0]) {
|
||||||
if (set_a_foreground && set_a_foreground[0])
|
|
||||||
{
|
|
||||||
return write_color_escape(set_a_foreground, idx, true);
|
return write_color_escape(set_a_foreground, idx, true);
|
||||||
}
|
} else if (set_foreground && set_foreground[0]) {
|
||||||
else if (set_foreground && set_foreground[0])
|
|
||||||
{
|
|
||||||
return write_color_escape(set_foreground, idx, true);
|
return write_color_escape(set_foreground, idx, true);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool write_background_color(unsigned char idx)
|
static bool write_background_color(unsigned char idx) {
|
||||||
{
|
if (set_a_background && set_a_background[0]) {
|
||||||
if (set_a_background && set_a_background[0])
|
|
||||||
{
|
|
||||||
return write_color_escape(set_a_background, idx, false);
|
return write_color_escape(set_a_background, idx, false);
|
||||||
}
|
} else if (set_background && set_background[0]) {
|
||||||
else if (set_background && set_background[0])
|
|
||||||
{
|
|
||||||
return write_color_escape(set_background, idx, false);
|
return write_color_escape(set_background, idx, false);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_color(rgb_color_t color, bool is_fg)
|
void write_color(rgb_color_t color, bool is_fg) {
|
||||||
{
|
bool supports_term24bit = !!(output_get_color_support() & color_support_term24bit);
|
||||||
bool supports_term24bit = !! (output_get_color_support() & color_support_term24bit);
|
if (!supports_term24bit || !color.is_rgb()) {
|
||||||
if (! supports_term24bit || ! color.is_rgb())
|
// Indexed or non-24 bit color.
|
||||||
{
|
|
||||||
/* Indexed or non-24 bit color */
|
|
||||||
unsigned char idx = index_for_color(color);
|
unsigned char idx = index_for_color(color);
|
||||||
(is_fg ? write_foreground_color : write_background_color)(idx);
|
(is_fg ? write_foreground_color : write_background_color)(idx);
|
||||||
}
|
} else {
|
||||||
else
|
// 24 bit! No tparm here, just ANSI escape sequences.
|
||||||
{
|
// Foreground: ^[38;2;<r>;<g>;<b>m
|
||||||
/* 24 bit! No tparm here, just ANSI escape sequences.
|
// Background: ^[48;2;<r>;<g>;<b>m
|
||||||
Foreground: ^[38;2;<r>;<g>;<b>m
|
|
||||||
Background: ^[48;2;<r>;<g>;<b>m
|
|
||||||
*/
|
|
||||||
color24_t rgb = color.to_color24();
|
color24_t rgb = color.to_color24();
|
||||||
char buff[128];
|
char buff[128];
|
||||||
snprintf(buff, sizeof buff, "\x1b[%u;2;%u;%u;%um", is_fg ? 38 : 48, rgb.rgb[0], rgb.rgb[1], rgb.rgb[2]);
|
snprintf(buff, sizeof buff, "\x1b[%u;2;%u;%u;%um", is_fg ? 38 : 48, rgb.rgb[0], rgb.rgb[1],
|
||||||
|
rgb.rgb[2]);
|
||||||
int (*writer)(char) = output_get_writer();
|
int (*writer)(char) = output_get_writer();
|
||||||
if (writer)
|
if (writer) {
|
||||||
{
|
for (size_t i = 0; buff[i]; i++) {
|
||||||
for (size_t i=0; buff[i]; i++)
|
|
||||||
{
|
|
||||||
writer(buff[i]);
|
writer(buff[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_color(rgb_color_t c, rgb_color_t c2)
|
void set_color(rgb_color_t c, rgb_color_t c2) {
|
||||||
{
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
wcstring tmp = c.description();
|
wcstring tmp = c.description();
|
||||||
wcstring tmp2 = c2.description();
|
wcstring tmp2 = c2.description();
|
||||||
|
@ -194,239 +144,180 @@ void set_color(rgb_color_t c, rgb_color_t c2)
|
||||||
const rgb_color_t normal = rgb_color_t::normal();
|
const rgb_color_t normal = rgb_color_t::normal();
|
||||||
static rgb_color_t last_color = rgb_color_t::normal();
|
static rgb_color_t last_color = rgb_color_t::normal();
|
||||||
static rgb_color_t last_color2 = rgb_color_t::normal();
|
static rgb_color_t last_color2 = rgb_color_t::normal();
|
||||||
static int was_bold=0;
|
static int was_bold = 0;
|
||||||
static int was_underline=0;
|
static int was_underline = 0;
|
||||||
int bg_set=0, last_bg_set=0;
|
int bg_set = 0, last_bg_set = 0;
|
||||||
|
|
||||||
int is_bold = 0;
|
int is_bold = 0;
|
||||||
int is_underline = 0;
|
int is_underline = 0;
|
||||||
|
|
||||||
/*
|
// Test if we have at least basic support for setting fonts, colors and related bits - otherwise
|
||||||
Test if we have at least basic support for setting fonts, colors
|
// just give up...
|
||||||
and related bits - otherwise just give up...
|
if (!exit_attribute_mode) {
|
||||||
*/
|
|
||||||
if (!exit_attribute_mode)
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
is_bold |= c.is_bold();
|
is_bold |= c.is_bold();
|
||||||
is_bold |= c2.is_bold();
|
is_bold |= c2.is_bold();
|
||||||
|
|
||||||
is_underline |= c.is_underline();
|
is_underline |= c.is_underline();
|
||||||
is_underline |= c2.is_underline();
|
is_underline |= c2.is_underline();
|
||||||
|
|
||||||
if (c.is_reset() || c2.is_reset())
|
if (c.is_reset() || c2.is_reset()) {
|
||||||
{
|
|
||||||
c = c2 = normal;
|
c = c2 = normal;
|
||||||
was_bold=0;
|
was_bold = 0;
|
||||||
was_underline=0;
|
was_underline = 0;
|
||||||
/*
|
// If we exit attibute mode, we must first set a color, or previously coloured text might
|
||||||
If we exit attibute mode, we must first set a color, or
|
// lose it's color. Terminals are weird...
|
||||||
previously coloured text might lose it's
|
|
||||||
color. Terminals are weird...
|
|
||||||
*/
|
|
||||||
write_foreground_color(0);
|
write_foreground_color(0);
|
||||||
writembs(exit_attribute_mode);
|
writembs(exit_attribute_mode);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (was_bold && !is_bold)
|
if (was_bold && !is_bold) {
|
||||||
{
|
// Only way to exit bold mode is a reset of all attributes.
|
||||||
/*
|
|
||||||
Only way to exit bold mode is a reset of all attributes.
|
|
||||||
*/
|
|
||||||
writembs(exit_attribute_mode);
|
writembs(exit_attribute_mode);
|
||||||
last_color = normal;
|
last_color = normal;
|
||||||
last_color2 = normal;
|
last_color2 = normal;
|
||||||
was_bold=0;
|
was_bold = 0;
|
||||||
was_underline=0;
|
was_underline = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!last_color2.is_normal() && !last_color2.is_reset())
|
if (!last_color2.is_normal() && !last_color2.is_reset()) {
|
||||||
{
|
|
||||||
// Background was set.
|
// Background was set.
|
||||||
last_bg_set = 1;
|
last_bg_set = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!c2.is_normal())
|
if (!c2.is_normal()) {
|
||||||
{
|
|
||||||
// Background is set.
|
// Background is set.
|
||||||
bg_set = 1;
|
bg_set = 1;
|
||||||
if (c == c2) c = (c2==rgb_color_t::white())?rgb_color_t::black():rgb_color_t::white();
|
if (c == c2) c = (c2 == rgb_color_t::white()) ? rgb_color_t::black() : rgb_color_t::white();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0))
|
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0)) {
|
||||||
{
|
if (bg_set && !last_bg_set) {
|
||||||
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.
|
||||||
/*
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
writembs(enter_bold_mode);
|
writembs(enter_bold_mode);
|
||||||
}
|
}
|
||||||
if (!bg_set && last_bg_set)
|
if (!bg_set && last_bg_set) {
|
||||||
{
|
// Background color changed and is no longer set, so we exit bold mode.
|
||||||
/*
|
|
||||||
Background color changed and is no longer set, so we
|
|
||||||
exit bold mode
|
|
||||||
*/
|
|
||||||
writembs(exit_attribute_mode);
|
writembs(exit_attribute_mode);
|
||||||
was_bold=0;
|
was_bold = 0;
|
||||||
was_underline=0;
|
was_underline = 0;
|
||||||
/*
|
// We don't know if exit_attribute_mode resets colors, so we set it to something known.
|
||||||
We don't know if exit_attribute_mode resets colors, so
|
if (write_foreground_color(0)) {
|
||||||
we set it to something known.
|
last_color = rgb_color_t::black();
|
||||||
*/
|
|
||||||
if (write_foreground_color(0))
|
|
||||||
{
|
|
||||||
last_color=rgb_color_t::black();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (last_color != c)
|
if (last_color != c) {
|
||||||
{
|
if (c.is_normal()) {
|
||||||
if (c.is_normal())
|
|
||||||
{
|
|
||||||
write_foreground_color(0);
|
write_foreground_color(0);
|
||||||
writembs(exit_attribute_mode);
|
writembs(exit_attribute_mode);
|
||||||
|
|
||||||
last_color2 = rgb_color_t::normal();
|
last_color2 = rgb_color_t::normal();
|
||||||
was_bold=0;
|
was_bold = 0;
|
||||||
was_underline=0;
|
was_underline = 0;
|
||||||
}
|
} else if (!c.is_special()) {
|
||||||
else if (! c.is_special())
|
|
||||||
{
|
|
||||||
write_color(c, true /* foreground */);
|
write_color(c, true /* foreground */);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
last_color = c;
|
last_color = c;
|
||||||
|
|
||||||
if (last_color2 != c2)
|
if (last_color2 != c2) {
|
||||||
{
|
if (c2.is_normal()) {
|
||||||
if (c2.is_normal())
|
|
||||||
{
|
|
||||||
write_background_color(0);
|
write_background_color(0);
|
||||||
|
|
||||||
writembs(exit_attribute_mode);
|
writembs(exit_attribute_mode);
|
||||||
if (! last_color.is_normal())
|
if (!last_color.is_normal()) {
|
||||||
{
|
|
||||||
write_color(last_color, true /* foreground */);
|
write_color(last_color, true /* foreground */);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
was_bold = 0;
|
||||||
was_bold=0;
|
was_underline = 0;
|
||||||
was_underline=0;
|
|
||||||
last_color2 = c2;
|
last_color2 = c2;
|
||||||
}
|
} else if (!c2.is_special()) {
|
||||||
else if (! c2.is_special())
|
|
||||||
{
|
|
||||||
write_color(c2, false /* not foreground */);
|
write_color(c2, false /* not foreground */);
|
||||||
last_color2 = c2;
|
last_color2 = c2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Lastly, we set bold mode and underline mode correctly.
|
||||||
Lastly, we set bold mode and underline mode correctly
|
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0) && !bg_set) {
|
||||||
*/
|
if (is_bold && !was_bold) {
|
||||||
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0) && !bg_set)
|
if (enter_bold_mode) {
|
||||||
{
|
|
||||||
if (is_bold && !was_bold)
|
|
||||||
{
|
|
||||||
if (enter_bold_mode)
|
|
||||||
{
|
|
||||||
writembs(tparm(enter_bold_mode));
|
writembs(tparm(enter_bold_mode));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
was_bold = is_bold;
|
was_bold = is_bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (was_underline && !is_underline)
|
if (was_underline && !is_underline) {
|
||||||
{
|
|
||||||
writembs(exit_underline_mode);
|
writembs(exit_underline_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!was_underline && is_underline)
|
if (!was_underline && is_underline) {
|
||||||
{
|
|
||||||
writembs(enter_underline_mode);
|
writembs(enter_underline_mode);
|
||||||
}
|
}
|
||||||
was_underline = is_underline;
|
was_underline = is_underline;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Default output method, simply calls write() on stdout.
|
||||||
Default output method, simply calls write() on stdout
|
static int writeb_internal(char c) {
|
||||||
*/
|
|
||||||
static int writeb_internal(char c)
|
|
||||||
{
|
|
||||||
write_loop(1, &c, 1);
|
write_loop(1, &c, 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int writeb(tputs_arg_t b)
|
int writeb(tputs_arg_t b) {
|
||||||
{
|
|
||||||
out(b);
|
out(b);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int writech(wint_t ch)
|
int writech(wint_t ch) {
|
||||||
{
|
char buff[MB_LEN_MAX + 1];
|
||||||
char buff[MB_LEN_MAX+1];
|
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
if (ch >= ENCODE_DIRECT_BASE && ch < ENCODE_DIRECT_BASE + 256)
|
if (ch >= ENCODE_DIRECT_BASE && ch < ENCODE_DIRECT_BASE + 256) {
|
||||||
{
|
|
||||||
buff[0] = ch - ENCODE_DIRECT_BASE;
|
buff[0] = ch - ENCODE_DIRECT_BASE;
|
||||||
len = 1;
|
len = 1;
|
||||||
}
|
} else if (MB_CUR_MAX == 1) // single-byte locale (C/POSIX/ISO-8859)
|
||||||
else if (MB_CUR_MAX == 1) // single-byte locale (C/POSIX/ISO-8859)
|
|
||||||
{
|
{
|
||||||
// If `wc` contains a wide character we emit a question-mark.
|
// If `wc` contains a wide character we emit a question-mark.
|
||||||
if (ch & ~0xFF)
|
if (ch & ~0xFF) {
|
||||||
{
|
|
||||||
ch = '?';
|
ch = '?';
|
||||||
}
|
}
|
||||||
buff[0] = ch;
|
buff[0] = ch;
|
||||||
len = 1;
|
len = 1;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
mbstate_t state = {};
|
mbstate_t state = {};
|
||||||
len = wcrtomb(buff, ch, &state);
|
len = wcrtomb(buff, ch, &state);
|
||||||
if (len == (size_t)-1)
|
if (len == (size_t)-1) {
|
||||||
{
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < len; i++)
|
for (size_t i = 0; i < len; i++) {
|
||||||
{
|
|
||||||
out(buff[i]);
|
out(buff[i]);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void writestr(const wchar_t *str)
|
void writestr(const wchar_t *str) {
|
||||||
{
|
CHECK(str, );
|
||||||
CHECK(str,);
|
|
||||||
|
|
||||||
if (MB_CUR_MAX == 1) // single-byte locale (C/POSIX/ISO-8859)
|
if (MB_CUR_MAX == 1) // single-byte locale (C/POSIX/ISO-8859)
|
||||||
{
|
{
|
||||||
while( *str )
|
while (*str) {
|
||||||
{
|
writech(*str++);
|
||||||
writech( *str++ );
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t len = wcstombs(0, str, 0); // figure amount of space needed
|
size_t len = wcstombs(0, str, 0); // figure amount of space needed
|
||||||
if (len == (size_t)-1)
|
if (len == (size_t)-1) {
|
||||||
{
|
|
||||||
debug(1, L"Tried to print invalid wide character string");
|
debug(1, L"Tried to print invalid wide character string");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -439,86 +330,65 @@ void writestr(const wchar_t *str)
|
||||||
else
|
else
|
||||||
buffer = new char[len];
|
buffer = new char[len];
|
||||||
|
|
||||||
wcstombs(buffer,
|
wcstombs(buffer, str, len);
|
||||||
str,
|
|
||||||
len);
|
|
||||||
|
|
||||||
/*
|
// Write the string.
|
||||||
Write
|
for (char *pos = buffer; *pos; pos++) {
|
||||||
*/
|
|
||||||
for (char *pos = buffer; *pos; pos++)
|
|
||||||
{
|
|
||||||
out(*pos);
|
out(*pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer != static_buffer)
|
if (buffer != static_buffer) delete[] buffer;
|
||||||
delete[] buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rgb_color_t best_color(const std::vector<rgb_color_t> &candidates, color_support_t support)
|
rgb_color_t best_color(const std::vector<rgb_color_t> &candidates, color_support_t support) {
|
||||||
{
|
if (candidates.empty()) {
|
||||||
if (candidates.empty())
|
|
||||||
{
|
|
||||||
return rgb_color_t::none();
|
return rgb_color_t::none();
|
||||||
}
|
}
|
||||||
|
|
||||||
rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none();
|
rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none();
|
||||||
for (size_t i=0; i < candidates.size(); i++)
|
for (size_t i = 0; i < candidates.size(); i++) {
|
||||||
{
|
|
||||||
const rgb_color_t &color = candidates.at(i);
|
const rgb_color_t &color = candidates.at(i);
|
||||||
if (first_rgb.is_none() && color.is_rgb())
|
if (first_rgb.is_none() && color.is_rgb()) {
|
||||||
{
|
|
||||||
first_rgb = color;
|
first_rgb = color;
|
||||||
}
|
}
|
||||||
if (first_named.is_none() && color.is_named())
|
if (first_named.is_none() && color.is_named()) {
|
||||||
{
|
|
||||||
first_named = color;
|
first_named = color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If we have both RGB and named colors, then prefer rgb if term256 is supported
|
// If we have both RGB and named colors, then prefer rgb if term256 is supported.
|
||||||
rgb_color_t result = rgb_color_t::none();
|
rgb_color_t result = rgb_color_t::none();
|
||||||
bool has_term256 = !! (support & color_support_term256);
|
bool has_term256 = !!(support & color_support_term256);
|
||||||
if ((!first_rgb.is_none() && has_term256) || first_named.is_none())
|
if ((!first_rgb.is_none() && has_term256) || first_named.is_none()) {
|
||||||
{
|
|
||||||
result = first_rgb;
|
result = first_rgb;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
result = first_named;
|
result = first_named;
|
||||||
}
|
}
|
||||||
if (result.is_none())
|
if (result.is_none()) {
|
||||||
{
|
|
||||||
result = candidates.at(0);
|
result = candidates.at(0);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This code should be refactored to enable sharing with builtin_set_color */
|
// This code should be refactored to enable sharing with builtin_set_color.
|
||||||
rgb_color_t parse_color(const wcstring &val, bool is_background)
|
rgb_color_t parse_color(const wcstring &val, bool is_background) {
|
||||||
{
|
int is_bold = 0;
|
||||||
int is_bold=0;
|
int is_underline = 0;
|
||||||
int is_underline=0;
|
|
||||||
|
|
||||||
std::vector<rgb_color_t> candidates;
|
std::vector<rgb_color_t> candidates;
|
||||||
|
|
||||||
wcstring_list_t el;
|
wcstring_list_t el;
|
||||||
tokenize_variable_array(val, el);
|
tokenize_variable_array(val, el);
|
||||||
|
|
||||||
for (size_t j=0; j < el.size(); j++)
|
for (size_t j = 0; j < el.size(); j++) {
|
||||||
{
|
|
||||||
const wcstring &next = el.at(j);
|
const wcstring &next = el.at(j);
|
||||||
wcstring color_name;
|
wcstring color_name;
|
||||||
if (is_background)
|
if (is_background) {
|
||||||
{
|
// Look for something like "--background=red".
|
||||||
// look for something like "--background=red"
|
|
||||||
const wcstring prefix = L"--background=";
|
const wcstring prefix = L"--background=";
|
||||||
if (string_prefixes_string(prefix, next))
|
if (string_prefixes_string(prefix, next)) {
|
||||||
{
|
|
||||||
color_name = wcstring(next, prefix.size());
|
color_name = wcstring(next, prefix.size());
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
if (next == L"--bold" || next == L"-o")
|
if (next == L"--bold" || next == L"-o")
|
||||||
is_bold = true;
|
is_bold = true;
|
||||||
else if (next == L"--underline" || next == L"-u")
|
else if (next == L"--underline" || next == L"-u")
|
||||||
|
@ -527,19 +397,16 @@ rgb_color_t parse_color(const wcstring &val, bool is_background)
|
||||||
color_name = next;
|
color_name = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! color_name.empty())
|
if (!color_name.empty()) {
|
||||||
{
|
|
||||||
rgb_color_t color = rgb_color_t(color_name);
|
rgb_color_t color = rgb_color_t(color_name);
|
||||||
if (! color.is_none())
|
if (!color.is_none()) {
|
||||||
{
|
|
||||||
candidates.push_back(color);
|
candidates.push_back(color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rgb_color_t result = best_color(candidates, output_get_color_support());
|
rgb_color_t result = best_color(candidates, output_get_color_support());
|
||||||
|
|
||||||
if (result.is_none())
|
if (result.is_none()) result = rgb_color_t::normal();
|
||||||
result = rgb_color_t::normal();
|
|
||||||
|
|
||||||
result.set_bold(is_bold);
|
result.set_bold(is_bold);
|
||||||
result.set_underline(is_underline);
|
result.set_underline(is_underline);
|
||||||
|
@ -552,29 +419,18 @@ rgb_color_t parse_color(const wcstring &val, bool is_background)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void output_set_term(const wcstring &term)
|
void output_set_term(const wcstring &term) { current_term.assign(term); }
|
||||||
{
|
|
||||||
current_term.assign(term);
|
|
||||||
}
|
|
||||||
|
|
||||||
const wchar_t *output_get_term()
|
const wchar_t *output_get_term() {
|
||||||
{
|
|
||||||
return current_term.empty() ? L"<unknown>" : current_term.c_str();
|
return current_term.empty() ? L"<unknown>" : current_term.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line)
|
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line) {
|
||||||
{
|
if (mbs != NULL) {
|
||||||
if (mbs != NULL)
|
|
||||||
{
|
|
||||||
tputs(mbs, 1, &writeb);
|
tputs(mbs, 1, &writeb);
|
||||||
}
|
} else {
|
||||||
else
|
debug(0, _(L"Tried to use terminfo string %s on line %ld of %s, which is undefined in "
|
||||||
{
|
L"terminal of type \"%ls\". Please report this error to %s"),
|
||||||
debug( 0, _(L"Tried to use terminfo string %s on line %ld of %s, which is undefined in terminal of type \"%ls\". Please report this error to %s"),
|
mbs_name, line, file, output_get_term(), PACKAGE_BUGREPORT);
|
||||||
mbs_name,
|
|
||||||
line,
|
|
||||||
file,
|
|
||||||
output_get_term(),
|
|
||||||
PACKAGE_BUGREPORT);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
131
src/output.h
131
src/output.h
|
@ -1,25 +1,20 @@
|
||||||
/** \file output.h
|
// Generic output functions.
|
||||||
Generic output functions
|
//
|
||||||
*/
|
// Constants for various character classifications. Each character of a command string can be
|
||||||
/**
|
// classified as one of the following types.
|
||||||
Constants for various character classifications. Each character of a command string can be classified as one of the following types.
|
|
||||||
*/
|
|
||||||
#ifndef FISH_OUTPUT_H
|
#ifndef FISH_OUTPUT_H
|
||||||
#define FISH_OUTPUT_H
|
#define FISH_OUTPUT_H
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "color.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "fallback.h" // IWYU pragma: keep
|
#include "fallback.h" // IWYU pragma: keep
|
||||||
#include "color.h"
|
|
||||||
|
|
||||||
/**
|
/// Constants for various colors as used by the set_color function.
|
||||||
Constants for various colors as used by the set_color function.
|
enum {
|
||||||
*/
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
FISH_COLOR_BLACK,
|
FISH_COLOR_BLACK,
|
||||||
FISH_COLOR_RED,
|
FISH_COLOR_RED,
|
||||||
FISH_COLOR_GREEN,
|
FISH_COLOR_GREEN,
|
||||||
|
@ -32,100 +27,72 @@ enum
|
||||||
FISH_COLOR_RESET
|
FISH_COLOR_RESET
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/// Sets the fg and bg color. May be called as often as you like, since if the new color is the same
|
||||||
Sets the fg and bg color. May be called as often as you like, since
|
/// as the previous, nothing will be written. Negative values for set_color will also be ignored.
|
||||||
if the new color is the same as the previous, nothing will be
|
/// Since the terminfo string this function emits can potentially cause the screen to flicker, the
|
||||||
written. Negative values for set_color will also be ignored. Since
|
/// function takes care to write as little as possible.
|
||||||
the terminfo string this function emits can potentially cause the
|
///
|
||||||
screen to flicker, the function takes care to write as little as
|
/// Possible values for color are any form the FISH_COLOR_* enum and FISH_COLOR_RESET.
|
||||||
possible.
|
/// FISH_COLOR_RESET will perform an exit_attribute_mode, even if set_color thinks it is already in
|
||||||
|
/// FISH_COLOR_NORMAL mode.
|
||||||
Possible values for color are any form the FISH_COLOR_* enum
|
///
|
||||||
and FISH_COLOR_RESET. FISH_COLOR_RESET will perform an
|
/// In order to set the color to normal, three terminfo strings may have to be written.
|
||||||
exit_attribute_mode, even if set_color thinks it is already in
|
///
|
||||||
FISH_COLOR_NORMAL mode.
|
/// - 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.
|
||||||
In order to set the color to normal, three terminfo strings may
|
///
|
||||||
have to be written.
|
/// - After that we write the exit_attribute_mode string to reset all color attributes.
|
||||||
|
///
|
||||||
- First a string to set the color, such as set_a_foreground. This
|
/// - Lastly we may need to write set_a_background or set_a_foreground to set the other half of the
|
||||||
is needed because otherwise the previous strings colors might be
|
/// color pair to what it should be.
|
||||||
removed as well.
|
///
|
||||||
|
/// \param c Foreground color.
|
||||||
- After that we write the exit_attribute_mode string to reset all
|
/// \param c2 Background color.
|
||||||
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.
|
|
||||||
|
|
||||||
\param c Foreground color.
|
|
||||||
\param c2 Background color.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
void set_color(rgb_color_t c, rgb_color_t c2);
|
void set_color(rgb_color_t c, rgb_color_t c2);
|
||||||
|
|
||||||
/**
|
/// Write specified multibyte string.
|
||||||
Write specified multibyte string
|
|
||||||
*/
|
|
||||||
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line);
|
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line);
|
||||||
#define writembs(mbs) writembs_check((mbs), #mbs, __FILE__, __LINE__)
|
#define writembs(mbs) writembs_check((mbs), #mbs, __FILE__, __LINE__)
|
||||||
|
|
||||||
/**
|
/// Write a wide character using the output method specified using output_set_writer().
|
||||||
Write a wide character using the output method specified using output_set_writer().
|
|
||||||
*/
|
|
||||||
int writech(wint_t ch);
|
int writech(wint_t ch);
|
||||||
|
|
||||||
/**
|
/// Write a wide character string to FD 1.
|
||||||
Write a wide character string to FD 1.
|
|
||||||
*/
|
|
||||||
void writestr(const wchar_t *str);
|
void writestr(const wchar_t *str);
|
||||||
|
|
||||||
/**
|
/// Return the internal color code representing the specified color.
|
||||||
Return the internal color code representing the specified color
|
|
||||||
*/
|
|
||||||
rgb_color_t parse_color(const wcstring &val, bool is_background);
|
rgb_color_t parse_color(const wcstring &val, bool is_background);
|
||||||
|
|
||||||
/**
|
/// This is for writing process notification messages. Has to write to stdout, so clr_eol and such
|
||||||
This is for writing process notification messages. Has to write to
|
/// functions will work correctly. Not an issue since this function is only used in interactive mode
|
||||||
stdout, so clr_eol and such functions will work correctly. Not an
|
/// anyway.
|
||||||
issue since this function is only used in interactive mode anyway.
|
|
||||||
*/
|
|
||||||
int writeb(tputs_arg_t b);
|
int writeb(tputs_arg_t b);
|
||||||
|
|
||||||
/**
|
/// Set the function used for writing in move_cursor, writespace and set_color and all other output
|
||||||
Set the function used for writing in move_cursor, writespace and
|
/// functions in this library. By default, the write call is used to give completely unbuffered
|
||||||
set_color and all other output functions in this library. By
|
/// output to stdout.
|
||||||
default, the write call is used to give completely unbuffered
|
|
||||||
output to stdout.
|
|
||||||
*/
|
|
||||||
void output_set_writer(int (*writer)(char));
|
void output_set_writer(int (*writer)(char));
|
||||||
|
|
||||||
/**
|
/// Return the current output writer.
|
||||||
Return the current output writer
|
int (*output_get_writer())(char);
|
||||||
*/
|
|
||||||
int (*output_get_writer())(char) ;
|
|
||||||
|
|
||||||
/** Set the terminal name */
|
/// Set the terminal name.
|
||||||
void output_set_term(const wcstring &term);
|
void output_set_term(const wcstring &term);
|
||||||
|
|
||||||
/** Return the terminal name */
|
/// Return the terminal name.
|
||||||
const wchar_t *output_get_term();
|
const wchar_t *output_get_term();
|
||||||
|
|
||||||
/** Sets what colors are supported */
|
/// Sets what colors are supported.
|
||||||
enum
|
enum { color_support_term256 = 1 << 0, color_support_term24bit = 1 << 1 };
|
||||||
{
|
|
||||||
color_support_term256 = 1 << 0,
|
|
||||||
color_support_term24bit = 1 << 1
|
|
||||||
};
|
|
||||||
typedef unsigned int color_support_t;
|
typedef unsigned int color_support_t;
|
||||||
color_support_t output_get_color_support();
|
color_support_t output_get_color_support();
|
||||||
void output_set_color_support(color_support_t support);
|
void output_set_color_support(color_support_t support);
|
||||||
|
|
||||||
/** Given a list of rgb_color_t, pick the "best" one, as determined by the color support. Returns rgb_color_t::none() if empty */
|
/// Given a list of rgb_color_t, pick the "best" one, as determined by the color support. Returns
|
||||||
|
/// rgb_color_t::none() if empty.
|
||||||
rgb_color_t best_color(const std::vector<rgb_color_t> &colors, color_support_t support);
|
rgb_color_t best_color(const std::vector<rgb_color_t> &colors, color_support_t support);
|
||||||
|
|
||||||
/* Exported for builtin_set_color's usage only */
|
// Exported for builtin_set_color's usage only.
|
||||||
void write_color(rgb_color_t color, bool is_fg);
|
void write_color(rgb_color_t color, bool is_fg);
|
||||||
unsigned char index_for_color(rgb_color_t c);
|
unsigned char index_for_color(rgb_color_t c);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue