More async
This commit is contained in:
parent
4a18b73bb0
commit
052e945250
|
@ -5,57 +5,65 @@
|
||||||
local wibox = require("wibox")
|
local wibox = require("wibox")
|
||||||
local naughty = require("naughty")
|
local naughty = require("naughty")
|
||||||
local beautiful = require("beautiful")
|
local beautiful = require("beautiful")
|
||||||
|
local awful = require('awful')
|
||||||
|
local su = require('su')
|
||||||
|
|
||||||
local previous_percent = 0
|
local previous_percent = 0
|
||||||
|
|
||||||
function batteryInfo()
|
function batteryInfo(callback)
|
||||||
local fd = io.popen('acpi | head -n 1 | cut -d ":" -f 2 | cut -d "," -f 2 | tr -d "%"')
|
awful.spawn.easy_async('acpi', function(stdout)
|
||||||
local percentage = fd:read('*all')
|
-- Consider only the first line
|
||||||
fd:close()
|
local line = su.split(stdout, '\n')[1]
|
||||||
return tonumber(percentage)
|
|
||||||
end
|
|
||||||
|
|
||||||
function isCharging()
|
local split = su.split(line, ':')
|
||||||
local _,_,error = os.execute('acpi | head -n 1 | grep Discharging')
|
|
||||||
return error ~= 0
|
-- 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
|
end
|
||||||
|
|
||||||
function update_battery()
|
function update_battery()
|
||||||
|
|
||||||
local percent = batteryInfo()
|
batteryInfo(function(percent, isCharging)
|
||||||
local color
|
|
||||||
local symbol
|
|
||||||
|
|
||||||
if percent < 15 then
|
local color
|
||||||
color="red"
|
local symbol
|
||||||
elseif percent < 30 then
|
|
||||||
color="orange"
|
|
||||||
elseif percent > 90 then
|
|
||||||
color="green"
|
|
||||||
else
|
|
||||||
color="white"
|
|
||||||
end
|
|
||||||
|
|
||||||
if isCharging() then
|
if percent < 15 then
|
||||||
color = 'green'
|
color="red"
|
||||||
symbol = '⚡'
|
elseif percent < 30 then
|
||||||
else
|
color="orange"
|
||||||
symbol = '%'
|
elseif percent > 90 then
|
||||||
end
|
color="green"
|
||||||
|
else
|
||||||
|
color="white"
|
||||||
|
end
|
||||||
|
|
||||||
if previous_percent >= 15 and percent < 15 then
|
if isCharging then
|
||||||
naughty.notify({
|
color = 'green'
|
||||||
title = "Low battery...",
|
symbol = '⚡'
|
||||||
text = "Battery level is lower than 15% !",
|
else
|
||||||
fg="#000000",
|
symbol = '%'
|
||||||
bg="#ff0000",
|
end
|
||||||
timeout=5
|
|
||||||
})
|
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
local naughty = require("naughty")
|
local naughty = require("naughty")
|
||||||
local wibox = require("wibox")
|
local wibox = require("wibox")
|
||||||
local awful = require("awful")
|
local awful = require("awful")
|
||||||
|
local su = require('su')
|
||||||
|
|
||||||
local ret = {}
|
local ret = {}
|
||||||
ret.text_widget = wibox.widget.textbox()
|
ret.text_widget = wibox.widget.textbox()
|
||||||
|
@ -18,33 +19,6 @@ local position = 1
|
||||||
local width = 30
|
local width = 30
|
||||||
local dots = 3
|
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)
|
local function set_text(text)
|
||||||
total_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)
|
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 =
|
local active_command =
|
||||||
string_starts_with(command, 'file') or
|
su.starts_with(command, 'file') or
|
||||||
string_starts_with(command, 'next') or
|
su.starts_with(command, 'next') or
|
||||||
string_starts_with(command, 'previous')
|
su.starts_with(command, 'previous')
|
||||||
|
|
||||||
local start_server_command = active_command or
|
local start_server_command = active_command or
|
||||||
string_starts_with(command, 'play') or
|
su.starts_with(command, 'play') or
|
||||||
string_starts_with(command, 'pause')
|
su.starts_with(command, 'pause')
|
||||||
|
|
||||||
if code == 0 then
|
if code == 0 then
|
||||||
|
|
||||||
if active_command then
|
if active_command then
|
||||||
set_text(text[1])
|
set_text(text[1])
|
||||||
elseif string_starts_with(command, 'noop') then
|
elseif su.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
|
||||||
|
@ -96,7 +70,7 @@ end
|
||||||
function update_widget()
|
function update_widget()
|
||||||
ret.icon_widget:set_text(' ' .. icon .. ' ')
|
ret.icon_widget:set_text(' ' .. icon .. ' ')
|
||||||
if string.len(total_text) < width then
|
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
|
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)
|
||||||
|
|
|
@ -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