repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
nushell/modules/wezterm_integration.nu
Raw# WezTerm Shell Integration for Nushell
# =====================================
# Emits OSC 1337 (user vars) and OSC 7 (cwd) so wezterm can:
# - Track the running command via WEZTERM_PROG
# - Track whether nushell is currently executing via WEZTERM_STATE
# - Know the current working directory (new tabs spawn in the right place)
# - Surface arbitrary data from nushell into the wezterm status bar
#
# OSC 133 (prompt zones) is NOT enabled — nushell's implementation
# has known bugs with wezterm (scrolling on keypress).
# See: https://github.com/wezterm/wezterm/discussions/5859
#
# To install: add `use wezterm_integration.nu` to your config.nu
# ---------------------------------------------------------------------------
# Core: set a user var via OSC 1337
# ---------------------------------------------------------------------------
# Values must be base64-encoded. The BEL (\x07) terminates the sequence.
# Mirrors __wezterm_set_user_var() from the bash/zsh wezterm.sh integration.
export-env {
def __wezterm_set_user_var [name: string, value: string] {
let b64 = ($value | encode base64)
let osc = (ansi --osc $"1337;SetUserVar=($name)=($b64)")
print -n ($osc + (char bel))
}
# Emit OSC 7 so wezterm knows the current working directory.
# New tabs opened from this pane will use this as their cwd.
def __wezterm_osc7 [] {
let hostname = (sys host).hostname
let cwd = ($env.PWD | str replace --all '\\' '/')
let osc = (ansi --osc $"7;file://($hostname)($cwd)")
print -n ($osc + (char bel))
}
# -------------------------------------------------------------------------
# pre_execution hook: tell wezterm what command is running
# -------------------------------------------------------------------------
# Fires just before each command executes. Sets WEZTERM_PROG so wezterm
# knows what is happening in the pane and WEZTERM_STATE=running for a stable
# activity signal. Cleared in pre_prompt after completion.
$env.config.hooks.pre_execution = (
$env.config.hooks.pre_execution
| default []
| append {
condition: { true }
code: {
let cmd = (commandline)
__wezterm_set_user_var "WEZTERM_PROG" $cmd
__wezterm_set_user_var "WEZTERM_STATE" "running"
}
}
)
# -------------------------------------------------------------------------
# pre_prompt hook: clean up and update cwd
# -------------------------------------------------------------------------
# Fires after every command completes, before the next prompt is shown.
# Clears WEZTERM_PROG/WEZTERM_STATE (shell is idle) and emits OSC 7 for the cwd.
$env.config.hooks.pre_prompt = (
$env.config.hooks.pre_prompt
| default []
| append {
condition: { true }
code: {
__wezterm_set_user_var "WEZTERM_PROG" ""
__wezterm_set_user_var "WEZTERM_STATE" "idle"
__wezterm_osc7
}
}
)
# -------------------------------------------------------------------------
# env_change.PWD hook: update cwd on cd
# -------------------------------------------------------------------------
# Fires when $PWD changes (e.g. via `cd`, `z`, nushell's block cd).
# Refreshes OSC 7 so wezterm always knows the current directory.
let current_pwd_hooks = ($env.config.hooks.env_change | get --optional PWD | default [])
$env.config.hooks.env_change = ($env.config.hooks.env_change | merge {
PWD: ($current_pwd_hooks | append {
condition: { true }
code: { __wezterm_osc7 }
})
})
}