commit 092e71b1e89970ad7ccacd9d321e1a9cef8cfc18
parent 0f3877d4c6ca07e205215ae38e90e67c0c3d2e44
Author: Michael Skec
Date: Mon, 25 Dec 2023 18:14:13 +1100
Add animated fetch indicator ellipsis
Diffstat:
M | mailstatus.c | | | 54 | +++++++++++++++++++++++++++++++++++++++++++++++++----- |
1 file changed, 49 insertions(+), 5 deletions(-)
diff --git a/mailstatus.c b/mailstatus.c
@@ -7,6 +7,7 @@
#include <string.h>
#include <sys/inotify.h>
#include <sys/types.h>
+#include <time.h>
#include <unistd.h>
/* Maximum bytes that can be displayed. */
@@ -28,6 +29,7 @@ static volatile sig_atomic_t _sigint = 0;
/* Path of "fetching" indication file to watch */
static int _fetch = 0;
static char _f_indicator[FILENAME_MAX];
+static int _f_ellipsis = 0;
static void
sigint_handler(int param)
@@ -37,6 +39,11 @@ sigint_handler(int param)
fprintf(stdout, "caught SIGINT\n");
}
+#define FETCHMSG "Fetching mail "
+#define FETCHMSG_ELLIPSIS_RATE 300
+#define FETCHMSG_ELLIPSIS "..."
+#define FETCHMSG_ELLIPSIS_LEN (sizeof(FETCHMSG_ELLIPSIS) - 1)
+
static void
update_status(void)
{
@@ -46,7 +53,14 @@ update_status(void)
if (_fetch)
{
_buffer_len = snprintf(_buffer, sizeof(_buffer),
- "Fetching mail...");
+ FETCHMSG FETCHMSG_ELLIPSIS);
+
+ /* Animate the ellipsis by truncating the string */
+ _buffer_len = _buffer_len - FETCHMSG_ELLIPSIS_LEN + _f_ellipsis;
+ _buffer[_buffer_len] = '\0';
+ ++_f_ellipsis;
+ _f_ellipsis %= (FETCHMSG_ELLIPSIS_LEN + 1);
+
return;
}
@@ -93,6 +107,18 @@ print_status(void)
fflush(stdout);
}
+/* sleep for milliseconds https://stackoverflow.com/a/1157217 */
+static void
+msleep(long msec)
+{
+ struct timespec ts;
+
+ ts.tv_sec = msec / 1000;
+ ts.tv_nsec = (msec % 1000) * 1000000;
+
+ nanosleep(&ts, NULL);
+}
+
int
main(int argc, char **argv)
{
@@ -160,6 +186,12 @@ main(int argc, char **argv)
struct sigaction sigact = { sigint_handler };
sigaction(SIGINT, &sigact, NULL);
+ /* Initial check for fetch indicator file */
+ if (access(_f_indicator, F_OK) == 0)
+ {
+ _fetch = 1;
+ }
+
/* Print initial mail status */
update_status();
print_status();
@@ -206,18 +238,30 @@ main(int argc, char **argv)
}
}
- if (fetch != -1 || count > 0)
+ if (fetch != -1)
{
- if (fetch != -1)
- _fetch = fetch;
+ _fetch = fetch;
+ _f_ellipsis = 0;
+ }
+ if (fetch == 0 || count > 0)
+ {
/* Update mail status */
update_status();
print_status();
}
}
- sleep(1);
+ if (_fetch)
+ {
+ msleep(FETCHMSG_ELLIPSIS_RATE);
+ update_status();
+ print_status();
+ }
+ else
+ {
+ msleep(500);
+ }
}
exit: