Skip to content

Commit

Permalink
Allow choosing time format
Browse files Browse the repository at this point in the history
Introduce new format string for playback time, supporting formats like
-remaining/total, elapsed/total, …

Fixes PromyLOPh#699.
  • Loading branch information
PromyLOPh committed Aug 9, 2020
1 parent bc6bd8a commit 4c228cd
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions contrib/config-example
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#tired_icon = zZ
#format_nowplaying_station = Station %n
#format_list_song = %i) %a - %t%r (%d)%@%s
#format_time = %e/%t

#rpc_host = internal-tuner.pandora.com
#partner_user = pandora one
Expand Down
16 changes: 16 additions & 0 deletions contrib/pianobar.1
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,22 @@ Station name
.B %i
Station id

.TP
.B format_time = %s%r/%t
Time format.

.B %e
Elapsed time

.B %r
Remaining time

.B %s
Sign

.B %t
Total time

.TP
.B gain_mul = 1.0
Pandora sends a ReplayGain value with every song. This sets a multiplier so that the gain adjustment can be
Expand Down
23 changes: 17 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ static void BarMainPlayerCleanup (BarApp_t *app, pthread_t *playerThread) {
*/
static void BarMainPrintTime (BarApp_t *app) {
unsigned int songRemaining;
char sign;
char sign[2] = {0, 0};
player_t * const player = &app->player;

pthread_mutex_lock (&player->lock);
Expand All @@ -322,15 +322,26 @@ static void BarMainPrintTime (BarApp_t *app) {

if (songPlayed <= songDuration) {
songRemaining = songDuration - songPlayed;
sign = '-';
sign[0] = '-';
} else {
/* longer than expected */
songRemaining = songPlayed - songDuration;
sign = '+';
sign[0] = '+';
}
BarUiMsg (&app->settings, MSG_TIME, "%c%02u:%02u/%02u:%02u\r",
sign, songRemaining / 60, songRemaining % 60,
songDuration / 60, songDuration % 60);

char outstr[512], totalFormatted[16], remainingFormatted[16],
elapsedFormatted[16];
const char *vals[] = {totalFormatted, remainingFormatted,
elapsedFormatted, sign};
snprintf (totalFormatted, sizeof (totalFormatted), "%02u:%02u",
songDuration/60, songDuration%60);
snprintf (remainingFormatted, sizeof (remainingFormatted), "%02u:%02u",
songRemaining/60, songRemaining%60);
snprintf (elapsedFormatted, sizeof (elapsedFormatted), "%02u:%02u",
songPlayed/60, songPlayed%60);
BarUiCustomFormat (outstr, sizeof (outstr), app->settings.timeFormat,
"tres", vals);
BarUiMsg (&app->settings, MSG_TIME, "%s\r", outstr);
}

/* main loop
Expand Down
5 changes: 5 additions & 0 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void BarSettingsDestroy (BarSettings_t *settings) {
free (settings->npSongFormat);
free (settings->npStationFormat);
free (settings->listSongFormat);
free (settings->timeFormat);
free (settings->fifo);
free (settings->audioPipe);
free (settings->rpcHost);
Expand Down Expand Up @@ -175,6 +176,7 @@ void BarSettingsRead (BarSettings_t *settings) {
settings->npSongFormat = strdup ("\"%t\" by \"%a\" on \"%l\"%r%@%s");
settings->npStationFormat = strdup ("Station \"%n\" (%i)");
settings->listSongFormat = strdup ("%i) %a - %t%r");
settings->timeFormat = strdup ("%s%r/%t");
settings->rpcHost = strdup (PIANO_RPC_HOST);
settings->rpcTlsPort = strdup ("443");
settings->partnerUser = strdup ("android");
Expand Down Expand Up @@ -389,6 +391,9 @@ void BarSettingsRead (BarSettings_t *settings) {
} else if (streq ("format_list_song", key)) {
free (settings->listSongFormat);
settings->listSongFormat = strdup (val);
} else if (streq ("format_time", key)) {
free (settings->timeFormat);
settings->timeFormat = strdup (val);
} else if (streq ("fifo", key)) {
free (settings->fifo);
settings->fifo = BarSettingsExpandTilde (val, userhome);
Expand Down
2 changes: 1 addition & 1 deletion src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ typedef struct {
char *atIcon;
char *npSongFormat;
char *npStationFormat;
char *listSongFormat;
char *listSongFormat, *timeFormat;
char *fifo;
char *rpcHost, *rpcTlsPort, *partnerUser, *partnerPassword, *device, *inkey, *outkey, *caBundle;
char *audioPipe;
Expand Down
2 changes: 1 addition & 1 deletion src/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ char *BarUiSelectMusicId (BarApp_t *app, PianoStation_t *station,
* @param format characters
* @param replacement for each given format character
*/
static void BarUiCustomFormat (char *dest, size_t destSize, const char *format,
void BarUiCustomFormat (char *dest, size_t destSize, const char *format,
const char *formatChars, const char **formatVals) {
bool haveFormatChar = false;

Expand Down
2 changes: 2 additions & 0 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ void BarUiStartEventCmd (const BarSettings_t *, const char *,
bool BarUiPianoCall (BarApp_t * const, const PianoRequestType_t,
void *, PianoReturn_t *, CURLcode *);
void BarUiHistoryPrepend (BarApp_t *app, PianoSong_t *song);
void BarUiCustomFormat (char *dest, size_t destSize, const char *format,
const char *formatChars, const char **formatVals);

0 comments on commit 4c228cd

Please sign in to comment.