More async

This commit is contained in:
Thomas Forgione 2017-10-20 11:05:08 +02:00
parent 4a18b73bb0
commit 052e945250
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
3 changed files with 85 additions and 73 deletions

View File

@ -5,57 +5,65 @@
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()
local color
local symbol
batteryInfo(function(percent, isCharging)
if percent < 15 then
color="red"
elseif percent < 30 then
color="orange"
elseif percent > 90 then
color="green"
else
color="white"
end
local color
local symbol
if isCharging() then
color = 'green'
symbol = ''
else
symbol = '%'
end
if percent < 15 then
color="red"
elseif percent < 30 then
color="orange"
elseif percent > 90 then
color="green"
else
color="white"
end
if previous_percent >= 15 and percent < 15 then
naughty.notify({
title = "Low battery...",
text = "Battery level is lower than 15% !",
fg="#000000",
bg="#ff0000",
timeout=5
})
end
if isCharging then
color = 'green'
symbol = ''
else
symbol = '%'
end
battery_widget:set_markup('<span color="' .. color .. '">' .. percent .. ' ' .. symbol .. '</span>')
if previous_percent >= 15 and percent < 15 then
naughty.notify({
title = "Low battery...",
text = "Battery level is lower than 15% !",
fg="#000000",
bg="#ff0000",
timeout=5
})
end
previous_percent = percent
battery_widget:set_markup('<span color="' .. color .. '">' .. percent .. ' ' .. symbol .. '</span>')
previous_percent = percent
end)
end

View File

@ -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)

30
awesome/su.lua Normal file
View File

@ -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