From fd450bcccc49a5781b0de169d809af824ee1d84a Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Fri, 25 Feb 2022 13:30:37 +0100 Subject: [PATCH] Date Time support --- CMakeLists.txt | 4 +++ README.md | 4 +-- completions/bash | 9 +++++ presets/all | 2 +- presets/verbose | 1 + src/common/init.c | 6 ++++ src/data/config.txt | 2 ++ src/data/help.txt | 6 ++++ src/data/modules.txt | 3 ++ src/detection/datetime.c | 73 ++++++++++++++++++++++++++++++++++++++++ src/fastfetch.c | 43 +++++++++++++++++++++++ src/fastfetch.h | 39 +++++++++++++++++++++ src/flashfetch.c | 3 ++ src/modules/date.c | 18 ++++++++++ src/modules/datetime.c | 46 +++++++++++++++++++++++++ src/modules/time.c | 18 ++++++++++ tests/performance.c | 1 + 17 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 src/detection/datetime.c create mode 100644 src/modules/date.c create mode 100644 src/modules/datetime.c create mode 100644 src/modules/time.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e645ab98f..41f67b457 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,7 @@ add_library(libfastfetch STATIC src/detection/gtk.c src/detection/terminalShell.c src/detection/media.c + src/detection/datetime.c src/detection/displayserver/displayServer.c src/detection/displayserver/wayland.c src/detection/displayserver/xcb.c @@ -126,6 +127,9 @@ add_library(libfastfetch STATIC src/modules/publicip.c src/modules/player.c src/modules/song.c + src/modules/datetime.c + src/modules/date.c + src/modules/time.c src/modules/colors.c ) diff --git a/README.md b/README.md index 42dd876d9..53fd22656 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,9 @@ The following libraries are used if present at runtime: ## Support status All categories not listed here should work without needing a specific implementation. -#### Available Modules +##### Available Modules ``` -Title, Separator, OS, Host, Kernel, Uptime, Processes, Packages, Shell, Resolution, DE, WM, WMTheme, Theme, Icons, Font, Cursor, Terminal, Terminal Font, CPU, CPUUsage, GPU, Memory, Disk, Battery, Player, Song, LocalIP, PublicIP, Locale, Colors, Break, Custom +Title, Separator, OS, Host, Kernel, Uptime, Processes, Packages, Shell, Resolution, DE, WM, WMTheme, Theme, Icons, Font, Cursor, Terminal, Terminal Font, CPU, CPUUsage, GPU, Memory, Disk, Battery, Player, Song, LocalIP, PublicIP, DateTime, Date, Time, Locale, Colors, Break, Custom ``` ##### Logos diff --git a/completions/bash b/completions/bash index 5185be387..1b1df9d1d 100644 --- a/completions/bash +++ b/completions/bash @@ -28,6 +28,9 @@ __fastfetch_complete_help() "locale-format" "local-ip-format" "public-ip-format" + "datetime-format" + "date-format" + "time-format" ) COMPREPLY=($(compgen -W "${__ff_helps[*]}" -- "$CURRENT_WORD")) } @@ -198,6 +201,12 @@ __fastfetch_completion() "--player-key" "--song-format" "--song-key" + "--datetime-key" + "--datetime-format" + "--date-format" + "--date-key" + "--time-format" + "--time-key" ) local FF_OPTIONS_PATH=( diff --git a/presets/all b/presets/all index d1d230c9e..ecc086397 100644 --- a/presets/all +++ b/presets/all @@ -1 +1 @@ ---structure Title:Separator:OS:Host:Kernel:Uptime:Processes:Packages:Shell:Resolution:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:GPU:Memory:Disk:Battery:Player:Song:PublicIP:LocalIP:Locale:Break:Colors +--structure Title:Separator:OS:Host:Kernel:Uptime:Processes:Packages:Shell:Resolution:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:GPU:Memory:Disk:Battery:Player:Song:PublicIP:LocalIP:DateTime:Locale:Break:Colors diff --git a/presets/verbose b/presets/verbose index ea3fa84a5..9f65e6159 100644 --- a/presets/verbose +++ b/presets/verbose @@ -25,3 +25,4 @@ --battery-format "Manufactor: {}; Model: {}; Technology: {}; Capacaty: {}; Status: {}" --player-format "Name: {}" --song-format "Song: {}; Artist: {}; Album: {}" +--datetime-format "year: {}; yearShort: {}; month: {}; monthPretty: {}; monthName: {}; monthNameShort: {}; weekNumber: {}; weekday: {}; weekdayShort: {}; dayInYear: {}; dayInMonth: {}; dayInWeek: {}; hour: {}; hourPretty: {}; hour12: {}; hour12Pretty: {}; minute: {}; minutePretty: {}; second: {}; secondPretty: {}" diff --git a/src/common/init.c b/src/common/init.c index e8e53520d..48ec48902 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -178,6 +178,12 @@ static void defaultConfig(FFinstance* instance) ffStrbufInitA(&instance->config.playerFormat, 0); ffStrbufInitA(&instance->config.songKey, 0); ffStrbufInitA(&instance->config.songFormat, 0); + ffStrbufInitA(&instance->config.dateTimeKey, 0); + ffStrbufInitA(&instance->config.dateTimeFormat, 0); + ffStrbufInitA(&instance->config.dateKey, 0); + ffStrbufInitA(&instance->config.dateFormat, 0); + ffStrbufInitA(&instance->config.timeKey, 0); + ffStrbufInitA(&instance->config.timeFormat, 0); ffStrbufInitA(&instance->config.libPCI, 0); ffStrbufInitA(&instance->config.libVulkan, 0); diff --git a/src/data/config.txt b/src/data/config.txt index 87df0bde1..990c4d339 100644 --- a/src/data/config.txt +++ b/src/data/config.txt @@ -170,6 +170,7 @@ #--public-ip-key Public IP #--player-key Media Player #--song-key Song +#--datetime-key Date Time # Format options: # Sets the format string for module values. @@ -204,6 +205,7 @@ #--public-ip-format #--player-format #--song-format +#--datetime-format # Library options: # Sets an user specific path to a library to load. diff --git a/src/data/help.txt b/src/data/help.txt index 765dde5c3..298ad2d97 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -64,6 +64,9 @@ Format options: Provide the format string for custom output. Use fastfetch --hel --public-ip-format --player-format --song-format + --datetime-format + --time-format + --date-format Key options: Provide a custom key for an output --os-key @@ -94,6 +97,9 @@ Key options: Provide a custom key for an output --public-ip-key --player-key --song-key + --datetime-key + --time-key + --date-key Library options: Set the path of a library to load --lib-PCI diff --git a/src/data/modules.txt b/src/data/modules.txt index b4d7d8517..25416f7cc 100644 --- a/src/data/modules.txt +++ b/src/data/modules.txt @@ -4,6 +4,8 @@ Colors CPU CPUUsage Cursor +Date +Datetime DE Disk Font @@ -26,6 +28,7 @@ Song Terminal TerminalFont Theme +Time Title Uptime WM diff --git a/src/detection/datetime.c b/src/detection/datetime.c new file mode 100644 index 000000000..139164784 --- /dev/null +++ b/src/detection/datetime.c @@ -0,0 +1,73 @@ +#include "fastfetch.h" + +#include + +#include + +const FFDateTimeResult* ffDetectDateTime(const FFinstance* instance) +{ + FF_UNUSED(instance); //We may need it later for additional configuration + + static FFDateTimeResult result; + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static bool init = false; + + pthread_mutex_lock(&mutex); + if (init) + { + pthread_mutex_unlock(&mutex); + return &result; + } + init = true; + + const time_t t = time(NULL); + struct tm* tm = localtime(&t); + + result.year = (uint16_t) tm->tm_year + 1900; + result.yearShort = (uint8_t) (result.year % 100); + result.month = (uint8_t) tm->tm_mon + 1; + + ffStrbufInit(&result.monthPretty); + result.monthPretty.length = (uint32_t) strftime(result.monthPretty.chars, ffStrbufGetFree(&result.monthPretty), "%m", tm); + + ffStrbufInit(&result.monthName); + result.monthName.length = (uint32_t) strftime(result.monthName.chars, ffStrbufGetFree(&result.monthName), "%B", tm); + + ffStrbufInit(&result.monthNameShort); + result.monthNameShort.length = (uint32_t) strftime(result.monthNameShort.chars, ffStrbufGetFree(&result.monthNameShort), "%b", tm); + + result.week = (uint8_t) (tm->tm_yday / 7 + 1); + + ffStrbufInit(&result.weekday); + result.weekday.length = (uint32_t) strftime(result.weekday.chars, ffStrbufGetFree(&result.weekday), "%A", tm); + + ffStrbufInit(&result.weekdayShort); + result.weekdayShort.length = (uint32_t) strftime(result.weekdayShort.chars, ffStrbufGetFree(&result.weekdayShort), "%a", tm); + + result.dayInYear = (uint8_t) tm->tm_yday + 1; + result.dayInMonth = (uint8_t) tm->tm_mday; + result.dayInWeek = tm->tm_wday == 0 ? 7 : (uint8_t) tm->tm_wday; + + result.hour = (uint8_t) tm->tm_hour; + + ffStrbufInit(&result.hourPretty); + result.hourPretty.length = (uint32_t) strftime(result.hourPretty.chars, ffStrbufGetFree(&result.hourPretty), "%H", tm); + + result.hour12 = (uint8_t) (result.hour % 12); + + ffStrbufInit(&result.hour12Pretty); + result.hour12Pretty.length = (uint32_t) strftime(result.hour12Pretty.chars, ffStrbufGetFree(&result.hour12Pretty), "%I", tm); + + result.minute = (uint8_t) tm->tm_min; + + ffStrbufInit(&result.minutePretty); + result.minutePretty.length = (uint32_t) strftime(result.minutePretty.chars, ffStrbufGetFree(&result.minutePretty), "%M", tm); + + result.second = (uint8_t) tm->tm_sec; + + ffStrbufInit(&result.secondPretty); + result.secondPretty.length = (uint32_t) strftime(result.secondPretty.chars, ffStrbufGetFree(&result.secondPretty), "%S", tm); + + pthread_mutex_unlock(&mutex); + return &result; +} diff --git a/src/fastfetch.c b/src/fastfetch.c index 707ef15b8..96f5a4bd4 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -316,6 +316,31 @@ static inline void printCommandHelp(const char* command) "Album name" ); } + else if(strcasecmp(command, "datetime-format") == 0 || strcasecmp(command, "date-format") == 0 || strcasecmp(command, "time-format") == 0) + { + constructAndPrintCommandHelpFormat("[date][time]", "{1}-{4}-{11} {14}:{18}:{20}", 20, + "year", + "last two digits of year", + "month", + "month with leading zero", + "month name", + "month name short", + "week number on year", + "weekday", + "weekday short", + "day in year", + "day in month", + "day in Week", + "hour", + "hout with leading zero", + "hour 12h format", + "hour 12h format with leading zero", + "minute", + "minute with leading zero", + "second", + "second with leading zero" + ); + } else fprintf(stderr, "No specific help for command %s provided\n", command); } @@ -826,6 +851,18 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.songKey); else if(strcasecmp(key, "--song-format") == 0) optionParseString(key, value, &instance->config.songFormat); + else if(strcasecmp(key, "--datetime-key") == 0) + optionParseString(key, value, &instance->config.dateTimeKey); + else if(strcasecmp(key, "--datetime-format") == 0) + optionParseString(key, value, &instance->config.dateTimeFormat); + else if(strcasecmp(key, "--date-key") == 0) + optionParseString(key, value, &instance->config.dateKey); + else if(strcasecmp(key, "--date-format") == 0) + optionParseString(key, value, &instance->config.dateFormat); + else if(strcasecmp(key, "--time-key") == 0) + optionParseString(key, value, &instance->config.timeKey); + else if(strcasecmp(key, "--time-format") == 0) + optionParseString(key, value, &instance->config.timeFormat); else if(strcasecmp(key, "--lib-PCI") == 0) optionParseString(key, value, &instance->config.libPCI); else if(strcasecmp(key, "--lib-vulkan") == 0) @@ -1022,6 +1059,12 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintPlayer(instance); else if(strcasecmp(line, "song") == 0) ffPrintSong(instance); + else if(strcasecmp(line, "datetime") == 0) + ffPrintDateTime(instance); + else if(strcasecmp(line, "date") == 0) + ffPrintDate(instance); + else if(strcasecmp(line, "time") == 0) + ffPrintTime(instance); else if(strcasecmp(line, "colors") == 0) ffPrintColors(instance); else diff --git a/src/fastfetch.h b/src/fastfetch.h index a463e44f7..88fd2c0a4 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -114,6 +114,12 @@ typedef struct FFconfig FFstrbuf playerFormat; FFstrbuf songKey; FFstrbuf songFormat; + FFstrbuf dateTimeKey; + FFstrbuf dateTimeFormat; + FFstrbuf dateKey; + FFstrbuf dateFormat; + FFstrbuf timeKey; + FFstrbuf timeFormat; FFstrbuf libPCI; FFstrbuf libVulkan; @@ -257,6 +263,31 @@ typedef struct FFMediaResult FFstrbuf album; } FFMediaResult; +typedef struct FFDateTimeResult +{ + //Examples for 21.02.2022 - 15:18:37 + uint16_t year; //2022 + uint8_t yearShort; //22 + uint8_t month; //2 + FFstrbuf monthPretty; //02 + FFstrbuf monthName; //February + FFstrbuf monthNameShort; //Feb + uint8_t week; //8 + FFstrbuf weekday; //Monday + FFstrbuf weekdayShort; //Mon + uint16_t dayInYear; //52 + uint8_t dayInMonth; //21 + uint8_t dayInWeek; //1 + uint8_t hour; //15 + FFstrbuf hourPretty; //15 + uint8_t hour12; //3 + FFstrbuf hour12Pretty; //03 + uint8_t minute; //18 + FFstrbuf minutePretty; //18 + uint8_t second; //37 + FFstrbuf secondPretty; //37 +} FFDateTimeResult; + typedef enum FFformatargtype { FF_FORMAT_ARG_TYPE_NULL = 0, @@ -466,6 +497,9 @@ const FFTempsResult* ffDetectTemps(const FFinstance* instance); //detection/media.c const FFMediaResult* ffDetectMedia(FFinstance* instance); +//detection/dateTime.c +const FFDateTimeResult* ffDetectDateTime(const FFinstance* instance); + /********************/ /* Module functions */ /********************/ @@ -473,6 +507,8 @@ const FFMediaResult* ffDetectMedia(FFinstance* instance); //Common const FFTitleResult* ffDetectTitle(FFinstance* instance); +void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFstrbuf* key, const FFstrbuf* format); + //Printing void ffPrintCustom(FFinstance* instance, const char* key, const char* value); @@ -505,6 +541,9 @@ void ffPrintBattery(FFinstance* instance); void ffPrintLocale(FFinstance* instance); void ffPrintPlayer(FFinstance* instance); void ffPrintSong(FFinstance* instance); +void ffPrintDateTime(FFinstance* instance); +void ffPrintDate(FFinstance* instance); +void ffPrintTime(FFinstance* instance); void ffPrintLocalIp(FFinstance* instance); void ffPrintPublicIp(FFinstance* instance); void ffPrintColors(FFinstance* instance); diff --git a/src/flashfetch.c b/src/flashfetch.c index 013f8da3b..313de8977 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -50,6 +50,9 @@ int main(int argc, char** argv) //ffPrintLocalIp(&instance); //ffPrintPublicIp(&instance); ffPrintLocale(&instance); + //ffPrintDateTime(&instance); + //ffPrintDate(&instance); + //ffPrintTime(&instance); ffPrintBreak(&instance); ffPrintColors(&instance); diff --git a/src/modules/date.c b/src/modules/date.c new file mode 100644 index 000000000..075acf7eb --- /dev/null +++ b/src/modules/date.c @@ -0,0 +1,18 @@ +#include "fastfetch.h" + +#define FF_DATE_MODULE_NAME "Date" + +void ffPrintDate(FFinstance* instance) +{ + if(instance->config.dateFormat.length > 0) + { + ffPrintDateTimeFormat(instance, FF_DATE_MODULE_NAME, &instance->config.dateKey, &instance->config.dateFormat); + return; + } + + const FFDateTimeResult* datetime = ffDetectDateTime(instance); + ffPrintLogoAndKey(instance, FF_DATE_MODULE_NAME, 0, &instance->config.dateKey); + + //yyyy-mm-dd + printf("%u-%s-%u\n", datetime->year, datetime->monthPretty.chars, datetime->dayInMonth); +} diff --git a/src/modules/datetime.c b/src/modules/datetime.c new file mode 100644 index 000000000..11aeaadea --- /dev/null +++ b/src/modules/datetime.c @@ -0,0 +1,46 @@ +#include "fastfetch.h" + +#define FF_DATETIME_MODULE_NAME "Date Time" +#define FF_DATETIME_NUM_FORMAT_ARGS 20 + +void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFstrbuf* key, const FFstrbuf* format) +{ + const FFDateTimeResult* result = ffDetectDateTime(instance); + ffPrintFormatString(instance, moduleName, 0, key, format, NULL, FF_DATETIME_NUM_FORMAT_ARGS, (FFformatarg[]) { + {FF_FORMAT_ARG_TYPE_UINT16, &result->year}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->yearShort}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->month}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->monthPretty}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->monthName}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->monthNameShort}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->week}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->weekday}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->weekdayShort}, + {FF_FORMAT_ARG_TYPE_UINT16, &result->dayInYear}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->dayInMonth}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->dayInWeek}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->hour}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->hourPretty}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->hour12}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->hour12Pretty}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->minute}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->minutePretty}, + {FF_FORMAT_ARG_TYPE_UINT8, &result->second}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->secondPretty} + }); +} + +void ffPrintDateTime(FFinstance* instance) +{ + if(instance->config.dateTimeFormat.length > 0) + { + ffPrintDateTimeFormat(instance, FF_DATETIME_MODULE_NAME, &instance->config.dateTimeKey, &instance->config.dateTimeFormat); + return; + } + + const FFDateTimeResult* datetime = ffDetectDateTime(instance); + ffPrintLogoAndKey(instance, FF_DATETIME_MODULE_NAME, 0, &instance->config.dateTimeKey); + + //yyyy-mm-dd hh:mm:ss + printf("%u-%s-%u %s:%s:%s\n", datetime->year, datetime->monthPretty.chars, datetime->dayInMonth, datetime->hourPretty.chars, datetime->minutePretty.chars, datetime->secondPretty.chars); +} diff --git a/src/modules/time.c b/src/modules/time.c new file mode 100644 index 000000000..693b0c85d --- /dev/null +++ b/src/modules/time.c @@ -0,0 +1,18 @@ +#include "fastfetch.h" + +#define FF_TIME_MODULE_NAME "Time" + +void ffPrintTime(FFinstance* instance) +{ + if(instance->config.timeFormat.length > 0) + { + ffPrintDateTimeFormat(instance, FF_TIME_MODULE_NAME, &instance->config.timeKey, &instance->config.timeFormat); + return; + } + + const FFDateTimeResult* datetime = ffDetectDateTime(instance); + ffPrintLogoAndKey(instance, FF_TIME_MODULE_NAME, 0, &instance->config.timeKey); + + //hh:mm:ss + printf("%s:%s:%s\n", datetime->hourPretty.chars, datetime->minutePretty.chars, datetime->secondPretty.chars); +} diff --git a/tests/performance.c b/tests/performance.c index 7a7b0a8ec..810a08d9b 100644 --- a/tests/performance.c +++ b/tests/performance.c @@ -69,6 +69,7 @@ int main(int argc, char** argv) FASTFETCH_TEST_PERFORMANCE(ffPrintSong(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintLocalIp(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintPublicIp(&instance)) + FASTFETCH_TEST_PERFORMANCE(ffPrintDateTime(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintLocale(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintBreak(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintColors(&instance))