Compare commits

..

4 Commits

Author SHA1 Message Date
Linus Dierheimer d647f9e3f2 Pretty song output, if the media is a browser playing a video 2022-03-23 15:43:30 +01:00
Linus Dierheimer 4895c68ca9 Capitalize the first letter of media player if appropiate 2022-03-23 11:54:05 +01:00
Linus Dierheimer f0b9800974 Fix fastfetch recaching every time if an old cache exists 2022-03-23 11:38:42 +01:00
Linus Dierheimer be84549f4c Refactored media detection 2022-03-23 11:15:50 +01:00
7 changed files with 133 additions and 76 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.1.0) # Threads::Threads
project(fastfetch
VERSION 1.1.0
VERSION 1.2.0
LANGUAGES C
)
+4 -4
View File
@@ -246,9 +246,10 @@ void ffCacheValidate(FFinstance* instance)
ffStrbufInit(&content);
ffAppendFileContent(path.chars, &content);
if(ffStrbufCompS(&content, FASTFETCH_PROJECT_VERSION) == 0)
bool isSameVersion = ffStrbufCompS(&content, FASTFETCH_PROJECT_VERSION) == 0;
ffStrbufDestroy(&content);
if(isSameVersion)
{
ffStrbufDestroy(&content);
ffStrbufDestroy(&path);
return;
}
@@ -261,7 +262,6 @@ void ffCacheValidate(FFinstance* instance)
ffWriteFileContent(path.chars, &version);
ffStrbufDestroy(&version);
ffStrbufDestroy(&content);
ffStrbufDestroy(&path);
}
@@ -409,7 +409,7 @@ bool ffWriteFDContent(int fd, const FFstrbuf* content)
void ffWriteFileContent(const char* fileName, const FFstrbuf* content)
{
int fd = open(fileName, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int fd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(fd == -1)
return;
+19 -63
View File
@@ -1,6 +1,5 @@
#include "fastfetch.h"
#include <ctype.h>
#include <string.h>
#include <pthread.h>
@@ -81,6 +80,22 @@ static bool getValue(DBusMessageIter* iter, FFstrbuf* result, DBusData* data)
return foundAValue;
}
static DBusMessage* getReply(DBusMessage* request, DBusData* data)
{
DBusPendingCall* pendingCall = NULL;
dbus_bool_t succesfull = data->ffdbus_connection_send_with_reply(data->connection, request, &pendingCall, DBUS_TIMEOUT_USE_DEFAULT);
data->ffdbus_message_unref(request);
if(!succesfull || pendingCall == NULL)
return NULL;
data->ffdbus_connection_flush(data->connection);
data->ffdbus_pending_call_block(pendingCall);
DBusMessage* reply = data->ffdbus_pending_call_steal_reply(pendingCall);
data->ffdbus_pending_call_unref(pendingCall);
return reply;
}
static DBusMessage* getProperty(const char* busName, const char* objectPath, const char* interface, const char* property, DBusData* data)
{
DBusMessage* message = data->ffdbus_message_new_method_call(busName, objectPath, "org.freedesktop.DBus.Properties", "Get");
@@ -102,20 +117,7 @@ static DBusMessage* getProperty(const char* busName, const char* objectPath, con
return NULL;
}
DBusPendingCall* pending;
bool sendSuccessfull = data->ffdbus_connection_send_with_reply(data->connection, message, &pending, DBUS_TIMEOUT_USE_DEFAULT);
data->ffdbus_message_unref(message);
if(pending == NULL || !sendSuccessfull)
return NULL;
data->ffdbus_connection_flush(data->connection);
data->ffdbus_pending_call_block(pending);
DBusMessage* reply = data->ffdbus_pending_call_steal_reply(pending);
data->ffdbus_pending_call_unref(pending);
return reply;
return getReply(message, data);
}
static void getPropertyString(const char* busName, const char* objectPath, const char* interface, const char* property, FFstrbuf* result, DBusData* data)
@@ -244,17 +246,7 @@ static void getBestBus(FFMediaResult* result, DBusData* data)
if(message == NULL)
return;
DBusPendingCall* pending;
bool sendSuccessfull = data->ffdbus_connection_send_with_reply(data->connection, message, &pending, DBUS_TIMEOUT_USE_DEFAULT);
data->ffdbus_message_unref(message);
if(pending == NULL || !sendSuccessfull)
return;
data->ffdbus_connection_flush(data->connection);
data->ffdbus_pending_call_block(pending);
DBusMessage* reply = data->ffdbus_pending_call_steal_reply(pending);
data->ffdbus_pending_call_unref(pending);
DBusMessage* reply = getReply(message, data);
if(reply == NULL)
return;
@@ -343,7 +335,6 @@ const FFMediaResult* ffDetectMedia(FFinstance* instance)
ffStrbufInit(&result.busNameShort);
ffStrbufInit(&result.player);
ffStrbufInit(&result.playerPretty);
ffStrbufInit(&result.song);
ffStrbufInit(&result.artist);
ffStrbufInit(&result.album);
@@ -354,7 +345,7 @@ const FFMediaResult* ffDetectMedia(FFinstance* instance)
#endif
//Set busNameShort if a custom player was given, but loading dbus failed
if(instance->config.playerName.length > 0 && result.player.length == 0)
if(instance->config.playerName.length > 0 && result.busNameShort.length == 0)
{
if(ffStrbufStartsWithS(&instance->config.playerName, FF_DBUS_MPRIS_PREFIX))
ffStrbufAppendS(&result.busNameShort, instance->config.playerName.chars + sizeof(FF_DBUS_MPRIS_PREFIX) - 1);
@@ -366,41 +357,6 @@ const FFMediaResult* ffDetectMedia(FFinstance* instance)
if(result.player.length == 0)
ffStrbufAppend(&result.player, &result.busNameShort);
//If we are on a website, prepend the website name
if(ffStrbufStartsWithS(&result.url, "https://www.")) {
ffStrbufAppendS(&result.playerPretty, result.url.chars + 12);
}
else if(ffStrbufStartsWithS(&result.url, "http://www.")) {
ffStrbufAppendS(&result.playerPretty, result.url.chars + 11);
}
else if(ffStrbufStartsWithS(&result.url, "https://")) {
ffStrbufAppendS(&result.playerPretty, result.url.chars + 8);
}
else if(ffStrbufStartsWithS(&result.url, "http://")) {
ffStrbufAppendS(&result.playerPretty, result.url.chars + 7);
}
//If we found a website name, make it more pretty
if(result.playerPretty.length > 0)
{
ffStrbufSubstrBeforeFirstC(&result.playerPretty, '/'); //Remove the path
ffStrbufSubstrBeforeLastC(&result.playerPretty, '.'); //Remove the TLD
}
//We may have removed everything
bool hasCustomPrettyName = result.playerPretty.length > 0;
if(hasCustomPrettyName)
{
result.playerPretty.chars[0] = (char) toupper(result.playerPretty.chars[0]);
ffStrbufAppendS(&result.playerPretty, " (");
}
ffStrbufAppend(&result.playerPretty, &result.player);
if(hasCustomPrettyName)
ffStrbufAppendC(&result.playerPretty, ')');
pthread_mutex_unlock(&mutex);
return &result;
}
+2 -1
View File
@@ -312,7 +312,8 @@ static inline void printCommandHelp(const char* command)
}
else if(strcasecmp(command, "song-format") == 0)
{
constructAndPrintCommandHelpFormat("song", "{2} - {3} - {1}", 4,
constructAndPrintCommandHelpFormat("song", "{3} - {4} - {1}", 5,
"Song name pretty",
"Song name",
"Artist name",
"Album name",
-1
View File
@@ -259,7 +259,6 @@ typedef struct FFMediaResult
{
FFstrbuf busNameShort; //e.g. plasma-browser-integration
FFstrbuf player; // e.g. Google Chrome
FFstrbuf playerPretty; // e.g. YouTube (Google Chrome)
FFstrbuf song;
FFstrbuf artist;
FFstrbuf album;
+39 -2
View File
@@ -1,5 +1,7 @@
#include "fastfetch.h"
#include <ctype.h>
#define FF_PLAYER_MODULE_NAME "Media Player"
#define FF_PLAYER_NUM_FORMAT_ARGS 3
@@ -13,15 +15,50 @@ void ffPrintPlayer(FFinstance* instance)
return;
}
FFstrbuf playerPretty;
ffStrbufInit(&playerPretty);
//If we are on a website, prepend the website name
if(ffStrbufStartsWithS(&media->url, "https://www."))
ffStrbufAppendS(&playerPretty, media->url.chars + 12);
else if(ffStrbufStartsWithS(&media->url, "http://www."))
ffStrbufAppendS(&playerPretty, media->url.chars + 11);
else if(ffStrbufStartsWithS(&media->url, "https://"))
ffStrbufAppendS(&playerPretty, media->url.chars + 8);
else if(ffStrbufStartsWithS(&media->url, "http://"))
ffStrbufAppendS(&playerPretty, media->url.chars + 7);
//If we found a website name, make it more pretty
if(playerPretty.length > 0)
{
ffStrbufSubstrBeforeFirstC(&playerPretty, '/'); //Remove the path
ffStrbufSubstrBeforeLastC(&playerPretty, '.'); //Remove the TLD
}
//Check again for length, as we may have removed everything.
bool playerPrettyIsCustom = playerPretty.length > 0;
//If we don't have subdomains, it is usually more pretty to capitalize the first letter.
if(playerPrettyIsCustom && ffStrbufFirstIndexC(&playerPretty, '.') == playerPretty.length)
playerPretty.chars[0] = (char) toupper(playerPretty.chars[0]);
if(playerPrettyIsCustom)
ffStrbufAppendS(&playerPretty, " (");
ffStrbufAppend(&playerPretty, &media->player);
if(playerPrettyIsCustom)
ffStrbufAppendC(&playerPretty, ')');
if(instance->config.playerFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.playerKey);
ffStrbufPutTo(&media->playerPretty, stdout);
ffStrbufPutTo(&playerPretty, stdout);
}
else
{
ffPrintFormatString(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.playerKey, &instance->config.playerFormat, NULL, FF_PLAYER_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &media->playerPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &playerPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->player},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->busNameShort}
});
+68 -4
View File
@@ -1,7 +1,44 @@
#include "fastfetch.h"
#include <ctype.h>
#define FF_SONG_MODULE_NAME "Song"
#define FF_SONG_NUM_FORMAT_ARGS 4
#define FF_SONG_NUM_FORMAT_ARGS 5
static bool shouldIgoreChar(char c)
{
return isblank(c) || c == '-' || c == '.';
}
static bool artistInSongTitle(const FFstrbuf* song, const FFstrbuf* artist)
{
uint32_t artistIndex = 0;
uint32_t songIndex = 0;
while(true)
{
while(shouldIgoreChar(song->chars[songIndex]))
++songIndex;
while(shouldIgoreChar(artist->chars[artistIndex]))
++artistIndex;
if(artist->chars[artistIndex] == '\0')
return true;
if(song->chars[songIndex] == '\0')
return false;
if(tolower(song->chars[songIndex]) != tolower(artist->chars[artistIndex]))
return false;
++artistIndex;
++songIndex;
}
//Unreachable
return false;
}
void ffPrintSong(FFinstance* instance)
{
@@ -13,13 +50,39 @@ void ffPrintSong(FFinstance* instance)
return;
}
FFstrbuf songPretty;
ffStrbufInitCopy(&songPretty, &media->song);
const char* removeStrings[] = {
"(Official Music Video)", "(Official Video)", "(Music Video)",
"[Official Music Video]", "[Official Video]", "[Music Video]",
"| Official Music Video", "| Official Video", "| Music Video",
"[Official Audio]", "[Audio]", "(Audio)", "| Official Audio", "| Audio", "| OFFICIAL AUDIO",
"(Lyric Video)", "(Official Lyric Video)", "(Lyrics)",
"(dirty version)", "(dirty)", "(Clean)", "(Clean Version)",
};
ffStrbufRemoveStringsA(&songPretty, sizeof(removeStrings) / sizeof(removeStrings[0]), removeStrings);
ffStrbufTrimRight(&songPretty, ' ');
if(songPretty.length == 0)
ffStrbufAppend(&songPretty, &media->song);
if(instance->config.songFormat.length == 0)
{
//We don't expose artistPretty to the format, as it might be empty (when the think that the artist is already in the song title)
FFstrbuf artistPretty;
ffStrbufInitCopy(&artistPretty, &media->artist);
ffStrbufRemoveIgnCaseEndS(&artistPretty, " - Topic");
ffStrbufRemoveIgnCaseEndS(&artistPretty, "VEVO");
ffStrbufTrimRight(&artistPretty, ' ');
if(artistInSongTitle(&songPretty, &artistPretty))
ffStrbufClear(&artistPretty);
ffPrintLogoAndKey(instance, FF_SONG_MODULE_NAME, 0, &instance->config.songKey);
if(media->artist.length > 0)
if(artistPretty.length > 0)
{
ffStrbufWriteTo(&media->artist, stdout);
ffStrbufWriteTo(&artistPretty, stdout);
fputs(" - ", stdout);
}
@@ -29,11 +92,12 @@ void ffPrintSong(FFinstance* instance)
fputs(" - ", stdout);
}
ffStrbufPutTo(&media->song, stdout);
ffStrbufPutTo(&songPretty, stdout);
}
else
{
ffPrintFormatString(instance, FF_SONG_MODULE_NAME, 0, &instance->config.songKey, &instance->config.songFormat, NULL, FF_SONG_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &songPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->song},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->artist},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->album},