This page describes Spaceship API for creating plugins and tweaking Spaceship's behavior.
Spaceship uses SPACESHIP_
prefix for variables and spaceship::
prefix for a function to avoid conflicts with other ones. All section, including custom ones, are being required to use spaceship_
prefix before their name to load properly.
Below is an example of a typical section for Spaceship. Pay attention to a few crucial moments:
- Define options for customization. Their names should start with
SPACESHIP_
. - Section's name should start with
spaceship_
. - Show section only where it's needed (in directories which contains specific files, when a specific command is available, etc).
Take a look at Contribution guidelines for further information.
#
# Foobar
#
# Foobar is a supa-dupa cool tool for making you development easier.
# Link: https://www.foobar.xyz
# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------
SPACESHIP_FOOBAR_SHOW="${SPACESHIP_FOOBAR_SHOW=true}"
SPACESHIP_FOOBAR_PREFIX="${SPACESHIP_FOOBAR_PREFIX="$SPACESHIP_PROMPT_DEFAULT_PREFIX"}"
SPACESHIP_FOOBAR_SUFFIX="${SPACESHIP_FOOBAR_SUFFIX="$SPACESHIP_PROMPT_DEFAULT_SUFFIX"}"
SPACESHIP_FOOBAR_SYMBOL="${SPACESHIP_FOOBAR_SYMBOL="🍷 "}"
SPACESHIP_FOOBAR_COLOR="${SPACESHIP_FOOBAR_COLOR="white"}"
# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------
# Show foobar status
# spaceship_ prefix before section's name is required!
# Otherwise this section won't be loaded.
spaceship_foobar() {
# If SPACESHIP_FOOBAR_SHOW is false, don't show foobar section
[[ $SPACESHIP_FOOBAR_SHOW == false ]] && return
# Check if foobar command is available for execution
spaceship::exists foobar || return
# Show foobar section only when there are foobar-specific files in current
# working directory.
# Here glob qualifiers are used to check if files with specific extension are
# present in directory. Read more about them here:
# http://zsh.sourceforge.net/Doc/Release/Expansion.html
[[ -f foobar.conf || -n *.foo(#qN^/) || -n *.bar(#qN^/) ]] || return
# Use quotes around unassigned local variables to prevent
# getting replaced by global aliases
# http://zsh.sourceforge.net/Doc/Release/Shell-Grammar.html#Aliasing
local 'foobar_status'
if [[ $SOME_CONDITION ]]; then
foobar_status=$(foobar baz)
else
foobar_status=$(foobar foo)
fi
# Exit section if variable is empty
[[ -z $foobar_status ]] && return
# Display foobar section
spaceship::section \
"$SPACESHIP_FOOBAR_COLOR" \
"$SPACESHIP_FOOBAR_PREFIX" \
"$SPACESHIP_FOOBAR_SYMBOL$foobar_status" \
"$SPACESHIP_FOOBAR_SUFFIX"
}
An environment variable that defines the version of currently running Spaceship prompt version. Can be used for issue reporting or debugging purposes.
Accessible to any program or script running in a current shell session.
echo $SPACESHIP_VERSION
#> 3.0.0
Attention! Do not modify the value of this variable! Changing the value may cause the damage to Spaceship installation!
An environment variable that defines the path to Spaceship prompt installation. Spaceship uses this variable for resolving path to sections and utils.
Accessible to any program or script running in a current shell session.
echo $SPACESHIP_ROOT
#> /path/to/spaceship-prompt
This command displays prompt section prefixed with prefix
, suffixed with suffix
and content
painted in color
. Bold style is applied by default.
prefix
, suffix
and content
can contain escapes to set additional foreground color, background color and other visual effects. Read more about escapes in 13 Prompt Expansion section of Zsh documentation.
If SPACESHIP_PROMPT_PREFIXES_SHOW
is false
or if the section is not the first in the prompt, then prefix
will be omitted.
If SPACESHIP_PROMPT_SUFFIXES_SHOW
is false
, then suffix
will be omitted.
Both prefix
and suffix
are optional. They are equal to empty strings by default.
color
Required — Color for displaying thecontent
. Can be any of basic colors or color codes.prefix
Optional — Prefix beforecontent
. Usually, it's the value ofSPACESHIP_*_PREFIX
.content
Required — The content of the section. Can be any valid value or result of command execution.suffix
Optional — Suffix aftercontent
. Usually, it's the value ofSPACESHIP_*_SUFFIX
.
# Display prompt section with prefix and suffix
# Backslash is used to escape line ending
spaceship::section \
"$SPACESHIP_SECTION_COLOR" \
"$SPACESHIP_SECTION_PREFIX" \
"$SPACESHIP_SECTION_SYMBOL$section_content" \
"$SPACESHIP_SECTION_SUFFIX"
# Display prompt section without prefix and suffix
spaceship::section "$color" "$SPACESHIP_CHAR_SYMBOL"
This command validates that given program is available for execution. It checks for PATH binaries, functions, and builtins. It returns zero exit code if a command
exists and non-zero code otherwise.
You can use this utility to check if some program is installed and perform actions conditionally. For example, you can either return an error and exit or continue script's execution.
command
Required — a command that needs to be checked.
# Check multiple commands for existing
if spaceship::exists nvm; then
# extract nvm version
elif spaceship::exists node; then
# extract node version
else
return
fi
# Do nothing if pyenv is not installed
spaceship::exists pyenv || return
The same as spaceship::exists
, but for functions. It returns zero exit code if a function
has been defined previously and non-zero if function
hasn't.
You can use this utility to check if a user has previously defined a function or not. Spaceship uses this utility internally to check if a custom section has been defined and available for execution.
function
Required — a function that needs to be checked.
# Check if section has been defined
if spaceship::defined spaceship_section; then
spaceship_section
else
# section is not found
fi
This utility returns zero exit code if a current working directory is a Git repository and non-zero if it's not.
# Return if current directory is not a git repository
spaceship::is_git || return
The same as spaceship::is_git
, but for Mercurial repositories. This utility returns zero exit code if a current working directory is a Mercurial repository and non-zero if it's not.
# Return if current directory is not a Mercurial repository
spaceship::is_hg || return
This utility checks if option
variable is set and if it is, prints the message
. The message
supports escapes to set foreground color, background color and other visual effects. Read more about escapes in 13 Prompt Expansion section of Zsh documentation.
option
Required — the name of a deprecated variable. If this variable is set (contains any value), then"%B$deprecated%b is deprecated.
will be printed.%B
and%b
is escapes to set the bold style for text.message
Optional — a string for additional deprecation message.
# Check if SPACESHIP_BATTERY_ALWAYS_SHOW is set
spaceship::deprecated SPACESHIP_BATTERY_ALWAYS_SHOW "Use %BSPACESHIP_BATTERY_SHOW='always'%b instead."
#> SPACESHIP_BATTERY_ALWAYS_SHOW is deprecated. Use SPACESHIP_BATTERY_SHOW='always' instead.
This utility converts seconds
into a human-readable format. It splits seconds
into days (d
), hours (h
), minutes (m
) and seconds (s
).
seconds
Required — seconds for conversion into the readable format.
spaceship::displaytime 123456
#> 1d 10h 17m 36s
A utility for performing a union (intersection) of arrays. It lists the contents found in two or more arrays.
Spaceship uses this utility internally for resolution of sections that need to be sourced.
arr...
— a list of arrays.
arr1=('a' 'b' 'c')
arr2=('b' 'c' 'd')
arr3=('c' 'd' 'e')
spaceship::union $arr1 $arr2 $arr3
#> a b c d e