1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-28 09:12:19 +03:00
wezterm/docs/config/lua/mux-events/mux-is-process-stateful.md
Wez Furlong ea46967e2d mux: understand process tree, add mux-is-process-stateful event
This commit adds plumbing to support mapping the process tree to
lua objects which in turn allows a new `mux-is-process-stateful`
event to be defined by the user for finer control over closing
prompt behavior.

refs: #1412
2021-12-18 09:46:00 -07:00

3.1 KiB

mux-is-process-stateful

Since: nightly builds only

The mux-is-process-stateful event is emitted when the multiplexer layer wants to determine whether a given Pane can be closed without prompting the user.

This event is synchronous and must return as quickly as possible in order to avoid blocking the multiplexer.

The event is passed a LocalProcessInfo object representing the process that corresponds to the pane. LocalProcessInfo has the following fields:

  • pid - the process id
  • ppid - the parent process id
  • name - a short name for the process. You probably should prefer to look at the executable or argv fields instead of this one
  • status - a string holding the status of the process; it can be Idle, Run, Sleep, Stop, Zombie, Tracing, Dead, Wakekill, Waking, Parked, LockBlocked, Unknown.
  • argv - a table holding the argument array for the process
  • executable - the full path to the executable image for the process (may be empty)
  • cwd - the current working directory for the process (may be empty)
  • children - a table keyed by child process id and whose values are themselves LocalProcessInfo objects that describe the child processes

The hook can return one of the following values:

  • true - to indicate that this process tree is considered to be stateful and that the user should be prompted before terminating the pane
  • false - to indicate that the process tree can be terminated without prompting the user
  • nil - to use the default behavior, which is to consider the skip_close_confirmation_for_processes_named configuration option
  • any other value, or an error, will be treated as equivalent to returning nil

Example

This example doesn't change any behavior, but demonstrates how to log the various fields of process tree, indenting the entries for each level of the process hierarchy.

Since it returns nil, it uses the default behavior.

local wezterm = require 'wezterm'

function log_proc(proc, indent)
  indent = indent or ""
  wezterm.log_info(indent .. "pid=" .. proc.pid .. ", name=" .. proc.name .. ", status=" .. proc.status)
  wezterm.log_info(indent .. "argv=" .. table.concat(proc.argv, " "))
  wezterm.log_info(indent .. "executable=" .. proc.executable .. ", cwd=" .. proc.cwd)
  for pid, child in pairs(proc.children) do
    log_proc(child, indent .. "  ")
  end
end

wezterm.on("mux-is-process-stateful", function(proc)
  log_proc(proc)

  -- Just use the default behavior
  return nil
end)

return {}

Produces the following logs for a zsh that spawned bash that spawned vim foo:

INFO  config::lua > lua: pid=1913470, name=zsh, status=Sleep
INFO  config::lua > lua: argv=-zsh
INFO  config::lua > lua: executable=/usr/bin/zsh, cwd=/home/wez
INFO  config::lua > lua:   pid=1913567, name=bash, status=Sleep
INFO  config::lua > lua:   argv=bash
INFO  config::lua > lua:   executable=/usr/bin/bash, cwd=/home/wez
INFO  config::lua > lua:     pid=1913624, name=vim, status=Sleep
INFO  config::lua > lua:     argv=vim foo
INFO  config::lua > lua:     executable=/usr/bin/vim, cwd=/home/wez