2016-05-02 05:29:21 +00:00
|
|
|
// Generic output functions.
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-05-02 05:29:21 +00:00
|
|
|
#include <stdlib.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <string.h>
|
|
|
|
#if HAVE_NCURSES_H
|
|
|
|
#include <ncurses.h>
|
2014-12-07 08:41:15 +00:00
|
|
|
#elif HAVE_NCURSES_CURSES_H
|
|
|
|
#include <ncurses/curses.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#else
|
|
|
|
#include <curses.h>
|
|
|
|
#endif
|
2006-01-19 12:22:07 +00:00
|
|
|
#if HAVE_TERM_H
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <term.h>
|
2006-01-19 12:22:07 +00:00
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <limits.h>
|
2016-05-02 05:29:21 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <memory>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <string>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <vector>
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
#include "color.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2016-06-02 03:03:50 +00:00
|
|
|
#include "env.h"
|
2016-05-02 05:29:21 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "output.h"
|
2016-05-02 05:29:21 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
static int writeb_internal(char c);
|
2006-02-16 14:21:00 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
/// The function used for output.
|
2016-05-29 05:28:26 +00:00
|
|
|
static int (*out)(char c) = writeb_internal;
|
2006-02-16 13:40:25 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
/// Whether term256 and term24bit are supported.
|
2014-09-19 22:37:31 +00:00
|
|
|
static color_support_t color_support = 0;
|
2012-03-05 18:44:08 +00:00
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// Set the function used for writing in move_cursor, writespace and set_color and all other output
|
|
|
|
/// functions in this library. By default, the write call is used to give completely unbuffered
|
|
|
|
/// output to stdout.
|
2016-05-02 05:29:21 +00:00
|
|
|
void output_set_writer(int (*writer)(char)) {
|
|
|
|
CHECK(writer, );
|
2012-11-19 00:30:30 +00:00
|
|
|
out = writer;
|
2006-02-16 13:40:25 +00:00
|
|
|
}
|
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// Return the current output writer.
|
2016-05-02 05:29:21 +00:00
|
|
|
int (*output_get_writer())(char) { return out; }
|
2006-10-01 16:02:58 +00:00
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// 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; }
|
2012-03-05 21:39:01 +00:00
|
|
|
|
2016-07-19 15:01:01 +00:00
|
|
|
color_support_t output_get_color_support(void) { return color_support; }
|
2012-03-05 18:44:08 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
void output_set_color_support(color_support_t val) { color_support = val; }
|
2012-02-13 02:05:59 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
unsigned char index_for_color(rgb_color_t c) {
|
|
|
|
if (c.is_named() || !(output_get_color_support() & color_support_term256)) {
|
2012-02-13 02:05:59 +00:00
|
|
|
return c.to_name_index();
|
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return c.to_term256_index();
|
2012-02-13 02:05:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
static bool write_color_escape(char *todo, unsigned char idx, bool is_fg) {
|
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 17:55:28 +00:00
|
|
|
if (term_supports_color_natively(idx)) {
|
2016-06-12 18:22:31 +00:00
|
|
|
// Use tparm to emit color escape.
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(tparm(todo, idx));
|
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 17:55:28 +00:00
|
|
|
return true;
|
2016-05-02 05:29:21 +00:00
|
|
|
} else {
|
|
|
|
// We are attempting to bypass the term here. Generate the ANSI escape sequence ourself.
|
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 17:55:28 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-04-01 00:24:11 +00:00
|
|
|
int (*writer)(char) = output_get_writer();
|
2016-05-02 05:29:21 +00:00
|
|
|
if (writer) {
|
|
|
|
for (size_t i = 0; buff[i]; i++) {
|
2012-04-01 00:24:11 +00:00
|
|
|
writer(buff[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
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 17:55:28 +00:00
|
|
|
return true;
|
2012-03-05 21:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
static bool write_foreground_color(unsigned char idx) {
|
|
|
|
if (set_a_foreground && set_a_foreground[0]) {
|
2014-09-20 01:44:18 +00:00
|
|
|
return write_color_escape(set_a_foreground, idx, true);
|
2016-05-02 05:29:21 +00:00
|
|
|
} else if (set_foreground && set_foreground[0]) {
|
2014-09-20 01:44:18 +00:00
|
|
|
return write_color_escape(set_foreground, idx, true);
|
2012-03-05 21:39:01 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return false;
|
2012-03-05 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
static bool write_background_color(unsigned char idx) {
|
|
|
|
if (set_a_background && set_a_background[0]) {
|
2014-09-20 01:44:18 +00:00
|
|
|
return write_color_escape(set_a_background, idx, false);
|
2016-05-02 05:29:21 +00:00
|
|
|
} else if (set_background && set_background[0]) {
|
2014-09-20 01:44:18 +00:00
|
|
|
return write_color_escape(set_background, idx, false);
|
2012-03-05 21:39:01 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return false;
|
2012-03-05 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
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 17:55:28 +00:00
|
|
|
// Exported for builtin_set_color's usage only.
|
2016-05-02 05:29:21 +00:00
|
|
|
void write_color(rgb_color_t color, bool is_fg) {
|
|
|
|
bool supports_term24bit = !!(output_get_color_support() & color_support_term24bit);
|
|
|
|
if (!supports_term24bit || !color.is_rgb()) {
|
|
|
|
// Indexed or non-24 bit color.
|
2014-09-20 01:44:18 +00:00
|
|
|
unsigned char idx = index_for_color(color);
|
|
|
|
(is_fg ? write_foreground_color : write_background_color)(idx);
|
2016-05-02 05:29:21 +00:00
|
|
|
} else {
|
|
|
|
// 24 bit! No tparm here, just ANSI escape sequences.
|
|
|
|
// Foreground: ^[38;2;<r>;<g>;<b>m
|
|
|
|
// Background: ^[48;2;<r>;<g>;<b>m
|
2014-09-20 01:44:18 +00:00
|
|
|
color24_t rgb = color.to_color24();
|
|
|
|
char buff[128];
|
2016-05-29 05:28:26 +00:00
|
|
|
snprintf(buff, sizeof buff, "\x1b[%d;2;%u;%u;%um", is_fg ? 38 : 48, rgb.rgb[0], rgb.rgb[1],
|
2016-05-02 05:29:21 +00:00
|
|
|
rgb.rgb[2]);
|
2014-09-20 01:44:18 +00:00
|
|
|
int (*writer)(char) = output_get_writer();
|
2016-05-02 05:29:21 +00:00
|
|
|
if (writer) {
|
|
|
|
for (size_t i = 0; buff[i]; i++) {
|
2014-09-20 01:44:18 +00:00
|
|
|
writer(buff[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 21:39:01 +00:00
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// 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 color are any form the FISH_COLOR_* enum and FISH_COLOR_RESET.
|
|
|
|
/// FISH_COLOR_RESET will perform an exit_attribute_mode, even if set_color thinks it is already in
|
|
|
|
/// FISH_COLOR_NORMAL mode.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// \param c Foreground color.
|
|
|
|
/// \param c2 Background color.
|
2016-05-02 05:29:21 +00:00
|
|
|
void set_color(rgb_color_t c, rgb_color_t c2) {
|
2012-02-13 02:05:59 +00:00
|
|
|
#if 0
|
|
|
|
wcstring tmp = c.description();
|
|
|
|
wcstring tmp2 = c2.description();
|
|
|
|
printf("set_color %ls : %ls\n", tmp.c_str(), tmp2.c_str());
|
2012-11-18 10:23:22 +00:00
|
|
|
#endif
|
2012-02-12 01:07:56 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-12 01:07:56 +00:00
|
|
|
const rgb_color_t normal = rgb_color_t::normal();
|
|
|
|
static rgb_color_t last_color = rgb_color_t::normal();
|
2012-11-19 00:30:30 +00:00
|
|
|
static rgb_color_t last_color2 = rgb_color_t::normal();
|
2016-05-02 05:29:21 +00:00
|
|
|
static int was_bold = 0;
|
|
|
|
static int was_underline = 0;
|
|
|
|
int bg_set = 0, last_bg_set = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
int is_bold = 0;
|
|
|
|
int is_underline = 0;
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
// Test if we have at least basic support for setting fonts, colors and related bits - otherwise
|
|
|
|
// just give up...
|
|
|
|
if (!exit_attribute_mode) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_bold |= c.is_bold();
|
|
|
|
is_bold |= c2.is_bold();
|
|
|
|
|
|
|
|
is_underline |= c.is_underline();
|
|
|
|
is_underline |= c2.is_underline();
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (c.is_reset() || c2.is_reset()) {
|
2012-11-19 00:30:30 +00:00
|
|
|
c = c2 = normal;
|
2016-05-02 05:29:21 +00:00
|
|
|
was_bold = 0;
|
|
|
|
was_underline = 0;
|
|
|
|
// If we exit attibute mode, we must first set a color, or previously coloured text might
|
|
|
|
// lose it's color. Terminals are weird...
|
2012-03-05 21:39:01 +00:00
|
|
|
write_foreground_color(0);
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(exit_attribute_mode);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (was_bold && !is_bold) {
|
|
|
|
// Only way to exit bold mode is a reset of all attributes.
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(exit_attribute_mode);
|
|
|
|
last_color = normal;
|
|
|
|
last_color2 = normal;
|
2016-05-02 05:29:21 +00:00
|
|
|
was_bold = 0;
|
|
|
|
was_underline = 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (!last_color2.is_normal() && !last_color2.is_reset()) {
|
2016-04-04 23:55:40 +00:00
|
|
|
// Background was set.
|
|
|
|
last_bg_set = 1;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (!c2.is_normal()) {
|
2016-04-04 23:55:40 +00:00
|
|
|
// Background is set.
|
|
|
|
bg_set = 1;
|
2016-05-02 05:29:21 +00:00
|
|
|
if (c == c2) c = (c2 == rgb_color_t::white()) ? rgb_color_t::black() : rgb_color_t::white();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0)) {
|
|
|
|
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.
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(enter_bold_mode);
|
|
|
|
}
|
2016-05-02 05:29:21 +00:00
|
|
|
if (!bg_set && last_bg_set) {
|
|
|
|
// Background color changed and is no longer set, so we exit bold mode.
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(exit_attribute_mode);
|
2016-05-02 05:29:21 +00:00
|
|
|
was_bold = 0;
|
|
|
|
was_underline = 0;
|
|
|
|
// We don't know if exit_attribute_mode resets colors, so we set it to something known.
|
|
|
|
if (write_foreground_color(0)) {
|
|
|
|
last_color = rgb_color_t::black();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (last_color != c) {
|
|
|
|
if (c.is_normal()) {
|
2012-11-19 00:30:30 +00:00
|
|
|
write_foreground_color(0);
|
|
|
|
writembs(exit_attribute_mode);
|
|
|
|
|
|
|
|
last_color2 = rgb_color_t::normal();
|
2016-05-02 05:29:21 +00:00
|
|
|
was_bold = 0;
|
|
|
|
was_underline = 0;
|
|
|
|
} else if (!c.is_special()) {
|
2014-09-20 01:44:18 +00:00
|
|
|
write_color(c, true /* foreground */);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
last_color = c;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (last_color2 != c2) {
|
|
|
|
if (c2.is_normal()) {
|
2012-03-05 21:39:01 +00:00
|
|
|
write_background_color(0);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(exit_attribute_mode);
|
2016-05-02 05:29:21 +00:00
|
|
|
if (!last_color.is_normal()) {
|
2014-09-20 01:44:18 +00:00
|
|
|
write_color(last_color, true /* foreground */);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
was_bold = 0;
|
|
|
|
was_underline = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
last_color2 = c2;
|
2016-05-02 05:29:21 +00:00
|
|
|
} else if (!c2.is_special()) {
|
2014-09-20 01:44:18 +00:00
|
|
|
write_color(c2, false /* not foreground */);
|
2012-11-19 00:30:30 +00:00
|
|
|
last_color2 = c2;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
// 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) {
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(tparm(enter_bold_mode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
was_bold = is_bold;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (was_underline && !is_underline) {
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(exit_underline_mode);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (!was_underline && is_underline) {
|
2012-11-19 00:30:30 +00:00
|
|
|
writembs(enter_underline_mode);
|
|
|
|
}
|
|
|
|
was_underline = is_underline;
|
2012-02-12 01:07:56 +00:00
|
|
|
}
|
2005-10-13 14:08:33 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
/// Default output method, simply calls write() on stdout.
|
2016-05-29 05:28:26 +00:00
|
|
|
static int writeb_internal(char c) { // cppcheck
|
2012-11-19 00:30:30 +00:00
|
|
|
write_loop(1, &c, 1);
|
|
|
|
return 0;
|
2006-02-16 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// This is for writing process notification messages. Has to write to stdout, so clr_eol and such
|
|
|
|
/// functions will work correctly. Not an issue since this function is only used in interactive mode
|
|
|
|
/// anyway.
|
2016-05-02 05:29:21 +00:00
|
|
|
int writeb(tputs_arg_t b) {
|
2012-11-19 00:30:30 +00:00
|
|
|
out(b);
|
|
|
|
return 0;
|
2006-02-16 13:40:25 +00:00
|
|
|
}
|
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// Write a wide character using the output method specified using output_set_writer().
|
2016-05-02 05:29:21 +00:00
|
|
|
int writech(wint_t ch) {
|
|
|
|
char buff[MB_LEN_MAX + 1];
|
2016-03-11 02:17:39 +00:00
|
|
|
size_t len;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (ch >= ENCODE_DIRECT_BASE && ch < ENCODE_DIRECT_BASE + 256) {
|
2012-11-19 00:30:30 +00:00
|
|
|
buff[0] = ch - ENCODE_DIRECT_BASE;
|
2016-03-11 02:17:39 +00:00
|
|
|
len = 1;
|
2016-05-02 05:29:21 +00:00
|
|
|
} else if (MB_CUR_MAX == 1) // single-byte locale (C/POSIX/ISO-8859)
|
2016-03-11 02:17:39 +00:00
|
|
|
{
|
|
|
|
// If `wc` contains a wide character we emit a question-mark.
|
2016-05-02 05:29:21 +00:00
|
|
|
if (ch & ~0xFF) {
|
2016-03-11 02:17:39 +00:00
|
|
|
ch = '?';
|
|
|
|
}
|
|
|
|
buff[0] = ch;
|
|
|
|
len = 1;
|
2016-05-02 05:29:21 +00:00
|
|
|
} else {
|
2016-03-11 02:17:39 +00:00
|
|
|
mbstate_t state = {};
|
|
|
|
len = wcrtomb(buff, ch, &state);
|
2016-05-02 05:29:21 +00:00
|
|
|
if (len == (size_t)-1) {
|
2016-03-11 02:17:39 +00:00
|
|
|
return 1;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2012-11-19 00:30:30 +00:00
|
|
|
out(buff[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// Write a wide character string to FD 1.
|
2016-05-02 05:29:21 +00:00
|
|
|
void writestr(const wchar_t *str) {
|
|
|
|
CHECK(str, );
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (MB_CUR_MAX == 1) // single-byte locale (C/POSIX/ISO-8859)
|
2016-03-11 02:17:39 +00:00
|
|
|
{
|
2016-05-02 05:29:21 +00:00
|
|
|
while (*str) {
|
|
|
|
writech(*str++);
|
2016-03-11 02:17:39 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-03-11 02:17:39 +00:00
|
|
|
size_t len = wcstombs(0, str, 0); // figure amount of space needed
|
2016-05-02 05:29:21 +00:00
|
|
|
if (len == (size_t)-1) {
|
2012-11-19 00:30:30 +00:00
|
|
|
debug(1, L"Tried to print invalid wide character string");
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-03-11 02:17:39 +00:00
|
|
|
// Convert the string.
|
2012-11-19 00:30:30 +00:00
|
|
|
len++;
|
2012-02-08 06:44:10 +00:00
|
|
|
char *buffer, static_buffer[256];
|
|
|
|
if (len <= sizeof static_buffer)
|
|
|
|
buffer = static_buffer;
|
|
|
|
else
|
|
|
|
buffer = new char[len];
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
wcstombs(buffer, str, len);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
// Write the string.
|
|
|
|
for (char *pos = buffer; *pos; pos++) {
|
2012-11-19 00:30:30 +00:00
|
|
|
out(*pos);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (buffer != static_buffer) delete[] buffer;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
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 17:55:28 +00: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.
|
2016-05-02 05:29:21 +00:00
|
|
|
rgb_color_t best_color(const std::vector<rgb_color_t> &candidates, color_support_t support) {
|
|
|
|
if (candidates.empty()) {
|
2014-11-10 00:42:35 +00:00
|
|
|
return rgb_color_t::none();
|
|
|
|
}
|
2016-05-02 05:29:21 +00:00
|
|
|
|
2014-11-10 00:42:35 +00:00
|
|
|
rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none();
|
2016-05-02 05:29:21 +00:00
|
|
|
for (size_t i = 0; i < candidates.size(); i++) {
|
2014-11-10 00:42:35 +00:00
|
|
|
const rgb_color_t &color = candidates.at(i);
|
2016-05-02 05:29:21 +00:00
|
|
|
if (first_rgb.is_none() && color.is_rgb()) {
|
2014-11-10 00:42:35 +00:00
|
|
|
first_rgb = color;
|
|
|
|
}
|
2016-05-02 05:29:21 +00:00
|
|
|
if (first_named.is_none() && color.is_named()) {
|
2014-11-10 00:42:35 +00:00
|
|
|
first_named = color;
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 05:29:21 +00:00
|
|
|
// If we have both RGB and named colors, then prefer rgb if term256 is supported.
|
2014-11-10 00:42:35 +00:00
|
|
|
rgb_color_t result = rgb_color_t::none();
|
2016-05-02 05:29:21 +00:00
|
|
|
bool has_term256 = !!(support & color_support_term256);
|
|
|
|
if ((!first_rgb.is_none() && has_term256) || first_named.is_none()) {
|
2014-11-10 00:42:35 +00:00
|
|
|
result = first_rgb;
|
2016-05-02 05:29:21 +00:00
|
|
|
} else {
|
2014-11-10 00:42:35 +00:00
|
|
|
result = first_named;
|
|
|
|
}
|
2016-05-02 05:29:21 +00:00
|
|
|
if (result.is_none()) {
|
2014-11-10 00:42:35 +00:00
|
|
|
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 17:55:28 +00:00
|
|
|
/// Return the internal color code representing the specified color.
|
|
|
|
/// XXX This code should be refactored to enable sharing with builtin_set_color.
|
2016-05-02 05:29:21 +00:00
|
|
|
rgb_color_t parse_color(const wcstring &val, bool is_background) {
|
|
|
|
int is_bold = 0;
|
|
|
|
int is_underline = 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-13 17:52:17 +00:00
|
|
|
std::vector<rgb_color_t> candidates;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-12 01:07:56 +00:00
|
|
|
wcstring_list_t el;
|
2012-11-19 00:30:30 +00:00
|
|
|
tokenize_variable_array(val, el);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
for (size_t j = 0; j < el.size(); j++) {
|
2012-02-12 01:07:56 +00:00
|
|
|
const wcstring &next = el.at(j);
|
|
|
|
wcstring color_name;
|
2016-05-02 05:29:21 +00:00
|
|
|
if (is_background) {
|
|
|
|
// Look for something like "--background=red".
|
2012-02-12 01:07:56 +00:00
|
|
|
const wcstring prefix = L"--background=";
|
2016-05-02 05:29:21 +00:00
|
|
|
if (string_prefixes_string(prefix, next)) {
|
2012-02-12 01:07:56 +00:00
|
|
|
color_name = wcstring(next, prefix.size());
|
|
|
|
}
|
2016-05-02 05:29:21 +00:00
|
|
|
} else {
|
2012-02-12 01:07:56 +00:00
|
|
|
if (next == L"--bold" || next == L"-o")
|
|
|
|
is_bold = true;
|
|
|
|
else if (next == L"--underline" || next == L"-u")
|
|
|
|
is_underline = true;
|
|
|
|
else
|
|
|
|
color_name = next;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 05:29:21 +00:00
|
|
|
if (!color_name.empty()) {
|
2012-02-13 17:52:17 +00:00
|
|
|
rgb_color_t color = rgb_color_t(color_name);
|
2016-05-02 05:29:21 +00:00
|
|
|
if (!color.is_none()) {
|
2012-02-13 17:52:17 +00:00
|
|
|
candidates.push_back(color);
|
2012-02-13 02:05:59 +00:00
|
|
|
}
|
2012-02-12 01:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-10 00:42:35 +00:00
|
|
|
rgb_color_t result = best_color(candidates, output_get_color_support());
|
2016-05-02 05:29:21 +00:00
|
|
|
|
|
|
|
if (result.is_none()) result = rgb_color_t::normal();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-13 17:52:17 +00:00
|
|
|
result.set_bold(is_bold);
|
|
|
|
result.set_underline(is_underline);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-13 02:05:59 +00:00
|
|
|
#if 0
|
|
|
|
wcstring desc = result.description();
|
|
|
|
printf("Parsed %ls from %ls (%s)\n", desc.c_str(), val.c_str(), is_background ? "background" : "foreground");
|
|
|
|
#endif
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-12 01:07:56 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2007-09-09 14:04:36 +00:00
|
|
|
|
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 17:55:28 +00:00
|
|
|
/// Write specified multibyte string.
|
2016-05-02 05:29:21 +00:00
|
|
|
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line) {
|
|
|
|
if (mbs != NULL) {
|
2014-05-09 21:37:23 +00:00
|
|
|
tputs(mbs, 1, &writeb);
|
2016-05-02 05:29:21 +00:00
|
|
|
} else {
|
2016-06-02 03:03:50 +00:00
|
|
|
env_var_t term = env_get_string(L"TERM");
|
2016-05-02 05:29:21 +00:00
|
|
|
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"),
|
2016-06-02 03:03:50 +00:00
|
|
|
mbs_name, line, file, term.c_str(), PACKAGE_BUGREPORT);
|
2014-05-09 21:37:23 +00:00
|
|
|
}
|
|
|
|
}
|