aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWolfgang Müller2021-11-14 12:52:07 +0100
committerWolfgang Müller2021-11-14 12:52:07 +0100
commit6b8788d86328e97ebc7442fb1924d13ce50add56 (patch)
tree73c32fd83106a94759290b7b6b470cd68ff7c2a0
downloadsailfish-6b8788d86328e97ebc7442fb1924d13ce50add56.tar.gz
Initial commit
-rw-r--r--README.md17
-rw-r--r--completions/sail.fish16
-rw-r--r--functions/sail.fish64
3 files changed, 97 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..522247c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+# sailfish
+
+A simple directory bookmark manager for the [fish](https://fishshell.com/)
+shell.
+
+## Installation
+
+Merge both `functions/` and `completions/` into your `~/.config/fish/` or
+`$XDG_CONFIG_HOME/fish/` directory.
+
+## Usage
+
+- `sail [name]` goes to the directory specified by `name`. If no name is given,
+ go back to `$HOME`.
+- `sail -a name` adds a bookmark for the current directory.
+- `sail -e` opens the bookmark file with `$EDITOR`.
+- `sail -p` prints all saved bookmarks.
diff --git a/completions/sail.fish b/completions/sail.fish
new file mode 100644
index 0000000..bf87670
--- /dev/null
+++ b/completions/sail.fish
@@ -0,0 +1,16 @@
+complete -c sail -s a -l add -d 'Add a jump for the current directory'
+complete -c sail -s e -l edit -d 'Edit the jump file'
+complete -c sail -s p -l print -d 'Print the jump file'
+
+complete -x -c sail -a "(__sailfish_suggest)"
+
+function __sailfish_suggest
+ set -l jumps (__sailfish_print_jumpfile)
+
+ test -r $jumps; or return 1
+
+ set -l cmd (commandline -co)
+ test (count $cmd) -gt 1; and return 1
+
+ cat $jumps
+end
diff --git a/functions/sail.fish b/functions/sail.fish
new file mode 100644
index 0000000..69548b1
--- /dev/null
+++ b/functions/sail.fish
@@ -0,0 +1,64 @@
+function sail
+ argparse --name=sail 'a/add=' 'e/edit' 'p/print' -- $argv
+ or return 1
+
+ set -l jumpfile (__sailfish_print_jumpfile)
+
+ if set -q _flag_add
+ set -l dir (string replace -r "^$HOME" '~' (pwd))
+ printf '%s\t%s\n' $_flag_a $dir >> $jumpfile
+ printf '%s -> %s\n' $_flag_a $dir
+ return 0
+ end
+
+ if set -q _flag_edit
+ $EDITOR $jumpfile
+ return 0
+ end
+
+ if set -q _flag_print
+ sort $jumpfile | column -t
+ return 0
+ end
+
+ set query $argv[1]
+
+ if test -z $query
+ cd -
+ return 0
+ end
+
+ if not set -l jump (__sailfish_print_jump $query)
+ echo "sailfish: no such jump: $query"; return 1
+ end
+
+ cd $jump
+end
+
+function __sailfish_print_jumpfile
+ set -l jumps $HOME/.config/sailfish/jumps
+ set -q XDG_CONFIG_HOME; and set jumps $XDG_CONFIG_HOME/sailfish/jumps
+
+ if not test -r $jumps
+ mkdir -p (dirname $jumps); and touch $jumps
+ end
+
+ echo $jumps
+end
+
+function __sailfish_print_jump -a query
+ set -l jumps (__sailfish_print_jumpfile)
+
+ test -z $query; and return 1
+
+ while read -d \t -al jump
+ if test $jump[1] = $query
+ test -z $jump[2]; and continue
+
+ echo (string replace -r '^~' $HOME $jump[2])
+ return 0
+ end
+ end < $jumps
+
+ return 1
+end