mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 09:33:39 +01:00
Date Time support
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -64,6 +64,9 @@ Format options: Provide the format string for custom output. Use fastfetch --hel
|
||||
--public-ip-format <format>
|
||||
--player-format <format>
|
||||
--song-format <format>
|
||||
--datetime-format <format>
|
||||
--time-format <format>
|
||||
--date-format <format>
|
||||
|
||||
Key options: Provide a custom key for an output
|
||||
--os-key <key>
|
||||
@@ -94,6 +97,9 @@ Key options: Provide a custom key for an output
|
||||
--public-ip-key <key>
|
||||
--player-key <key>
|
||||
--song-key <key>
|
||||
--datetime-key <key>
|
||||
--time-key <key>
|
||||
--date-key <key>
|
||||
|
||||
Library options: Set the path of a library to load
|
||||
--lib-PCI <path>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user