some terminal improvements

This commit is contained in:
Linus Dierheimer
2021-04-02 16:21:52 +02:00
parent 9c26a76c9d
commit a8821cf6f0
8 changed files with 148 additions and 71 deletions
-6
View File
@@ -13,10 +13,6 @@ void ffInitState(FFstate* state)
state->passwd = getpwuid(getuid());
uname(&state->utsname);
sysinfo(&state->sysinfo);
ffStrbufInit(&state->terminal.value);
state->terminal.error = NULL;
state->terminal.calculated = false;
}
void ffDefaultConfig(FFconfig* config)
@@ -91,8 +87,6 @@ static void ffCleanup(FFinstance* instance)
ffStrbufDestroy(&instance->config.libXrandr);
ffStrbufDestroy(&instance->config.diskFolders);
ffStrbufDestroy(&instance->state.terminal.value);
}
void ffFinish(FFinstance* instance)
+1 -1
View File
@@ -230,7 +230,7 @@ static inline void printCommandHelp(const char* command)
else if(strcasecmp(command, "font-format") == 0)
constructAndPrintCommandHelpFormat("font", "{} [Plasma], {5}", 5, "Plasma font", "GTK2 font", "GTK3 font", "GTK4 font", "Combined GTK fonts");
else if(strcasecmp(command, "terminal-format") == 0)
constructAndPrintCommandHelpFormat("terminal", "{}", 1, "Terminal name");
constructAndPrintCommandHelpFormat("terminal", "{3}", 3, "Terminal executable name", "Terminal process name", "Terminal name");
else if(strcasecmp(command, "terminal-font-format") == 0)
constructAndPrintCommandHelpFormat("terminal-font", "{}", 1, "Terminal font name");
else if(strcasecmp(command, "cpu-format") == 0)
+1 -10
View File
@@ -75,21 +75,12 @@ typedef struct FFconfig
} FFconfig;
typedef struct FFvalue
{
FFstrbuf value;
char* error;
bool calculated;
} FFvalue;
typedef struct FFstate
{
uint8_t current_row;
struct passwd* passwd;
struct utsname utsname;
struct sysinfo sysinfo;
FFvalue terminal;
} FFstate;
typedef struct FFinstance
@@ -146,7 +137,7 @@ void ffPrintLogos(bool color);
//Module functions
void ffPopulateTerminal(FFinstance* instance);
void ffGetTerminal(FFinstance* instance, FFstrbuf** exeName, FFstrbuf** processName, FFstrbuf** error);
void ffPrintCustom(FFinstance* instance, const char* key, const char* value);
void ffPrintBreak(FFinstance* instance);
+1 -1
View File
@@ -54,7 +54,7 @@ void ffPrintCPU(FFinstance* instance)
ffStrbufRemoveStrings(&namePretty, 11,
"(R)", "(r)", "(TM)", "(tm)", " CPU", " FPU", " Processor", " Dual-Core", " Quad-Core", " Six-Core", " Eight-Core"
);
ffStrbufLimitLength(&namePretty, ffStrbufLastIndexC(&namePretty, '@')); //Cut the speed output in the name as we append our own
ffStrbufSubstrBeforeFirstC(&namePretty, '@'); //Cut the speed output in the name as we append our own
ffStrbufTrimRight(&namePretty, ' '); //If we removed the @ in previous step there was most likely a space before it
double biosLimit = getGhz("/sys/devices/system/cpu/cpu0/cpufreq/bios_limit");
+57 -43
View File
@@ -1,14 +1,14 @@
#include "fastfetch.h"
static void getTerminalName(FFinstance* instance, const char* pid, char* terminal, char* error)
static void getTerminalName(FFinstance* instance, const char* pid, FFstrbuf* exeName, FFstrbuf* processName, FFstrbuf* error)
{
char file[234];
sprintf(file, "/proc/%s/stat", pid);
char statFile[234];
sprintf(statFile, "/proc/%s/stat", pid);
FILE* stat = fopen(file, "r");
FILE* stat = fopen(statFile, "r");
if(stat == NULL)
{
sprintf(error, "fopen(\"%s\", \"r\") == NULL", file);
ffStrbufSetF(error, "fopen(\"%s\", \"r\") == NULL", statFile);
return;
}
@@ -16,7 +16,7 @@ static void getTerminalName(FFinstance* instance, const char* pid, char* termina
char ppid[256];
if(fscanf(stat, "%*s (%[^)])%*s%s", name, ppid) != 2)
{
strcpy(error, "fscanf(stat, \"%*s (%[^)])%*s%s\", name, ppid) != 2");
ffStrbufSetS(error, "fscanf(stat, \"%*s (%[^)])%*s%s\", name, ppid) != 2");
return;
}
@@ -33,68 +33,82 @@ static void getTerminalName(FFinstance* instance, const char* pid, char* termina
strcasecmp(name, "doas") == 0 ||
strcasecmp(name, "strace") == 0 )
{
getTerminalName(instance, ppid, terminal, error);
getTerminalName(instance, ppid, exeName, processName, error);
return;
}
if(
strcasecmp(name, "systemd") == 0 ||
strcasecmp(name, "init") == 0 ||
strcasecmp(name, "login") == 0 ||
strcasecmp(name, "0") == 0 ||
strcasecmp(ppid, "0") == 0 )
{
strcpy(terminal, "TTY");
}
else
{
strcpy(terminal, name);
}
char cmdlineFile[234];
sprintf(cmdlineFile, "/proc/%s/cmdline", pid);
ffGetFileContent(cmdlineFile, exeName);
ffStrbufSubstrBeforeFirstC(exeName, '\0');
ffStrbufSubstrAfterLastC(exeName, '/');
ffStrbufSetS(processName, name);
}
void ffPopulateTerminal(FFinstance* instance)
void ffGetTerminal(FFinstance* instance, FFstrbuf** exeNamePtr, FFstrbuf** processNamePtr, FFstrbuf** errorPtr)
{
if(instance->state.terminal.calculated)
static FFstrbuf exeName;
static FFstrbuf processName;
static FFstrbuf error;
static bool init = false;
if(exeNamePtr != NULL)
*exeNamePtr = &exeName;
if(processNamePtr != NULL)
*processNamePtr = &processName;
if(errorPtr != NULL)
*errorPtr = &error;
if(init)
return;
static char terminal[256];
static char error[256];
terminal[0] = '\0';
error[0] = '\0';
ffStrbufInit(&exeName);
ffStrbufInit(&processName);
char ppid[256];
sprintf(ppid, "%i", getppid());
getTerminalName(instance, ppid, terminal, error);
if(terminal[0] != '\0')
ffStrbufSetS(&instance->state.terminal.value, terminal);
getTerminalName(instance, ppid, &exeName, &processName, &error);
if(error[0] != '\0')
instance->state.terminal.error = error;
instance->state.terminal.calculated = true;
init = true;
}
void ffPrintTerminal(FFinstance* instance)
{
ffPopulateTerminal(instance);
FFstrbuf* exeName;
FFstrbuf* processName;
FFstrbuf* error;
if(instance->state.terminal.error != NULL)
{
ffPrintError(instance, "Terminal", instance->state.terminal.error);
return;
}
ffGetTerminal(instance, &exeName, &processName, &error);
FFstrbuf* name;
if(ffStrbufStartsWith(exeName, processName))
name = exeName;
else
name = processName;
if(instance->config.terminalFormat.length == 0)
{
if(error->length > 0)
{
ffPrintError(instance, "Terminal", error->chars);
return;
}
ffPrintLogoAndKey(instance, "Terminal");
ffStrbufPutTo(&instance->state.terminal.value, stdout);
ffStrbufPutTo(name, stdout);
}
else
{
ffPrintFormatString(instance, "Terminal", &instance->config.terminalFormat, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &instance->state.terminal.value}
ffPrintFormatString(instance, "Terminal", &instance->config.terminalFormat, 3,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, exeName},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, processName},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, name}
);
}
}
+17 -6
View File
@@ -54,18 +54,29 @@ static void printTTY(FFinstance* instance)
void ffPrintTerminalFont(FFinstance* instance)
{
ffPopulateTerminal(instance);
FFstrbuf* procName;
FFstrbuf* error;
ffGetTerminal(instance, NULL, &procName, &error);
if(instance->state.terminal.error != NULL)
if(error->length > 0)
{
ffPrintError(instance, "Terminal Font", "Terminal Font needs successfull terminal detection");
if(instance->config.termFontFormat.length == 0)
ffPrintError(instance, "Terminal Font", "Terminal Font needs successfull terminal detection");
else
printTerminalFont(instance, "");
return;
}
if(ffStrbufIgnCaseCompS(&instance->state.terminal.value, "konsole") == 0)
if(ffStrbufIgnCaseCompS(procName, "konsole") == 0)
printKonsole(instance);
else if(ffStrbufIgnCaseCompS(&instance->state.terminal.value, "TTY") == 0)
else if(ffStrbufIgnCaseCompS(procName, "login") == 0)
printTTY(instance);
else
ffPrintError(instance, "Terminal Font", "Unknown terminal: %s", instance->state.terminal.value);
{
if(instance->config.termFontFormat.length > 0)
printTerminalFont(instance, "");
else
ffPrintError(instance, "Terminal Font", "Unknown terminal: %s", error->chars);
}
}
+63 -3
View File
@@ -359,15 +359,75 @@ uint32_t ffStrbufLastIndexC(FFstrbuf* strbuf, const char c)
return strbuf->length;
}
void ffStrbufLimitLength(FFstrbuf* strbuf, uint32_t length)
void ffStrbufSubstrBefore(FFstrbuf* strbuf, uint32_t index)
{
if(strbuf->length <= length)
if(strbuf->length <= index)
return;
strbuf->length = length;
strbuf->length = index;
strbuf->chars[strbuf->length] = '\0';
}
void ffStrbufSubstrBeforeFirstC(FFstrbuf* strbuf, const char c)
{
ffStrbufSubstrBefore(strbuf, ffStrbufFirstIndexC(strbuf, c));
}
bool ffStrbufStartsWith(FFstrbuf* strbuf, FFstrbuf* start)
{
return ffStrbufStartsWithNS(strbuf, start->length, start->chars);
}
void ffStrbufSubstrAfter(FFstrbuf* strbuf, uint32_t index)
{
if(index >= strbuf->length)
{
ffStrbufClear(strbuf);
return;
}
memmove(strbuf->chars, strbuf->chars + index + 1, strbuf->length - index - 1);
strbuf->length -= (index + 1);
}
void ffStrbufSubstrAfterLastC(FFstrbuf* strbuf, const char c)
{
uint32_t index = ffStrbufLastIndexC(strbuf, c);
if(index < strbuf->length)
ffStrbufSubstrAfter(strbuf, index);
}
static inline bool testSame(FFstrbuf* strbuf, uint32_t i, const char* str)
{
if(i > strbuf->length)
return false;
if(strbuf->chars[i] != str[i])
return false;
return true;
}
bool ffStrbufStartsWithS(FFstrbuf* strbuf, const char* start)
{
for(uint32_t i = 0; start[i] != '\0'; i++)
{
if(!testSame(strbuf, i, start))
return false;
}
return true;
}
bool ffStrbufStartsWithNS(FFstrbuf* strbuf, uint32_t length, const char* start)
{
for(uint32_t i = 0; i < length; i++)
{
if(!testSame(strbuf, i, start))
return false;
}
return true;
}
void ffStrbufDestroy(FFstrbuf* strbuf)
{
free(strbuf->chars);
+8 -1
View File
@@ -72,7 +72,14 @@ uint32_t ffStrbufFirstIndexAfterC(FFstrbuf* strbuf, uint32_t start, const char c
uint32_t ffStrbufFirstIndexC(FFstrbuf* strbuf, const char c);
uint32_t ffStrbufLastIndexC(FFstrbuf* strbuf, const char c);
void ffStrbufLimitLength(FFstrbuf* strbuf, uint32_t length);
void ffStrbufSubstrBefore(FFstrbuf* strbuf, uint32_t index);
void ffStrbufSubstrBeforeFirstC(FFstrbuf* strbuf, const char c);
void ffStrbufSubstrAfter(FFstrbuf* strbuf, uint32_t index);
void ffStrbufSubstrAfterLastC(FFstrbuf* strbuf, const char c);
bool ffStrbufStartsWith(FFstrbuf* strbuf, FFstrbuf* start);
bool ffStrbufStartsWithS(FFstrbuf* strbuf, const char* start);
bool ffStrbufStartsWithNS(FFstrbuf* strbuf, uint32_t length, const char* start);
void ffStrbufDestroy(FFstrbuf* strbuf);