From 5ea554c068a3bec089f152fb737556e0c1989812 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Thu, 19 Oct 2017 18:04:37 +0200 Subject: [PATCH] awful.spawn.easy_async --- awesome/calendar.lua | 17 +++----------- awesome/music.lua | 56 ++++++++++++++++++++++++++------------------ awesome/volume.lua | 7 +++--- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/awesome/calendar.lua b/awesome/calendar.lua index 0279171..0533bdf 100644 --- a/awesome/calendar.lua +++ b/awesome/calendar.lua @@ -1,19 +1,8 @@ local awful = require("awful") -function get_calendar() - local f = io.popen('gcal -i- -s1 .+') - local l = nil - if f ~= nil then - f:read() - l = f:read("*all") - else - l = '?' - end - f:close() - return l -end - textclock_widget = awful.widget.textclock(" %a %d %b %H:%M ") calendar_widget = awful.tooltip({objects={textclock_widget}}) -calendar_widget:set_text(get_calendar()) +awful.spawn.easy_async('gcal -i- -s1 .+', function(stdout) + calendar_widget:set_text(stdout) +end) diff --git a/awesome/music.lua b/awesome/music.lua index b9dbb1b..f5f5388 100644 --- a/awesome/music.lua +++ b/awesome/music.lua @@ -54,33 +54,43 @@ local function set_text(text) 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()} - 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 active_command then - set_text(text[1]) - elseif string_starts_with(command, 'noop') then - total_text = text[1] - position = math.floor((width - string.len(total_text)) / 2) + awful.spawn.easy_async('music-client ' .. command, function(stdout, stderr, reason, code) + + local text = string_split(stdout, '\n') + 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 code == 0 then + + if active_command then + set_text(text[1]) + elseif string_starts_with(command, 'noop') then + 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 + os.execute('music-server&') + os.execute('sleep 0.2') + ret.execute_command('noop') + else + set_text('music-server not running') 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 - set_text('music-server not running') - end - if text[2] ~= nil then - icon = text[2] - end + if text[2] ~= nil then + icon = text[2] + end + + end) - update_widget() end function update_widget() diff --git a/awesome/volume.lua b/awesome/volume.lua index 09f969e..5cabf58 100644 --- a/awesome/volume.lua +++ b/awesome/volume.lua @@ -5,10 +5,10 @@ volume_widget = wibox.widget.textbox() volume_widget:set_align("right") function update_volume() - fd = io.popen("amixer sget Master") - local status = fd:read("*all") + + awful.spawn.easy_async('amixer sget Master', function(stdout, stderr) + local status = stdout local widget = volume_widget - fd:close() -- local volume = tonumber(string.match(status, "(%d?%d?%d)%%")) / 100 local volume = string.match(status, "(%d?%d?%d)%%") @@ -23,6 +23,7 @@ function update_volume() volume = volume .. "M" end widget:set_markup("V :" .. volume) + end) end update_volume()