Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nowplaying.lua #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions HexChat/nowplaying.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ local function format_timestamp (microsecs)
return str
end

local NUL = _VERSION == "Lua 5.1" and "%z" or "\000"

local function template_string (template, replacements)
template = template:gsub("%$%$", "\000") -- $$ -> \000
template = template:gsub("%$([%w_]+)", replacements) -- $field
Expand All @@ -144,7 +146,7 @@ local function template_string (template, replacements)
end
return repl
end)
template = template:gsub("\000", "$") -- \000 -> $
template = template:gsub(NUL, "$") -- \000 -> $
return template
end

Expand All @@ -158,38 +160,31 @@ local function print_nowplaying (player)
return
end

-- I don't have the desire to track this down atm but the template system is
-- broken in luajit so just bypass it for now...
if type(jit) == 'table' then
local command = string.format('me is now playing %s by %s', metadata['xesam:title'], metadata['xesam:artist'][1])
hexchat.command(command)
else
local replacements = metadata -- there's no built-in function to make a shallow copy

-- aggregate artist and albumArtist fields
for _, key in pairs({'artist', 'albumArtist'}) do
local source = metadata['xesam:' .. key]
if source then
replacements[key] = ""
for i, item in ipairs(source) do
if i ~= 1 then
replacements[key] = replacements[key] .. " & "
end
replacements[key] = replacements[key] .. item
local replacements = metadata -- there's no built-in function to make a shallow copy

-- aggregate artist and albumArtist fields
for _, key in pairs({'artist', 'albumArtist'}) do
local source = metadata['xesam:' .. key]
if source then
replacements[key] = ""
for i, item in ipairs(source) do
if i ~= 1 then
replacements[key] = replacements[key] .. " & "
end
replacements[key] = replacements[key] .. item
end
end
-- formatted timestamps
replacements['position'] = format_timestamp(position)
replacements['length'] = format_timestamp(metadata['mpris:length'])
-- shorthands
replacements['title'] = metadata['xesam:title']
replacements['album'] = metadata['xesam:album']
-- other
replacements['player'] = player

hexchat.command(template_string(COMMAND_TEMPLATE, replacements))
end
-- formatted timestamps
replacements['position'] = format_timestamp(position)
replacements['length'] = format_timestamp(metadata['mpris:length'])
-- shorthands
replacements['title'] = metadata['xesam:title']
replacements['album'] = metadata['xesam:album']
-- other
replacements['player'] = player

hexchat.command(template_string(COMMAND_TEMPLATE, replacements))
end)
end)
end
Expand Down Expand Up @@ -234,4 +229,4 @@ hexchat.hook_unload (function ()
-- /np
-- /lua unload ~/.config/hexchat/addons/nowplaying.lua
cancellable:cancel()
end)
end)