Skip to content

Instantly share code, notes, and snippets.

@ShadOoW
Last active December 19, 2015 18:16
Show Gist options
  • Save ShadOoW/dfee084ed34348c5f885 to your computer and use it in GitHub Desktop.
Save ShadOoW/dfee084ed34348c5f885 to your computer and use it in GitHub Desktop.
ihasit
--For debugging
local naughty = require("naughty")
helper = {
inTable = function(tbl, item)
for key, value in pairs(tbl) do
if value == item then return key end
end
return false
end,
nextIndex = function(target, index)
local next_index = 1
if index < #target then
next_index = index + 1
end
return next_index
end,
timeout = function(timer)
timer.emit_signal("timeout")
end,
bindKey = function (boundKey, boundPressAction, boundReleaseAction, boundTick, boundDelay)
--default values
boundPressAction = boundPressAction or function() end
boundReleaseAction = boundReleaseAction or function() end
boundTick = boundTick or 100
boundDelay = boundDelay or 300
--helper functions
function getSystemTime()
fh = assert(io.popen("date +%s%N | cut -b1-13"))
time = fh:read("*l")
fh:close()
return time
end
--time calculation vars
startMark = getSystemTime()
elapsedMark = 0
--action state bools
isDelayElapsed = false
isTicked = false
--stop existing keygrabber
keygrabber.stop()
keygrabber.run(function(mod, key, event)
if boundKey == key then
if event == "press" then
elapsedMark = getSystemTime() - startMark
if isDelayElapsed then
if elapsedMark > boundTick then
isTicked = true;
startMark = getSystemTime()
boundPressAction()
end
else
if elapsedMark > boundDelay then
isDelayElapsed = true;
startMark = getSystemTime()
end
end
else
if not isTicked then
boundReleaseAction()
end
--stop keygrabber on release
keygrabber.stop()
end
end
end)
end
}
return helper
local module_path = (...):match ("(.+/)[^/]+$") or ""
package.loaded.net_widgets = nil
local ihasit = {
helper = require(module_path .. "ihasit.helper"),
widget = require(module_path .. "ihasit.widget")
}
return ihasit
local ihasit = require("ihasit")
-- ..
local volumewidget = ihasit.widget.volume()
local wifiwidget = ihasit.widget.wifi()
local batterywidget = ihasit.widget.battery()
local clockwidget = ihasit.widget.clock()
local memorywidget = ihasit.widget.memory()
local hddswidget = ihasit.widget.hdds()
local poweroffwidget = ihasit.widget.poweroff()
local updownwidget = ihasit.widget.updown()
-- ..
-- Widgets that are aligned to the right
local right_layout = wibox.layout.fixed.horizontal()
if s == 1 then right_layout:add(wibox.widget.systray()) end
if s == 1 then right_layout:add(hddswidget) end
if s == 1 then right_layout:add(memorywidget) end
if s == 1 then right_layout:add(updownwidget) end
if s == 1 then right_layout:add(batterywidget) end
if s == 1 then right_layout:add(volumewidget) end
if s == 1 then right_layout:add(wifiwidget) end
right_layout:add(clockwidget)
right_layout:add(poweroffwidget)
--..
-- ..
local widget_view_mode = true
-- {{{ Key bindings
globalkeys = awful.util.table.join(
-- ..
awful.key({ }, "XF86MonBrightnessUp", function ()
widget_view_mode = not widget_view_mode
volumewidget.toggle(widget_view_mode)
batterywidget.toggle(widget_view_mode)
wifiwidget.toggle(widget_view_mode)
hddswidget.toggle(widget_view_mode)
end),
-- ..
-- ..
-- {{{ Rules
-- Rules to apply to new clients (through the "manage" signal).
awful.rules.rules = {
-- ..
{ rule = { name = "echo ~~~monitoring network;vnstat --live" },
properties = {floating = true, ontop = true},
callback = function(c) c:tags({tags[1][1], tags[1][2], tags[1][3], tags[1][4], tags[1][5]}) end },
{ rule = { name = "Tasks" },
properties = {floating = true, ontop = true},
callback = function(c) c:tags({tags[1][1], tags[1][2], tags[1][3], tags[1][4], tags[1][5]}) end },
--..
-..
local awful = require("awful")
-- Widget and layout library
local wibox = require("wibox")
local naughty = require("naughty")
widget = {
wifi = function()
local manager = "xterm -fa 'Monospace' -fs 11 -geometry 49x24-0+727 -e 'echo ~~~Wifi Menu requested;sudo wifi-menu'"
local icons = { connected = "&#xf1eb;", disconnected = "&#xf072;"}
local simple = true
-- Create a wifi widgets
local widget = wibox.widget.textbox()
widget:set_text(" Wifi | ")
local timer = timer({ timeout = 5 })
timer:connect_signal("timeout",
function()
fh = assert(io.popen("iwgetid -r", "r"))
local wifi = fh:read("*l") or false
if wifi then
if simple then
widget:set_markup(" " .. icons.connected .. " | ")
else
widget:set_markup(" " .. icons.connected .. " " .. wifi .. " | ")
end
else
widget:set_markup(" " .. icons.disconnected .. " | ")
end
fh:close()
end
)
timer:start()
timer:emit_signal("timeout")
widget:buttons(
awful.util.table.join(
awful.button({ }, 3, function ()
simple = true
awful.util.spawn(manager)
end),
awful.button({ }, 1, function ()
simple = not simple
timer:emit_signal("timeout")
end)
)
)
widget.simple = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
widget.toggle = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
return widget
end,
volume = function()
local icons = { muted = '&#xf026;', unmuted = '&#xf028;'}
local simple = true
-- Create a volume widgets
local widget = wibox.widget.textbox()
widget:set_markup(" Volume | ")
local timer = timer({ timeout = 5 })
timer:connect_signal("timeout",
function()
local volume = 0
local isMuted = 'no'
local icon = icons.unmuted
fh = assert(io.popen("amixer get Master | awk '$0~/%/{print $5; exit}' | tr -d '[]%'", "r"))
volume = fh:read("*l")
fh:close()
fh = assert(io.popen("pacmd dump | awk '$1 == \"set-sink-mute\" {m[$2] = $3} $1 == \"set-default-sink\" {s = $2} END {print m[s]}'", "r"))
isMuted = fh:read("*l")
fh:close()
if isMuted == 'yes' then
icon = icons.muted
widget:set_markup(icon .. ' <span color="#ff6666">' .. volume .. '%</span>' .. " | ")
else
widget:set_markup(icon .. ' ' .. volume .. '%' .. " | ")
end
if simple then
widget:set_markup(icon .. " | ")
end
end
)
timer:start()
timer:emit_signal("timeout")
widget:buttons(awful.util.table.join(
awful.button({ }, 1, function ()
simple = not simple
timer:emit_signal("timeout")
end)
))
widget.simple = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
widget.toggle = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
widget.timeout = function() timer:emit_signal("timeout") end
return widget
end,
battery = function()
local simple = true
local icons = { lower = '&#xf244;', low = '&#xf243;', medium = '&#xf242;', high = '&#xf241;', higher = '&#xf240;'}
-- Create a battery widgets
local widget = wibox.widget.textbox()
widget:set_text(" Battery | ")
local timer = timer({ timeout = 5 })
timer:connect_signal("timeout",
function()
fh = assert(io.popen("acpi | cut -d, -f 2 -", "r"))
local num = tonumber(string.sub(fh:read("*l"),1,-2))
fh:close()
fh = assert(io.popen("acpi | cut -d ' ' -f 3 -", "r"))
local status = fh:read("*l")
fh:close()
if status == "Charging," then
status = "+"
else
status = "-"
end
local icon = ""
if num < 15 then
icon = icons.lower
elseif num < 40 then
icon = icons.low
elseif num < 70 then
icon = icons.medium
elseif num < 85 then
icon = icons.high
else
icon = icons.higher
end
if num < 15 and status == '-' then
naughty.notify({text="Battery is Low"})
if simple then
widget:set_markup(status .. ' <span color="#ff6666">' .. icon .. '</span> ' .. " | ")
else
widget:set_markup('<span color="#ff6666">' .. status .. '</span> ' .. icon .. ' <span color="#ff6666">' .. num .. '%</span> ' .. " | ")
end
else
if simple then
widget:set_markup(status .. ' <span color="#34bb99">' .. icon .. '</span> ' .. " | ")
else
widget:set_markup('<span color="#34bb99">' .. status .. '</span> ' .. icon .. ' <span color="#34bb99">' .. num .. '%</span> ' .. " | ")
end
end
end
)
timer:start()
timer:emit_signal("timeout")
widget:buttons(awful.util.table.join(
awful.button({ }, 1, function ()
simple = not simple
timer:emit_signal("timeout")
end)
))
widget.simple = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
widget.toggle = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
return widget
end,
updown = function()
local vnstat = "xterm -fa 'Monospace' -fs 11 -geometry 39x2-0+1124 -e 'echo ~~~monitoring network;vnstat --live'"
local speedtest = "xterm -fa 'Monospace' -fs 11 -geometry 39x11-0+960 -e 'echo ~~~monitoring network;wget -O - https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py | python;read'"
-- Create a updown widgets
local widget = wibox.widget.textbox()
widget:set_markup(" &#xf0ac; | ")
widget:buttons(awful.util.table.join(
awful.button({ }, 1, function ()
awful.util.spawn(vnstat)
end),
awful.button({ }, 3, function ()
awful.util.spawn(speedtest)
end)
))
return widget
end,
clock = function()
local manager = "gtg"
-- Create a textclock widget
local widget = awful.widget.textclock()
widget:buttons(awful.util.table.join(
awful.button({ }, 1, function ()
awful.util.spawn(manager)
end)
))
return widget
end,
poweroff = function()
local action = "xterm -fa 'Monospace' -fs 11 -geometry 38x11-0+960 -e 'echo ~~~Shutdown requested;sudo shutdown now'"
local widget = wibox.widget.textbox()
widget:set_markup(" | &#xf011; ")
widget:buttons(awful.util.table.join(
awful.button({ }, 1, function ()
awful.util.spawn(action)
end)
))
return widget
end,
memory = function()
local widget = wibox.widget.textbox()
widget:set_text(" Memory | ")
local timer = timer({ timeout = 5 })
timer:connect_signal("timeout",
function()
local index = 0
local used = 0
local total = 0
local humanused = 0
local humantotal = 0
index = 0
fh = assert(io.popen("free -m | awk '/Mem:/ { print $2 } /Mem:/ { print $3 }'", "r"))
for line in fh:lines() do
if index == 0 then
total = line
elseif index == 1 then
used = line
end
index = index + 1
end
fh:close()
index = 0
fh = assert(io.popen("free -mh | awk '/Mem:/ { print $2 } /Mem:/ { print $3 }'", "r"))
for line in fh:lines() do
if index == 0 then
humantotal = line
elseif index == 1 then
humanused = line
end
index = index + 1
--naughty.notify({text= line })
end
fh:close()
local numUsed = tonumber(used)
if numUsed < (1024 * 3) then
widget:set_markup("&#xf013; <span color='#34bb99'>" .. humanused .. "</span>" .. " | ")
elseif numUsed < (1024 * 5) then
widget:set_markup("&#xf013; <span color='#ffff66'>" .. humanused .. "</span>" .. " | ")
else
widget:set_markup("&#xf013; <span color='#ff6666'>" .. humanused .. "</span>" .. " | ")
end
end
)
timer:start()
timer:emit_signal("timeout")
return widget
end,
hdds = function()
local widget = wibox.widget.textbox()
local allowed_hdds = {'/dev/sda5', '/dev/sdc1'}
local allowed_hdds_name = {'root', 'ibuki'}
local allowed_hdds_icons = {'&#xf17c;', '&#xf0a0;' }
local selected_hdd = 1
local found_hdds = {}
local simple = true
function display_hdds (hdd_index, widget)
selected_hdd = hdd_index
local fh = io.popen("df -h")
local found = false;
local hdds = {}
local found_hdds = {}
for l in fh:lines() do
local hdd = {}
for word in l:gmatch("%S+") do
hdd[#hdd+1] = word
end
hdds[#hdds+1] = hdd
end
fh:close()
for i=1, #hdds, 1 do
if helper.inTable(allowed_hdds, hdds[i][1]) then
found_hdds[#found_hdds+1] = hdds[i]
end
end
for i=1, #found_hdds, 1 do
if i == selected_hdd then
if simple then
widget:set_markup(" | " .. allowed_hdds_icons[selected_hdd] .. " " .. allowed_hdds_name[selected_hdd] .. " <span color='#34bb99'>" .. found_hdds[i][4] .. "</span>" .. " | ")
else
widget:set_markup(
" | " .. allowed_hdds_icons[selected_hdd] .. " " .. allowed_hdds_name[selected_hdd] .. " <span color='#34bb99'>" .. found_hdds[i][4] .. "</span>" .. " <span color='#ff6666'>(" .. found_hdds[i][5] .. ")</span>" .. " | "
)
end
end
end
return found_hdds
end
simple = true
widget:set_text(" HDD Avail. | ")
local timer = timer({ timeout = 5 })
timer:connect_signal("timeout",
function()
found_hdds = display_hdds (selected_hdd, widget)
end
)
timer:start()
timer:emit_signal("timeout")
widget:buttons(awful.util.table.join(
awful.button({ }, 3, function ()
found_hdds = display_hdds(helper.nextIndex(found_hdds, selected_hdd), widget)
end),
awful.button({ }, 1, function ()
simple = not simple
timer:emit_signal("timeout")
end)
))
widget.simple = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
widget.toggle = function(new_simple) simple = new_simple timer:emit_signal("timeout") end
return widget
end
}
return widget
@ShadOoW
Copy link
Author

ShadOoW commented Dec 19, 2015

create folder "ihasit" in .config/awesome and put widget.lua - helper.lua and init.lua in it

put rest of code in appropriate place in rc.lua

@ShadOoW
Copy link
Author

ShadOoW commented Dec 19, 2015

vnstat iwgetid pacmd amixer acpi free df wifi-menu are needed
gtg is the task manager i'm using you should modify to use your own

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment