From 0973c7586ec0db7e6ec51297d7e38c4ee915b472 Mon Sep 17 00:00:00 2001 From: streetturtle Date: Sun, 16 Aug 2020 22:05:26 -0400 Subject: [docker] refactoring --- docker-widget/docker.lua | 183 ++++++++++++++++++++++---------------- docker-widget/icons/play-btn.svg | 2 +- docker-widget/icons/play-btn.svg- | 9 ++ 3 files changed, 116 insertions(+), 78 deletions(-) create mode 100644 docker-widget/icons/play-btn.svg- (limited to 'docker-widget') diff --git a/docker-widget/docker.lua b/docker-widget/docker.lua index 3bf92b4..918e3b5 100644 --- a/docker-widget/docker.lua +++ b/docker-widget/docker.lua @@ -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 { { { @@ -154,15 +207,19 @@ local function worker(args) }, { { - markup = '' .. container['name'] .. '', - widget = wibox.widget.textbox + { + markup = '' .. container['name'] .. '', + widget = wibox.widget.textbox + }, + { + text = container['how_long'], + widget = wibox.widget.textbox + }, + forced_width = 180, + layout = wibox.layout.fixed.vertical }, - { - text = container['how_long'], - widget = wibox.widget.textbox - }, - forced_width = 180, - layout = wibox.layout.fixed.vertical + valigh = 'center', + layout = wibox.container.place }, { { @@ -170,7 +227,7 @@ local function worker(args) 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) - popup:move_next_to(mouse.current_widget_geometry) + spawn.easy_async(LIST_CONTAINERS_CMD, function(stdout, stderr) + rebuild_widget(stdout, stderr) + popup:move_next_to(mouse.current_widget_geometry) + end) end end) ) diff --git a/docker-widget/icons/play-btn.svg b/docker-widget/icons/play-btn.svg index 455a61d..573ae7e 100644 --- a/docker-widget/icons/play-btn.svg +++ b/docker-widget/icons/play-btn.svg @@ -5,5 +5,5 @@ fill="none" xmlns="http://www.w3.org/2000/svg" > - + \ No newline at end of file diff --git a/docker-widget/icons/play-btn.svg- b/docker-widget/icons/play-btn.svg- new file mode 100644 index 0000000..455a61d --- /dev/null +++ b/docker-widget/icons/play-btn.svg- @@ -0,0 +1,9 @@ + + + \ No newline at end of file -- cgit v1.2.3