From b082025a1f16836453b76e5c97d85c7d705c8373 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Fri, 7 Jan 2022 10:02:17 +0800 Subject: [PATCH] Add support for fractional values in -t argument. Fixes #8 --- src/xcowsay.c | 22 ++++++++++++++++++---- test.sh | 3 +++ xcowsay.6 | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/xcowsay.c b/src/xcowsay.c index daba63f..f441c80 100644 --- a/src/xcowsay.c +++ b/src/xcowsay.c @@ -1,5 +1,5 @@ /* xcowsay.c -- Cute talking cow for GTK+. - * Copyright (C) 2008-2020 Nick Gasson + * Copyright (C) 2008-2022 Nick Gasson * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -155,7 +155,7 @@ static void usage() static void version() { static const char *copy = - "Copyright (C) 2008-2020 Nick Gasson\n" + "Copyright (C) 2008-2022 Nick Gasson\n" "This program comes with ABSOLUTELY NO WARRANTY. This is free software, and\n" "you are welcome to redistribute it under certain conditions. See the GNU\n" "General Public Licence for details."; @@ -179,6 +179,18 @@ static int parse_int_option(const char *optarg) } } +static double parse_float_option(const char *optarg) +{ + char *endptr; + double r = strtod(optarg, &endptr); + if ('\0' == *endptr) + return r; + else { + fprintf(stderr, i18n("Error: %s is not a valid number\n"), optarg); + exit(EXIT_FAILURE); + } +} + static void parse_position_option(const char *optarg, int *x, int *y) { const char *failmsg = i18n("Error: failed to parse '%s' as position\n"); @@ -256,7 +268,7 @@ int main(int argc, char **argv) parse_config_file(); - int c, index = 0, failure = 0; + int c, index = 0, failure = 0, dtime; const char *spec = "hvld:r:t:f:"; const char *dream_file = NULL; while ((c = getopt_long(argc, argv, spec, long_options, &index)) != -1) { @@ -277,7 +289,9 @@ int main(int argc, char **argv) version(); exit(EXIT_SUCCESS); case 't': - set_int_option("display_time", parse_int_option(optarg)*1000); + dtime = (int)(parse_float_option(optarg)*1000.0); + set_int_option("display_time", dtime); + set_int_option("min_display_time", dtime); break; case 'r': set_int_option("reading_speed", parse_int_option(optarg)); diff --git a/test.sh b/test.sh index 80651bc..55cc6de 100755 --- a/test.sh +++ b/test.sh @@ -19,6 +19,9 @@ $BUILD_DIR/src/xcowsay --think Hello World echo Left thought bubble $BUILD_DIR/src/xcowsay --think Hello World --left +echo Fractional time +$BUILD_DIR/src/xcowsay Very quick message -t 0.5 + echo Dream $BUILD_DIR/src/xcowsay --dream $SRC_DIR/cow_small.png -t 2 diff --git a/xcowsay.6 b/xcowsay.6 index 8d801f5..8fa6147 100644 --- a/xcowsay.6 +++ b/xcowsay.6 @@ -93,6 +93,7 @@ Setting to zero displays the cow until it is clicked. The corresponding config file option is .IR display_time . +This can also be set to a floating point value like 0.5. .TP .BI "-r " speed ", --reading-speed=" speed Number of milliseconds to display the cow for each word in the input -- 2.39.2