Prompt integration
About 645 words
Once you have more than one context, the question "which network am I about to change" deserves an answer you can see. unifi prompt prints the current context as a short segment for your shell prompt.
$ unifi prompt
demo
$ unifi prompt --format '{context} · {site}'
demo · DefaultThe segment is built from the configuration file alone. There's no network call and no keychain access, so it's safe to run on every prompt. When no context is selected, it prints nothing and exits 0, and your prompt stays empty.
Set it up
unifi prompt init <tool> prints a ready-made snippet, and the first lines of each snippet say where it goes. The same snippets are in the repository under examples/prompt.
Starship
unifi prompt init starship >> ~/.config/starship.toml[custom.unifi]
command = "unifi prompt"
when = "unifi prompt --check"
symbol = " "
style = "bold blue"
format = "[$symbol$output]($style) "
description = "Current unifi-cli context"when uses unifi prompt --check, which prints nothing and exits 0 when a context is selected and 1 otherwise. The symbol is a Nerd Font glyph; replace it if your font doesn't have it.
To show the segment only in directories that hold UniFi manifests, replace when with a detection rule, as the comment in the snippet suggests:
detect_extensions = ["unifi.yaml"]oh-my-posh
unifi prompt init oh-my-poshThe snippet has two parts. The first is a command segment for one of your blocks, which always shows the context:
- type: command
style: plain
foreground: "#006FFF"
template: " {{ .Output }} "
properties:
shell: bash
command: unifi promptThe second is a tooltip, which shows the context and site only while you're typing a unifi command. That keeps the prompt clean the rest of the time and puts the information right where the decision happens:
tooltips:
- type: command
tips:
- unifi
style: plain
foreground: "#006FFF"
template: " {{ .Output }} "
properties:
shell: bash
command: unifi prompt --format '{context} · {site}'JSON and TOML themes take the same fields. Tooltips need a shell oh-my-posh supports them in; its documentation lists which.
zsh
unifi prompt init zsh >> ~/.zshrcunifi_prompt_info() {
local segment
segment="$(unifi prompt 2>/dev/null)" || return
[[ -n "$segment" ]] && print -n "%F{33}unifi:${segment}%f"
}
setopt PROMPT_SUBST
RPROMPT='$(unifi_prompt_info)'"${RPROMPT:+ $RPROMPT}"The context appears in the right prompt, in front of whatever your RPROMPT already shows.
bash
unifi prompt init bash >> ~/.bashrcThe snippet defines unifi_prompt_info and prefixes PS1 with (unifi:<context>).
fish
unifi prompt init fish >> ~/.config/fish/config.fishThe snippet defines a function, unifi_prompt_segment, and leaves your prompt alone. Call it from the prompt function you already have:
function fish_right_prompt
unifi_prompt_segment
endPowerShell
unifi prompt init powershell | Out-File -Append $PROFILEThe snippet wraps your existing prompt function and writes (unifi:<context>) in front of it.
Change what it shows
The segment is a template with four placeholders:
| Placeholder | Value |
|---|---|
{context} | name of the current context |
{site} | its default site |
{controller} | name of its controller entry |
{account} | name of its account entry |
Set the template once, and every integration picks it up:
unifi config set prompt.format '{context} · {site}'--format overrides it for a single call, which is how the oh-my-posh tooltip shows more than the always-on segment. Separators left dangling by an empty value are trimmed, so a context without a default site doesn't render as home · . A placeholder that doesn't exist, such as {sit}, is an error that lists the four valid ones, both for --format and for unifi config set prompt.format.
Use it in scripts
unifi prompt --check && echo "a context is selected"
unifi prompt show --context lake --format '{controller}'
unifi prompt show --output json{
"context": "demo",
"site": "Default",
"controller": "demo",
"account": "demo",
"segment": "demo"
}unifi prompt and unifi prompt show are the same command.
