icon now can be from a file or from web

This commit is contained in:
streetturtle 2021-04-26 23:15:02 -04:00
parent a794fec642
commit 62ab2059e7
4 changed files with 75 additions and 16 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

View File

@ -9,7 +9,7 @@ For example, if your script returns a following JSON:
```json ```json
{ {
"widget": { "widget": {
"icon_path": "smile", "icon": "smile",
"text": "noobie", "text": "noobie",
"mouse_actions": { "mouse_actions": {
"on_scroll_up": "echo 'scroll up'", "on_scroll_up": "echo 'scroll up'",
@ -29,7 +29,7 @@ You can also create widgets with menu:
```json ```json
{ {
"widget": { "widget": {
"icon_path": "smile", "icon": "smile",
"text": "noobie", "text": "noobie",
"mouse_actions": { "mouse_actions": {
"on_scroll_up": "echo 'scroll up'", "on_scroll_up": "echo 'scroll up'",
@ -63,6 +63,13 @@ gives:
![](./screenshots/screenshot2.png) ![](./screenshots/screenshot2.png)
## Features:
- icon (either a widget or a menu item) can be one of:
- a name of an icon from [feather icons](https://feathericons.com/): `arrow-down-circle`;
- a path to a file: `/tmp/someicon.png;
- a URL pointing to the icon: `http://some-icon.online/image.png`;
## Plugins ## Plugins
You can create your own scripts in any language, the only rule is - it should return a proper JSON. You can create your own scripts in any language, the only rule is - it should return a proper JSON.

View File

@ -1,6 +1,6 @@
{ {
"widget": { "widget": {
"icon_path": "smile", "icon": "https://avatars.githubusercontent.com/u/9363150?s=60&v=4/2x/circled-down--v2.png",
"text": "noobie", "text": "noobie",
"mouse_actions": { "mouse_actions": {
"on_scroll_up": "echo 'scroll up'", "on_scroll_up": "echo 'scroll up'",
@ -11,7 +11,7 @@
"menu": { "menu": {
"items": [ "items": [
{ {
"icon": "bell", "icon": "https://avatars.githubusercontent.com/u/9363150?s=60&v=4/2x/circled-down--v2.png",
"title": "Say hi!", "title": "Say hi!",
"onclick": "notify-send 'hi!'" "onclick": "notify-send 'hi!'"
}, },

View File

@ -4,10 +4,14 @@ local wibox = require("wibox")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local gears = require("gears") local gears = require("gears")
local json = require("json") local json = require("json")
local gfs = require("gears.filesystem")
local spawn = require("awful.spawn")
local HOME_DIR = os.getenv("HOME") local HOME_DIR = os.getenv("HOME")
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/noobie' local WIDGET_DIR = HOME_DIR .. '/.config/awesome/noobie'
local ICONS_DIR = WIDGET_DIR .. '/feather_icons/' local ICONS_DIR = WIDGET_DIR .. '/feather_icons/'
local CACHE_DIR = os.getenv("HOME") .. '/.cache/noobie/icons'
local cur_stdout local cur_stdout
local noobie_widget = {} local noobie_widget = {}
@ -29,6 +33,11 @@ local function worker(user_args)
show_warning("Cannot create a widget, required parameter 'path' is not provided") show_warning("Cannot create a widget, required parameter 'path' is not provided")
return return
end end
if not gfs.dir_readable(CACHE_DIR) then
gfs.make_directories(CACHE_DIR)
end
local noobie_popup = awful.popup{ local noobie_popup = awful.popup{
ontop = true, ontop = true,
visible = false, visible = false,
@ -52,8 +61,8 @@ local function worker(user_args)
{ {
{ {
id = 'icn', id = 'icn',
forced_height = 20, --forced_height = 20,
forced_width = 20, --forced_width = 20,
resize = true, resize = true,
widget = wibox.widget.imagebox widget = wibox.widget.imagebox
}, },
@ -85,8 +94,27 @@ local function worker(user_args)
end end
end, end,
set_icon = function(self, new_icon) set_icon = function(self, new_icon)
self:get_children_by_id('icn')[1]:set_image(ICONS_DIR .. new_icon .. '.svg') -- new_icon is a path to a file
end, if new_icon:sub(1, 1) == '/' then
self:get_children_by_id('icn')[1]:set_image(new_icon)
-- new_icon is a url of the icon
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)
print(download_cmd)
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
} }
local update_widget = function(widget, stdout, stderr) local update_widget = function(widget, stdout, stderr)
@ -102,7 +130,7 @@ local function worker(user_args)
local result = json.decode(stdout) local result = json.decode(stdout)
widget:set_text(result.widget.text) widget:set_text(result.widget.text)
widget:set_icon(result.widget.icon_path) widget:set_icon(result.widget.icon)
has_menu = result.menu ~= nil and result.menu.items ~= nil and #result.menu.items > 0 has_menu = result.menu ~= nil and result.menu.items ~= nil and #result.menu.items > 0
@ -111,16 +139,39 @@ local function worker(user_args)
for i = 0, #rows do rows[i]=nil end for i = 0, #rows do rows[i]=nil end
for _, item in ipairs(result.menu.items) do for _, item in ipairs(result.menu.items) do
local item_image = wibox.widget{
resize = true,
forced_height = 20,
forced_width = 20,
widget = wibox.widget.imagebox
}
-- new_icon is a path to a file
if item.icon:sub(1, 1) == '/' then
item_image:set_image(item.icon)
-- new_icon is a url of the icon
elseif item.icon:sub(1, 4) == 'http' then
local icon_path = CACHE_DIR .. '/' .. item.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, item.icon)
print(download_cmd)
spawn.easy_async(download_cmd,
function() item_image:set_image(icon_path) end)
else
item_image:set_image(icon_path)
end
-- new_icon is a feather icon
else
item_image:set_image(ICONS_DIR .. item.icon .. '.svg')
end
local row = wibox.widget { local row = wibox.widget {
{ {
{ {
{ item_image,
image = ICONS_DIR .. item.icon .. '.svg',
resize = true,
forced_height = 20,
forced_width = 20,
widget = wibox.widget.imagebox
},
{ {
text = item.title, text = item.title,
font = font, font = font,