feat: implement dynamic pane resizing

- Add `resize_panes_equal()` to `Window`.
- Dynamically apply `tmux` layouts based on pane count.
- Implement `get_width()` and `get_height()` for `Pane`.
- Update test to create 4 panes and use equal resizing.
This commit is contained in:
Mahmoud-Emad
2025-08-31 17:56:03 +03:00
parent 9657e9aa97
commit 7d28129f06
5 changed files with 81 additions and 20 deletions

View File

@@ -655,6 +655,9 @@ fn play_window_ensure(mut plbook PlayBook, mut tmux_instance Tmux) ! {
env: env
)!
}
// After creating all panes, resize them to equal dimensions dynamically
window.resize_panes_equal()!
}
action.done = true

View File

@@ -201,3 +201,21 @@ pub fn (mut p Pane) resize_left(cells int) ! {
pub fn (mut p Pane) resize_right(cells int) ! {
p.resize(direction: 'right', cells: cells)!
}
// Get current pane width
pub fn (p Pane) get_width() !int {
cmd := 'tmux display-message -t ${p.window.session.name}:@${p.window.id}.%${p.id} -p "#{pane_width}"'
res := osal.exec(cmd: cmd, stdout: false, name: 'tmux_get_pane_width') or {
return error("Can't get pane width: ${err}")
}
return res.output.trim_space().int()
}
// Get current pane height
pub fn (p Pane) get_height() !int {
cmd := 'tmux display-message -t ${p.window.session.name}:@${p.window.id}.%${p.id} -p "#{pane_height}"'
res := osal.exec(cmd: cmd, stdout: false, name: 'tmux_get_pane_height') or {
return error("Can't get pane height: ${err}")
}
return res.output.trim_space().int()
}

View File

@@ -274,6 +274,54 @@ pub fn (mut w Window) pane_split_vertical(cmd string) !&Pane {
return w.pane_split(cmd: cmd, horizontal: false)
}
// Resize panes to equal dimensions dynamically based on pane count
pub fn (mut w Window) resize_panes_equal() ! {
w.scan()! // Refresh pane information
pane_count := w.panes.len
if pane_count <= 1 {
return
}
// Dynamic layout based on actual pane count
match pane_count {
1 {
// Single pane, no resizing needed
console.print_debug('Single pane, no resizing needed')
}
2 {
// Two panes: use even-horizontal layout (side by side)
cmd := 'tmux select-layout -t ${w.session.name}:@${w.id} even-horizontal'
osal.execute_silent(cmd) or {
console.print_debug('Could not apply even-horizontal layout: ${err}')
}
}
3 {
// Three panes: use main-horizontal layout (one large top, two smaller bottom)
cmd := 'tmux select-layout -t ${w.session.name}:@${w.id} main-horizontal'
osal.execute_silent(cmd) or {
console.print_debug('Could not apply main-horizontal layout: ${err}')
}
}
4 {
// Four panes: use tiled layout (2x2 grid)
cmd := 'tmux select-layout -t ${w.session.name}:@${w.id} tiled'
osal.execute_silent(cmd) or {
console.print_debug('Could not apply tiled layout: ${err}')
}
}
else {
// For 5+ panes: use tiled layout which works well for any number
if pane_count >= 5 {
cmd := 'tmux select-layout -t ${w.session.name}:@${w.id} tiled'
osal.execute_silent(cmd) or {
console.print_debug('Could not apply tiled layout for ${pane_count} panes: ${err}')
}
}
}
}
}
@[params]
pub struct TtydArgs {
pub mut: