From ef03c3e6614481154c015487f87cf27a5ed788b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Sun, 20 Nov 2022 02:57:10 +0300 Subject: Add flag to limit the length of the title --- README.md | 2 ++ main.go | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a8712c..b617c52 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ When running, the program will pipe out json in waybar's format. Add something l Usage of ./waybar-mpris: --autofocus Auto switch to currently playing music players. --interpolate Interpolate track position (helpful for players that don't update regularly, e.g mpDris2) + --max-title int Maximum length of title. If the title's longer N-3 characters will be printed and ... will be printed at the end. (default 70) --pause string Pause symbol/text to use. (default "\uf8e3") --play string Play symbol/text to use. (default "▶") --replace Replace existing waybar-mpris if found. When false, new instance will clone the original instances output. @@ -83,6 +84,7 @@ Usage of ./waybar-mpris: * `--autofocus` makes waybar-mpris automatically focus on currently playing music players. * `--position` enables the display of the track position. * `--interpolate` increments the track position every second. This is useful for players (e.g mpDris2) that don't regularly update the position. +* `--max-title` the maximum number of characters before the title cuts off. * `--replace`: By default, new instances will attach to the existing one so that the output is identical. This lets this instance replace any others running. It isn't recommended. * `--send` sends commands to an already running waybar-mpris instance via a unix socket. Commands: * `player-next`: Switch to displaying and controlling next available player. diff --git a/main.go b/main.go index 4593ca2..7597155 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ var ( replace = false isSharing = false isDataSharing = false + maxTitleLen = 70 writer io.Writer = os.Stdout shareWriter, dataWriter io.Writer ) @@ -184,7 +185,11 @@ func formatOutput(p *player, icon string, fstr string) string { case tokAlbum: buf = append(buf, []byte(p.Album)...) case tokTitle: - buf = append(buf, []byte(p.Title)...) + title := p.Title + if maxTitleLen > 0 && len(title) > maxTitleLen { + title = title[:maxTitleLen-3] + "..." + } + buf = append(buf, []byte(title)...) case tokPosition: position := secondsToString(int(p.Position / 1000000)) buf = append(buf, []byte(position)...) @@ -571,6 +576,8 @@ func main() { "Interpolate track position (helpful for players that don't update regularly, e.g mpDris2)") flag.BoolVar(&replace, "replace", replace, "Replace existing waybar-mpris if found. When false, new instance will clone the original instances output.") + flag.IntVar(&maxTitleLen, "max-title", maxTitleLen, + "Maximum length of title. If the title's longer N-3 characters will be printed and ... will be printed at the end.") var command string flag.StringVar(&command, "send", "", "send command to already runnning waybar-mpris instance. (options: "+strings.Join(commands, "/")+")") -- cgit v1.2.3