More async
This commit is contained in:
parent
4a18b73bb0
commit
052e945250
|
@ -5,24 +5,30 @@
|
|||
local wibox = require("wibox")
|
||||
local naughty = require("naughty")
|
||||
local beautiful = require("beautiful")
|
||||
local awful = require('awful')
|
||||
local su = require('su')
|
||||
|
||||
local previous_percent = 0
|
||||
|
||||
function batteryInfo()
|
||||
local fd = io.popen('acpi | head -n 1 | cut -d ":" -f 2 | cut -d "," -f 2 | tr -d "%"')
|
||||
local percentage = fd:read('*all')
|
||||
fd:close()
|
||||
return tonumber(percentage)
|
||||
end
|
||||
function batteryInfo(callback)
|
||||
awful.spawn.easy_async('acpi', function(stdout)
|
||||
-- Consider only the first line
|
||||
local line = su.split(stdout, '\n')[1]
|
||||
|
||||
function isCharging()
|
||||
local _,_,error = os.execute('acpi | head -n 1 | grep Discharging')
|
||||
return error ~= 0
|
||||
local split = su.split(line, ':')
|
||||
|
||||
-- Extract the percentage from the string
|
||||
local percent = tonumber(su.split(split[2],',')[2]:sub(1, -2))
|
||||
|
||||
-- callback(percent, isCharging)
|
||||
callback(tonumber(percent), not string.find(line, 'Discharging'))
|
||||
end)
|
||||
end
|
||||
|
||||
function update_battery()
|
||||
|
||||
local percent = batteryInfo()
|
||||
batteryInfo(function(percent, isCharging)
|
||||
|
||||
local color
|
||||
local symbol
|
||||
|
||||
|
@ -36,7 +42,7 @@ function update_battery()
|
|||
color="white"
|
||||
end
|
||||
|
||||
if isCharging() then
|
||||
if isCharging then
|
||||
color = 'green'
|
||||
symbol = '⚡'
|
||||
else
|
||||
|
@ -57,6 +63,8 @@ function update_battery()
|
|||
|
||||
previous_percent = percent
|
||||
|
||||
end)
|
||||
|
||||
end
|
||||
|
||||
battery_widget = wibox.widget.textbox()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
local naughty = require("naughty")
|
||||
local wibox = require("wibox")
|
||||
local awful = require("awful")
|
||||
local su = require('su')
|
||||
|
||||
local ret = {}
|
||||
ret.text_widget = wibox.widget.textbox()
|
||||
|
@ -18,33 +19,6 @@ 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)
|
||||
total_text = text
|
||||
|
||||
|
@ -57,21 +31,21 @@ ret.execute_command = function(command)
|
|||
|
||||
awful.spawn.easy_async('music-client ' .. command, function(stdout, stderr, reason, code)
|
||||
|
||||
local text = string_split(stdout, '\n')
|
||||
local text = su.split(stdout, '\n')
|
||||
local active_command =
|
||||
string_starts_with(command, 'file') or
|
||||
string_starts_with(command, 'next') or
|
||||
string_starts_with(command, 'previous')
|
||||
su.starts_with(command, 'file') or
|
||||
su.starts_with(command, 'next') or
|
||||
su.starts_with(command, 'previous')
|
||||
|
||||
local start_server_command = active_command or
|
||||
string_starts_with(command, 'play') or
|
||||
string_starts_with(command, 'pause')
|
||||
su.starts_with(command, 'play') or
|
||||
su.starts_with(command, 'pause')
|
||||
|
||||
if code == 0 then
|
||||
|
||||
if active_command then
|
||||
set_text(text[1])
|
||||
elseif string_starts_with(command, 'noop') then
|
||||
elseif su.starts_with(command, 'noop') then
|
||||
total_text = text[1]
|
||||
position = math.floor((width - string.len(total_text)) / 2)
|
||||
end
|
||||
|
@ -96,7 +70,7 @@ end
|
|||
function update_widget()
|
||||
ret.icon_widget:set_text(' ' .. icon .. ' ')
|
||||
if string.len(total_text) < width then
|
||||
ret.text_widget:set_text(' ' .. pad(total_text, width + 1, ' '))
|
||||
ret.text_widget:set_text(' ' .. su.pad(total_text, width + 1, ' '))
|
||||
else
|
||||
local pos = math.min(position, string.len(total_text) - width)
|
||||
pos = math.max(1, pos)
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
local module = {}
|
||||
|
||||
module.starts_with = function(String,Start)
|
||||
return string.sub(String,1,string.len(Start))==Start
|
||||
end
|
||||
|
||||
module.split = function(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
|
||||
|
||||
module.pad = function(str, len, char)
|
||||
if char == nil then char = ' ' end
|
||||
return str .. string.rep(char, len - #str)
|
||||
end
|
||||
|
||||
return module
|
Loading…
Reference in New Issue