From 7a392b2a6351e527789fa7718505b14afb4b224a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Thu, 20 Jun 2024 00:47:11 +0900 Subject: [PATCH] =?UTF-8?q?=E8=B5=B7=E5=8B=95=E6=99=82=E9=96=93=E3=81=A70?= =?UTF-8?q?=E6=97=A5=E3=82=82=E5=8F=AF=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 44 ++++++++++++++++++++++++-------------------- src/uptime.c | 38 +++++++++++++++++++++++++++++++++----- src/uptime.h | 4 ++-- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/main.c b/main.c index d06846b..81f31ef 100644 --- a/main.c +++ b/main.c @@ -40,11 +40,9 @@ int main(int argc, char *argv[]) { #include "src/logo/linux.h" getDistro(distroname); #else -const char *color = MAGENTA; -const char *titlecolor = MAGENTA; -size_t logosize = 11; -#define COLOR "\e[1;30m" -#define RESET "\e[0m" + const char *color = MAGENTA; + const char *titlecolor = MAGENTA; + size_t logosize = 11; char *LOGO[] = { " ⢀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⣠⠾⠛⠶⣄⢀⣠⣤⠴⢦⡀⠀⠀⠀⠀", "⠀⠀⠀⢠⡿⠉⠉⠉⠛⠶⠶⠖⠒⠒⣾⠋⠀⢀⣀⣙⣯⡁⠀⠀⠀⣿⠀⠀⠀⠀", @@ -83,8 +81,8 @@ size_t logosize = 11; ls = ne >= MIN_SIZE ? MIN_SIZE : ne; } - printf("%s ", LOGO[lc]); if (display_user_name() || display_user_host()) { + printf("%s ", LOGO[lc]); printf( "%s%s%s@%s%s%s\n", titlecolor, display_user_name(), reset, @@ -97,15 +95,15 @@ size_t logosize = 11; printf("------------------\n"); lc++; - printf("%s ", LOGO[lc]); if (display_os()) { + printf("%s ", LOGO[lc]); printf("%sOS%s: %s\n", color, reset, display_os()); lc++; } #if defined(__linux__) - printf("%s ", LOGO[lc]); if (display_distro()) { + printf("%s ", LOGO[lc]); printf("%sDistro%s: %s\n", color, reset, display_distro()); lc++; } @@ -117,17 +115,23 @@ size_t logosize = 11; printf("\n"); lc++; - printf("%s ", LOGO[lc]); - printf("%s%s%s%s", color, "Uptime", reset, ": "); - display_days(); - printf(", "); - display_time(); - printf("\n"); - lc++; + if (display_days() || display_time()) { + printf("%s ", LOGO[lc]); + printf("%s%s%s%s", color, "Uptime", reset, ": "); + if (display_days()) { + printf("%s", display_days()); + if (display_time()) printf(" "); + } + if (display_time()) { + printf("%s", display_time()); + } + printf("\n"); + lc++; + } #if defined(__OpenBSD__) - printf("%s ", LOGO[lc]); if (display_recording_audio() || display_recording_video()) { + printf("%s ", LOGO[lc]); printf("%sRecording%s: ", color, reset); if (display_recording_audio()) { printf("audio = %s", display_recording_audio()); @@ -141,26 +145,26 @@ size_t logosize = 11; } #endif - printf("%s ", LOGO[lc]); if (display_packages()) { + printf("%s ", LOGO[lc]); printf("%sPackages%s: %s\n", color, reset, display_packages()); lc++; } - printf("%s ", LOGO[lc]); if (display_resolution()) { + printf("%s ", LOGO[lc]); printf("%sResolution%s: %s\n", color, reset, display_resolution()); lc++; } - printf("%s ", LOGO[lc]); if (display_cpu()) { + printf("%s ", LOGO[lc]); printf("%sCPU%s: %s\n", color, reset, display_cpu()); lc++; } - printf("%s ", LOGO[lc]); if (display_gpu()) { + printf("%s ", LOGO[lc]); printf("%sGPU%s: %s\n", color, reset, display_gpu()); lc++; } diff --git a/src/uptime.c b/src/uptime.c index 56ea6b7..269cdda 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -2,12 +2,40 @@ #include "common.h" #include +#include +#include -void display_days() { - printf("%s", run_command_s("uptime | awk '{print $3}' && echo \" days\"")); +const char *display_days() { + const char *days = run_command_s("uptime | awk '{for (i=1; i<=NF; i++) " + "if ($i ~ /day/) print $(i-1), $i}'"); + + if (days == NULL || strlen(days) == 0) { + return strndup("0 days,", 7); + } + + return days; } -void display_time() { - printf("%s", run_command_s("uptime | awk '{print $5}' | sed 's/,//' | " - "sed 's/:/ hours, /' && echo \" mins\"")); +const char *display_time() { + const char *uptime = run_command_s("uptime"); + if (uptime == NULL) { + return NULL; + } + + char *days = strstr(uptime, "day"); + int nodays = 0; + if (days == NULL) { + nodays = 1; + days = strstr(uptime, "days"); + } + + free((void *)uptime); + + if (nodays) { + return run_command_s("uptime | awk '{print $3}' | sed 's/,//' | " + "sed 's/:/ hours, /' && echo \" mins\""); + } + + return run_command_s("uptime | awk '{print $5}' | sed 's/,//' | " + "sed 's/:/ hours, /' && echo \" mins\""); } diff --git a/src/uptime.h b/src/uptime.h index 12ee668..92ba7ad 100644 --- a/src/uptime.h +++ b/src/uptime.h @@ -1,7 +1,7 @@ #ifndef UPTIME_H #define UPTIME_H -void display_days(); -void display_time(); +const char *display_days(); +const char *display_time(); #endif