Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added chain.insert to add an item to chain at a specific index #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Remove the first link in the prompt chain.
### `chain.unshift <function>`
Add a _link function_ to the beginning of the prompt chain.

### `chain.insert <function> <position>`
Insert a _link function_ in front of the given position.

## Customization
Chain uses several global variables to customize the prompt appearance. The most important one is `$chain_links`: a list of function names that print out a single link in the prompt.
Expand Down
33 changes: 33 additions & 0 deletions functions/chain.insert.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function chain.insert -a command position -d 'Adds a link to your prompt chain at given position'

if test -z $position
echo "Position argument is required"
return 1
end

if not string match -qr '^-?\d+$' $position
echo "Position not an integer"
return 1
end

set -l current_length (count $chain_links)
if test $position -lt 0 -o $position -gt $current_length
echo "Invalid position"
return 1
end

if test $position -eq $current_length
set -U chain_links $chain_links "$command"
else if test $position -eq 0
set -U chain_links "$command" $chain_links
else
set -l before_slice $chain_links[1..$position]
set -l after_slice $chain_links[(math $position + 1)..-1]
set -U chain_links $before_slice "$command" $after_slice
end

# If the user runs this command, recompile the prompt for them so that their changes are immediately visible.
if test "$_" = chain.insert
chain.compile
end
end