Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/lua
- local lgi = require('lgi')
- local player = lgi.Playerctl.Player{}
- -- penlight func, temporary.
- function assert_arg (n,val,tp,verify,msg,lev)
- if type(val) ~= tp then
- error(("argument %d expected a '%s', got a '%s'"):format(n,tp,type(val)),lev or 2)
- end
- if verify and not verify(val) then
- error(("argument %d: '%s' %s"):format(n,val,msg),lev or 2)
- end
- return val
- end
- -- penlight func
- local function assert_string (n,s)
- assert_arg(n,s,'string')
- end
- -- decode uri to standard unix path
- local decodeURI
- do
- local char, gsub, tonumber = string.char, string.gsub, tonumber
- local function _(hex) return char(tonumber(hex, 16)) end
- function decodeURI(s)
- s = gsub(s, '%%(%x%x)', _)
- s = gsub(s, 'file://', '')
- return s
- end
- end
- -- check if file is available, if not return
- local raw_path = player:print_metadata_prop('xesam:url')
- if raw_path == nil then
- return
- else
- path = decodeURI(raw_path)
- end
- -- capture system cmd
- function os.capture(cmd)
- local f = io.popen(cmd, 'r')
- local s = f:read()
- f:close()
- return s
- end
- -- get bitrate
- function string_to_decode(str)
- local cmd = ('mediainfo --inform="Audio;%BitRate/String%" "' ..path.. '" ')
- local buf = os.capture(cmd)
- return buf
- end
- -- when albumarArtist is a list, filter all but first artist
- local raw_albumArtist = player:print_metadata_prop('xesam:albumArtist')
- function format_artist()
- for word in string.gmatch(raw_albumArtist, '([^,;]+)') do
- return word
- end
- end
- -- fetch metadata via playerctl lib
- local title = player:get_title()
- local albumartist = format_artist()
- local album = player:get_album()
- -- get bitrate from mediainfo
- local bitrate = string_to_decode(str)
- -- concat metadata values into a single string
- function listvalues(s)
- local t = { }
- for k,v in ipairs(s) do
- t[#t+1] = tostring(v)
- end
- return table.concat(t," / ")
- end
- -- calculate at what length to truncate
- local total_length = utf8.len(title) +utf8.len(albumartist) +utf8.len(album) +utf8.len(bitrate)
- local chars = total_length - 75
- local max_title = math.floor( utf8.len(title) - chars )
- local max_albumartist = math.floor( utf8.len(albumartist) - chars )
- local max_album = math.floor( utf8.len(album) - chars )
- function shorten(s,w)
- local ellipsis = "…"
- local n_ellipsis = utf8.len(ellipsis)
- assert_string(1,s)
- if utf8.len(s) > w then
- return s:sub(1,w-n_ellipsis) .. ellipsis
- end
- return s
- end
- -- only truncate the longest string...
- -- figure out a better way to determine the longset variable.
- if utf8.len(title) >= utf8.len(albumartist) and utf8.len(title) >= utf8.len(album) then
- metadata = { shorten(title, max_title), albumartist, album, bitrate }
- elseif utf8.len(albumartist) >= utf8.len(title) and utf8.len(albumartist) >= utf8.len(album) then
- metadata = { title, shorten(albumartist, max_albumartist), album, bitrate }
- elseif utf8.len(album) >= utf8.len(title) and utf8.len(album) >= utf8.len(albumartist) then
- metadata = { title, albumartist, shorten(album, max_album), bitrate }
- end
- print(listvalues(metadata))
Advertisement
Add Comment
Please, Sign In to add comment