2021-02-20 21:07:05 +01:00
|
|
|
local awful = require("awful")
|
|
|
|
local watch = require("awful.widget.watch")
|
|
|
|
local wibox = require("wibox")
|
|
|
|
local beautiful = require("beautiful")
|
|
|
|
local gears = require("gears")
|
|
|
|
local json = require("json")
|
2021-04-27 05:15:02 +02:00
|
|
|
local gfs = require("gears.filesystem")
|
|
|
|
local spawn = require("awful.spawn")
|
2021-04-30 19:32:11 +02:00
|
|
|
local naughty = require("naughty")
|
|
|
|
local row_builder = require("noobie.row_builder")
|
2021-04-27 05:15:02 +02:00
|
|
|
|
2021-02-20 21:07:05 +01:00
|
|
|
|
|
|
|
local HOME_DIR = os.getenv("HOME")
|
|
|
|
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/noobie'
|
|
|
|
local ICONS_DIR = WIDGET_DIR .. '/feather_icons/'
|
2021-04-27 05:15:02 +02:00
|
|
|
local CACHE_DIR = os.getenv("HOME") .. '/.cache/noobie/icons'
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-06-20 02:46:04 +02:00
|
|
|
|
2021-02-24 04:07:10 +01:00
|
|
|
local cur_stdout
|
2021-02-20 21:07:05 +01:00
|
|
|
local noobie_widget = {}
|
|
|
|
|
2021-02-24 04:07:10 +01:00
|
|
|
local function show_warning(message)
|
|
|
|
naughty.notify{
|
|
|
|
preset = naughty.config.presets.critical,
|
|
|
|
title = 'Noobie',
|
|
|
|
text = message}
|
|
|
|
end
|
|
|
|
|
2021-06-20 02:46:04 +02:00
|
|
|
|
2021-02-20 21:07:05 +01:00
|
|
|
local function worker(user_args)
|
|
|
|
local args = user_args or {}
|
2021-02-20 21:36:21 +01:00
|
|
|
local refresh_rate = args.refresh_rate or 600
|
2021-02-24 04:07:10 +01:00
|
|
|
local path = args.path
|
2021-03-13 04:43:13 +01:00
|
|
|
local background = args.background or '#00000000'
|
2021-02-24 04:07:10 +01:00
|
|
|
|
|
|
|
if path == nil then
|
|
|
|
show_warning("Cannot create a widget, required parameter 'path' is not provided")
|
|
|
|
return
|
|
|
|
end
|
2021-04-27 05:15:02 +02:00
|
|
|
|
|
|
|
if not gfs.dir_readable(CACHE_DIR) then
|
|
|
|
gfs.make_directories(CACHE_DIR)
|
|
|
|
end
|
|
|
|
|
2021-03-13 04:43:13 +01:00
|
|
|
local noobie_popup = awful.popup{
|
|
|
|
ontop = true,
|
|
|
|
visible = false,
|
|
|
|
shape = function(cr, width, height)
|
|
|
|
gears.shape.rounded_rect(cr, width, height, 4)
|
|
|
|
end,
|
|
|
|
border_width = 1,
|
|
|
|
border_color = beautiful.bg_focus,
|
2021-04-30 19:32:11 +02:00
|
|
|
width = 200,
|
|
|
|
minimum_width = 100,
|
2021-03-13 04:43:13 +01:00
|
|
|
maximum_width = 400,
|
|
|
|
offset = { y = 5 },
|
|
|
|
widget = {}
|
|
|
|
}
|
2021-02-22 03:16:09 +01:00
|
|
|
local has_menu = false
|
2021-02-24 04:07:10 +01:00
|
|
|
local has_mouse_actions = false
|
2021-03-13 04:43:13 +01:00
|
|
|
local menu_buttons = {}
|
|
|
|
local mouse_actions_buttons = {}
|
2021-02-22 03:16:09 +01:00
|
|
|
|
2021-02-20 21:07:05 +01:00
|
|
|
noobie_widget = wibox.widget {
|
|
|
|
{
|
|
|
|
{
|
2021-02-20 21:36:21 +01:00
|
|
|
{
|
|
|
|
{
|
|
|
|
id = 'icn',
|
2021-04-27 05:15:02 +02:00
|
|
|
--forced_height = 20,
|
|
|
|
--forced_width = 20,
|
2021-02-20 21:36:21 +01:00
|
|
|
resize = true,
|
|
|
|
widget = wibox.widget.imagebox
|
|
|
|
},
|
|
|
|
valign = 'center',
|
|
|
|
layout = wibox.container.place
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id = 'txt',
|
|
|
|
widget = wibox.widget.textbox
|
|
|
|
},
|
2021-02-22 03:16:09 +01:00
|
|
|
id = 'spc',
|
2021-02-20 21:36:21 +01:00
|
|
|
spacing = 4,
|
|
|
|
layout = wibox.layout.fixed.horizontal
|
2021-02-20 21:07:05 +01:00
|
|
|
},
|
2021-02-20 21:36:21 +01:00
|
|
|
margins = 4,
|
|
|
|
widget = wibox.container.margin
|
2021-02-20 21:07:05 +01:00
|
|
|
},
|
2021-02-20 21:36:21 +01:00
|
|
|
shape = function(cr, width, height)
|
|
|
|
gears.shape.rounded_rect(cr, width, height, 4)
|
|
|
|
end,
|
2021-03-13 04:43:13 +01:00
|
|
|
bg = background,
|
2021-02-20 21:36:21 +01:00
|
|
|
widget = wibox.container.background,
|
2021-02-20 21:07:05 +01:00
|
|
|
set_text = function(self, new_text)
|
2021-02-22 03:16:09 +01:00
|
|
|
if new_text == nil or new_text == '' then
|
|
|
|
self:get_children_by_id('txt')[1]:set_text('')
|
|
|
|
self:get_children_by_id('spc')[1]:set_spacing(0)
|
|
|
|
else
|
|
|
|
self:get_children_by_id('txt')[1]:set_text(new_text)
|
|
|
|
end
|
2021-02-20 21:07:05 +01:00
|
|
|
end,
|
|
|
|
set_icon = function(self, new_icon)
|
2021-04-27 05:15:02 +02:00
|
|
|
-- new_icon is a path to a file
|
|
|
|
if new_icon:sub(1, 1) == '/' then
|
|
|
|
self:get_children_by_id('icn')[1]:set_image(new_icon)
|
|
|
|
|
2021-05-12 16:22:30 +02:00
|
|
|
-- new_icon is a relative path to the file
|
|
|
|
elseif new_icon:sub(1, 1) == '~' then
|
|
|
|
self:get_children_by_id('icn')[1]:set_image(HOME_DIR .. '/' .. new_icon:sub(3))
|
|
|
|
|
2021-06-20 02:46:04 +02:00
|
|
|
-- new_icon is a url to the icon
|
2021-04-27 05:15:02 +02:00
|
|
|
elseif new_icon:sub(1, 4) == 'http' then
|
|
|
|
local icon_path = CACHE_DIR .. '/' .. new_icon:sub(-16)
|
|
|
|
if not gfs.file_readable(icon_path) then
|
|
|
|
local download_cmd = string.format([[sh -c "curl -n --create-dirs -o %s %s"]], icon_path, new_icon)
|
|
|
|
spawn.easy_async(download_cmd,
|
|
|
|
function() self:get_children_by_id('icn')[1]:set_image(icon_path) end)
|
|
|
|
else
|
|
|
|
self:get_children_by_id('icn')[1]:set_image(icon_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- new_icon is a feather icon
|
|
|
|
else
|
|
|
|
self:get_children_by_id('icn')[1]:set_image(ICONS_DIR .. new_icon .. '.svg')
|
|
|
|
end
|
|
|
|
end
|
2021-02-20 21:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
local update_widget = function(widget, stdout, stderr)
|
2021-03-13 04:43:13 +01:00
|
|
|
if stderr ~= '' then
|
|
|
|
show_warning(stderr)
|
|
|
|
return
|
|
|
|
end
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-03-13 04:43:13 +01:00
|
|
|
--- do nothing if the output hasn't changed
|
2021-02-24 04:07:10 +01:00
|
|
|
if (cur_stdout == stdout) then return
|
|
|
|
else cur_stdout = stdout
|
|
|
|
end
|
|
|
|
|
2021-02-20 21:07:05 +01:00
|
|
|
local result = json.decode(stdout)
|
|
|
|
widget:set_text(result.widget.text)
|
2021-04-27 05:15:02 +02:00
|
|
|
widget:set_icon(result.widget.icon)
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-02-22 03:16:09 +01:00
|
|
|
has_menu = result.menu ~= nil and result.menu.items ~= nil and #result.menu.items > 0
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-02-22 03:16:09 +01:00
|
|
|
if has_menu then
|
2021-03-13 04:43:13 +01:00
|
|
|
local rows = { layout = wibox.layout.fixed.vertical }
|
2021-02-20 21:36:21 +01:00
|
|
|
|
|
|
|
for i = 0, #rows do rows[i]=nil end
|
|
|
|
for _, item in ipairs(result.menu.items) do
|
2021-04-27 05:15:02 +02:00
|
|
|
|
2021-04-30 19:32:11 +02:00
|
|
|
local row = row_builder:build_row(item, widget, noobie_popup)
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-02-20 21:36:21 +01:00
|
|
|
table.insert(rows, row)
|
|
|
|
end
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-02-20 21:36:21 +01:00
|
|
|
noobie_popup:setup(rows)
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-03-13 04:43:13 +01:00
|
|
|
menu_buttons = gears.table.join(
|
2021-02-22 03:16:09 +01:00
|
|
|
awful.button({}, 1, function()
|
|
|
|
if noobie_popup.visible then
|
2021-03-13 04:43:13 +01:00
|
|
|
widget:set_bg(background)
|
2021-02-22 03:16:09 +01:00
|
|
|
noobie_popup.visible = not noobie_popup.visible
|
|
|
|
else
|
2021-03-13 04:43:13 +01:00
|
|
|
widget:set_bg(beautiful.bg_focus)
|
2021-02-22 03:16:09 +01:00
|
|
|
noobie_popup:move_next_to(mouse.current_widget_geometry)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
)
|
|
|
|
end
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-02-24 04:07:10 +01:00
|
|
|
local actions = result.widget.mouse_actions
|
|
|
|
has_mouse_actions = actions ~= nil
|
|
|
|
|
|
|
|
if has_mouse_actions then
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-03-13 04:43:13 +01:00
|
|
|
mouse_actions_buttons = gears.table.join(
|
|
|
|
awful.button({}, 1, function() if actions.on_left_click ~= nil then awful.spawn.with_shell(actions.on_left_click) end end),
|
|
|
|
awful.button({}, 2, function() if actions.on_right_click ~= nil then awful.spawn.with_shell(actions.on_right_click) end end),
|
|
|
|
awful.button({}, 4, function() if actions.on_scroll_up ~= nil then awful.spawn.with_shell(actions.on_scroll_up) end end),
|
|
|
|
awful.button({}, 5, function() if actions.on_scroll_down ~= nil then awful.spawn.with_shell(actions.on_scroll_down) end end)
|
|
|
|
)
|
2021-02-24 04:07:10 +01:00
|
|
|
end
|
2021-03-13 04:43:13 +01:00
|
|
|
|
2021-06-20 02:46:04 +02:00
|
|
|
widget:buttons(gears.table.join(mouse_actions_buttons, menu_buttons))
|
2021-02-24 04:07:10 +01:00
|
|
|
end
|
2021-02-20 21:07:05 +01:00
|
|
|
|
2021-02-24 04:07:10 +01:00
|
|
|
watch(string.format([[sh -c "%s"]], path), refresh_rate, update_widget, noobie_widget)
|
2021-02-20 21:07:05 +01:00
|
|
|
|
|
|
|
return noobie_widget
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(noobie_widget, { __call = function(_, ...)
|
|
|
|
return worker(...)
|
|
|
|
end })
|