# Resize panes and windows

Source: https://libtmux.org/en/haskell/latest/guides/resizing/

> resizePane and resizeWindow request absolute geometry for a captured physical reference.

[`resizePane` and `resizeWindow`](https://libtmux.org/en/haskell/latest/reference/libtmux-server/) request absolute
geometry for a captured physical reference. Choose a width, a height, or both
with [`Resize`](https://libtmux.org/en/haskell/latest/reference/libtmux-value-t-resize/), then capture again for actual cells.
A successful command can leave dimensions clamped by native layout constraints.

Run from a [provisioned checkout](https://github.com/libtmux/libtmux-haskell/blob/72edb54317bf619c6dc54bff311844f062f8984c/.github/CONTRIBUTING.md#toolchain).

```console
$ cabal run --verbose=0 resize-geometry
```

The program creates three panes in its own session. It changes one pane and
then its window, refuses an invalid request, checks the original windows and
removes its session before retiring the private daemon.

<!-- example: resize-geometry -->
```haskell
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Request absolute geometry and observe the resulting cells.
module Main (main) where

import Control.Exception (ExceptionWithContext (..), bracket, rethrowIO)
import Control.Monad (forM_)
import Data.List.NonEmpty (NonEmpty (..))
import LibTmux qualified as Tmux
import LibTmux.Creation qualified as Creation
import System.Exit (die)
import System.OsString.Posix qualified as Os

main :: IO ()
main = do
  columns <- dimension 120
  rows <- dimension 60
  side <- dimension 30
  lower <- dimension 20
  paneWidth <- dimension 50
  paneHeight <- dimension 10
  windowWidth <- dimension 100
  windowHeight <- dimension 40
  finalWidth <- dimension 80
  oversized <- dimension 10001
  let process = Creation.DirectCommand (Os.fromBytestring "/bin/cat") (Os.fromBytestring "-" :| [])
      options = Creation.defaultSessionOptions
        {Creation.width = Just columns, Creation.height = Just rows, Creation.process = process}
  result <- Tmux.tryTmux $ Tmux.withOwnedServer Tmux.defaultOwnedServerConfig $ \server -> do
    before <- Tmux.capture server
    bracket (Tmux.createSession server options) (Tmux.killSession server . (.session)) $ \created -> do
      let slot = Creation.WindowSlot created.session created.windowIndex
      _ <- Tmux.splitPane server slot created.pane Creation.defaultSplitOptions
        {Creation.direction = Creation.LeftRight, Creation.size = Creation.SplitCells side,
          Creation.splitProcess = process}
      _ <- Tmux.splitPane server slot created.pane Creation.defaultSplitOptions
        {Creation.size = Creation.SplitCells lower, Creation.splitProcess = process}
      forM_ [("pane width", Tmux.ResizeWidth paneWidth), ("pane height", Tmux.ResizeHeight paneHeight),
             ("pane both", Tmux.ResizeBoth paneWidth lower)] $ \(label, size) -> do
        Tmux.resizePane server created.pane size
        fresh <- Tmux.capture server
        pane <- require (Tmux.lookupPane fresh created.pane)
        putStrLn $ label <> ": " <> show (Tmux.dimensionValue pane.width, Tmux.dimensionValue pane.height)
      forM_ [("window width", Tmux.ResizeWidth windowWidth), ("window height", Tmux.ResizeHeight windowHeight),
             ("window both", Tmux.ResizeBoth finalWidth side)] $ \(label, size) -> do
        Tmux.resizeWindow server created.window size
        fresh <- Tmux.capture server
        window <- require (Tmux.lookupWindow fresh created.window)
        putStrLn $ label <> ": " <> show (Tmux.dimensionValue window.width, Tmux.dimensionValue window.height)
      invalid <- Tmux.tryTmux (Tmux.resizePane server created.pane (Tmux.ResizeBoth paneWidth oversized))
      refused <- case invalid of
        Left (ExceptionWithContext _ (Tmux.InvalidMutationArgument (Tmux.InvalidOperand "pane.height" _))) ->
          pure True
        Left problem -> rethrowIO problem
        Right () -> die "Oversized resize unexpectedly succeeded"
      after <- Tmux.capture server
      let preserved old = case Tmux.lookupWindow after old.ref of
            Right current -> (current.width, current.height) == (old.width, old.height)
            Left _ -> False
      putStrLn $ "invalid size refused: " <> show refused
      putStrLn $ "bystander preserved: " <> show (all preserved (Tmux.windows before))
    remaining <- Tmux.capture server
    putStrLn $ "original sessions after cleanup: " <>
      show (map (.ref) (Tmux.sessions remaining) == map (.ref) (Tmux.sessions before))
  either rethrowIO pure result

dimension :: Integer -> IO Tmux.Dimension
dimension = require . Tmux.mkDimension

require :: Show problem => Either problem a -> IO a
require = either (die . show) pure
```

It prints:

<!-- example-output: resize-geometry -->
```text
pane width: (50,39)
pane height: (50,10)
pane both: (50,20)
window width: (100,60)
window height: (100,40)
window both: (80,30)
invalid size refused: True
bystander preserved: True
original sessions after cleanup: True
```

## Choose and validate dimensions

`ResizeWidth width` retains the native height; `ResizeHeight height` retains
the native width. `ResizeBoth width height` supplies both axes. Every request
contains at least one [`Dimension`](https://libtmux.org/en/haskell/latest/reference/libtmux-value-t-dimension/).
[`mkDimension`](https://libtmux.org/en/haskell/latest/reference/libtmux-value-v-mkdimension/) rejects zero and negative values.
The resize operations additionally require every axis to be at most 10000
cells, the shared bound for tmux 3.3a and 3.8.

Validation checks the whole request before admitting a mutation. An oversized
second axis cannot unzoom the window or leave the first axis resized.
[`InvalidMutationArgument`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-invalidmutationargument/>) contains the invalid field, and no operation is
retained. A positive [`Dimension`](<https://libtmux.org/en/haskell/latest/reference/libtmux-value-t-dimension/>) alone does not establish a command's upper
bound.

## Observe native layout effects

[`resizePane`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-resizepane/>) targets the exact [`PaneRef`](<https://libtmux.org/en/haskell/latest/reference/libtmux-snapshot-t-paneref/>), including an inactive pane. Native
resizing can change neighboring panes or unzoom the containing window.
The requested size can clamp or leave the layout unchanged. It does not
select the target pane.

[`resizeWindow`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-resizewindow/>) targets one physical [`WindowRef`](<https://libtmux.org/en/haskell/latest/reference/libtmux-snapshot-t-windowref/>) across all its links and sets
its [`window-size`](https://libtmux.org/en/tmux/latest/catalog/options/window-size/) option to `manual`. Absolute sizing needs no active link or
client selection. Layout constraints determine the resulting cells.
Both operations return [`IO ()`](<https://hackage-content.haskell.org/package/base-4.22.0.0/docs/System-IO.html#t:IO>); use a new [`capture`](https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-capture/)
to read geometry. Earlier snapshots retain their old values.

## Handle errors and interruption

The default operations use [`defaultMutationOptions`](https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-defaultmutationoptions/).
[`resizePaneWith`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-resizepanewith/>) and [`resizeWindowWith`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-resizewindowwith/>) accept an explicit deadline.
Generation checks refuse foreign references; a missing native target raises
[`MutationRejected`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-mutationrejected/>). A failing after-hook can report
[`MutationAppliedWithErrors`](<https://libtmux.org/en/haskell/latest/reference/libtmux-server-v-mutationappliedwitherrors/>) after the primary resize has happened.

Keep exception context when catching failures. Cancellation preserves its
exception class and correlated [`MutationRecovery`](https://libtmux.org/en/haskell/latest/reference/libtmux-server/).
An uncertain outcome does not trigger an automatic retry. See
[mutation recovery](https://libtmux.org/en/haskell/latest/guides/mutations/#known-and-uncertain-outcomes).

Absolute width and height are implemented. Directional adjustments, pane
percentages, largest/smallest-client fitting, zoom toggles, history trimming,
mouse actions and floating-specific modes remain separate work in the
[pane and window contract](https://github.com/libtmux/libtmux-haskell/blob/72edb54317bf619c6dc54bff311844f062f8984c/docs/contracts/pane-commands.md).
