Skip to content

Commit 44c8fa0

Browse files
committed
Logo (Image): fix padding* not working correctly
Fix #1121
1 parent 9f9a5ba commit 44c8fa0

File tree

2 files changed

+81
-31
lines changed

2 files changed

+81
-31
lines changed

src/logo/image/image.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ static bool printImageIterm(bool printError)
3838
if (options->position == FF_LOGO_POSITION_LEFT)
3939
{
4040
ffStrbufAppendF(&buf, "\e[2J\e[3J\e[%u;%uH",
41-
(unsigned) options->paddingTop,
42-
(unsigned) options->paddingLeft
41+
(unsigned) options->paddingTop + 1,
42+
(unsigned) options->paddingLeft + 1
4343
);
4444
}
4545
else if (options->position == FF_LOGO_POSITION_TOP)
@@ -73,7 +73,7 @@ static bool printImageIterm(bool printError)
7373
return true; // We already printed image logo, don't print ascii logo then
7474
}
7575
if (options->position == FF_LOGO_POSITION_LEFT)
76-
instance.state.logoWidth = X + options->paddingRight;
76+
instance.state.logoWidth = X + options->paddingRight - 1;
7777
instance.state.logoHeight = Y;
7878
fputs("\e[H", stdout);
7979
}
@@ -88,7 +88,7 @@ static bool printImageIterm(bool printError)
8888
ffStrbufAppendNC(&buf, options->paddingTop, '\n');
8989
if (options->position == FF_LOGO_POSITION_RIGHT)
9090
ffStrbufAppendF(&buf, "\e[9999999C\e[%uD", (unsigned) options->paddingRight + options->width);
91-
else
91+
else if (options->paddingLeft)
9292
ffStrbufAppendF(&buf, "\e[%uC", (unsigned) options->paddingLeft);
9393
ffStrbufAppendF(&buf, "\e]1337;File=inline=1;width=%u;height=%u;preserveAspectRatio=%u:%s\a\n",
9494
(unsigned) options->width,
@@ -138,8 +138,8 @@ static bool printImageKittyDirect(bool printError)
138138
{
139139
// We must clear the entre screen to make sure that terminal buffer won't scroll up
140140
printf("\e[2J\e[3J\e[%u;%uH",
141-
(unsigned) options->paddingTop,
142-
(unsigned) options->paddingLeft
141+
(unsigned) options->paddingTop + 1,
142+
(unsigned) options->paddingLeft + 1
143143
);
144144
}
145145
else if (options->position == FF_LOGO_POSITION_TOP)
@@ -169,11 +169,12 @@ static bool printImageKittyDirect(bool printError)
169169
const char* error = ffGetTerminalResponse("\e[6n", "\e[%hu;%huR", &Y, &X);
170170
if (error)
171171
{
172-
fprintf(stderr, "\nLogo (kitty-direct): fail to query cursor position: %s\n", error);
172+
if (printError)
173+
fprintf(stderr, "\nLogo (kitty-direct): fail to query cursor position: %s\n", error);
173174
return true; // We already printed image logo, don't print ascii logo then
174175
}
175176
if (options->position == FF_LOGO_POSITION_LEFT)
176-
instance.state.logoWidth = X + options->paddingRight;
177+
instance.state.logoWidth = X + options->paddingRight - 1;
177178
instance.state.logoHeight = Y;
178179
fputs("\e[H", stdout);
179180
}
@@ -188,7 +189,7 @@ static bool printImageKittyDirect(bool printError)
188189
ffPrintCharTimes('\n', options->paddingTop);
189190
if (options->position == FF_LOGO_POSITION_RIGHT)
190191
printf("\e[9999999C\e[%uD", (unsigned) options->paddingRight + options->width);
191-
else
192+
else if (options->paddingLeft)
192193
printf("\e[%uC", (unsigned) options->paddingLeft);
193194

194195
printf("\e_Ga=T,f=100,t=f,c=%u,r=%u;%s\e\\\n",
@@ -348,7 +349,7 @@ static void printImagePixels(FFLogoRequestData* requestData, const FFstrbuf* res
348349
ffPrintCharTimes('\n', options->paddingTop);
349350
if (options->position == FF_LOGO_POSITION_RIGHT)
350351
printf("\e[9999999C\e[%uD", (unsigned) options->paddingRight + requestData->logoCharacterWidth);
351-
else
352+
else if (options->paddingLeft)
352353
printf("\e[%uC", (unsigned) options->paddingLeft);
353354
fflush(stdout);
354355
ffWriteFDBuffer(FFUnixFD2NativeFD(STDOUT_FILENO), result);
@@ -732,7 +733,7 @@ static bool printCachedPixel(FFLogoRequestData* requestData)
732733
ffPrintCharTimes('\n', options->paddingTop);
733734
if (options->position == FF_LOGO_POSITION_RIGHT)
734735
printf("\e[9999999C\e[%uD", (unsigned) options->paddingRight + requestData->logoCharacterWidth);
735-
else
736+
else if (options->paddingLeft)
736737
printf("\e[%uC", (unsigned) options->paddingLeft);
737738
fflush(stdout);
738739

src/logo/logo.c

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,91 @@ typedef enum FFLogoSize
1616
FF_LOGO_SIZE_SMALL,
1717
} FFLogoSize;
1818

19-
static void ffLogoPrintCharsRaw(const char* data, size_t length)
19+
static bool ffLogoPrintCharsRaw(const char* data, size_t length, bool printError)
2020
{
2121
FFOptionsLogo* options = &instance.config.logo;
2222
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
2323

2424
if (!options->width || !options->height)
2525
{
26-
ffStrbufAppendF(&buf, "\e[2J\e[3J\e[%u;%uH",
27-
(unsigned) options->paddingTop,
28-
(unsigned) options->paddingLeft
29-
);
26+
if (options->position == FF_LOGO_POSITION_LEFT)
27+
{
28+
ffStrbufAppendF(&buf, "\e[2J\e[3J\e[%u;%uH",
29+
(unsigned) options->paddingTop + 1,
30+
(unsigned) options->paddingLeft + 1
31+
);
32+
}
33+
else if (options->position == FF_LOGO_POSITION_TOP)
34+
{
35+
ffStrbufAppendNC(&buf, options->paddingTop, '\n');
36+
ffStrbufAppendNC(&buf, options->paddingLeft, ' ');
37+
}
38+
else if (options->position == FF_LOGO_POSITION_RIGHT)
39+
{
40+
if (!options->width)
41+
{
42+
if (printError)
43+
fputs("Logo (iterm): Must set logo width when using position right\n", stderr);
44+
return false;
45+
}
46+
ffStrbufAppendF(&buf, "\e[2J\e[3J\e[H\e[9999999C\e[%uD", (unsigned) options->paddingRight + options->width);
47+
}
3048
ffStrbufAppendNS(&buf, (uint32_t) length, data);
3149
ffWriteFDBuffer(FFUnixFD2NativeFD(STDOUT_FILENO), &buf);
3250

33-
uint16_t X = 0, Y = 0;
34-
const char* error = ffGetTerminalResponse("\e[6n", "\e[%hu;%huR", &Y, &X);
35-
if (error)
51+
if (options->position == FF_LOGO_POSITION_LEFT || options->position == FF_LOGO_POSITION_RIGHT)
52+
{
53+
uint16_t X = 0, Y = 0;
54+
const char* error = ffGetTerminalResponse("\e[6n", "\e[%hu;%huR", &Y, &X);
55+
if (error)
56+
{
57+
if (printError)
58+
fprintf(stderr, "\nLogo (image-raw): fail to query cursor position: %s\n", error);
59+
return true;
60+
}
61+
if (options->position == FF_LOGO_POSITION_LEFT)
62+
instance.state.logoWidth = X + instance.config.logo.paddingRight - 1;
63+
instance.state.logoHeight = Y;
64+
fputs("\e[H", stdout);
65+
}
66+
else if (options->position == FF_LOGO_POSITION_TOP)
3667
{
37-
fprintf(stderr, "\nLogo (image-raw): fail to query cursor position: %s\n", error);
38-
return;
68+
instance.state.logoWidth = instance.state.logoHeight = 0;
69+
ffPrintCharTimes('\n', options->paddingRight);
3970
}
40-
instance.state.logoWidth = X + instance.config.logo.paddingRight;
41-
instance.state.logoHeight = Y;
42-
fputs("\e[H", stdout);
4371
}
4472
else
4573
{
4674
ffStrbufAppendNC(&buf, options->paddingTop, '\n');
47-
ffStrbufAppendNC(&buf, options->paddingLeft, ' ');
75+
76+
if (options->position == FF_LOGO_POSITION_RIGHT)
77+
ffStrbufAppendF(&buf, "\e[9999999C\e[%uD", (unsigned) options->paddingRight + options->width);
78+
else if (options->paddingLeft)
79+
ffStrbufAppendF(&buf, "\e[%uC", (unsigned) options->paddingLeft);
80+
4881
ffStrbufAppendNS(&buf, (uint32_t) length, data);
49-
instance.state.logoHeight = options->paddingTop + options->height;
50-
instance.state.logoWidth = options->paddingLeft + options->width + options->paddingRight;
51-
ffStrbufAppendF(&buf, "\n\e[%uA", instance.state.logoHeight);
82+
ffStrbufAppendC(&buf, '\n');
83+
84+
if (options->position == FF_LOGO_POSITION_LEFT)
85+
{
86+
instance.state.logoWidth = options->width + options->paddingLeft + options->paddingRight;
87+
instance.state.logoHeight = options->paddingTop + options->height;
88+
ffStrbufAppendF(&buf, "\e[%uA", (unsigned) instance.state.logoHeight);
89+
}
90+
else if (options->position == FF_LOGO_POSITION_TOP)
91+
{
92+
instance.state.logoWidth = instance.state.logoHeight = 0;
93+
ffStrbufAppendNC(&buf, options->paddingRight, '\n');
94+
}
95+
else if (options->position == FF_LOGO_POSITION_RIGHT)
96+
{
97+
instance.state.logoWidth = instance.state.logoHeight = 0;
98+
ffStrbufAppendF(&buf, "\e[%uA", (unsigned) options->height);
99+
}
52100
ffWriteFDBuffer(FFUnixFD2NativeFD(STDOUT_FILENO), &buf);
53101
}
102+
103+
return true;
54104
}
55105

56106
// If result is NULL, calculate logo width
@@ -441,10 +491,9 @@ static bool logoPrintFileIfExists(bool doColorReplacement, bool raw)
441491

442492
logoApplyColors(logoGetBuiltinDetected(FF_LOGO_SIZE_NORMAL), doColorReplacement);
443493
if(raw)
444-
ffLogoPrintCharsRaw(content.chars, content.length);
445-
else
446-
ffLogoPrintChars(content.chars, doColorReplacement);
494+
return ffLogoPrintCharsRaw(content.chars, content.length, instance.config.display.showErrors);
447495

496+
ffLogoPrintChars(content.chars, doColorReplacement);
448497
return true;
449498
}
450499

0 commit comments

Comments
 (0)