Skip to content

Commit

Permalink
feat: split panes
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Apr 15, 2023
1 parent bb33f0b commit 33b4732
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ If you don't want the `WeztermSpawn` user command, use

## Usage

### Functions
### Functions: General

<details>
<summary>
Expand Down Expand Up @@ -59,6 +59,35 @@ wezterm.spawn(program, opts)
- cwd? (string): Set the cwd for the spawned program
- args? (string[]): Args to pass to the program

#### Execute any wezterm CLI command (does not yet return result)

```lua
wezterm.exec(args, handler)
```

- args (string[]): Wezterm CLI arguments
- handler (fun(exitcode, signal))

#### Set window title

```lua
wezterm.set_window_title(title, id)
```

- title (string): The new window title
- id? (number): Optional window id to set (defaults to current tab)

### Functions: Tabs

#### Set tab title

```lua
wezterm.set_tab_title(title, id)
```

- title (string): The new tab title
- id? (number): Optional tab id to set (defaults to current tab)

#### Switch tabs by relative number

```lua
Expand All @@ -83,6 +112,36 @@ wezterm.switch_tab.id(id)

- id (number): The id of the tab to switch to

### Functions: Panes

#### Split pane vertically

```lua
wezterm.split_pane.vertical(opts)
```

- opts (table):
- pane? (number): The pane to split (default current)
- top? (boolean): Place the pane on top (default bottom)
- percent? (number): The percentage of the pane that the split should take up (default 50%)
- top_level? (boolean): Split the top level window instead of the selected pane
- move_pane? (boolean): Move the pane instead of spawning a command in it (cannot be used with program)
- program? (string): The program to spawn in the new pane (wezterm default if nil)

#### Split pane horizontally

```lua
wezterm.split_pane.horizontal(opts)
```

- opts (table):
- pane? (number): The pane to split (default current)
- left? (boolean): Place the pane on the left (default right)
- percent? (number): The percentage of the pane that the split should take up (default 50%)
- top_level? (boolean): Split the top level window instead of the selected pane
- move_pane? (boolean): Move the pane instead of spawning a command in it (cannot be used with program)
- program? (string): The program to spawn in the new pane (wezterm default if nil)

#### Switch panes by id

```lua
Expand Down
87 changes: 87 additions & 0 deletions lua/wezterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end
local wezterm = {
switch_tab = {},
switch_pane = {},
split_pane = {},
}

---@param args string[]
Expand All @@ -17,6 +18,92 @@ function wezterm.exec(args, handler)
uv.spawn("wezterm", { args = args }, handler)
end

---@param opts SplitOpts
---@class SplitOpts
---@field pane? number The pane to split (default current)
---@field top? boolean (default false)
---@field move_pane? number|nil Move a pane instead of spawning a command in it (default nil/disabled)
---@field percent? number|nil The percentage of the pane to split (default nil)
---@field program string[]|nil The program to spawn in the new pane (default nil/Wezterm default)
---@field top_level boolean Split the window instead of the pane (default false)
function wezterm.split_pane.vertical(opts)
opts = opts or {}
local args = { "cli", "split-pane" }
if opts.top then
table.insert(args, "--top")
end
if opts.percent then
table.insert(args, "--percent")
table.insert(args, fmt("%d", opts.percent))
end
if opts.pane then
table.insert(args, "--pane-id")
table.insert(args, fmt("%d", opts.pane))
end
if opts.top_level then
table.insert(args, "--top-level")
end
if opts.move_pane then
if opts.program then
err("split: move_pane and program are mutually exclusive")
return
end
elseif opts.program then
for _, arg in ipairs(opts.program) do
table.insert(args, arg)
end
end
wezterm.exec(args, function(code)
if code ~= 0 then
err("split pane")
end
end)
end

---@param opts SplitOpts
---@class SplitOpts
---@field left? boolean (default false)
---@field pane? number The pane to split (default current)
---@field move_pane? number|nil Move a pane instead of spawning a command in it (default nil/disabled)
---@field percent? number|nil The percentage of the pane to split (default nil)
---@field program string[]|nil The program to spawn in the new pane (default nil/Wezterm default)
---@field top_level boolean Split the window instead of the pane (default false)
function wezterm.split_pane.horizontal(opts)
opts = opts or {}
local args = { "cli", "split-pane" }
if opts.left then
table.insert(args, "--left")
else
table.insert(args, "--horizontal")
end
if opts.percent then
table.insert(args, "--percent")
table.insert(args, fmt("%d", opts.percent))
end
if opts.pane then
table.insert(args, "--pane-id")
table.insert(args, fmt("%d", opts.pane))
end
if opts.top_level then
table.insert(args, "--top-level")
end
if opts.move_pane then
if opts.program then
err("split: move_pane and program are mutually exclusive")
return
end
elseif opts.program then
for _, arg in ipairs(opts.program) do
table.insert(args, arg)
end
end
wezterm.exec(args, function(code)
if code ~= 0 then
err("split pane")
end
end)
end

function wezterm.set_tab_title(title, id)
if not title then
return
Expand Down

0 comments on commit 33b4732

Please sign in to comment.