[docker] refactoring

This commit is contained in:
streetturtle 2020-08-16 22:05:26 -04:00
parent eb5222eda3
commit 0973c7586e
3 changed files with 116 additions and 78 deletions

View File

@ -10,17 +10,17 @@
local awful = require("awful")
local wibox = require("wibox")
local watch = require("awful.widget.watch")
local spawn = require("awful.spawn")
local naughty = require("naughty")
local gears = require("gears")
local beautiful = require("beautiful")
local gfs = require("gears.filesystem")
local HOME_DIR = os.getenv("HOME")
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/docker-widget'
local ICONS_DIR = WIDGET_DIR .. '/icons/'
local LIST_CONTAINERS_CMD = [[bash -c "docker container ls -a --format '{{.Names}}::{{.ID}}::{{.Image}}::{{.Status}}'"]]
--- Utility function to show warning messages
local function show_warning(message)
naughty.notify{
@ -46,20 +46,12 @@ local docker_widget = wibox.widget {
id = 'icon',
widget = wibox.widget.imagebox
},
id = "m",
margins = 4,
layout = wibox.container.margin
},
{
id = "txt",
widget = wibox.widget.textbox
},
layout = wibox.layout.fixed.horizontal,
set_icon = function(self, new_icon)
self.m.icon.image = new_icon
end,
set_text = function(self, new_value)
self.txt.text = new_value
self:get_children_by_id("icon")[1].image = new_icon
end
}
@ -77,7 +69,8 @@ local parse_container = function(line)
status = actual_status,
how_long = how_long:gsub('%s?%(.*%)%s?', ''),
is_up = function() return status == 'Up' end,
is_paused = function() return how_long:find('Paused') end
is_paused = function() return how_long:find('Paused') end,
is_exited = function() return status == 'Exited' end
}
return container
end
@ -112,31 +105,7 @@ local function worker(args)
for line in stdout:gmatch("[^\r\n]+") do
local container = parse_container(line)
print(container:is_up())
local name, container_id, image, status, how_long = line:match('(.*)::(.*)::(.*)::(%w*) (.*)')
local is_visible
if status == 'Up' or 'Exited' then is_visible = true else is_visible = false end
local start_stop_button = wibox.widget {
image = ICONS_DIR .. (container:is_up() and 'stop-btn.svg' or 'play-btn.svg'),
visible = is_visible,
opacity = 0.4,
resize = false,
widget = wibox.widget.imagebox
}
start_stop_button:connect_signal("mouse::enter", function(c) c:set_opacity(1) c:emit_signal('widget::redraw_needed') end)
start_stop_button:connect_signal("mouse::leave", function(c) c:set_opacity(0.4) c:emit_signal('widget::redraw_needed') end)
local pause_unpause_button = wibox.widget {
image = ICONS_DIR .. (container:is_paused() and 'unpause-btn.svg' or 'pause-btn.svg'),
visible = container.is_up(),
opacity = 0.4,
resize = false,
widget = wibox.widget.imagebox
}
pause_unpause_button:connect_signal("mouse::enter", function(c) c:set_opacity(1) c:emit_signal('widget::redraw_needed') end)
pause_unpause_button:connect_signal("mouse::leave", function(c) c:set_opacity(0.4) c:emit_signal('widget::redraw_needed') end)
local status_icon = wibox.widget {
image = status_to_icon_name[container['status']],
@ -144,6 +113,90 @@ local function worker(args)
widget = wibox.widget.imagebox
}
local start_stop_button
if container.is_up() or container.is_exited() then
start_stop_button = wibox.widget {
{
id = 'icon',
image = ICONS_DIR .. (container:is_up() and 'stop-btn.svg' or 'play-btn.svg'),
opacity = 0.4,
resize = false,
widget = wibox.widget.imagebox
},
left = 2,
right = 2,
layout = wibox.container.margin
}
start_stop_button:connect_signal("mouse::enter", function(c)
c:get_children_by_id("icon")[1]:set_opacity(1)
c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed') end)
start_stop_button:connect_signal("mouse::leave", function(c)
c:get_children_by_id("icon")[1]:set_opacity(0.4)
c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed')
end)
start_stop_button:buttons(
awful.util.table.join( awful.button({}, 1, function()
local command
if container:is_up() then command = 'stop' else command = 'start' end
status_icon:set_opacity(0.2)
status_icon:emit_signal('widget::redraw_needed')
awful.spawn.easy_async('docker ' .. command .. ' ' .. container['name'], function(stdout, stderr)
if stderr ~= '' then show_warning(stderr) end
spawn.easy_async(LIST_CONTAINERS_CMD, function(stdout, stderr)
rebuild_widget(stdout, stderr) end)
end)
end) ) )
else
start_stop_button = nil
end
local pause_unpause_button
if container.is_up() then
pause_unpause_button = wibox.widget {
{
id = 'icon',
image = ICONS_DIR .. (container:is_paused() and 'unpause-btn.svg' or 'pause-btn.svg'),
opacity = 0.4,
resize = false,
widget = wibox.widget.imagebox
},
left = 2,
right = 2,
layout = wibox.container.margin
}
pause_unpause_button:connect_signal("mouse::enter", function(c)
c:get_children_by_id("icon")[1]:set_opacity(1)
c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed')
end)
pause_unpause_button:connect_signal("mouse::leave", function(c)
c:get_children_by_id("icon")[1]:set_opacity(0.4)
c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed')
end)
pause_unpause_button:buttons(
awful.util.table.join( awful.button({}, 1, function()
local command
if container:is_paused() then command = 'unpause' else command = 'pause' end
status_icon:set_opacity(0.2)
status_icon:emit_signal('widget::redraw_needed')
awful.spawn.easy_async('docker ' .. command .. ' ' .. container['name'], function(stdout, stderr)
if stderr ~= '' then show_warning(stderr) end
spawn.easy_async(LIST_CONTAINERS_CMD, function(stdout, stderr)
rebuild_widget(stdout, stderr) end)
end)
end) ) )
else
pause_unpause_button = nil
end
local row = wibox.widget {
{
{
@ -152,6 +205,7 @@ local function worker(args)
margins = 8,
layout = wibox.container.margin
},
{
{
{
markup = '<b>' .. container['name'] .. '</b>',
@ -164,13 +218,16 @@ local function worker(args)
forced_width = 180,
layout = wibox.layout.fixed.vertical
},
valigh = 'center',
layout = wibox.container.place
},
{
{
start_stop_button,
pause_unpause_button,
layout = wibox.layout.align.horizontal
},
forced_width = 40,
forced_width = 60,
valign = 'center',
haligh = 'center',
layout = wibox.container.place,
@ -186,36 +243,6 @@ local function worker(args)
}
start_stop_button:buttons(
awful.util.table.join( awful.button({}, 1, function()
local command
if container:is_up() then command = 'stop' else command = 'start' end
status_icon:set_opacity(0.2)
status_icon:emit_signal('widget::redraw_needed')
awful.spawn.easy_async('docker ' .. command .. ' ' .. container['name'], function(stdout, stderr)
if stderr ~= '' then show_warning(stderr) return end
spawn.easy_async([[bash -c "docker container ls -a --format '{{.Names}}::{{.ID}}::{{.Image}}::{{.Status}}'"]], function(stdout, stderr)
rebuild_widget(stdout, stderr) end)
end)
end) ) )
pause_unpause_button:buttons(
awful.util.table.join( awful.button({}, 1, function()
local command
if container:is_paused() then command = 'unpause' else command = 'pause' end
status_icon:set_opacity(0.2)
status_icon:emit_signal('widget::redraw_needed')
awful.spawn.easy_async('docker ' .. command .. ' ' .. container['name'], function(stdout, stderr)
if stderr ~= '' then show_warning(stderr) return end
spawn.easy_async([[bash -c "docker container ls -a --format '{{.Names}}::{{.ID}}::{{.Image}}::{{.Status}}'"]], function(stdout, stderr)
rebuild_widget(stdout, stderr) end)
end)
end) ) )
row:connect_signal("mouse::enter", function(c) c:set_bg(beautiful.bg_focus) end)
row:connect_signal("mouse::leave", function(c) c:set_bg(beautiful.bg_normal) end)
@ -231,8 +258,10 @@ local function worker(args)
if popup.visible then
popup.visible = not popup.visible
else
spawn.easy_async([[bash -c "docker container ls -a --format '{{.Names}}::{{.ID}}::{{.Image}}::{{.Status}}'"]], function(stdout, stderr) rebuild_widget(stdout, stderr) end)
spawn.easy_async(LIST_CONTAINERS_CMD, function(stdout, stderr)
rebuild_widget(stdout, stderr)
popup:move_next_to(mouse.current_widget_geometry)
end)
end
end)
)

View File

@ -5,5 +5,5 @@
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M15 12.3301L9 16.6603L9 8L15 12.3301Z" fill="#D8DEE9" />
<path d="M9 17L17 12L9 7V17Z" fill="#D8DEE9" />
</svg>

Before

Width:  |  Height:  |  Size: 181 B

After

Width:  |  Height:  |  Size: 163 B

View File

@ -0,0 +1,9 @@
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M15 12.3301L9 16.6603L9 8L15 12.3301Z" fill="#D8DEE9" />
</svg>

After

Width:  |  Height:  |  Size: 181 B