From 3e5f378f663db4bde69a3849d567c6d1a32616fc Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sun, 10 Dec 2023 12:03:47 +0100 Subject: [PATCH] Improved status display --- src/common/common.c | 2 ++ src/common/display_status.c | 58 +++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/common/common.c b/src/common/common.c index bffcd7b..6f92932 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -45,6 +45,8 @@ void print_help_common(void) printf(" a0c: national number\n"); printf(" bXc: international number, digit X is removed\n"); printf(" and are separated automatically by known list of country codes.\n"); + printf("\n"); + printf("Press 'c' to toggle status display on or off.\n"); } /* diff --git a/src/common/display_status.c b/src/common/display_status.c index c59ff8f..df86df8 100644 --- a/src/common/display_status.c +++ b/src/common/display_status.c @@ -96,11 +96,60 @@ void display_status_start(void) line_count = 1; } +const char *display_digits(const char *digits) +{ + static char display[256]; + int i, j; + + if (*digits == '\0') + return ""; + + for (i = 0, j = 0; digits[i]; i++) { + /* We need minimum of 5 digits space: e.g. '-C11\0' */ + if (j + 5 > (int)sizeof(display)) + break; + /* if this is not the first digits && this is special digit or previous digit was special digit */ + if (i && ((digits[i - 1] >= 'a' && digits[i - 1] <= 'e') || (digits[i] >= 'a' && digits[i] <= 'e'))) + display[j++] = '-'; + switch (digits[i]) { + case 'a': + display[j++] = 'K'; + display[j++] = 'P'; + display[j++] = '1'; + break; + case 'b': + display[j++] = 'K'; + display[j++] = 'P'; + display[j++] = '2'; + break; + case 'c': + display[j++] = 'S'; + display[j++] = 'T'; + break; + case 'd': + display[j++] = 'C'; + display[j++] = '1'; + display[j++] = '1'; + break; + case 'e': + display[j++] = 'C'; + display[j++] = '1'; + display[j++] = '2'; + break; + default: + display[j++] = digits[i]; + } + } + + display[j] = '\0'; + return display; +} + void display_status_line(const char *if_name, int link_count, const char *from_id, const char *to_id, const char *state_name) { char line[MAX_DISPLAY_WIDTH + 4096]; char color[MAX_DISPLAY_WIDTH + 4096]; - int index, from_len, to_len; + int index, from_len; memset(color, 7, sizeof(color)); // make valgrind happy @@ -126,13 +175,12 @@ void display_status_line(const char *if_name, int link_count, const char *from_i if (from_id && to_id) { /* '' */ - sprintf(line + index, " \'%s\' -> \'%s\' ", from_id, to_id); + sprintf(line + index, " \'%s\' -> \'%s\' (%s) ", from_id, to_id, display_digits(to_id)); from_len = strlen(from_id); - to_len = strlen(to_id); memset(color + index, 1, 4 + from_len); memset(color + index + 4 + from_len, 7, 2); - memset(color + index + 4 + from_len + 2, 2, 4 + to_len); - index += 4 + from_len + 2 + 4 + to_len; + memset(color + index + 4 + from_len + 2, 2, strlen(line + index) - 4 - from_len - 2); + index += strlen(line + index); line[index] = '\0'; strcpy(line + index, state_name);