wez / wezterm

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
https://wezfurlong.org/wezterm/
Other
17.06k stars 770 forks source link

CloseWorkspace api and in cli #3658

Open sudo-tee opened 1 year ago

sudo-tee commented 1 year ago

Is your feature request related to a problem? Please describe. Comming from tmux, I really enjoy wezterm. Hovewer I used to script a lot of things with tmux. A feature I use is a key-bind for for closing a workspace. It removes the clutter and free some system ressources when I don't need the workspace anymore. Having to close all the panes to close a workspace is tedious.

Describe the solution you'd like I would like to have an api to close a workspace like mux.CloseWorkspace(workspace_name, {confirm = false}) Also having a wezterm cli kill-workspace my-workspace

Describe alternatives you've considered Creating a bash script to call wezterm cli kill-pane for all the panes in a workspace since there is no native api to kill a pane by id.

Thanks for your help and your wonderful application

sudo-tee commented 3 months ago

In the meantime I have this function in my config

function kill_wokspace(workspace)
  return function(window, pane, line)
    workspace = workspace or window:get_active_workspace()
    local success, stdout = wezterm.run_child_process({ "wezterm", "cli", "list", "--format=json" })

    if success then
      local json = wezterm.json_parse(stdout)
      if not json then
        return
      end

      local workspace_panes = u.filter(json, function(p)
        return p.workspace == workspace
      end)

      for _, p in ipairs(workspace_panes) do
        wezterm.run_child_process({ "wezterm", "cli", "kill-pane", "--pane-id=" .. p.pane_id })
      end
    end
  end
end
tjex commented 2 months ago

In case it helps others, this is how I built u.filter() to look.

local M = {}
local wezterm = require("wezterm")

M.filter = function(tbl, callback)
    local filt_table = {}

    for i, v in ipairs(tbl) do
        if callback(v, i) then
            table.insert(filt_table, v)
        end
    end
    return filt_table
end

return M

Keybind then looks like:

        {
            key = "k",
            mods = "LEADER",
            action = wezterm.action_callback(function(window)
                local w = window:active_workspace()
                func.kill_workspace(w)
            end),
        },

And the actual function (note some slight tweaks):

local util = require("util")
local wezterm = require("wezterm")
local M = {}

M.kill_workspace = function(workspace)
    local success, stdout =
        wezterm.run_child_process({ "/opt/homebrew/bin/wezterm", "cli", "list", "--format=json" })

    if success then
        local json = wezterm.json_parse(stdout)
        if not json then
            return
        end

        local workspace_panes = util.filter(json, function(p)
            return p.workspace == workspace
        end)

        for _, p in ipairs(workspace_panes) do
            wezterm.run_child_process({
                "/opt/homebrew/bin/wezterm",
                "cli",
                "kill-pane",
                "--pane-id=" .. p.pane_id,
            })
        end
    end
end
return M