1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
Forwarded: not-needed
Last-Update: 2025-09-01
Author: Kirill Rekhov <krekhov.dev@gmail.com>
Description: Add a new option for centering the output text
--- toilet-0.3.orig/doc/toilet.1.in
+++ toilet-0.3/doc/toilet.1.in
@@ -4,7 +4,7 @@ TOIlet \- display large colourful charac
.SH SYNOPSIS
.B toilet
[
-.B \-hkostvSW
+.B \-hkosctvSW
] [
.B \-d
.I fontdirectory
@@ -88,6 +88,9 @@ will wrap its output at 80 character col
.B \-t, \-\-termwidth
Set the output width to the terminal width.
.TP
+.B \-c, \-\-center
+Center the output text horizontally within the terminal width.
+.TP
.B \-F, \-\-filter <filters>
.PD 0
.TP
--- toilet-0.3.orig/src/filter.c
+++ toilet-0.3/src/filter.c
@@ -19,6 +19,9 @@
#if defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#endif
+#if defined HAVE_SYS_IOCTL_H && defined HAVE_TIOCGWINSZ
+# include <sys/ioctl.h>
+#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@@ -55,8 +58,32 @@ const lookup[] =
{ "left", filter_left, "rotate 90 degrees counterclockwise" },
{ "right", filter_right, "rotate 90 degrees clockwise" },
{ "border", filter_border, "surround text with a border" },
+ { "center", filter_center, "center the output text" },
};
+void filter_center(context_t *cx)
+{
+ int w = caca_get_canvas_width(cx->torender);
+ int h = caca_get_canvas_height(cx->torender);
+ int max_width = cx->term_width;
+
+ #if defined HAVE_SYS_IOCTL_H && defined HAVE_TIOCGWINSZ
+ struct winsize ws;
+ if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0) {
+ max_width = ws.ws_col;
+ }
+ #endif
+
+ int offset = (max_width - w) / 2;
+
+ if (offset > 0) {
+ caca_canvas_t *new_canvas = caca_create_canvas(max_width, h);
+ caca_blit(new_canvas, offset, 0, cx->torender, NULL);
+ caca_free_canvas(cx->torender);
+ cx->torender = new_canvas;
+ }
+}
+
int filter_list(void)
{
unsigned int i;
--- toilet-0.3.orig/src/filter.h
+++ toilet-0.3/src/filter.h
@@ -18,4 +18,5 @@ extern int filter_list(void);
extern int filter_add(context_t *, char const *);
extern int filter_do(context_t *);
extern int filter_end(context_t *);
+extern void filter_center(context_t *cx);
--- toilet-0.3.orig/src/main.c
+++ toilet-0.3/src/main.c
@@ -64,6 +64,7 @@ int main(int argc, char *argv[])
{ "directory", 1, NULL, 'd' },
{ "width", 1, NULL, 'w' },
{ "termwidth", 0, NULL, 't' },
+ { "center", 0, NULL, 'c' },
{ "filter", 1, NULL, 'F' },
{ "gay", 0, NULL, 130 },
{ "metal", 0, NULL, 131 },
@@ -76,7 +77,7 @@ int main(int argc, char *argv[])
{ NULL, 0, NULL, 0 }
};
- int c = caca_getopt(argc, argv, "f:d:w:tsSkWoF:E:hI:v",
+ int c = caca_getopt(argc, argv, "f:d:w:ctsSkWoF:E:hI:v",
long_options, &option_index);
if(c == -1)
break;
@@ -125,6 +126,14 @@ int main(int argc, char *argv[])
#endif
break;
}
+ case 'c':
+#if defined HAVE_SYS_IOCTL_H && defined HAVE_TIOCGWINSZ
+ struct winsize ws;
+ if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0)
+ cx->term_width = ws.ws_col;
+#endif
+ filter_add(cx, "center");
+ break;
case 's':
cx->hmode = "default";
break;
@@ -200,7 +209,7 @@ int main(int argc, char *argv[])
}
#define USAGE \
- "Usage: toilet [ -hkostvSW ] [ -d fontdirectory ]\n" \
+ "Usage: toilet [ -hkosctvSW ] [ -d fontdirectory ]\n" \
" [ -f fontfile ] [ -F filter ] [ -w outputwidth ]\n" \
" [ -I infocode ] [ -E format ] [ message ]\n"
@@ -211,6 +220,7 @@ int main(int argc, char *argv[])
" kerning, full width, overlap)\n" \
" -w, --width <width> set output width\n" \
" -t, --termwidth adapt to terminal's width\n" \
+ " -c, --center center text horizontally within terminal width\n" \
" -F, --filter <filters> apply one or several filters to the text\n" \
" -F, --filter list list available filters\n" \
" --gay rainbow filter (same as -F gay)\n" \
|