Skip to content

Commit

Permalink
feat(cli): CLI for inserting and removing sections
Browse files Browse the repository at this point in the history
This PR introduces two commands for adding and removing sections in runtime without disturbing other
sections.

Closes #318
  • Loading branch information
denysdovhan committed Sep 20, 2021
1 parent a8554cf commit 5dcce40
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 4 deletions.
123 changes: 119 additions & 4 deletions lib/cli.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,97 @@ _spaceship::cli::bug-report() {
echo "$short_url"
}

_spaceship::cli::add() {
# Parse CLI options
zparseopts -E -D - \
A:=after_ -after:=after_ \
B:=before_ -before:=before_ \
P:=prompt_ -prompt:=prompt_

local sections=("$@") index
local after_section="${after_[2]}" before_section="${before_[2]=line_sep}"

local prompt_type="${prompt_[2]=prompt}"
local prompt_option="SPACESHIP_${(U)prompt_type}_ORDER"
local prompt_order=("${(P@)prompt_option}")
local new_order=()

for section in "${sections[@]}"; do
if ! spaceship::defined "spaceship_$section"; then
spaceship::skip_section "$section"
fi
done

if [[ -n "$before_section" ]]; then
index="${prompt_order[(i)${before_section}]=0}"
new_order=(
"${(@)prompt_order[0,$((index-1))]}"
"${sections[@]}"
"${(@)prompt_order[${index},$]}"
)
fi

if [[ -n "$after_section" ]]; then
index="${prompt_order[(i)${after_section}]=0}"
new_order=(
"${(@)prompt_order[0,${index}]}"
"${sections[@]}"
"${(@)prompt_order[$((index+1)),$]}"
)
fi

# Modifying orders
case "$prompt_type" in
prompt)
export SPACESHIP_PROMPT_ORDER=("${new_order[@]}")
;;
rprompt)
export SPACESHIP_RPROMPT_ORDER=("${new_order[@]}")
;;
*)
echo "Unknown prompt type: $prompt_type"
echo "Available types: prompt, rprompt"
;;
esac
}

_spaceship::cli::remove() {
# Parse CLI options
zparseopts -E -D - P:=prompt_ -prompt:=prompt_

local sections=("$@")
local prompt_type="${prompt_[2]=prompt}"

# Modifying orders
for section in "${sections[@]}"; do
# Modifying orders
case "$prompt_type" in
prompt)
export SPACESHIP_PROMPT_ORDER=("${(@)SPACESHIP_PROMPT_ORDER:#${section}}")
;;
rprompt)
export SPACESHIP_RPROMPT_ORDER=("${(@)SPACESHIP_RPROMPT_ORDER:#${section}}")
;;
*)
echo "Unknown prompt type: $prompt_type"
echo "Available types: prompt, rprompt"
;;
esac
done
}

# ------------------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------------------

spaceship() {
local cmd="$1"

# Parse CLI options
zparseopts -D \
zparseopts -E -D \
h=help -help=help \
v=version -version=version

local cmd="$1"

if [[ -n $help ]]; then
_spaceship::cli::help "${(@)@:2}"
return 0
Expand All @@ -177,6 +256,10 @@ spaceship() {
return 1
}

# Omit the command from arguments
shift

# Call the subcommand function
_spaceship::cli::$cmd "$@"
}

Expand All @@ -189,12 +272,44 @@ _spaceship() {

cmds=(
'bug-report:Create a GitHub issue with information about your environment'
'add:Add a section to the prompt at specific position'
'remove:Remove a section from prompt'
'version:Print Spaceship version'
'help:Print this help message'
)

if (( CURRENT == 2 )); then
_describe 'commands' cmds
_describe 'command' cmds
elif (( CURRENT > 2 )); then
local -a sections
for section in ${(kM)functions:#spaceship_*}; do
sections+=("${section##spaceship_}")
done

case "$words[2]" in
add)
local -a subcmds=("$sections[@]")

subcmds+=("--prompt: A prompt to include the section to")
subcmds+=("-P: A prompt to include the section to")

subcmds+=("--after: A section to insert the section after")
subcmds+=("-A: A section to insert the section after")

subcmds+=("--before: A section to insert the section before")
subcmds+=("-B: A section to insert the section before")

_describe 'command' subcmds
;;
remove)
local -a subcmds=("$sections[@]")

subcmds+=("--prompt: A prompt to include the section to")
subcmds+=("-P: A prompt to include the section to")

_describe 'command' subcmds
;;
esac
fi

return 0
Expand Down
11 changes: 11 additions & 0 deletions lib/renderer.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ spaceship::refresh_section() {
fi
}

# Removes a section from both prompts and prints a message,
# so that we avoid printing errors over and over.
# USAGE:
# spaceship::skip_section <section>
spaceship::skip_section() {
local section="$1"
print -P "%F{yellow}Warning!%f The '%F{cyan}${section}%f' section was not found. Removing it from the prompt."
SPACESHIP_PROMPT_ORDER=("${(@)SPACESHIP_PROMPT_ORDER:#${section}}")
SPACESHIP_RPROMPT_ORDER=("${(@)SPACESHIP_RPROMPT_ORDER:#${section}}")
}

# Render and reset the prompt asyncronously.
# USAGE:
# spaceship::render
Expand Down

0 comments on commit 5dcce40

Please sign in to comment.