fish-shell/src/output.h

61 lines
1.7 KiB
C
Raw Normal View History

// Generic output functions.
//
// Constants for various character classifications. Each character of a command string can be
// classified as one of the following types.
#ifndef FISH_OUTPUT_H
#define FISH_OUTPUT_H
#include <stddef.h>
#include <vector>
#include "color.h"
#include "fallback.h" // IWYU pragma: keep
class env_var_t;
/// Constants for various colors as used by the set_color function.
enum {
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-22 01:55:28 +08:00
FISH_COLOR_BLACK, // 0
FISH_COLOR_RED, // 1
FISH_COLOR_GREEN, // 2
FISH_COLOR_YELLOW, // 3
FISH_COLOR_BLUE, // 4
FISH_COLOR_MAGENTA, // 5
FISH_COLOR_CYAN, // 6
FISH_COLOR_WHITE, // 7
FISH_COLOR_NORMAL, // 8 terminal default
FISH_COLOR_RESET // 9
2015-08-11 16:00:05 +08:00
};
2012-02-12 09:07:56 +08:00
void set_color(rgb_color_t c, rgb_color_t c2);
void writembs_check(char *mbs, const char *mbs_name, bool critical, const char *file, long line);
#define writembs(mbs) writembs_check((mbs), #mbs, true, __FILE__, __LINE__)
#define writembs_nofail(mbs) writembs_check((mbs), #mbs, false, __FILE__, __LINE__)
int writech(wint_t ch);
void writestr(const wchar_t *str);
rgb_color_t parse_color(const env_var_t &val, bool is_background);
int writeb(tputs_arg_t b);
void output_set_writer(int (*writer)(char));
int (*output_get_writer())(char);
/// Sets what colors are supported.
enum { color_support_term256 = 1 << 0, color_support_term24bit = 1 << 1 };
typedef unsigned int color_support_t;
color_support_t output_get_color_support();
void output_set_color_support(color_support_t support);
rgb_color_t best_color(const std::vector<rgb_color_t> &colors, color_support_t support);
bool write_color(rgb_color_t color, bool is_fg);
unsigned char index_for_color(rgb_color_t c);
#endif