Skip to content

Commit 4f26150

Browse files
committed
Logo (Builtin): support --logo-position right
Fix fastfetch-cli#1034
1 parent 6413656 commit 4f26150

File tree

1 file changed

+67
-33
lines changed

1 file changed

+67
-33
lines changed

src/logo/logo.c

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,54 +53,54 @@ static void ffLogoPrintCharsRaw(const char* data, size_t length)
5353
}
5454
}
5555

56-
void ffLogoPrintChars(const char* data, bool doColorReplacement)
56+
// If result is NULL, calculate logo width
57+
// Returns logo height
58+
static uint32_t logoAppendChars(const char* data, bool doColorReplacement, FFstrbuf* result)
5759
{
5860
FFOptionsLogo* options = &instance.config.logo;
59-
6061
uint32_t currentlineLength = 0;
62+
uint32_t logoHeight = 0;
6163

62-
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateA(2048);
63-
64-
if (!instance.config.display.pipe && instance.config.display.brightColor)
65-
ffStrbufAppendS(&result, FASTFETCH_TEXT_MODIFIER_BOLT);
66-
67-
ffStrbufAppendNC(&result, options->paddingTop, '\n');
68-
ffStrbufAppendNC(&result, options->paddingLeft, ' ');
69-
70-
instance.state.logoHeight = options->paddingTop;
71-
72-
//Use logoColor[0] as the default color
73-
if(doColorReplacement && !instance.config.display.pipe)
74-
ffStrbufAppendF(&result, "\e[%sm", options->colors[0].chars);
64+
if (result)
65+
{
66+
if (options->position != FF_LOGO_POSITION_RIGHT)
67+
ffStrbufAppendNC(result, options->paddingLeft, ' ');
68+
else
69+
ffStrbufAppendF(result, "\e[9999999C\e[%dD", options->paddingRight + instance.state.logoWidth);
70+
}
7571

7672
while(*data != '\0')
7773
{
7874
//We are at the end of a line. Print paddings and update max line length
7975
if(*data == '\n' || (*data == '\r' && *(data + 1) == '\n'))
8076
{
81-
ffStrbufAppendNC(&result, options->paddingRight, ' ');
82-
8377
//We have \r\n, skip the \r
8478
if(*data == '\r')
8579
++data;
8680

87-
ffStrbufAppendC(&result, '\n');
81+
if(result) ffStrbufAppendC(result, '\n');
8882
++data;
8983

90-
ffStrbufAppendNC(&result, options->paddingLeft, ' ');
84+
if (result)
85+
{
86+
if (options->position != FF_LOGO_POSITION_RIGHT)
87+
ffStrbufAppendNC(result, options->paddingLeft, ' ');
88+
else
89+
ffStrbufAppendF(result, "\e[9999999C\e[%dD", options->paddingRight + instance.state.logoWidth);
90+
}
9191

9292
if(currentlineLength > instance.state.logoWidth)
9393
instance.state.logoWidth = currentlineLength;
9494

9595
currentlineLength = 0;
96-
++instance.state.logoHeight;
96+
++logoHeight;
9797
continue;
9898
}
9999

100100
//Always print tabs as 4 spaces, to have consistent spacing
101101
if(*data == '\t')
102102
{
103-
ffStrbufAppendNC(&result, 4, ' ');
103+
if(result) ffStrbufAppendNC(result, 4, ' ');
104104
++data;
105105
continue;
106106
}
@@ -110,16 +110,20 @@ void ffLogoPrintChars(const char* data, bool doColorReplacement)
110110
{
111111
const char* start = data;
112112

113-
ffStrbufAppendS(&result, "\e[");
113+
if(result) ffStrbufAppendS(result, "\e[");
114114
data += 2;
115115

116116
while(ffCharIsDigit(*data) || *data == ';')
117-
ffStrbufAppendC(&result, *data++); // number
117+
{
118+
if(result) ffStrbufAppendC(result, *data); // number
119+
++data;
120+
}
118121

119122
//We have a valid control sequence, print it and continue with next char
120123
if(isascii(*data))
121124
{
122-
ffStrbufAppendC(&result, *data++); // single letter, end of control sequence
125+
if(result) ffStrbufAppendC(result, *data); // single letter, end of control sequence
126+
++data;
123127
continue;
124128
}
125129

@@ -137,7 +141,7 @@ void ffLogoPrintChars(const char* data, bool doColorReplacement)
137141
//If we have $$, or $\0, print it as single $
138142
if(*data == '$' || *data == '\0')
139143
{
140-
ffStrbufAppendC(&result, '$');
144+
if(result) ffStrbufAppendC(result, '$');
141145
++currentlineLength;
142146
++data;
143147
continue;
@@ -146,18 +150,18 @@ void ffLogoPrintChars(const char* data, bool doColorReplacement)
146150
if(!instance.config.display.pipe)
147151
{
148152
//Map the number to an array index, so that '1' -> 0, '2' -> 1, etc.
149-
int index = ((int) *data) - 49;
153+
int index = *data - '1';
150154

151155
//If the index is valid, print the color. Otherwise continue as normal
152156
if(index < 0 || index >= FASTFETCH_LOGO_MAX_COLORS)
153157
{
154-
ffStrbufAppendC(&result, '$');
158+
if(result) ffStrbufAppendC(result, '$');
155159
++currentlineLength;
156160
//Don't continue here, we want to print the current char as unicode
157161
}
158162
else
159163
{
160-
ffStrbufAppendF(&result, "\e[%sm", options->colors[index].chars);
164+
if(result) ffStrbufAppendF(result, "\e[%sm", options->colors[index].chars);
161165
++data;
162166
continue;
163167
}
@@ -192,24 +196,54 @@ void ffLogoPrintChars(const char* data, bool doColorReplacement)
192196
if(*data == '\0')
193197
break;
194198

195-
ffStrbufAppendC(&result, *data++);
199+
if(result) ffStrbufAppendC(result, *data);
200+
++data;
196201
}
197202
}
203+
//Happens if the last line is the longest
204+
if(currentlineLength > instance.state.logoWidth)
205+
instance.state.logoWidth = currentlineLength;
206+
207+
return logoHeight;
208+
}
209+
210+
void ffLogoPrintChars(const char* data, bool doColorReplacement)
211+
{
212+
FFOptionsLogo* options = &instance.config.logo;
213+
214+
if (options->position == FF_LOGO_POSITION_RIGHT)
215+
logoAppendChars(data, doColorReplacement, NULL);
216+
217+
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateA(2048);
218+
219+
if (!instance.config.display.pipe && instance.config.display.brightColor)
220+
ffStrbufAppendS(&result, FASTFETCH_TEXT_MODIFIER_BOLT);
221+
222+
ffStrbufAppendNC(&result, options->paddingTop, '\n');
223+
224+
//Use logoColor[0] as the default color
225+
if(doColorReplacement && !instance.config.display.pipe)
226+
ffStrbufAppendF(&result, "\e[%sm", options->colors[0].chars);
227+
228+
instance.state.logoHeight = options->paddingTop + logoAppendChars(data, doColorReplacement, &result);
198229

199230
if(!instance.config.display.pipe)
200231
ffStrbufAppendS(&result, FASTFETCH_TEXT_MODIFIER_RESET);
201232

202233
if(options->position == FF_LOGO_POSITION_LEFT)
203234
{
204-
//Happens if the last line is the longest
205-
if(currentlineLength > instance.state.logoWidth)
206-
instance.state.logoWidth = currentlineLength;
207-
208235
instance.state.logoWidth += options->paddingLeft + options->paddingRight;
209236

210237
//Go to the leftmost position and go up the height
211238
ffStrbufAppendF(&result, "\e[1G\e[%uA", instance.state.logoHeight);
212239
}
240+
else if(options->position == FF_LOGO_POSITION_RIGHT)
241+
{
242+
instance.state.logoWidth = 0;
243+
244+
//Go to the leftmost position and go up the height
245+
ffStrbufAppendF(&result, "\e[1G\e[%uA", instance.state.logoHeight);
246+
}
213247
else if (options->position == FF_LOGO_POSITION_TOP)
214248
{
215249
instance.state.logoWidth = instance.state.logoHeight = 0;

0 commit comments

Comments
 (0)