Skip to content

Commit 6accb58

Browse files
zlib support
1 parent 373e041 commit 6accb58

File tree

9 files changed

+72
-6
lines changed

9 files changed

+72
-6
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ OPTION(ENABLE_XFCONF "Enable libxfconf-0" ON)
3030
OPTION(ENABLE_RPM "Enable rpm" ON)
3131
OPTION(ENABLE_IMAGEMAGICK7 "Enable imagemagick 7" ON)
3232
OPTION(ENABLE_IMAGEMAGICK6 "Enable imagemagick 6" ON)
33+
OPTION(ENABLE_ZLIB "Enable zlib" ON)
3334

3435
if(NOT CMAKE_BUILD_TYPE)
3536
set(CMAKE_BUILD_TYPE Release)
@@ -311,6 +312,15 @@ if(ENABLE_IMAGEMAGICK6)
311312
endif()
312313
endif()
313314

315+
if(ENABLE_ZLIB)
316+
pkg_check_modules(ZLIB zlib)
317+
if(ZLIB_FOUND)
318+
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_ZLIB=1)
319+
else()
320+
message(WARNING "Package zlib not found. Building without support")
321+
endif()
322+
endif()
323+
314324
target_include_directories(libfastfetch
315325
PUBLIC ${PROJECT_BINARY_DIR}
316326
PUBLIC ${PROJECT_SOURCE_DIR}/src
@@ -329,6 +339,7 @@ target_include_directories(libfastfetch
329339
PRIVATE ${RPM_INCLUDE_DIRS}
330340
PRIVATE ${IMAGEMAGICK7_INCLUDE_DIRS}
331341
PRIVATE ${IMAGEMAGICK6_INCLUDE_DIRS}
342+
PRIVATE ${ZLIB_INCLUDE_DIRS}
332343
)
333344

334345
target_link_libraries(libfastfetch

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following libraries are used if present at runtime:
2929
* [`libGIO`](https://developer.gnome.org/gio/unstable/): Needed for values that are only stored GSettings.
3030
* [`libDConf`](https://developer.gnome.org/dconf/unstable/): Needed for values that are only stored in DConf + Fallback for GSettings.
3131
* [`libmagickcore` (ImageMagick)](https://www.imagemagick.org/): Images in terminal using sixel or kitty graphics protocol.
32+
* [`libZ`](https://www.zlib.net/): Faster image output when using kitty graphics protocol.
3233
* [`libDBus`](https://www.freedesktop.org/wiki/Software/dbus): Needed for detecting current media player and song.
3334
* [`libXFConf`](https://gitlab.xfce.org/xfce/xfconf): Needed for XFWM theme and XFCE Terminal font.
3435
* [`librpm`](http://rpm.org/): Needed for rpm package count.

completions/bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ __fastfetch_completion()
232232
"--lib-DBus"
233233
"--lib-XFConf"
234234
"--lib-RPM"
235+
"--lib-imagemagick"
236+
"--lib-z"
235237
"--battery-dir"
236238
"--load-config"
237239
)

src/common/init.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ static void defaultConfig(FFinstance* instance)
200200
ffStrbufInitA(&instance->config.libXFConf, 0);
201201
ffStrbufInitA(&instance->config.librpm, 0);
202202
ffStrbufInitA(&instance->config.libImageMagick, 0);
203+
ffStrbufInitA(&instance->config.libZ, 0);
203204

204205
ffStrbufInitA(&instance->config.diskFolders, 0);
205206

@@ -316,6 +317,9 @@ void ffListFeatures()
316317
#ifdef FF_HAVE_IMAGEMAGICK6
317318
"imagemagick6\n"
318319
#endif
320+
#ifdef FF_HAVE_ZLIB
321+
"zlib\n"
322+
#endif
319323
#ifdef FF_HAVE_XFCONF
320324
"xfconf\n"
321325
#endif

src/data/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,5 @@
228228
#--lib-DBus /usr/lib/libdbus-1.so
229229
#--lib-XFConf /usr/lib/libxfconf-0.so
230230
#--lib-rpm /usr/lib/librpm.so
231+
#--lib-imagemagick /usr/lib/libMagickCore-7.Q16HDRI.so
232+
#--lib-z /usr/lib/libz.so

src/data/help.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ Library options: Set the path of a library to load
119119
--lib-DBus <path>
120120
--lib-XFConf <path>
121121
--lib-RPM <path>
122+
--lib-imagemagick <path>
123+
--lib-z <path>
122124

123125
Module specific options:
124126
--separator-string <str>: Set the string printed by the separator module

src/fastfetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
976976
optionParseString(key, value, &instance->config.librpm);
977977
else if(strcasecmp(key, "--lib-imagemagick") == 0)
978978
optionParseString(key, value, &instance->config.libImageMagick);
979+
else if(strcasecmp(key, "--lib-z") == 0)
980+
optionParseString(key, value, &instance->config.libZ);
979981

980982
//////////////////
981983
//Module options//

src/fastfetch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ typedef struct FFconfig
137137
FFstrbuf libXFConf;
138138
FFstrbuf librpm;
139139
FFstrbuf libImageMagick;
140+
FFstrbuf libZ;
140141

141142
FFstrbuf diskFolders;
142143

src/logo/image/image.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,40 @@
44

55
#define FF_KITTY_MAX_CHUNK_SIZE 4096
66

7+
#ifdef FF_HAVE_ZLIB
8+
#include <zlib.h>
9+
10+
static bool compressBlob(const FFinstance* instance, void** blob, size_t* length)
11+
{
12+
FF_LIBRARY_LOAD(zlib, instance->config.libZ, false, "libz.so", 2)
13+
FF_LIBRARY_LOAD_SYMBOL(zlib, compressBound, false)
14+
FF_LIBRARY_LOAD_SYMBOL(zlib, compress2, false)
15+
16+
uLong compressedLength = ffcompressBound(*length);
17+
void* compressed = malloc(compressedLength);
18+
if(compressed == NULL)
19+
{
20+
dlclose(zlib);
21+
return false;
22+
}
23+
24+
int compressResult = ffcompress2(compressed, &compressedLength, *blob, *length, Z_BEST_COMPRESSION);
25+
dlclose(zlib);
26+
if(compressResult != Z_OK)
27+
{
28+
free(compressed);
29+
return false;
30+
}
31+
32+
free(*blob);
33+
34+
*length = (size_t) compressedLength;
35+
*blob = compressed;
36+
return true;
37+
}
38+
39+
#endif // FF_HAVE_ZLIB
40+
741
//We use only the defines from here, that are exactly the same in both versions
842
#ifdef FF_HAVE_IMAGEMAGICK7
943
#include <MagickCore/MagickCore.h>
@@ -44,7 +78,7 @@ static void printImageKittyChunc(char** blob, size_t* length, uint32_t chunkSize
4478
*length -= toWrite;
4579
}
4680

47-
static bool printImageKitty(ImageInfo* imageInfo, Image* image, ExceptionInfo* ExceptionInfo, uint32_t paddingLeft, ImageMagickFunctions* functions)
81+
static bool printImageKitty(const FFinstance* instance, ImageInfo* imageInfo, Image* image, ExceptionInfo* ExceptionInfo, uint32_t paddingLeft, ImageMagickFunctions* functions)
4882
{
4983
functions->ffCopyMagickString(imageInfo->magick, "RGBA", 5);
5084

@@ -53,16 +87,23 @@ static bool printImageKitty(ImageInfo* imageInfo, Image* image, ExceptionInfo* E
5387
if(!checkAllocationResult(blob, length))
5488
return false;
5589

56-
void* encoded = functions->ffBase64Encode(blob, length, &length);
90+
#ifdef FF_HAVE_ZLIB
91+
bool isCompressed = compressBlob(instance, &blob, &length);
92+
#else
93+
FF_UNUSED(instance)
94+
bool isCompressed = false;
95+
#endif
96+
97+
char* encoded = functions->ffBase64Encode(blob, length, &length);
5798
free(blob);
5899
if(!checkAllocationResult(encoded, length))
59100
return false;
60101

61102
ffPrintCharTimes(' ', paddingLeft);
62103

63-
char* currentPos = (char*) encoded;
104+
char* currentPos = encoded;
64105

65-
printf("\033_Ga=T,f=32,s=%u,v=%u,m=1;\033\\", (uint32_t) image->columns, (uint32_t) image->rows);
106+
printf("\033_Ga=T,f=32,s=%u,v=%u,m=1%s;\033\\", (uint32_t) image->columns, (uint32_t) image->rows, isCompressed ? ",o=z" : "");
66107
while (length > 0)
67108
printImageKittyChunc(&currentPos, &length, FF_KITTY_MAX_CHUNK_SIZE);
68109
fputs("\033_Gm=0;\033\\", stdout);
@@ -159,7 +200,7 @@ FFLogoImageResult ffLogoPrintImageImpl(FFinstance* instance, void* imageMagick,
159200

160201
bool printSuccessful;
161202
if(type == FF_LOGO_TYPE_KITTY)
162-
printSuccessful = printImageKitty(imageInfoOut, resizedImage, exceptionInfo, instance->config.logoPaddingLeft, &functions);
203+
printSuccessful = printImageKitty(instance, imageInfoOut, resizedImage, exceptionInfo, instance->config.logoPaddingLeft, &functions);
163204
else
164205
printSuccessful = printImageSixel(imageInfoOut, resizedImage, exceptionInfo, instance->config.logoPaddingLeft, &functions);
165206

@@ -179,7 +220,7 @@ FFLogoImageResult ffLogoPrintImageImpl(FFinstance* instance, void* imageMagick,
179220
return FF_LOGO_IMAGE_RESULT_SUCCESS;
180221
}
181222

182-
#endif
223+
#endif //FF_HAVE_IMAGEMAGICK{6, 7}
183224

184225
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
185226
{

0 commit comments

Comments
 (0)