From f25a84cc4de2fb886389d2168621cfac88aa7003 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 26 Feb 2023 18:53:36 +0100 Subject: [PATCH 01/10] Fix handling of repeated escape sequences and certain XTERM title sequences --- lolcat.c | 64 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/lolcat.c b/lolcat.c index 38b4198..1a9533e 100755 --- a/lolcat.c +++ b/lolcat.c @@ -110,12 +110,28 @@ enum esc_st { ST_ESC_STRING_TERM, ST_ESC_CSI_TERM, ST_ESC_TERM, + NUM_ST +}; + +const char * esc_st_names[NUM_ST] = { + [ST_NONE] = "NONE", + [ST_ESC_BEGIN] = "BEGIN", + [ST_ESC_STRING] = "STRING", + [ST_ESC_CSI] = "CSI", + [ST_ESC_STRING_TERM] = "STRING_TERM", + [ST_ESC_CSI_TERM] = "CSI_TERM", + [ST_ESC_TERM] = "TERM", }; static enum esc_st find_escape_sequences(wint_t c, enum esc_st st) { - if (st == ST_NONE && c == '\033') { /* Escape sequence YAY */ - return ST_ESC_BEGIN; + + if (st == ST_NONE || st == ST_ESC_CSI_TERM) { + if (c == '\033') { /* Escape sequence YAY */ + return ST_ESC_BEGIN; + } else { + return ST_NONE; + } } else if (st == ST_ESC_BEGIN) { if (c == '[') { @@ -134,7 +150,9 @@ static enum esc_st find_escape_sequences(wint_t c, enum esc_st st) } } else if (st == ST_ESC_STRING) { - if (c == '\033') { + if (c == '\007') { + return ST_NONE; + } else if (c == '\033') { return ST_ESC_STRING_TERM; } else { return st; @@ -142,7 +160,7 @@ static enum esc_st find_escape_sequences(wint_t c, enum esc_st st) } else if (st == ST_ESC_STRING_TERM) { if (c == '\\') { - return ST_ESC_TERM; + return ST_NONE; } else { return ST_ESC_STRING; } @@ -155,6 +173,8 @@ static enum esc_st find_escape_sequences(wint_t c, enum esc_st st) static void usage(void) { wprintf(L"Usage: lolcat [-h horizontal_speed] [-v vertical_speed] [--] [FILES...]\n"); + wprintf(L"\n"); + wprintf(L"Use lolcat --help to print the full help text.\n"); exit(2); } @@ -355,9 +375,18 @@ int main(int argc, char** argv) while ((c = this_file_read_wchar(f)) != WEOF) { if (colors) { - escape_state = find_escape_sequences(c, escape_state); - if (!escape_state) { + + escape_state = find_escape_sequences(c, escape_state); +#ifdef ESC_DEBUG + fprintf(stderr, "%02x %c %s\n", c, c > 32 ? c : '.', esc_st_names[escape_state]); +#endif + + if (escape_state == ST_ESC_CSI_TERM) { + putwchar(c); + } + + if (escape_state == ST_NONE || escape_state == ST_ESC_CSI_TERM) { if (c == '\n') { l++; i = 0; @@ -366,8 +395,11 @@ int main(int argc, char** argv) } } else { - if (rgb) { + if (escape_state == ST_NONE) { i += wcwidth(c); + } + + if (rgb) { float theta = i * freq_h / 5.0f + l * freq_v + (offx + 2.0f * (rand_offset + start_color) / RAND_MAX)*M_PI; union rgb_c c; @@ -386,15 +418,15 @@ int main(int argc, char** argv) wprintf(L"\033[%d;2;%d;%d;%dm", (invert ? 48 : 38), c.r, c.g, c.b); } else if (ansi16) { - int ncc = offx * ARRAY_SIZE(codes16) + (int)((i += wcwidth(c)) * freq_h + l * freq_v); - if (cc != ncc) { + int ncc = offx * ARRAY_SIZE(codes16) + (int)(i * freq_h + l * freq_v); + if (cc != ncc || escape_state == ST_ESC_CSI_TERM) { wprintf(L"\033[%hhum", (invert ? 10 : 0) + codes16[(rand_offset + start_color + (cc = ncc)) % ARRAY_SIZE(codes16)]); } } else { if (gradient) { - int ncc = offx * ARRAY_SIZE(codes_gradient) + (int)((i += wcwidth(c)) * freq_h + l * freq_v); - if (cc != ncc) { + int ncc = offx * ARRAY_SIZE(codes_gradient) + (int)(i * freq_h + l * freq_v); + if (cc != ncc || escape_state == ST_ESC_CSI_TERM) { size_t lookup = (rand_offset + start_color + (cc = ncc)) % (2*ARRAY_SIZE(codes_gradient)); if (lookup >= ARRAY_SIZE(codes_gradient)) { lookup = 2*ARRAY_SIZE(codes_gradient) - 1 - lookup; @@ -403,8 +435,8 @@ int main(int argc, char** argv) } } else { - int ncc = offx * ARRAY_SIZE(codes) + (int)((i += wcwidth(c)) * freq_h + l * freq_v); - if (cc != ncc) { + int ncc = offx * ARRAY_SIZE(codes) + (int)(i * freq_h + l * freq_v); + if (cc != ncc || escape_state == ST_ESC_CSI_TERM) { wprintf(L"\033[%d;5;%hhum", (invert ? 48 : 38), codes[(rand_offset + start_color + (cc = ncc)) % ARRAY_SIZE(codes)]); } } @@ -413,10 +445,8 @@ int main(int argc, char** argv) } } - putwchar(c); - - if (escape_state == ST_ESC_CSI_TERM) { /* implies "colors" */ - wprintf(L"\033[38;5;%hhum", codes[(rand_offset + start_color + cc) % ARRAY_SIZE(codes)]); + if (escape_state != ST_ESC_CSI_TERM) { + putwchar(c); } } From dc3ddf395017bf903f5bfb8f0c5c6d08d84fcdc0 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 26 Feb 2023 18:54:34 +0100 Subject: [PATCH 02/10] Bump version to v1.4 --- lolcat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lolcat.c b/lolcat.c index 1a9533e..a323720 100755 --- a/lolcat.c +++ b/lolcat.c @@ -180,7 +180,7 @@ static void usage(void) static void version(void) { - wprintf(L"lolcat version 1.3, (c) 2020 jaseg\n"); + wprintf(L"lolcat version 1.4, (c) 2023 jaseg\n"); exit(0); } From f2c26ed7a72828f94f174ab8c84676856fbe2097 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 4 Jul 2024 11:44:47 +0200 Subject: [PATCH 03/10] Make lolcat interleave nicely with non-colored stderr This works by making lolcat always reset the color at the end of a line on its input, and only setting a new color immediately before printing the first character on the next line. This way, assuming that whatever process is piped into lolcat follow the standard convention where stdout is buffered line-wise, and only writes full lines to stderr, a process that outputs to both stdout and stderr but that has only stdout piped through lolcat (as most people would do) would have it's stderr output interleave nicely uncolored with the lolcat-colored stdout output. This won't always work, but it gets us most of the way there with little effort and negligible (2 extra escape sequences per line of output) performance overhead. Thanks to github user @kolayne for reporting this as an issue! --- lolcat.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lolcat.c b/lolcat.c index a323720..ff39189 100755 --- a/lolcat.c +++ b/lolcat.c @@ -390,8 +390,11 @@ int main(int argc, char** argv) if (c == '\n') { l++; i = 0; + cc = -1; if (invert) { wprintf(L"\033[49m"); + } else { + wprintf(L"\033[0m"); } } else { From a9ab81839f2a00fa1d831648a1d79e268bb1ddac Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 4 Jul 2024 11:49:59 +0200 Subject: [PATCH 04/10] Bump version to v1.5 --- lolcat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lolcat.c b/lolcat.c index ff39189..011f5dc 100755 --- a/lolcat.c +++ b/lolcat.c @@ -180,7 +180,7 @@ static void usage(void) static void version(void) { - wprintf(L"lolcat version 1.4, (c) 2023 jaseg\n"); + wprintf(L"lolcat version 1.5, (c) 2024 jaseg\n"); exit(0); } From bfd74461983817b76b5c96824fa83e486fc1d416 Mon Sep 17 00:00:00 2001 From: Ellam ByDefault Date: Sun, 14 Jul 2024 00:20:17 +0700 Subject: [PATCH 05/10] ignore `lolcat` and `censor`, add Debian dependency --- .gitignore | 2 ++ README.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 12b43b0..a464544 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ lolcat.o censor.o +lolcat +censor diff --git a/README.md b/README.md index f2d4af9..3cbfba4 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ $ make lolcat ### Others +Note: Debian users may need the `python-is-python3` package. + ```bash $ make && sudo make install ``` From 10b56bcdc25698de7898bcc090ab30757e33241d Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 16 Jul 2024 11:24:39 +0200 Subject: [PATCH 06/10] Fix version number in autotools and PKGBUILD build scripts --- PKGBUILD | 2 +- autotools/configure.ac | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 5c2652e..baec990 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,7 +1,7 @@ # Maintainer: Ricardo (XenGi) Band pkgname=c-lolcat -pkgver=v1.3 +pkgver=v1.5 pkgrel=1 pkgdesc="High-performance implementation of lolcat" arch=('i686' 'x86_64') diff --git a/autotools/configure.ac b/autotools/configure.ac index 8b9871b..28f5d08 100644 --- a/autotools/configure.ac +++ b/autotools/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) -AC_INIT([lolcat], [1.3], [jaseg ]) +AC_INIT([lolcat], [1.5], [jaseg ]) AC_CONFIG_SRCDIR([../lolcat.c]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_AUX_DIR([autoscripts]) From e460e3d1328f3e9e7d468da7901fe52f0fc7018a Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 16 Jul 2024 11:35:08 +0200 Subject: [PATCH 07/10] Add a little release script to keep the version numbers in sync Fixes #58 --- do_release.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 do_release.sh diff --git a/do_release.sh b/do_release.sh new file mode 100755 index 0000000..4d437e8 --- /dev/null +++ b/do_release.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +set -e + +if [ $# -lt 1 ]; then + echo "do_release.sh must be called with a version number as its first argument." + exit 1 +fi + +if [ -n "$(git status --porcelain|grep -v '^??')" ]; then + echo "do_release.sh must be called from a clean working directory." + exit 2 +fi + +VER="$1" + +if echo "$VER" | grep -v '^[0-9]\+\.[0-9]\+$'; then + echo "do_release.sh must be called with a version number formatted like "1.23" as its first argument, with no leading \"v\"." + exit 1 +fi + +echo "Updating files for version v$VER" + +sed -i "/L\"lolcat version [0-9.]\+, (c) [0-9]\+ jaseg\\\\n\"/s/version [0-9.]\+/version $VER/" lolcat.c +sed -i "s/^pkgver=v[0-9.]\+/pkgver=v$VER/" PKGBUILD +sed -i "/^AC_INIT/s/\[[0-9.]\+\]/[$VER]/" autotools/configure.ac +git add lolcat.c PKGBUILD autotools/configure.ac +git commit -m 'Bump version to v$VER' +git tag "v$VER" +echo "Success." + From 1ea54a14d5705cd0bd2959acbe77941bff484661 Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 16 Jul 2024 11:36:27 +0200 Subject: [PATCH 08/10] Make images embedded in README use relative paths Fixes #59 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3cbfba4..602a390 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # What? -![](https://raw.githubusercontent.com/jaseg/lolcat/master/LOLCat-Rainbow.jpg) +![](./LOLCat-Rainbow.jpg) ## Screenshot -![](https://raw.githubusercontent.com/jaseg/lolcat/master/screenshot.png) +![](./screenshot.png) ![](./sl.gif) From 621b604edd146343c5d43fe5d889287edb4531a5 Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 16 Jul 2024 11:39:09 +0200 Subject: [PATCH 09/10] Add CPPFLAGS and LDFLAGS to compiler invocation Fixes #57 --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 9ce889d..c89fcf8 100644 --- a/Makefile +++ b/Makefile @@ -16,10 +16,10 @@ xterm256lut.h: xterm256lut_gen.py python $< > $@ lolcat: lolcat.c xterm256lut.h - $(CC) $(CFLAGS) -o $@ $< $(LIBS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) censor: censor.c - $(CC) $(CFLAGS) -o $@ $< $(LIBS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) install: lolcat censor install lolcat $(DESTDIR)/lolcat From 1212a9cc6c2a092359db460d9f62822b56dc02ef Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 16 Jul 2024 11:43:44 +0200 Subject: [PATCH 10/10] Correct DESTDIR/PREFIX usage in Makefile Closes #56 --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index c89fcf8..c751508 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC ?= gcc CFLAGS ?= -std=c11 -Wall -Wextra -O3 -Wno-sign-compare LIBS := -lm -DESTDIR ?= /usr/local/bin +PREFIX ?= /usr/local all: lolcat censor @@ -22,8 +22,8 @@ censor: censor.c $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) install: lolcat censor - install lolcat $(DESTDIR)/lolcat - install censor $(DESTDIR)/censor + install lolcat $(DESTDIR)$(PREFIX)/bin/lolcat + install censor $(DESTDIR)$(PREFIX)/bin/censor clean: rm -f lolcat censor