diff options
Diffstat (limited to 'docker-widget')
-rw-r--r-- | docker-widget/docker.lua | 159 | ||||
-rw-r--r-- | docker-widget/icons/trash-btn.svg | 1 |
2 files changed, 124 insertions, 36 deletions
diff --git a/docker-widget/docker.lua b/docker-widget/docker.lua index 6e04d8a..6ea0179 100644 --- a/docker-widget/docker.lua +++ b/docker-widget/docker.lua @@ -19,7 +19,8 @@ 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 -s -n %s --format '{{.Names}}::{{.ID}}::{{.Image}}::{{.Status}}::{{.Size}}'"]] +local LIST_CONTAINERS_CMD = [[bash -c "docker container ls -a -s -n %s]] + .. [[ --format '{{.Names}}::{{.ID}}::{{.Image}}::{{.Status}}::{{.Size}}'"]] --- Utility function to show warning messages local function show_warning(message) @@ -62,7 +63,6 @@ local parse_container = function(line) else actual_status = status end how_long = how_long:gsub('%s?%(.*%)%s?', '') - -- if how_long:find('seconds') then how_long = 'less than a minute ago' end local container = { name = name, @@ -84,9 +84,9 @@ local status_to_icon_name = { Paused = ICONS_DIR .. 'pause.svg' } -local function worker(args) +local function worker(user_args) - local args = args or {} + local args = user_args or {} local icon = args.icon or ICONS_DIR .. 'docker.svg' local number_of_containers = args.number_of_containers or -1 @@ -98,15 +98,15 @@ local function worker(args) layout = wibox.layout.fixed.vertical, } - local function rebuild_widget(stdout, stderr, _, _) - if stderr ~= '' then - show_warning(stderr) + local function rebuild_widget(containers, errors, _, _) + if errors ~= '' then + show_warning(errors) return end for i = 0, #rows do rows[i]=nil end - for line in stdout:gmatch("[^\r\n]+") do + for line in containers:gmatch("[^\r\n]+") do local container = parse_container(line) @@ -122,20 +122,36 @@ local function worker(args) 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 + { + 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 }, - left = 2, - right = 2, - layout = wibox.container.margin + shape = gears.shape.circle, + bg = '#00000000', + widget = wibox.container.background } + local old_cursor, old_wibox start_stop_button:connect_signal("mouse::enter", function(c) + c:set_bg('#3B4252') + + local wb = mouse.current_wibox + old_cursor, old_wibox = wb.cursor, wb + wb.cursor = "hand1" 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:set_bg('#00000000') + if old_wibox then + old_wibox.cursor = old_cursor + old_wibox = nil + end c:get_children_by_id("icon")[1]:set_opacity(0.4) c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed') end) @@ -148,10 +164,12 @@ local function worker(args) 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(string.format(LIST_CONTAINERS_CMD, number_of_containers), function(stdout, stderr) - rebuild_widget(stdout, stderr) end) + spawn.easy_async('docker ' .. command .. ' ' .. container['name'], function() + if errors ~= '' then show_warning(errors) end + spawn.easy_async(string.format(LIST_CONTAINERS_CMD, number_of_containers), + function(stdout, stderr) + rebuild_widget(stdout, stderr) + end) end) end) ) ) else @@ -163,21 +181,36 @@ local function worker(args) 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 + { + 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 }, - left = 2, - right = 2, - layout = wibox.container.margin + shape = gears.shape.circle, + bg = '#00000000', + widget = wibox.container.background } + local old_cursor, old_wibox pause_unpause_button:connect_signal("mouse::enter", function(c) + c:set_bg('#3B4252') + local wb = mouse.current_wibox + old_cursor, old_wibox = wb.cursor, wb + wb.cursor = "hand1" 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:set_bg('#00000000') + if old_wibox then + old_wibox.cursor = old_cursor + old_wibox = nil + end c:get_children_by_id("icon")[1]:set_opacity(0.4) c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed') end) @@ -190,16 +223,68 @@ local function worker(args) status_icon:set_opacity(0.2) status_icon:emit_signal('widget::redraw_needed') - awful.spawn.easy_async('docker ' .. command .. ' ' .. container['name'], function(stdout, stderr) + awful.spawn.easy_async('docker ' .. command .. ' ' .. container['name'], function(_, stderr) if stderr ~= '' then show_warning(stderr) end - spawn.easy_async(string.format(LIST_CONTAINERS_CMD, number_of_containers), function(stdout, stderr) - rebuild_widget(stdout, stderr) end) + spawn.easy_async(string.format(LIST_CONTAINERS_CMD, number_of_containers), + function(stdout, container_errors) + rebuild_widget(stdout, container_errors) + end) end) end) ) ) else pause_unpause_button = nil end + local delete_button + if not container.is_up() then + delete_button = wibox.widget { + { + { + id = 'icon', + image = ICONS_DIR .. 'trash-btn.svg', + opacity = 0.4, + resize = false, + widget = wibox.widget.imagebox + }, + margins = 4, + layout = wibox.container.margin + }, + shape = gears.shape.circle, + bg = '#00000000', + widget = wibox.container.background + } + delete_button:buttons( + awful.util.table.join( awful.button({}, 1, function() + awful.spawn.easy_async('docker rm ' .. container['name'], function(_, rm_stderr) + if rm_stderr ~= '' then show_warning(rm_stderr) end + spawn.easy_async(string.format(LIST_CONTAINERS_CMD, number_of_containers), + function(lc_stdout, lc_stderr) + rebuild_widget(lc_stdout, lc_stderr) end) + end) + end))) + + local old_cursor, old_wibox + delete_button:connect_signal("mouse::enter", function(c) + c:set_bg('#3B4252') + local wb = mouse.current_wibox + old_cursor, old_wibox = wb.cursor, wb + wb.cursor = "hand1" + c:get_children_by_id("icon")[1]:set_opacity(1) + c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed') + end) + delete_button:connect_signal("mouse::leave", function(c) + c:set_bg('#00000000') + if old_wibox then + old_wibox.cursor = old_cursor + old_wibox = nil + end + c:get_children_by_id("icon")[1]:set_opacity(0.4) + c:get_children_by_id("icon")[1]:emit_signal('widget::redraw_needed') + end) + else + delete_button = nil + end + local row = wibox.widget { { @@ -237,9 +322,10 @@ local function worker(args) { start_stop_button, pause_unpause_button, + delete_button, layout = wibox.layout.align.horizontal }, - forced_width = 60, + forced_width = 90, valign = 'center', haligh = 'center', layout = wibox.container.place, @@ -270,10 +356,11 @@ local function worker(args) if popup.visible then popup.visible = not popup.visible else - spawn.easy_async(string.format(LIST_CONTAINERS_CMD, number_of_containers), function(stdout, stderr) - rebuild_widget(stdout, stderr) - popup:move_next_to(mouse.current_widget_geometry) - end) + spawn.easy_async(string.format(LIST_CONTAINERS_CMD, number_of_containers), + function(stdout, stderr) + rebuild_widget(stdout, stderr) + popup:move_next_to(mouse.current_widget_geometry) + end) end end) ) diff --git a/docker-widget/icons/trash-btn.svg b/docker-widget/icons/trash-btn.svg new file mode 100644 index 0000000..78d8035 --- /dev/null +++ b/docker-widget/icons/trash-btn.svg @@ -0,0 +1 @@ +<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#BF616A" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg>
\ No newline at end of file |