aboutsummaryrefslogtreecommitdiffstats
path: root/functions/sail.fish
blob: 69548b17432565a8b6ff6625e7400092d8d0d907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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