From ba0d031826f8f27a0cd8a3157cd1a6e7c1829c8c Mon Sep 17 00:00:00 2001 From: Yuichi Kobayashi Date: Sat, 3 Feb 2024 08:34:10 +0900 Subject: [PATCH] Collect common code from info.[ch] and added tpbar.[ch] --- README_en.md | 6 +- README_ja.md | 6 +- doc/DEV_ja.md | 1 + psync.png | Bin 43503 -> 43503 bytes src/Makefile.in | 9 +-- src/configure | 28 ++++---- src/configure.ac | 20 +++--- src/info.c | 171 +++++++---------------------------------------- src/tpbar.c | 146 ++++++++++++++++++++++++++++++++++++++++ src/tpbar.h | 53 +++++++++++++++ 10 files changed, 259 insertions(+), 181 deletions(-) create mode 100644 src/tpbar.c create mode 100644 src/tpbar.h diff --git a/README_en.md b/README_en.md index 63b3c6d..4e93406 100644 --- a/README_en.md +++ b/README_en.md @@ -22,9 +22,9 @@ So for this to work, you must be able to login to the sync-host with SSH. This work is required for each sync-host include local-host. 1. Do the following to build and install. ``` -curl -LOJs https://github.com/kobayasy/pSync/releases/download/2.32/psync-2.32.tar.gz -tar xzf psync-2.32.tar.gz -cd psync-2.32 +curl -LOJs https://github.com/kobayasy/pSync/releases/download/2.33/psync-2.33.tar.gz +tar xzf psync-2.33.tar.gz +cd psync-2.33 ./configure --prefix=$HOME make install diff --git a/README_ja.md b/README_ja.md index 006923c..bb71fe3 100644 --- a/README_ja.md +++ b/README_ja.md @@ -26,9 +26,9 @@ SSH によって認証/暗号化/圧縮を行い、安全かつ高速にファ ローカルホストを含めた全ての同期ホストに対してこの作業が必要です。 1. 以下の手順でビルドとインストールを行ないます。 ``` -curl -LOJs https://github.com/kobayasy/pSync/releases/download/2.32/psync-2.32.tar.gz -tar xzf psync-2.32.tar.gz -cd psync-2.32 +curl -LOJs https://github.com/kobayasy/pSync/releases/download/2.33/psync-2.33.tar.gz +tar xzf psync-2.33.tar.gz +cd psync-2.33 ./configure --prefix=$HOME make install diff --git a/doc/DEV_ja.md b/doc/DEV_ja.md index 424f50d..1902bc1 100644 --- a/doc/DEV_ja.md +++ b/doc/DEV_ja.md @@ -22,6 +22,7 @@ |main.c|設定ファイル解析, 引数解析, 通信プロトコル起動| |popen3.{c,h}|プロセス起動, プロセス間通信| |progress.{c,h}|進捗通知| +|tpbar.{c,h}|プロブレスバー表示| |info.{c,h}|進捗表示| |common.{c,h}|エラー判定/分岐, 中断判定/分岐, 数値データ[デ]シリアライザ, リスト処理| |ja/|日本語manマニュアル| diff --git a/psync.png b/psync.png index e987c379e311c14e159fb69cfa16f5e0581f3981..f0c89769d682cad14ff6f7fb66771c90e3ef06fc 100644 GIT binary patch delta 71 zcmaEVnd$vyrVTACrA%}UjC2i*Lkujej18?!%(M+Gtqcrye|Y93=FM|Ewv3StqctMLUpf9p0`pQhxC7rsTq^c HuT%g4N^TaE diff --git a/src/Makefile.in b/src/Makefile.in index bf5e80c..aef8d4a 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -1,7 +1,7 @@ # @configure_input@ -# Makefile.in - Last modified: 02-Apr-2023 (kobayasy) +# Makefile.in - Last modified: 03-Feb-2024 (kobayasy) # -# Copyright (C) 2018-2023 by Yuichi Kobayashi +# Copyright (C) 2018-2024 by Yuichi Kobayashi # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files @@ -25,7 +25,7 @@ TARGET = @PACKAGE_TARNAME@@EXEEXT@ OBJS = psync.@OBJEXT@ common.@OBJEXT@ progress.@OBJEXT@ psync_psp1.@OBJEXT@ -OBJS += popen3.@OBJEXT@ info.@OBJEXT@ main.@OBJEXT@ +OBJS += popen3.@OBJEXT@ tpbar.@OBJEXT@ info.@OBJEXT@ main.@OBJEXT@ MAN1JA = ja/@PACKAGE_TARNAME@.1 MAN5JA = ja/@PACKAGE_TARNAME@.conf.5 @@ -58,7 +58,8 @@ common.@OBJEXT@ : common.c common.h config.h progress.@OBJEXT@ : progress.c progress.h psync_psp1.@OBJEXT@ : psync_psp1.c common.h psync.h psync_psp1.h config.h popen3.@OBJEXT@ : popen3.c popen3.h -info.@OBJEXT@ : info.c info.h config.h +tpbar.@OBJEXT@ : tpbar.c tpbar.h config.h +info.@OBJEXT@ : info.c tpbar.h info.h config.h main.@OBJEXT@ : main.c common.h psync_psp1.h psync.h popen3.h info.h config.h $(TARGET) : $(OBJS) diff --git a/src/configure b/src/configure index 1a5c5da..071ef3e 100755 --- a/src/configure +++ b/src/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.69 for pSync 2.32. +# Generated by GNU Autoconf 2.69 for pSync 2.33. # # Report bugs to . # @@ -11,7 +11,7 @@ # This configure script is free software; the Free Software Foundation # gives unlimited permission to copy, distribute and modify it. # -# Copyright (C) 2018-2023 by Yuichi Kobayashi +# Copyright (C) 2018-2024 by Yuichi Kobayashi ## -------------------- ## ## M4sh Initialization. ## ## -------------------- ## @@ -582,8 +582,8 @@ MAKEFLAGS= # Identity of this package. PACKAGE_NAME='pSync' PACKAGE_TARNAME='psync' -PACKAGE_VERSION='2.32' -PACKAGE_STRING='pSync 2.32' +PACKAGE_VERSION='2.33' +PACKAGE_STRING='pSync 2.33' PACKAGE_BUGREPORT='kobayasy@kobayasy.com' PACKAGE_URL='http://kobayasy.com/psync/' @@ -1258,7 +1258,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures pSync 2.32 to adapt to many kinds of systems. +\`configure' configures pSync 2.33 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1323,7 +1323,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of pSync 2.32:";; + short | recursive ) echo "Configuration of pSync 2.33:";; esac cat <<\_ACEOF @@ -1422,14 +1422,14 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -pSync configure 2.32 +pSync configure 2.33 generated by GNU Autoconf 2.69 Copyright (C) 2012 Free Software Foundation, Inc. This configure script is free software; the Free Software Foundation gives unlimited permission to copy, distribute and modify it. -Copyright (C) 2018-2023 by Yuichi Kobayashi +Copyright (C) 2018-2024 by Yuichi Kobayashi _ACEOF exit fi @@ -1793,7 +1793,7 @@ cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by pSync $as_me 2.32, which was +It was created by pSync $as_me 2.33, which was generated by GNU Autoconf 2.69. Invocation command line was $ $0 $@ @@ -2173,9 +2173,6 @@ ac_configure="$SHELL $ac_aux_dir/configure" # Please don't use this var. - - - CONFFILE='~/.psync.conf' BACKUP='3' @@ -2187,6 +2184,9 @@ SSH='ssh' SSHPORT='22' + + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${MAKE-make} sets \$(MAKE)" >&5 $as_echo_n "checking whether ${MAKE-make} sets \$(MAKE)... " >&6; } set x ${MAKE-make} @@ -5087,7 +5087,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by pSync $as_me 2.32, which was +This file was extended by pSync $as_me 2.33, which was generated by GNU Autoconf 2.69. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -5150,7 +5150,7 @@ _ACEOF cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`" ac_cs_version="\\ -pSync config.status 2.32 +pSync config.status 2.33 configured by $0, generated by GNU Autoconf 2.69, with options \\"\$ac_cs_config\\" diff --git a/src/configure.ac b/src/configure.ac index 12f406e..2081d72 100644 --- a/src/configure.ac +++ b/src/configure.ac @@ -1,6 +1,6 @@ -# configure.ac - Last modified: 02-Apr-2023 (kobayasy) +# configure.ac - Last modified: 03-Feb-2024 (kobayasy) # -# Copyright (C) 2018-2023 by Yuichi Kobayashi +# Copyright (C) 2018-2024 by Yuichi Kobayashi # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files @@ -23,11 +23,17 @@ # SOFTWARE. AC_PREREQ(2.69) -AC_INIT([pSync], [2.32], [kobayasy@kobayasy.com], [psync], [http://kobayasy.com/psync/]) -AC_COPYRIGHT([Copyright @{:@C@:}@ 2018-2023 by Yuichi Kobayashi ]) +AC_INIT([pSync], [2.33], [kobayasy@kobayasy.com], [psync], [http://kobayasy.com/psync/]) +AC_COPYRIGHT([Copyright @{:@C@:}@ 2018-2024 by Yuichi Kobayashi ]) AC_CONFIG_SRCDIR([psync.c]) AC_CONFIG_AUX_DIR([conf]) +AC_SUBST([CONFFILE], ['~/.psync.conf']) +AC_SUBST([BACKUP], ['3']) +AC_SUBST([EXPIRE], ['400']) +AC_SUBST([SSH], ['ssh']) +AC_SUBST([SSHPORT], ['22']) + AC_DEFUN([MY_ARG_ENABLE], [AC_ARG_ENABLE([$1], [AS_HELP_STRING([--$2-$1], [$3])], [AS_CASE([$enableval], @@ -44,12 +50,6 @@ AC_DEFUN([MY_ARG_WITH], AS_UNSET([withval]) ] )], [AS_UNSET([withval])] )] ) -AC_SUBST([CONFFILE], ['~/.psync.conf']) -AC_SUBST([BACKUP], ['3']) -AC_SUBST([EXPIRE], ['400']) -AC_SUBST([SSH], ['ssh']) -AC_SUBST([SSHPORT], ['22']) - AC_PROG_MAKE_SET AC_PROG_CC AC_PROG_INSTALL diff --git a/src/info.c b/src/info.c index d955e4a..5d8d5d1 100644 --- a/src/info.c +++ b/src/info.c @@ -1,6 +1,6 @@ -/* info.c - Last modified: 29-Mar-2023 (kobayasy) +/* info.c - Last modified: 03-Feb-2024 (kobayasy) * - * Copyright (C) 2023 by Yuichi Kobayashi + * Copyright (C) 2023-2024 by Yuichi Kobayashi * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files @@ -28,15 +28,12 @@ #endif /* #ifdef HAVE_CONFIG_H */ #include -#include #include #include #include #include #include -#ifdef HAVE_TGETENT -#include -#endif /* #ifdef HAVE_TGETENT */ +#include "tpbar.h" #include "info.h" static const char *strmes(int status) { @@ -105,126 +102,6 @@ static char *strfnum(char *str, size_t length, intmax_t num) { return str; } -typedef struct { - char *up; - int co; - char *bar; - struct { - int cur; - int min, max; - } row; -#ifdef HAVE_TGETENT - char buffer[1024]; -#endif /* #ifdef HAVE_TGETENT */ -} TENT; - -static void tinit(TENT *tent) { -#ifdef HAVE_TGETENT - char ent[1024], *s; -#endif /* #ifdef HAVE_TGETENT */ - - tent->up = NULL; - tent->co = -1; - tent->bar = NULL; - tent->row.cur = 0; - tent->row.min = INT_MAX; - tent->row.max = INT_MIN; -#ifdef HAVE_TGETENT - s = getenv("TERM"); - if (s == NULL) - goto error; - if (tgetent(ent, s) != 1) - goto error; - tent->up = UP; - tent->co = tgetnum("co"); - s = tent->buffer; - if (tgetstr("mr", &s) == NULL) - goto error; - --s; - s += sprintf(s, "%%.*s"); - if (tgetstr("me", &s) == NULL) - goto error; - --s; - s += sprintf(s, "%%s"); - tent->bar = tent->buffer; -error: - ; -#endif /* #ifdef HAVE_TGETENT */ -} - -static int tgetrow(int row, TENT *tent) { - switch (row) { - case INT_MIN: - row = tent->row.min == INT_MAX ? 0 : tent->row.min - 1; - break; - case INT_MAX: - row = tent->row.max == INT_MIN ? 0 : tent->row.max + 1; - break; - default: - row += tent->row.cur; - } - return row; -} - -static int tsetrow(char *str, int row, TENT *tent) { - int length = 0; - char *s; - int n; - - switch (row) { - case INT_MAX: - case INT_MIN: - row = tgetrow(row, tent); - break; - } - row -= tent->row.cur; - s = str; - if (row > 0) { - memset(s, '\n', row); - length += row, s += row; - *s = 0; - tent->row.cur += row; - } - else { - *s++ = '\r', ++length; - if (tent->up != NULL && row < 0) { - do { - n = sprintf(s, "%s", tent->up); - length += n, s += n; - --tent->row.cur; - } while (++row < 0); - } - } - if (tent->row.cur < tent->row.min) - tent->row.min = tent->row.cur; - if (tent->row.cur > tent->row.max) - tent->row.max = tent->row.cur; - return length; -} - -static int tbar(char *str, intmax_t current, intmax_t goal, TENT *tent, - const char *format, ... ) { - int length = -1; - va_list ap; - unsigned int n; - char *s; - - va_start(ap, format); - length = vsprintf(str, format, ap); - va_end(ap); - if (length < 0 || tent->bar == NULL) - n = 0; - else if (current < goal) - n = current * length / goal; - else - n = length; - if (n > 0 && (s = strdup(str)) != NULL) { - length = sprintf(str, tent->bar, n, s, s + n); - free(s); - } - return length; -} - typedef struct { int sid; int row; @@ -240,17 +117,17 @@ typedef struct { struct { int sid[2]; PROGRESS progress[2]; - TENT tent; + TPBAR tpbar; size_t namelen; } g; void info_init(size_t namelen) { g.sid[0] = 0, g.sid[1] = 0; g.progress[0].sid = INT_MIN, g.progress[1].sid = INT_MIN; - tinit(&g.tent); + tpbar_init(&g.tpbar); g.namelen = namelen; - if (g.tent.co < g.namelen + 1+27+1) - g.tent.up = NULL; + if (g.tpbar.co < g.namelen + 1+27+1) + g.tpbar.up = NULL; } void info_print(unsigned int host, const char *line) { @@ -277,7 +154,7 @@ void info_print(unsigned int host, const char *line) { case '[': if (progress->sid != g.sid[host]) { progress->sid = g.sid[host]; - progress->row = tgetrow(INT_MAX, &g.tent); + progress->row = tpbar_getrow(INT_MAX, &g.tpbar); strcpy(progress->name, line); memset(progress->host, 0, sizeof(progress->host)); update = 1; @@ -287,31 +164,31 @@ void info_print(unsigned int host, const char *line) { ++g.sid[host]; break; case 'S': - if (g.tent.up != NULL) { + if (g.tpbar.up != NULL) { progress->host[host].filescan = strtoll(line, NULL, 10); update = 2; } break; case 'U': - if (g.tent.up != NULL) { + if (g.tpbar.up != NULL) { progress->host[host].upload = strtoll(line, NULL, 10); update = 3; } break; case 'D': - if (g.tent.up != NULL) { + if (g.tpbar.up != NULL) { progress->host[host].downloaded = strtoll(line, NULL, 10); update = 3; } break; case 'R': - if (g.tent.up != NULL) { + if (g.tpbar.up != NULL) { progress->host[host].fileremove = strtoll(line, NULL, 10); update = 4; } break; case 'C': - if (g.tent.up != NULL) { + if (g.tpbar.up != NULL) { progress->host[host].filecopy = strtoll(line, NULL, 10); update = 4; } @@ -320,34 +197,34 @@ void info_print(unsigned int host, const char *line) { switch (update) { case -1: s = buffer; - s += tsetrow(s, INT_MAX, &g.tent); + s += tpbar_setrow(s, INT_MAX, &g.tpbar); s += sprintf(s, "%s", line); write(STDOUT_FILENO, buffer, s - buffer); break; case -2: s = buffer; - s += tsetrow(s, INT_MAX, &g.tent); + s += tpbar_setrow(s, INT_MAX, &g.tpbar); s += sprintf(s, "%s%s", hostname[host], line); write(STDOUT_FILENO, buffer, s - buffer); break; case -3: s = buffer; - s += tsetrow(s, INT_MAX, &g.tent); + s += tpbar_setrow(s, INT_MAX, &g.tpbar); s += sprintf(s, "%sError - %s", hostname[host], line); write(STDOUT_FILENO, buffer, s - buffer); break; case 1: s = buffer; - s += tsetrow(s, progress->row, &g.tent); + s += tpbar_setrow(s, progress->row, &g.tpbar); s += sprintf(s, "%s", progress->name); write(STDOUT_FILENO, buffer, s - buffer); break; case 2: s = buffer; - s += tsetrow(s, progress->row, &g.tent); + s += tpbar_setrow(s, progress->row, &g.tpbar); s += sprintf(s, "%-*s ", (int)g.namelen, progress->name); - s += tbar(s, 0, 1, &g.tent, "[%24jd ]", - progress->host[0].filescan + progress->host[1].filescan ); + s += tpbar_printf(s, 0, 1, &g.tpbar, "[%24jd ]", + progress->host[0].filescan + progress->host[1].filescan ); write(STDOUT_FILENO, buffer, s - buffer); break; case 3: @@ -362,9 +239,9 @@ void info_print(unsigned int host, const char *line) { else *buffer2 = 0; s = buffer; - s += tsetrow(s, progress->row, &g.tent); + s += tpbar_setrow(s, progress->row, &g.tpbar); s += sprintf(s, "%-*s ", (int)g.namelen, progress->name); - s += tbar(s, n1, n2, &g.tent, "[%11s /%11s ]", buffer1, buffer2); + s += tpbar_printf(s, n1, n2, &g.tpbar, "[%11s /%11s ]", buffer1, buffer2); write(STDOUT_FILENO, buffer, s - buffer); break; case 4: @@ -379,9 +256,9 @@ void info_print(unsigned int host, const char *line) { else *buffer2 = 0; s = buffer; - s += tsetrow(s, progress->row, &g.tent); + s += tpbar_setrow(s, progress->row, &g.tpbar); s += sprintf(s, "%-*s ", (int)g.namelen, progress->name); - s += tbar(s, 1, 1, &g.tent, "[%11s :%11s ]", buffer1, buffer2); + s += tpbar_printf(s, 1, 1, &g.tpbar, "[%11s :%11s ]", buffer1, buffer2); write(STDOUT_FILENO, buffer, s - buffer); break; } diff --git a/src/tpbar.c b/src/tpbar.c new file mode 100644 index 0000000..53c1262 --- /dev/null +++ b/src/tpbar.c @@ -0,0 +1,146 @@ +/* tpbar.c - Last modified: 03-Feb-2024 (kobayasy) + * + * Copyright (C) 2023-2024 by Yuichi Kobayashi + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* #ifdef HAVE_CONFIG_H */ + +#include +#include +#include +#include +#include +#include +#ifdef HAVE_TGETENT +#include +#endif /* #ifdef HAVE_TGETENT */ +#include "tpbar.h" + +void tpbar_init(TPBAR *tpbar) { +#ifdef HAVE_TGETENT + char ent[1024], *s; +#endif /* #ifdef HAVE_TGETENT */ + + tpbar->up = NULL; + tpbar->co = -1; + tpbar->bar = NULL; + tpbar->row.cur = 0; + tpbar->row.min = INT_MAX; + tpbar->row.max = INT_MIN; +#ifdef HAVE_TGETENT + s = getenv("TERM"); + if (s == NULL) + goto error; + if (tgetent(ent, s) != 1) + goto error; + tpbar->up = UP; + tpbar->co = tgetnum("co"); + s = tpbar->buffer; + if (tgetstr("mr", &s) == NULL) + goto error; + --s; + s += sprintf(s, "%%.*s"); + if (tgetstr("me", &s) == NULL) + goto error; + --s; + s += sprintf(s, "%%s"); + tpbar->bar = tpbar->buffer; +error: + ; +#endif /* #ifdef HAVE_TGETENT */ +} + +int tpbar_getrow(int row, TPBAR *tpbar) { + switch (row) { + case INT_MIN: + row = tpbar->row.min == INT_MAX ? 0 : tpbar->row.min - 1; + break; + case INT_MAX: + row = tpbar->row.max == INT_MIN ? 0 : tpbar->row.max + 1; + break; + default: + row += tpbar->row.cur; + } + return row; +} + +int tpbar_setrow(char *str, int row, TPBAR *tpbar) { + int length = 0; + char *s; + int n; + + switch (row) { + case INT_MAX: + case INT_MIN: + row = tpbar_getrow(row, tpbar); + break; + } + row -= tpbar->row.cur; + s = str; + if (row > 0) { + memset(s, '\n', row); + length += row, s += row; + *s = 0; + tpbar->row.cur += row; + } + else { + *s++ = '\r', ++length; + if (tpbar->up != NULL && row < 0) { + do { + n = sprintf(s, "%s", tpbar->up); + length += n, s += n; + --tpbar->row.cur; + } while (++row < 0); + } + } + if (tpbar->row.cur < tpbar->row.min) + tpbar->row.min = tpbar->row.cur; + if (tpbar->row.cur > tpbar->row.max) + tpbar->row.max = tpbar->row.cur; + return length; +} + +int tpbar_printf(char *str, intmax_t current, intmax_t goal, TPBAR *tpbar, + const char *format, ... ) { + int length = -1; + va_list ap; + unsigned int n; + char *s; + + va_start(ap, format); + length = vsprintf(str, format, ap); + va_end(ap); + if (length < 0 || tpbar->bar == NULL) + n = 0; + else if (current < goal) + n = current * length / goal; + else + n = length; + if (n > 0 && (s = strdup(str)) != NULL) { + length = sprintf(str, tpbar->bar, n, s, s + n); + free(s); + } + return length; +} diff --git a/src/tpbar.h b/src/tpbar.h new file mode 100644 index 0000000..c516acb --- /dev/null +++ b/src/tpbar.h @@ -0,0 +1,53 @@ +/* tpbar.h - Last modified: 03-Feb-2024 (kobayasy) + * + * Copyright (C) 2023-2024 by Yuichi Kobayashi + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _INCLUDE_tpbar_h +#define _INCLUDE_tpbar_h + +#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* #ifdef HAVE_CONFIG_H */ + +typedef struct { + char *up; + int co; + char *bar; + struct { + int cur; + int min, max; + } row; +#ifdef HAVE_TGETENT + char buffer[1024]; +#endif /* #ifdef HAVE_TGETENT */ +} TPBAR; + +extern void tpbar_init(TPBAR *tpbar); +extern int tpbar_getrow(int row, TPBAR *tpbar); +extern int tpbar_setrow(char *str, int row, TPBAR *tpbar); +extern int tpbar_printf(char *str, intmax_t current, intmax_t goal, TPBAR *tpbar, + const char *format, ... ); + +#endif /* #ifndef _INCLUDE_tpbar_h */