2017-09-12 14:19:05 +02:00
|
|
|
local naughty = require("naughty")
|
|
|
|
local wibox = require("wibox")
|
|
|
|
local awful = require("awful")
|
|
|
|
|
|
|
|
local ret = {}
|
2017-10-19 15:02:13 +02:00
|
|
|
ret.text_widget = wibox.widget.textbox()
|
|
|
|
ret.text_widget:set_align("right")
|
|
|
|
ret.text_widget.font = "Ubuntu Mono"
|
2017-09-12 14:19:05 +02:00
|
|
|
|
2017-10-19 15:02:13 +02:00
|
|
|
ret.icon_widget = wibox.widget.textbox()
|
|
|
|
ret.icon_widget:set_align("right")
|
|
|
|
ret.icon_widget.font = "Ubuntu Mono"
|
|
|
|
|
|
|
|
|
|
|
|
local icon = ""
|
2017-09-12 14:19:05 +02:00
|
|
|
local total_text = ""
|
|
|
|
local position = 1
|
|
|
|
local width = 30
|
|
|
|
local dots = 3
|
|
|
|
|
|
|
|
local function string_starts_with(String,Start)
|
|
|
|
return string.sub(String,1,string.len(Start))==Start
|
|
|
|
end
|
|
|
|
|
|
|
|
local function string_split(str, delimiter)
|
|
|
|
if str == nil then
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
local ret = {''}
|
|
|
|
count = 1
|
|
|
|
for i = 1, string.len(str) do
|
|
|
|
local c = str:sub(i,i)
|
|
|
|
if c == delimiter then
|
|
|
|
count = count + 1
|
|
|
|
ret[count] = ''
|
|
|
|
else
|
|
|
|
ret[count] = ret[count] .. c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function pad(str, len, char)
|
|
|
|
if char == nil then char = ' ' end
|
|
|
|
return str .. string.rep(char, len - #str)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_text(text)
|
2017-10-19 15:02:13 +02:00
|
|
|
total_text = text
|
2017-09-12 14:19:05 +02:00
|
|
|
|
|
|
|
if string.len(total_text) >= width then
|
|
|
|
position = math.floor((width - string.len(total_text)) / 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ret.execute_command = function(command)
|
|
|
|
local fd = io.popen('music-client ' .. command)
|
|
|
|
local text = string_split(fd:read('*all'), '\n')
|
|
|
|
local rc = {fd:close()}
|
2017-10-19 15:02:13 +02:00
|
|
|
local active_command = string_starts_with(command, 'file') or string_starts_with(command, 'next') or string_starts_with(command, 'previous')
|
|
|
|
local start_server_command = active_command or string_starts_with(command, 'play') or string_starts_with(command, 'pause')
|
|
|
|
|
2017-09-12 14:19:05 +02:00
|
|
|
if rc[3] == 0 then
|
2017-10-19 15:02:13 +02:00
|
|
|
if active_command then
|
|
|
|
set_text(text[1])
|
2017-09-12 14:19:05 +02:00
|
|
|
elseif string_starts_with(command, 'noop') then
|
|
|
|
total_text = text[1]
|
|
|
|
position = math.floor((width - string.len(total_text)) / 2)
|
|
|
|
end
|
2017-10-19 15:02:13 +02:00
|
|
|
elseif start_server_command then
|
|
|
|
-- Start the server and re-exec the command
|
|
|
|
os.execute('music-server&')
|
|
|
|
os.execute('sleep 0.2')
|
|
|
|
ret.execute_command('noop')
|
2017-09-12 14:19:05 +02:00
|
|
|
else
|
2017-10-19 15:02:13 +02:00
|
|
|
set_text('music-server not running')
|
|
|
|
end
|
|
|
|
|
|
|
|
if text[2] ~= nil then
|
|
|
|
icon = text[2]
|
2017-09-12 14:19:05 +02:00
|
|
|
end
|
2017-10-19 15:02:13 +02:00
|
|
|
|
|
|
|
update_widget()
|
2017-09-12 14:19:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function update_widget()
|
2017-10-19 15:02:13 +02:00
|
|
|
ret.icon_widget:set_text(' ' .. icon .. ' ')
|
2017-09-12 14:19:05 +02:00
|
|
|
if string.len(total_text) < width then
|
2017-10-19 15:02:13 +02:00
|
|
|
ret.text_widget:set_text(' ' .. pad(total_text, width + 1, ' '))
|
2017-09-12 14:19:05 +02:00
|
|
|
else
|
|
|
|
local pos = math.min(position, string.len(total_text) - width)
|
|
|
|
pos = math.max(1, pos)
|
2017-10-19 15:02:13 +02:00
|
|
|
ret.text_widget:set_text(' ' .. string.sub(total_text, pos, pos + width))
|
2017-09-12 14:19:05 +02: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')
|
|
|
|
|
2017-10-19 15:02:13 +02:00
|
|
|
ret.timer = timer({timeout=0.2})
|
|
|
|
ret.timer:connect_signal("timeout", update_widget)
|
|
|
|
ret.timer:start()
|
2017-09-12 14:19:05 +02:00
|
|
|
|
|
|
|
return ret
|