Skip to content

Commit d647f9e

Browse files
Pretty song output, if the media is a browser playing a video
1 parent 4895c68 commit d647f9e

File tree

6 files changed

+110
-43
lines changed

6 files changed

+110
-43
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.1.0) # Threads::Threads
22

33
project(fastfetch
4-
VERSION 1.1.2
4+
VERSION 1.2.0
55
LANGUAGES C
66
)
77

src/detection/media.c

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "fastfetch.h"
22

3-
#include <ctype.h>
43
#include <string.h>
54
#include <pthread.h>
65

@@ -316,28 +315,6 @@ static void getMedia(FFinstance* instance, FFMediaResult* result)
316315
getBestBus(result, &data);
317316

318317
dlclose(dbus);
319-
320-
//If we are on a website, prepend the website name
321-
if(ffStrbufStartsWithS(&result->url, "https://www."))
322-
ffStrbufAppendS(&result->playerPretty, result->url.chars + 12);
323-
else if(ffStrbufStartsWithS(&result->url, "http://www."))
324-
ffStrbufAppendS(&result->playerPretty, result->url.chars + 11);
325-
else if(ffStrbufStartsWithS(&result->url, "https://"))
326-
ffStrbufAppendS(&result->playerPretty, result->url.chars + 8);
327-
else if(ffStrbufStartsWithS(&result->url, "http://"))
328-
ffStrbufAppendS(&result->playerPretty, result->url.chars + 7);
329-
330-
//If we found a website name, make it more pretty
331-
if(result->playerPretty.length > 0)
332-
{
333-
ffStrbufSubstrBeforeFirstC(&result->playerPretty, '/'); //Remove the path
334-
ffStrbufSubstrBeforeLastC(&result->playerPretty, '.'); //Remove the TLD
335-
}
336-
337-
//Check again for length, as we may have removed everything.
338-
//If we don't have subdomains, it is usually more pretty to capitalize the first letter.
339-
if(result->playerPretty.length > 0 && ffStrbufFirstIndexC(&result->playerPretty, '.') == result->playerPretty.length)
340-
result->playerPretty.chars[0] = (char) toupper(result->playerPretty.chars[0]);
341318
}
342319

343320
#endif
@@ -358,7 +335,6 @@ const FFMediaResult* ffDetectMedia(FFinstance* instance)
358335

359336
ffStrbufInit(&result.busNameShort);
360337
ffStrbufInit(&result.player);
361-
ffStrbufInit(&result.playerPretty);
362338
ffStrbufInit(&result.song);
363339
ffStrbufInit(&result.artist);
364340
ffStrbufInit(&result.album);
@@ -381,16 +357,6 @@ const FFMediaResult* ffDetectMedia(FFinstance* instance)
381357
if(result.player.length == 0)
382358
ffStrbufAppend(&result.player, &result.busNameShort);
383359

384-
bool hasCustomPrettyName = result.playerPretty.length > 0;
385-
386-
if(hasCustomPrettyName)
387-
ffStrbufAppendS(&result.playerPretty, " (");
388-
389-
ffStrbufAppend(&result.playerPretty, &result.player);
390-
391-
if(hasCustomPrettyName)
392-
ffStrbufAppendC(&result.playerPretty, ')');
393-
394360
pthread_mutex_unlock(&mutex);
395361
return &result;
396362
}

src/fastfetch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ static inline void printCommandHelp(const char* command)
312312
}
313313
else if(strcasecmp(command, "song-format") == 0)
314314
{
315-
constructAndPrintCommandHelpFormat("song", "{2} - {3} - {1}", 4,
315+
constructAndPrintCommandHelpFormat("song", "{3} - {4} - {1}", 5,
316+
"Song name pretty",
316317
"Song name",
317318
"Artist name",
318319
"Album name",

src/fastfetch.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ typedef struct FFMediaResult
259259
{
260260
FFstrbuf busNameShort; //e.g. plasma-browser-integration
261261
FFstrbuf player; // e.g. Google Chrome
262-
FFstrbuf playerPretty; // e.g. YouTube (Google Chrome)
263262
FFstrbuf song;
264263
FFstrbuf artist;
265264
FFstrbuf album;

src/modules/player.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "fastfetch.h"
22

3+
#include <ctype.h>
4+
35
#define FF_PLAYER_MODULE_NAME "Media Player"
46
#define FF_PLAYER_NUM_FORMAT_ARGS 3
57

@@ -13,15 +15,50 @@ void ffPrintPlayer(FFinstance* instance)
1315
return;
1416
}
1517

18+
FFstrbuf playerPretty;
19+
ffStrbufInit(&playerPretty);
20+
21+
//If we are on a website, prepend the website name
22+
if(ffStrbufStartsWithS(&media->url, "https://www."))
23+
ffStrbufAppendS(&playerPretty, media->url.chars + 12);
24+
else if(ffStrbufStartsWithS(&media->url, "http://www."))
25+
ffStrbufAppendS(&playerPretty, media->url.chars + 11);
26+
else if(ffStrbufStartsWithS(&media->url, "https://"))
27+
ffStrbufAppendS(&playerPretty, media->url.chars + 8);
28+
else if(ffStrbufStartsWithS(&media->url, "http://"))
29+
ffStrbufAppendS(&playerPretty, media->url.chars + 7);
30+
31+
//If we found a website name, make it more pretty
32+
if(playerPretty.length > 0)
33+
{
34+
ffStrbufSubstrBeforeFirstC(&playerPretty, '/'); //Remove the path
35+
ffStrbufSubstrBeforeLastC(&playerPretty, '.'); //Remove the TLD
36+
}
37+
38+
//Check again for length, as we may have removed everything.
39+
bool playerPrettyIsCustom = playerPretty.length > 0;
40+
41+
//If we don't have subdomains, it is usually more pretty to capitalize the first letter.
42+
if(playerPrettyIsCustom && ffStrbufFirstIndexC(&playerPretty, '.') == playerPretty.length)
43+
playerPretty.chars[0] = (char) toupper(playerPretty.chars[0]);
44+
45+
if(playerPrettyIsCustom)
46+
ffStrbufAppendS(&playerPretty, " (");
47+
48+
ffStrbufAppend(&playerPretty, &media->player);
49+
50+
if(playerPrettyIsCustom)
51+
ffStrbufAppendC(&playerPretty, ')');
52+
1653
if(instance->config.playerFormat.length == 0)
1754
{
1855
ffPrintLogoAndKey(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.playerKey);
19-
ffStrbufPutTo(&media->playerPretty, stdout);
56+
ffStrbufPutTo(&playerPretty, stdout);
2057
}
2158
else
2259
{
2360
ffPrintFormatString(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.playerKey, &instance->config.playerFormat, NULL, FF_PLAYER_NUM_FORMAT_ARGS, (FFformatarg[]){
24-
{FF_FORMAT_ARG_TYPE_STRBUF, &media->playerPretty},
61+
{FF_FORMAT_ARG_TYPE_STRBUF, &playerPretty},
2562
{FF_FORMAT_ARG_TYPE_STRBUF, &media->player},
2663
{FF_FORMAT_ARG_TYPE_STRBUF, &media->busNameShort}
2764
});

src/modules/song.c

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
#include "fastfetch.h"
22

3+
#include <ctype.h>
4+
35
#define FF_SONG_MODULE_NAME "Song"
4-
#define FF_SONG_NUM_FORMAT_ARGS 4
6+
#define FF_SONG_NUM_FORMAT_ARGS 5
7+
8+
static bool shouldIgoreChar(char c)
9+
{
10+
return isblank(c) || c == '-' || c == '.';
11+
}
12+
13+
static bool artistInSongTitle(const FFstrbuf* song, const FFstrbuf* artist)
14+
{
15+
uint32_t artistIndex = 0;
16+
uint32_t songIndex = 0;
17+
18+
while(true)
19+
{
20+
while(shouldIgoreChar(song->chars[songIndex]))
21+
++songIndex;
22+
23+
while(shouldIgoreChar(artist->chars[artistIndex]))
24+
++artistIndex;
25+
26+
if(artist->chars[artistIndex] == '\0')
27+
return true;
28+
29+
if(song->chars[songIndex] == '\0')
30+
return false;
31+
32+
if(tolower(song->chars[songIndex]) != tolower(artist->chars[artistIndex]))
33+
return false;
34+
35+
++artistIndex;
36+
++songIndex;
37+
}
38+
39+
//Unreachable
40+
return false;
41+
}
542

643
void ffPrintSong(FFinstance* instance)
744
{
@@ -13,13 +50,39 @@ void ffPrintSong(FFinstance* instance)
1350
return;
1451
}
1552

53+
FFstrbuf songPretty;
54+
ffStrbufInitCopy(&songPretty, &media->song);
55+
const char* removeStrings[] = {
56+
"(Official Music Video)", "(Official Video)", "(Music Video)",
57+
"[Official Music Video]", "[Official Video]", "[Music Video]",
58+
"| Official Music Video", "| Official Video", "| Music Video",
59+
"[Official Audio]", "[Audio]", "(Audio)", "| Official Audio", "| Audio", "| OFFICIAL AUDIO",
60+
"(Lyric Video)", "(Official Lyric Video)", "(Lyrics)",
61+
"(dirty version)", "(dirty)", "(Clean)", "(Clean Version)",
62+
};
63+
ffStrbufRemoveStringsA(&songPretty, sizeof(removeStrings) / sizeof(removeStrings[0]), removeStrings);
64+
ffStrbufTrimRight(&songPretty, ' ');
65+
66+
if(songPretty.length == 0)
67+
ffStrbufAppend(&songPretty, &media->song);
68+
1669
if(instance->config.songFormat.length == 0)
1770
{
71+
//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)
72+
FFstrbuf artistPretty;
73+
ffStrbufInitCopy(&artistPretty, &media->artist);
74+
ffStrbufRemoveIgnCaseEndS(&artistPretty, " - Topic");
75+
ffStrbufRemoveIgnCaseEndS(&artistPretty, "VEVO");
76+
ffStrbufTrimRight(&artistPretty, ' ');
77+
78+
if(artistInSongTitle(&songPretty, &artistPretty))
79+
ffStrbufClear(&artistPretty);
80+
1881
ffPrintLogoAndKey(instance, FF_SONG_MODULE_NAME, 0, &instance->config.songKey);
1982

20-
if(media->artist.length > 0)
83+
if(artistPretty.length > 0)
2184
{
22-
ffStrbufWriteTo(&media->artist, stdout);
85+
ffStrbufWriteTo(&artistPretty, stdout);
2386
fputs(" - ", stdout);
2487
}
2588

@@ -29,11 +92,12 @@ void ffPrintSong(FFinstance* instance)
2992
fputs(" - ", stdout);
3093
}
3194

32-
ffStrbufPutTo(&media->song, stdout);
95+
ffStrbufPutTo(&songPretty, stdout);
3396
}
3497
else
3598
{
3699
ffPrintFormatString(instance, FF_SONG_MODULE_NAME, 0, &instance->config.songKey, &instance->config.songFormat, NULL, FF_SONG_NUM_FORMAT_ARGS, (FFformatarg[]){
100+
{FF_FORMAT_ARG_TYPE_STRBUF, &songPretty},
37101
{FF_FORMAT_ARG_TYPE_STRBUF, &media->song},
38102
{FF_FORMAT_ARG_TYPE_STRBUF, &media->artist},
39103
{FF_FORMAT_ARG_TYPE_STRBUF, &media->album},

0 commit comments

Comments
 (0)