Skip to content

Commit

Permalink
docs: refine docker execdomain example
Browse files Browse the repository at this point in the history
Thanks to @SalahEddineGhamri for filling in some of the gaps
in the existing example

refs: #5510
  • Loading branch information
wez committed Jun 4, 2024
1 parent b8f94c4 commit f53145f
Showing 1 changed file with 50 additions and 29 deletions.
79 changes: 50 additions & 29 deletions docs/config/lua/ExecDomain.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,53 @@ return config

## Example: docker domains

Fully working example is yet to be completely fleshed out (volunteers welcome!) but the
gist of it is:
This example shows how to add each running docker container as a domain,
so that you can spawn a shell into it and/or split it:

{% raw %}
```lua
local wezterm = require 'wezterm'
local config = {}
local config = wezterm.config_builder()

function docker_list()
-- Use wezterm.run_child_process to run
-- `docker container ls --format '{{.ID}}:{{.Names}}'` and parse
-- the output and return a mapping from ID -> name
local docker_list = {}
local success, stdout, stderr = wezterm.run_child_process {
'docker',
'container',
'ls',
'--format',
'{{.ID}}:{{.Names}}',
}
for _, line in ipairs(wezterm.split_by_newlines(stdout)) do
local id, name = line:match '(.-):(.+)'
if id and name then
docker_list[id] = name
end
end
return docker_list
end

function make_docker_label_func(id)
return function(name)
local success, stdout, stderr = wezterm.run_child_process {
'docker',
'inspect',
'--format',
'{{.State.Running}}',
id,
}
local running = stdout == 'true\n'
local color = running and 'Green' or 'Red'
return wezterm.format {
{ Foreground = { AnsiColor = color } },
{ Text = 'docker container named ' .. name },
}
end
end

function make_docker_fixup_func(id)
return function(cmd)
cmd.args = cmd.args or { '/bin/bash' }
cmd.args = cmd.args or { '/bin/sh' }
local wrapped = {
'docker',
'exec',
Expand All @@ -178,32 +208,23 @@ function make_docker_fixup_func(id)
end
end

function make_docker_label_func(id)
return function(name)
-- TODO: query the container state and show info about
-- whether it is running or stopped.
-- If it stopped, you may wish to change the color to red
-- to make it stand out
return wezterm.format {
{ Foreground = { AnsiColor = 'Red' } },
{ Text = 'docker container named ' .. name },
}
function compute_exec_domains()
local exec_domains = {}
for id, name in pairs(docker_list()) do
table.insert(
exec_domains,
wezterm.exec_domain(
'docker:' .. name,
make_docker_fixup_func(id),
make_docker_label_func(id)
)
)
end
return exec_domains
end

local exec_domains = {}
for id, name in pairs(docker_list()) do
table.insert(
exec_domains,
wezterm.exec_domain(
'docker: ' .. name,
make_docker_fixup_func(id),
make_docker_label_func(id)
)
)
end
config.exec_domains = compute_exec_domains()

config.exec_domains = exec_domains
return config
```
{% endraw %}
Expand Down

0 comments on commit f53145f

Please sign in to comment.