2023-03-09 18:11:03 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-03-09 18:11:03 +08:00
|
|
|
#include "modules/title/title.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/textModifier.h"
|
|
|
|
|
#include "util/stringUtils.h"
|
2023-03-09 18:11:03 +08:00
|
|
|
|
2023-07-05 15:47:47 +08:00
|
|
|
#define FF_TITLE_NUM_FORMAT_ARGS 2
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
static inline void printTitlePart(const FFstrbuf* content)
|
2023-03-09 18:11:03 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
if(!instance.config.pipe)
|
2023-03-09 18:11:03 +08:00
|
|
|
{
|
|
|
|
|
fputs(FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintColor(&instance.config.colorTitle);
|
2023-03-09 18:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufWriteTo(content, stdout);
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
if(!instance.config.pipe)
|
2023-03-09 18:11:03 +08:00
|
|
|
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintTitle(FFTitleOptions* options)
|
2023-03-09 18:11:03 +08:00
|
|
|
{
|
2023-07-05 15:47:47 +08:00
|
|
|
FFstrbuf* host = options->fqdn ?
|
2023-06-24 10:59:53 +08:00
|
|
|
&instance.state.platform.domainName :
|
|
|
|
|
&instance.state.platform.hostName;
|
2023-03-09 18:11:03 +08:00
|
|
|
|
2023-07-05 15:47:47 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintLogoAndKey(options->moduleArgs.key.length == 0 ? NULL : FF_TITLE_MODULE_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor);
|
|
|
|
|
printTitlePart(&instance.state.platform.userName);
|
|
|
|
|
putchar('@');
|
|
|
|
|
printTitlePart(host);
|
|
|
|
|
putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffPrintFormat(FF_TITLE_MODULE_NAME, 0, &options->moduleArgs, FF_TITLE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.userName},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, host},
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-24 10:59:53 +08:00
|
|
|
instance.state.titleLength = instance.state.platform.userName.length + host->length + 1;
|
2023-03-09 18:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitTitleOptions(FFTitleOptions* options)
|
|
|
|
|
{
|
|
|
|
|
options->moduleName = FF_TITLE_MODULE_NAME;
|
2023-07-05 15:47:47 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
options->fqdn = false;
|
2023-03-09 18:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseTitleCommandOptions(FFTitleOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_TITLE_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
2023-07-05 15:47:47 +08:00
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
2023-03-09 18:11:03 +08:00
|
|
|
|
2023-07-05 15:47:47 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "fqdn"))
|
2023-03-09 18:11:03 +08:00
|
|
|
{
|
2023-07-05 15:47:47 +08:00
|
|
|
options->fqdn = ffOptionParseBoolean(value);
|
2023-03-09 18:11:03 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyTitleOptions(FFTitleOptions* options)
|
|
|
|
|
{
|
2023-07-05 15:47:47 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
2023-03-09 18:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffParseTitleJsonObject(yyjson_val* module)
|
2023-03-09 18:11:03 +08:00
|
|
|
{
|
|
|
|
|
FFTitleOptions __attribute__((__cleanup__(ffDestroyTitleOptions))) options;
|
|
|
|
|
ffInitTitleOptions(&options);
|
|
|
|
|
|
2023-03-15 15:24:40 +08:00
|
|
|
if (module)
|
2023-03-09 18:11:03 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-03-09 18:11:03 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
2023-06-10 15:01:54 +08:00
|
|
|
continue;
|
|
|
|
|
|
2023-07-05 15:47:47 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ffStrEqualsIgnCase(key, "fqdn"))
|
2023-03-15 15:24:40 +08:00
|
|
|
{
|
2023-07-05 15:47:47 +08:00
|
|
|
options.fqdn = yyjson_get_bool(val);
|
2023-03-15 15:24:40 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintErrorString(FF_TITLE_MODULE_NAME, 0, NULL, NULL, "Unknown JSON key %s", key);
|
2023-03-15 15:24:40 +08:00
|
|
|
}
|
2023-03-09 18:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintTitle(&options);
|
2023-03-09 18:11:03 +08:00
|
|
|
}
|