Skip to content

Commit d64716a

Browse files
committed
fastfetch: pretty printing --help
1 parent 0ffbb8c commit d64716a

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

src/fastfetch.c

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,93 @@ static void printCommandFormatHelp(const char* command)
4040
fprintf(stderr, "Error: Module '%s' is not supported\n", type.chars);
4141
}
4242

43-
static inline void printCommandHelp(const char* command)
43+
static void printCommandHelp(const char* command)
4444
{
4545
if(command == NULL)
46-
puts(FASTFETCH_DATATEXT_HELP);
46+
{
47+
static char input[] = FASTFETCH_DATATEXT_HELP;
48+
if (isatty(STDOUT_FILENO))
49+
{
50+
char* token = strtok(input, "\n");
51+
puts(token);
52+
53+
while ((token = strtok(NULL, "\n")))
54+
{
55+
// handle empty lines
56+
char* back = token;
57+
while (*--back != '\0');
58+
ffPrintCharTimes('\n', (uint32_t) (token - back - 1));
59+
60+
if (isalpha(*token)) // highlight subjects
61+
{
62+
char* colon = strchr(token, ':');
63+
if (colon)
64+
{
65+
fputs("\e[1;4m", stdout); // Bold + Underline
66+
fwrite(token, 1, (uint32_t) (colon - token), stdout);
67+
fputs("\e[m", stdout);
68+
69+
if (colon - token == 5 && memcmp(token, "Usage", 5) == 0)
70+
{
71+
char* cmd = strstr(colon, "fastfetch ");
72+
if (cmd)
73+
{
74+
fwrite(colon, 1, (uint32_t) (cmd - colon), stdout);
75+
fputs("\e[1mfastfetch \e[m", stdout);
76+
cmd += strlen("fastfetch ");
77+
if (*cmd == '<')
78+
{
79+
char* gt = strchr(cmd, '>');
80+
if (gt)
81+
{
82+
fputs("\e[3m", stdout);
83+
fwrite(cmd, 1, (uint32_t) (gt - cmd + 1), stdout);
84+
fputs("\e[m", stdout);
85+
cmd = gt + 1;
86+
}
87+
}
88+
puts(cmd);
89+
continue;
90+
}
91+
}
92+
puts(colon);
93+
continue;
94+
}
95+
}
96+
else if (*token == ' ') // highlight options. All options are indented
97+
{
98+
char* dash = strchr(token, '-'); // start of an option
99+
if (dash)
100+
{
101+
char* colon = strchr(dash, ':'); // end of an option
102+
if (colon)
103+
{
104+
*colon = '\0';
105+
char* lt = strrchr(dash, '<');
106+
*colon = ':';
107+
fputs("\e[1m", stdout); // Bold
108+
fwrite(token, 1, (uint32_t) ((lt ? lt : colon) - token), stdout);
109+
fputs("\e[m", stdout);
110+
if (lt)
111+
{
112+
fputs("\e[3m", stdout);
113+
fwrite(lt, 1, (uint32_t) (colon - lt), stdout);
114+
fputs("\e[m", stdout);
115+
}
116+
puts(colon);
117+
continue;
118+
}
119+
}
120+
}
121+
122+
puts(token);
123+
}
124+
}
125+
else
126+
{
127+
puts(input);
128+
}
129+
}
47130
else if(ffStrEqualsIgnCase(command, "color"))
48131
puts(FASTFETCH_DATATEXT_HELP_COLOR);
49132
else if(ffStrEqualsIgnCase(command, "format"))

0 commit comments

Comments
 (0)