From c025bc4f2b8611b6df7b3e16a9ba791e9a2077df Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 27 Sep 2006 07:18:20 +0000 Subject: [PATCH] * Implemented figlet's -t (adapt to terminal's width). --- configure.ac | 15 ++++++++++++++- src/main.c | 24 +++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index ec4f78c..5d5384f 100644 --- a/configure.ac +++ b/configure.ac @@ -25,7 +25,8 @@ if test "$build" != "$host" -a "${PKG_CONFIG_LIBDIR}" = ""; then export PKG_CONFIG_LIBDIR=/dev/null fi -AC_CHECK_HEADERS(getopt.h) +AC_CHECK_HEADERS(getopt.h sys/ioctl.h) + AC_CHECK_FUNCS(getopt_long, [AC_DEFINE(HAVE_GETOPT_LONG, 1, Define to 1 if you have the `getopt_long' function.)], [AC_CHECK_LIB(gnugetopt, getopt_long, @@ -33,6 +34,18 @@ AC_CHECK_FUNCS(getopt_long, GETOPT_LIBS="${GETOPT_LIBS} -lgnugetopt"])]) AC_SUBST(GETOPT_LIBS) +AC_CACHE_CHECK([for TIOCGWINSZ], + [ac_cv_have_tiocgwinsz], + [AC_TRY_COMPILE( + [#include ], + [struct winsize ws; + ioctl(1, TIOCGWINSZ, &ws);], + ac_cv_have_tiocgwinsz=yes, + ac_cv_have_tiocgwinsz=no)]) +if test "${ac_cv_have_tiocgwinsz}" = "yes"; then + AC_DEFINE(HAVE_TIOCGWINSZ, 1, [Define if you have TIOCGWINSZ]) +fi + CUCUL="no" PKG_CHECK_MODULES(cucul, cucul >= 0.99.beta5, [CUCUL="yes"], diff --git a/src/main.c b/src/main.c index e058347..d92885d 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,9 @@ #if defined(HAVE_GETOPT_H) # include #endif +#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) +# include +#endif #include #include #include @@ -67,6 +70,7 @@ int main(int argc, char *argv[]) { "font", 1, NULL, 'f' }, { "directory", 1, NULL, 'd' }, { "width", 1, NULL, 'w' }, + { "termwidth", 0, NULL, 't' }, { "gay", 0, NULL, 'g' }, { "metal", 0, NULL, 'm' }, { "irc", 0, NULL, 'i' }, @@ -76,11 +80,11 @@ int main(int argc, char *argv[]) { NULL, 0, NULL, 0 } }; - int c = getopt_long(argc, argv, "d:f:I:w:ghimv", + int c = getopt_long(argc, argv, "d:f:I:w:ghimtv", long_options, &option_index); # else # define MOREINFO "Try `%s -h' for more information.\n" - int c = getopt(argc, argv, "d:f:I:w:ghimv"); + int c = getopt(argc, argv, "d:f:I:w:ghimtv"); # endif if(c == -1) break; @@ -111,6 +115,18 @@ int main(int argc, char *argv[]) case 'w': /* --width */ term_width = atoi(optarg); break; + case 't': /* --termwidth */ + { +#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) + struct winsize ws; + + if((ioctl(1, TIOCGWINSZ, &ws) != -1 || + ioctl(2, TIOCGWINSZ, &ws) != -1 || + ioctl(0, TIOCGWINSZ, &ws) != -1) && ws.ws_col != 0) + term_width = ws.ws_col; +#endif + break; + } case 'i': /* --irc */ export = "irc"; break; @@ -221,13 +237,14 @@ static void version(void) #if defined(HAVE_GETOPT_H) static void usage(void) { - printf("Usage: toilet [ -ghimv ] [ -d fontdirectory ]\n"); + printf("Usage: toilet [ -ghimtv ] [ -d fontdirectory ]\n"); printf(" [ -f fontfile ] [ -w outputwidth ]\n"); printf(" [ -I infocode ] [ message ]\n"); # ifdef HAVE_GETOPT_LONG printf(" -f, --font select the font\n"); printf(" -d, --directory specify font directory\n"); printf(" -w, --width set output width\n"); + printf(" -t, --termwidth adapt to terminal's width\n"); printf(" -g, --gay add a rainbow effect to the text\n"); printf(" -m, --metal add a metal effect to the text\n"); printf(" -i, --irc output IRC colour codes\n"); @@ -238,6 +255,7 @@ static void usage(void) printf(" -f select the font\n"); printf(" -d specify font directory\n"); printf(" -w set output width\n"); + printf(" -t adapt to terminal's width\n"); printf(" -g add a rainbow effect to the text\n"); printf(" -m add a metal effect to the text\n"); printf(" -i output IRC colour codes\n");