Allow loading images from data dirs too

#375
This commit is contained in:
Linus Dierheimer
2023-01-28 11:14:16 +01:00
parent 4afba798ba
commit 9bb6d8fd6e
3 changed files with 78 additions and 54 deletions
+26 -20
View File
@@ -673,7 +673,7 @@ static bool getCharacterPixelDimensions(FFLogoRequestData* requestData)
return requestData->characterPixelWidth > 1.0 && requestData->characterPixelHeight > 1.0;
}
static bool printImageIfExistsSlowPath(FFinstance* instance, FFLogoType type)
static bool printImageIfExistsSlowPath(FFinstance* instance, FFLogoType type, bool printError)
{
FFLogoRequestData requestData;
requestData.type = type;
@@ -684,7 +684,8 @@ static bool printImageIfExistsSlowPath(FFinstance* instance, FFLogoType type)
(type != FF_LOGO_TYPE_IMAGE_CHAFA || instance->config.logo.width == 0 || instance->config.logo.height == 0) &&
!getCharacterPixelDimensions(&requestData)
) {
fputs("Logo: getCharacterPixelDimensions() failed", stderr);
if(printError)
fputs("Logo: getCharacterPixelDimensions() failed", stderr);
return false;
}
@@ -700,7 +701,8 @@ static bool printImageIfExistsSlowPath(FFinstance* instance, FFLogoType type)
{
//We can safely return here, because if realpath failed, we surely won't be able to read the file
ffStrbufDestroy(&requestData.cacheDir);
fputs("Logo: Querying realpath of the image source failed", stderr);
if(printError)
fputs("Logo: Querying realpath of the image source failed", stderr);
return false;
}
ffStrbufRecalculateLength(&requestData.cacheDir);
@@ -730,26 +732,28 @@ static bool printImageIfExistsSlowPath(FFinstance* instance, FFLogoType type)
ffStrbufDestroy(&requestData.cacheDir);
switch(result)
if(result == FF_LOGO_IMAGE_RESULT_SUCCESS)
return true;
if(printError)
{
case FF_LOGO_IMAGE_RESULT_INIT_ERROR:
fputs("Logo: Init Image Magick library failed\n", stderr);
return false;
case FF_LOGO_IMAGE_RESULT_RUN_ERROR:
if(result == FF_LOGO_IMAGE_RESULT_INIT_ERROR)
fputs("Logo: Image Magick library not found\n", stderr);
else
fputs("Logo: Failed to load / convert the image source\n", stderr);
return false;
default:
return true;
}
return false;
}
#endif //FF_HAVE_IMAGEMAGICK{6, 7}
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type, bool printError)
{
if(!ffFileExists(instance->config.logo.source.chars, S_IFREG))
if(!ffFileExists(instance->config.logo.source.chars, S_IFREG & S_IFLNK))
{
fputs("Logo: Image file not found\n", stderr);
if(printError)
fprintf(stderr, "Logo: Image source \"%s\" does not exist\n", instance->config.logo.source.chars);
return false;
}
@@ -764,18 +768,20 @@ bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
)
return printImageKittyDirect(instance);
#ifndef FF_HAVE_CHAFA
#if !defined(FF_HAVE_CHAFA)
if(type == FF_LOGO_TYPE_IMAGE_CHAFA)
{
fputs("Logo: Fastfetch was built without Chafa support\n", stderr);
if(printError)
fputs("Logo: Chafa support is not compiled in\n", stderr);
return false;
}
#endif
#if defined(FF_HAVE_IMAGEMAGICK7) || defined(FF_HAVE_IMAGEMAGICK6)
return printImageIfExistsSlowPath(instance, type);
#else
fputs("Logo: Fastfetch was built without ImageMagick support\n", stderr);
#if !defined(FF_HAVE_IMAGEMAGICK7) && !defined(FF_HAVE_IMAGEMAGICK6)
if(printError)
fputs("Logo: Image Magick support is not compiled in\n", stderr);
return false;
#else
return printImageIfExistsSlowPath(instance, type, printError);
#endif
}
+51 -33
View File
@@ -280,15 +280,19 @@ static inline void logoPrintDetected(FFinstance* instance)
logoPrintStruct(instance, logoGetBuiltinDetected(instance));
}
static void logoPrintData(FFinstance* instance, bool doColorReplacement) {
static bool logoPrintData(FFinstance* instance, bool doColorReplacement) {
if(instance->config.logo.source.length == 0)
return false;
ffLogoPrintChars(instance, instance->config.logo.source.chars, doColorReplacement);
logoApplyColorsDetected(instance);
return true;
}
static bool loadLogoFile(const FFinstance* instance, FFstrbuf* buffer)
static void updateLogoPath(FFinstance* instance)
{
if(ffAppendFileBuffer(instance->config.logo.source.chars, buffer))
return true;
if(ffFileExists(instance->config.logo.source.chars, S_IFREG & S_IFLNK))
return;
FFstrbuf fullPath;
ffStrbufInit(&fullPath);
@@ -300,15 +304,14 @@ static bool loadLogoFile(const FFinstance* instance, FFstrbuf* buffer)
ffStrbufAppendS(&fullPath, "fastfetch/logos/");
ffStrbufAppend(&fullPath, &instance->config.logo.source);
if(ffAppendFileBuffer(fullPath.chars, buffer))
if(ffFileExists(fullPath.chars, S_IFREG & S_IFLNK))
{
ffStrbufDestroy(&fullPath);
return true;
ffStrbufSet(&instance->config.logo.source, &fullPath);
break;
}
}
ffStrbufDestroy(&fullPath);
return false;
}
static bool logoPrintFileIfExists(FFinstance* instance, bool doColorReplacement, bool raw)
@@ -316,7 +319,7 @@ static bool logoPrintFileIfExists(FFinstance* instance, bool doColorReplacement,
FFstrbuf content;
ffStrbufInit(&content);
if(!loadLogoFile(instance, &content))
if(!ffAppendFileBuffer(instance->config.logo.source.chars, &content))
{
ffStrbufDestroy(&content);
fputs("Logo: Failed to load file content from logo source\n", stderr);
@@ -332,45 +335,57 @@ static bool logoPrintFileIfExists(FFinstance* instance, bool doColorReplacement,
return true;
}
static bool logoPrintImageIfExists(FFinstance* instance, FFLogoType logo)
static bool logoPrintImageIfExists(FFinstance* instance, FFLogoType logo, bool printError)
{
if(!ffLogoPrintImageIfExists(instance, logo))
if(!ffLogoPrintImageIfExists(instance, logo, printError))
return false;
logoApplyColorsDetected(instance);
return true;
}
static void logoPrintKnownType(FFinstance* instance)
static bool logoTryKnownType(FFinstance* instance)
{
bool successfull = true;
if(instance->config.logo.type == FF_LOGO_TYPE_NONE)
{
logoApplyColorsDetected(instance);
return true;
}
if(instance->config.logo.type == FF_LOGO_TYPE_BUILTIN)
successfull = logoPrintBuiltinIfExists(instance, instance->config.logo.source.chars);
else if(instance->config.logo.type == FF_LOGO_TYPE_FILE)
successfull = logoPrintFileIfExists(instance, true, false);
else if(instance->config.logo.type == FF_LOGO_TYPE_FILE_RAW)
successfull = logoPrintFileIfExists(instance, false, false);
else if(instance->config.logo.type == FF_LOGO_TYPE_DATA)
logoPrintData(instance, true);
else if(instance->config.logo.type == FF_LOGO_TYPE_DATA_RAW)
logoPrintData(instance, false);
else if(instance->config.logo.type == FF_LOGO_TYPE_IMAGE_RAW)
return logoPrintBuiltinIfExists(instance, instance->config.logo.source.chars);
if(instance->config.logo.type == FF_LOGO_TYPE_DATA)
return logoPrintData(instance, true);
if(instance->config.logo.type == FF_LOGO_TYPE_DATA_RAW)
return logoPrintData(instance, false);
updateLogoPath(instance); //We sure have a file, resolve relative paths
if(instance->config.logo.type == FF_LOGO_TYPE_FILE)
return logoPrintFileIfExists(instance, true, false);
if(instance->config.logo.type == FF_LOGO_TYPE_FILE_RAW)
return logoPrintFileIfExists(instance, false, false);
if(instance->config.logo.type == FF_LOGO_TYPE_IMAGE_RAW)
{
if(instance->config.logo.width == 0 || instance->config.logo.height == 0)
{
fputs("both `--logo-width` and `--logo-height` must be specified\n", stderr);
successfull = false;
return false;
}
else
successfull = logoPrintFileIfExists(instance, false, true);
}
else if(instance->config.logo.type == FF_LOGO_TYPE_NONE)
logoApplyColorsDetected(instance);
else //image
successfull = logoPrintImageIfExists(instance, instance->config.logo.type);
if(!successfull)
return logoPrintFileIfExists(instance, false, true);
}
return logoPrintImageIfExists(instance, instance->config.logo.type, true);
}
static void logoPrintKnownType(FFinstance* instance)
{
if(!logoTryKnownType(instance))
logoPrintDetected(instance);
}
@@ -404,6 +419,9 @@ void ffLogoPrint(FFinstance* instance)
if(logoPrintBuiltinIfExists(instance, instance->config.logo.source.chars))
return;
//Make sure the logo path is set correctly.
updateLogoPath(instance);
const FFTerminalShellResult* terminalShell = ffDetectTerminalShell(instance);
//Terminal emulators that support kitty graphics protocol.
@@ -414,7 +432,7 @@ void ffLogoPrint(FFinstance* instance)
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "wayst") == 0;
//Try to load the logo as an image. If it succeeds, print it and return.
if(logoPrintImageIfExists(instance, supportsKitty ? FF_LOGO_TYPE_IMAGE_KITTY : FF_LOGO_TYPE_IMAGE_CHAFA))
if(logoPrintImageIfExists(instance, supportsKitty ? FF_LOGO_TYPE_IMAGE_KITTY : FF_LOGO_TYPE_IMAGE_CHAFA, false))
return;
//Try to load the logo as a file. If it succeeds, print it and return.
+1 -1
View File
@@ -24,6 +24,6 @@ const FFlogo* ffLogoBuiltinGetUnknown();
GetLogoMethod* ffLogoBuiltinGetAll();
//image/image.c
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type);
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type, bool printError);
#endif