dotfiles/awesome/music.lua

106 lines
2.8 KiB
Lua
Raw Normal View History

2017-09-12 13:19:05 +01:00
local naughty = require("naughty")
local wibox = require("wibox")
local awful = require("awful")
2017-10-20 10:05:08 +01:00
local su = require('su')
2017-09-12 13:19:05 +01:00
local ret = {}
ret.text_widget = wibox.widget.textbox()
ret.text_widget:set_align("right")
ret.text_widget.font = "Ubuntu Mono"
2017-09-12 13:19:05 +01:00
ret.icon_widget = wibox.widget.textbox()
ret.icon_widget:set_align("right")
ret.icon_widget.font = "Ubuntu Mono"
local icon = ""
2017-09-12 13:19:05 +01:00
local total_text = ""
local position = 1
local width = 30
local dots = 3
local function set_text(text)
total_text = text
2017-09-12 13:19:05 +01:00
if string.len(total_text) >= width then
position = math.floor((width - string.len(total_text)) / 2)
end
end
local function try_noop()
awful.spawn.easy_async_with_shell('sleep 2s && music-client noop', function(stdout, stderr, reason, code)
if code == 1 then
try_noop()
elseif code == 0 then
ret.execute_command('noop')
end
end);
end
ret.execute_command = function(command, arg)
2017-10-19 17:04:37 +01:00
awful.spawn.easy_async({'music-client', command, arg}, function(stdout, stderr, reason, code)
2017-10-19 17:04:37 +01:00
2017-10-20 10:05:08 +01:00
local text = su.split(stdout, '\n')
2017-10-19 17:04:37 +01:00
local active_command =
2017-10-20 10:05:08 +01:00
su.starts_with(command, 'file') or
su.starts_with(command, 'next') or
su.starts_with(command, 'previous')
2017-10-19 17:04:37 +01:00
local start_server_command = active_command or
2017-10-20 10:05:08 +01:00
su.starts_with(command, 'play') or
su.starts_with(command, 'pause')
2017-10-19 17:04:37 +01:00
if code == 0 then
if active_command then
set_text(text[1])
2017-10-20 10:05:08 +01:00
elseif su.starts_with(command, 'noop') then
2017-10-19 17:04:37 +01:00
total_text = text[1]
position = math.floor((width - string.len(total_text)) / 2)
end
elseif start_server_command then
-- Start the server and re-exec the command
awful.spawn.easy_async({'music-server', command, arg}, function(stdout, stderr, reason, code)
end)
2017-10-19 17:04:37 +01:00
os.execute('music-server&')
else
set_text('music-server not running')
2017-09-12 13:19:05 +01:00
end
2017-10-19 17:04:37 +01:00
if text[2] ~= nil then
icon = text[2]
end
end)
2017-09-12 13:19:05 +01:00
end
function update_widget()
ret.icon_widget:set_text(' ' .. icon .. ' ')
2017-09-12 13:19:05 +01:00
if string.len(total_text) < width then
2017-10-20 10:05:08 +01:00
ret.text_widget:set_text(' ' .. su.pad(total_text, width + 1, ' '))
2017-09-12 13:19:05 +01:00
else
local pos = math.min(position, string.len(total_text) - width)
pos = math.max(1, pos)
ret.text_widget:set_text(' ' .. string.sub(total_text, pos, pos + width))
2017-09-12 13:19:05 +01:00
position = position + 1
if position > string.len(total_text) then
position = math.floor((width - string.len(total_text)) / 2)
end
end
end
ret.execute_command('noop')
ret.timer = timer({timeout=0.2})
ret.timer:connect_signal("timeout", update_widget)
ret.timer:start()
2017-09-12 13:19:05 +01:00
return ret