diff options
author | streetturtle <streetturtle@gmail.com> | 2020-08-16 22:05:26 -0400 |
---|---|---|
committer | streetturtle <streetturtle@gmail.com> | 2020-08-16 22:05:26 -0400 |
commit | 0973c7586ec0db7e6ec51297d7e38c4ee915b472 (patch) | |
tree | b6ccbd74b9d5a955d424b2de435732f8c722275b /docker-widget | |
parent | eb5222eda325594e4773526a052402b4478f06e1 (diff) |
[docker] refactoring
Diffstat (limited to 'docker-widget')
-rw-r--r-- | docker-widget/docker.lua | 183 | ||||
-rw-r--r-- | docker-widget/icons/play-btn.svg | 2 | ||||
-rw-r--r-- | docker-widget/icons/play-btn.svg- | 9 |
3 files changed, 116 insertions, 78 deletions
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 = '<b>' .. container['name'] .. '</b>', - widget = wibox.widget.textbox + { + markup = '<b>' .. container['name'] .. '</b>', + 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" > - <path d="M15 12.3301L9 16.6603L9 8L15 12.3301Z" fill="#D8DEE9" /> + <path d="M9 17L17 12L9 7V17Z" fill="#D8DEE9" /> </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 @@ +<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>
\ No newline at end of file |