fish-shell/src/output.cpp

148 lines
4.7 KiB
C++
Raw Normal View History

// Generic output functions.
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#if HAVE_CURSES_H
#include <curses.h> // IWYU pragma: keep
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_NCURSES_CURSES_H
#include <ncurses/curses.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#endif
#include <cwchar>
#include <mutex>
2015-07-25 23:14:25 +08:00
#include <string>
#include <vector>
#include "color.h"
#include "common.h"
#include "env.h"
#include "fallback.h" // IWYU pragma: keep
2019-05-27 15:56:53 -07:00
#include "flog.h"
#include "maybe.h"
#include "output.h"
#include "threads.rs.h"
#include "wcstringutil.h"
#include "wutil.h" // IWYU pragma: keep
Improve compatibility with 0-16 color terminals. Fish assumed that it could use tparm to emit escapes to set colors as long as the color was under 16 or max_colors from terminfo was 256:: if (idx < 16 || term256_support_is_native()) { // Use tparm to emit color escape writembs(tparm(todo, idx); If a terminal has max_colors = 8, here is what happenened, except inside fish: > env TERM=xterm tput setaf 7 | xxd 00000000: 1b5b 3337 6d .[37m > env TERM=xterm tput setaf 9 | xxd 00000000: 1b5b 3338 6d .[39m The first escape is good, that second escape is not valid. Bright colors should start at \e[90m: > env TERM=xterm-16color tput setaf 9 | xxd 00000000: 1b5b 3931 6d .[91m This is what caused "white" not to work in #3176 in Terminal.app, and obviously isn't good for real low-color terminals either. So we replace the term256_support_is_native(), which just checked if max_colors is 256 or not, with a function that takes an argument and checks terminfo for that to see if tparm can handle it. We only use this test, because otherwise, tparm should be expected to output garbage: /// Returns true if we think tparm can handle outputting a color index static bool term_supports_color_natively(unsigned int c) { return max_colors >= c; } ... if (term_supports_color_natively(idx) { And if terminfo can't do it, the "forced" escapes no longer use the fancy format when handling colors under 16, as this is not going to be compatible with low color terminals. The code before used: else { char buff[16] = ""; snprintf(buff, sizeof buff, "\x1b[%d;5;%dm", is_fg ? 38 : 48, idx); I added an intermediate format for colors 0-15: else { // We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. char buff[16] = ""; if (idx < 16) { snprintf(buff, sizeof buff, "\x1b[%dm", ((idx > 7) ? 82 : 30) + idx + !is_fg * 10); } else { snprintf(buff, sizeof buff, "\x1b[%d;5;%dm", is_fg ? 38 : 48, idx); } Restores harmony to white, brwhite, brblack, black color names. We don't want "white" to refer to color color #16, but to the standard color #8. #16 is "brwhite". Move comments from output.h to output.cpp Nuke the config.fish set_color hack for linux VTs. Sync up our various incomplete color lists and fix all color values. Colors 0-8 are assumed to be brights - e.g. red was FF0000. Perplexing! Using this table: <http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html> Fixes #3176
2016-07-21 10:55:28 -07:00
/// Given a list of rgb_color_t, pick the "best" one, as determined by the color support. Returns
/// rgb_color_t::none() if empty.
/// TODO: This is duplicated with Rust.
rgb_color_t best_color(const std::vector<rgb_color_t> &candidates, color_support_t support) {
if (candidates.empty()) {
return rgb_color_t::none();
}
rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none();
for (const auto &color : 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.
rgb_color_t result = rgb_color_t::none();
bool has_term256 = static_cast<bool>(support & color_support_term256);
if ((!first_rgb.is_none() && has_term256) || first_named.is_none()) {
result = first_rgb;
} else {
result = first_named;
}
if (result.is_none()) {
result = candidates.at(0);
}
return result;
}
Improve compatibility with 0-16 color terminals. Fish assumed that it could use tparm to emit escapes to set colors as long as the color was under 16 or max_colors from terminfo was 256:: if (idx < 16 || term256_support_is_native()) { // Use tparm to emit color escape writembs(tparm(todo, idx); If a terminal has max_colors = 8, here is what happenened, except inside fish: > env TERM=xterm tput setaf 7 | xxd 00000000: 1b5b 3337 6d .[37m > env TERM=xterm tput setaf 9 | xxd 00000000: 1b5b 3338 6d .[39m The first escape is good, that second escape is not valid. Bright colors should start at \e[90m: > env TERM=xterm-16color tput setaf 9 | xxd 00000000: 1b5b 3931 6d .[91m This is what caused "white" not to work in #3176 in Terminal.app, and obviously isn't good for real low-color terminals either. So we replace the term256_support_is_native(), which just checked if max_colors is 256 or not, with a function that takes an argument and checks terminfo for that to see if tparm can handle it. We only use this test, because otherwise, tparm should be expected to output garbage: /// Returns true if we think tparm can handle outputting a color index static bool term_supports_color_natively(unsigned int c) { return max_colors >= c; } ... if (term_supports_color_natively(idx) { And if terminfo can't do it, the "forced" escapes no longer use the fancy format when handling colors under 16, as this is not going to be compatible with low color terminals. The code before used: else { char buff[16] = ""; snprintf(buff, sizeof buff, "\x1b[%d;5;%dm", is_fg ? 38 : 48, idx); I added an intermediate format for colors 0-15: else { // We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. char buff[16] = ""; if (idx < 16) { snprintf(buff, sizeof buff, "\x1b[%dm", ((idx > 7) ? 82 : 30) + idx + !is_fg * 10); } else { snprintf(buff, sizeof buff, "\x1b[%d;5;%dm", is_fg ? 38 : 48, idx); } Restores harmony to white, brwhite, brblack, black color names. We don't want "white" to refer to color color #16, but to the standard color #8. #16 is "brwhite". Move comments from output.h to output.cpp Nuke the config.fish set_color hack for linux VTs. Sync up our various incomplete color lists and fix all color values. Colors 0-8 are assumed to be brights - e.g. red was FF0000. Perplexing! Using this table: <http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html> Fixes #3176
2016-07-21 10:55:28 -07:00
/// 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.
/// TODO: This is duplicated with Rust.
rgb_color_t parse_color(const env_var_t &var, bool is_background) {
2020-09-24 17:21:49 +02:00
bool is_bold = false;
bool is_underline = false;
bool is_italics = false;
bool is_dim = false;
bool is_reverse = false;
std::vector<rgb_color_t> candidates;
const wchar_t *prefix = L"--background=";
// wcslen is not available as constexpr
size_t prefix_len = wcslen(prefix);
bool next_is_background = false;
2019-09-21 19:36:56 -07:00
wcstring color_name;
auto vals = var.as_list();
for (const wcstring &next : vals) {
2019-09-21 19:36:56 -07:00
color_name.clear();
if (is_background) {
if (color_name.empty() && next_is_background) {
color_name = next;
next_is_background = false;
} else if (string_prefixes_string(prefix, next)) {
// Look for something like "--background=red".
color_name = wcstring(next, prefix_len);
} else if (next == L"--background" || next == L"-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 == L"--reverse" || next == L"-r") {
// Reverse should be meaningful in either context
is_reverse = true;
} else if (string_prefixes_string(L"-b", next)) {
// Look for something like "-bred".
// Yes, that length is hardcoded.
color_name = wcstring(next, 2);
}
} else {
2012-02-11 17:07:56 -08:00
if (next == L"--bold" || next == L"-o")
is_bold = true;
else if (next == L"--underline" || next == L"-u")
is_underline = true;
else if (next == L"--italics" || next == L"-i")
is_italics = true;
else if (next == L"--dim" || next == L"-d")
is_dim = true;
else if (next == L"--reverse" || next == L"-r")
is_reverse = true;
2012-02-11 17:07:56 -08:00
else
color_name = next;
}
if (!color_name.empty()) {
rgb_color_t color = rgb_color_t(color_name);
if (!color.is_none()) {
candidates.push_back(color);
}
2012-02-11 17:07:56 -08:00
}
}
rgb_color_t result = best_color(candidates, output_get_color_support());
if (result.is_none()) result = rgb_color_t::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);
2012-02-11 17:07:56 -08:00
return result;
}
void writembs_nofail(outputter_t &outp, const char *str) {
assert(str != nullptr && "Null string");
outp.writembs(str);
}
void writembs(outputter_t &outp, const char *str) { writembs_nofail(outp, str); }