Rust-music-server is now pretty cool...
This commit is contained in:
parent
f788defa4a
commit
707aa98908
|
@ -3,17 +3,21 @@ local wibox = require("wibox")
|
||||||
local awful = require("awful")
|
local awful = require("awful")
|
||||||
|
|
||||||
local ret = {}
|
local ret = {}
|
||||||
ret.widget = wibox.widget.textbox()
|
ret.text_widget = wibox.widget.textbox()
|
||||||
ret.widget:set_align("right")
|
ret.text_widget:set_align("right")
|
||||||
ret.widget.font = "Ubuntu Mono"
|
ret.text_widget.font = "Ubuntu Mono"
|
||||||
|
|
||||||
|
ret.icon_widget = wibox.widget.textbox()
|
||||||
|
ret.icon_widget:set_align("right")
|
||||||
|
ret.icon_widget.font = "Ubuntu Mono"
|
||||||
|
|
||||||
|
|
||||||
|
local icon = ""
|
||||||
local total_text = ""
|
local total_text = ""
|
||||||
local position = 1
|
local position = 1
|
||||||
local width = 30
|
local width = 30
|
||||||
local dots = 3
|
local dots = 3
|
||||||
|
|
||||||
local last_notification = nil
|
|
||||||
|
|
||||||
local function string_starts_with(String,Start)
|
local function string_starts_with(String,Start)
|
||||||
return string.sub(String,1,string.len(Start))==Start
|
return string.sub(String,1,string.len(Start))==Start
|
||||||
end
|
end
|
||||||
|
@ -42,53 +46,51 @@ local function pad(str, len, char)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function set_text(text)
|
local function set_text(text)
|
||||||
|
total_text = text
|
||||||
total_text = text[1]
|
|
||||||
|
|
||||||
if last_notification ~= nil then
|
|
||||||
naughty.destroy(last_notification)
|
|
||||||
end
|
|
||||||
|
|
||||||
if text[2] == nil then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
elts = string_split(text[1], '-')
|
|
||||||
artist = string.sub(elts[1], 1, -1)
|
|
||||||
album = string.sub(elts[2], 2, -1)
|
|
||||||
song = string.sub(elts[3], 2)
|
|
||||||
notification_text = '\n' .. album .. '\n' .. song .. '\n'
|
|
||||||
|
|
||||||
if string.len(total_text) >= width then
|
if string.len(total_text) >= width then
|
||||||
position = math.floor((width - string.len(total_text)) / 2)
|
position = math.floor((width - string.len(total_text)) / 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
update_widget()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ret.execute_command = function(command)
|
ret.execute_command = function(command)
|
||||||
local fd = io.popen('music-client ' .. command)
|
local fd = io.popen('music-client ' .. command)
|
||||||
local text = string_split(fd:read('*all'), '\n')
|
local text = string_split(fd:read('*all'), '\n')
|
||||||
local rc = {fd:close()}
|
local rc = {fd:close()}
|
||||||
|
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')
|
||||||
|
|
||||||
if rc[3] == 0 then
|
if rc[3] == 0 then
|
||||||
if string_starts_with(command, 'file') or string_starts_with(command, 'next') or string_starts_with(command, 'previous') then
|
if active_command then
|
||||||
set_text(text)
|
set_text(text[1])
|
||||||
elseif string_starts_with(command, 'noop') then
|
elseif string_starts_with(command, 'noop') then
|
||||||
total_text = text[1]
|
total_text = text[1]
|
||||||
position = math.floor((width - string.len(total_text)) / 2)
|
position = math.floor((width - string.len(total_text)) / 2)
|
||||||
end
|
end
|
||||||
|
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')
|
||||||
else
|
else
|
||||||
set_text({'music-server not running'})
|
set_text('music-server not running')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if text[2] ~= nil then
|
||||||
|
icon = text[2]
|
||||||
|
end
|
||||||
|
|
||||||
|
update_widget()
|
||||||
end
|
end
|
||||||
|
|
||||||
function update_widget()
|
function update_widget()
|
||||||
|
ret.icon_widget:set_text(' ' .. icon .. ' ')
|
||||||
if string.len(total_text) < width then
|
if string.len(total_text) < width then
|
||||||
ret.widget:set_text(pad(total_text, width, ' '))
|
ret.text_widget:set_text(' ' .. pad(total_text, width + 1, ' '))
|
||||||
else
|
else
|
||||||
local pos = math.min(position, string.len(total_text) - width)
|
local pos = math.min(position, string.len(total_text) - width)
|
||||||
pos = math.max(1, pos)
|
pos = math.max(1, pos)
|
||||||
ret.widget:set_text(string.sub(total_text, pos, pos + width))
|
ret.text_widget:set_text(' ' .. string.sub(total_text, pos, pos + width))
|
||||||
position = position + 1
|
position = position + 1
|
||||||
if position > string.len(total_text) then
|
if position > string.len(total_text) then
|
||||||
position = math.floor((width - string.len(total_text)) / 2)
|
position = math.floor((width - string.len(total_text)) / 2)
|
||||||
|
@ -98,10 +100,8 @@ end
|
||||||
|
|
||||||
ret.execute_command('noop')
|
ret.execute_command('noop')
|
||||||
|
|
||||||
ret.widget.timer = timer({timeout=0.2})
|
ret.timer = timer({timeout=0.2})
|
||||||
ret.widget.timer:connect_signal("timeout", update_widget)
|
ret.timer:connect_signal("timeout", update_widget)
|
||||||
ret.widget.timer:start()
|
ret.timer:start()
|
||||||
|
|
||||||
ret.widget = ret.widget
|
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -261,8 +261,9 @@ awful.screen.connect_for_each_screen(function(s)
|
||||||
{ -- Right widgets
|
{ -- Right widgets
|
||||||
layout = wibox.layout.fixed.horizontal,
|
layout = wibox.layout.fixed.horizontal,
|
||||||
delimiter,
|
delimiter,
|
||||||
-- music.widget,
|
music.icon_widget,
|
||||||
-- delimiter,
|
music.text_widget,
|
||||||
|
delimiter,
|
||||||
volume_widget,
|
volume_widget,
|
||||||
delimiter,
|
delimiter,
|
||||||
battery_widget,
|
battery_widget,
|
||||||
|
|
Loading…
Reference in New Issue