nyancat

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.inz.fi/nyancat
Log | Files | Refs | README

commit 40f1336b51cb6804cd3981eda56354317a973258
parent c2c783fc3676b448f5555188501d5fa428c5b476
Author: Santtu Lakkala <inz@inz.fi>
Date:   Mon, 20 Jul 2020 17:10:52 +0300

Add option to bash-escape color codes

Bash needs invisible characters to be escaped with \[ \] in prompt, add
option to output those.

Diffstat:
MREADME.md | 4++++
Mnyancat.c | 45++++++++++++++++++++++++++++++++++-----------
2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md @@ -87,3 +87,7 @@ $ for i in `seq 1 3`; do lorem -s 10; echo; echo; done | par -w 60 | nyancat -1 officia quam sed. Vel nulla odio voluptates beatae et.  ``` +```ansi +$ PROMPT_COMMAND='PS1="$(echo -n "$USER@$HOSTNAME:${PWD/#$HOME/\~}\$ "|nyancat -vb)"' +user@host:~/nyancat$  +``` diff --git a/nyancat.c b/nyancat.c @@ -40,7 +40,8 @@ struct lolcat { int h; int sx; int sy; - int transpose; + bool transpose; + bool bash_escape; uint32_t fg; uint32_t bg; bool bg_set; @@ -442,18 +443,23 @@ static void _lc_colorize(struct lolcat *lc, const char *u8_char, size_t len) } if (lc->mode == INDEXED16) - bw += sprintf(bw, "3%dm%.*s", - col, (int)len, u8_char); + bw += sprintf(bw, "3%dm", + col); else if (lc->mode == INDEXED256) - bw += sprintf(bw, "38;5;%dm%.*s", - col, (int)len, u8_char); + bw += sprintf(bw, "38;5;%dm", + col); else - bw += sprintf(bw, "38;2;%d;%d;%dm%.*s", + bw += sprintf(bw, "38;2;%d;%d;%dm", col >> 16, (col >> 8) & 255, - col & 255, (int)len, u8_char); + col & 255); + if (lc->bash_escape) + lc->write("\\[", 2, lc->write_data); lc->write(buffer, bw - buffer, lc->write_data); + if (lc->bash_escape) + lc->write("\\]", 2, lc->write_data); + lc->write(u8_char, len, lc->write_data); } struct strnsplit_int_data { @@ -642,8 +648,12 @@ ssize_t lc_process(struct lolcat *lc, const char *buffer, size_t len) lc->was_bgset = lc->was_reverse = lc->was_bold = lc->was_underline = false; + if (lc->bash_escape) + lc->write("\\[", 2, lc->write_data); lc->write(buffer, sizeof(buffer), lc->write_data); + if (lc->bash_escape) + lc->write("\\[", 2, lc->write_data); } lc->x = 0; lc->y++; @@ -681,9 +691,16 @@ void lolcat_set_size(struct lolcat *lc, int w, int h) lc->h = h; } -static void usage() +static void usage(const char *binname) { - fprintf(stderr, "Usage: nyancat [-t2] [file...]\n"); + fprintf(stderr, + "Usage: %s [-t12vb] [file...]\n" + " -t 24-bit true color output (default)\n" + " -1 16 color output\n" + " -2 256 color output\n" + " -b Escape color codes with \\[ \\] for bash prompt\n" + " -v Make rainbowaves vertical\n", + binname); exit(0); } @@ -725,9 +742,12 @@ int main(int argc, char **argv) case 'v': lc->transpose = true; break; + case 'b': + lc->bash_escape = true; + break; case 'h': default: - usage(); + usage(argv[0]); break; } } @@ -792,7 +812,10 @@ int main(int argc, char **argv) } } - fprintf(stdout, "\x1b[0m"); + if (lc->bash_escape) + fprintf(stdout, "\\[\x1b[0m\\]"); + else + fprintf(stdout, "\x1b[0m"); return 0; }