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:
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
[34mo[34mf[34mf[34mi[34mc[34mi[35ma [35mq[35mu[35ma[35mm [34ms[34me[34md[34m. [34mV[36me[36ml [36mn[36mu[36ml[36ml[36ma [36mo[36md[36mi[36mo [36mv[36mo[36ml[34mu[34mp[34mt[34ma[34mt[34me[35ms [35mb[35me[35ma[35mt[35ma[34me [34me[34mt[34m.
[0m
```
+```ansi
+$ PROMPT_COMMAND='PS1="$(echo -n "$USER@$HOSTNAME:${PWD/#$HOME/\~}\$ "|nyancat -vb)"'
+[38;2;128;237;18mu[38;2;193;189;1ms[38;2;240;123;20me[38;2;254;59;69mr[38;2;233;14;136m@[38;2;182;1;200mh[38;2;115;24;243mo[38;2;52;77;254ms[38;2;10;144;228mt[38;2;2;206;174m:[38;2;29;246;107m~[38;2;85;252;45m/[38;2;152;223;7mn[38;2;213;166;3my[38;2;249;99;35ma[38;2;251;39;93mn[38;2;217;5;160mc[38;2;158;5;219ma[38;2;91;41;251mt[38;2;33;101;248m$ [0m
+```
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;
}