-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellmarks
More file actions
executable file
·96 lines (80 loc) · 2.01 KB
/
shellmarks
File metadata and controls
executable file
·96 lines (80 loc) · 2.01 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
#
## Copyright 2021-2024 NRK
## Licensed under GPL v3. See LICENSE for more details.
### Config ###
# shellmarks directory. uses ~/.local/share/shellmarks if not set
SH_MARKS="${SH_MARKS:-${XDG_DATA_HOME:-$HOME/.local/share}/shellmarks}"
# colors
# For a list of ANSI color codes, check the link below
# https://gist.github.com/Prakasaka/219fe5695beeb4d6311583e79933a009
col_error="\033[1;31m" # Red
col_name="\033[1;33m" # Yellow
col_path="\033[0;34m" # Blue
col_reset="\033[m" # Don't change this
### Functions ###
die() {
[ -n "$1" ] &&
printf "${col_error}%s${col_reset}\n" "$@" >&2
exit 1
}
# expects full file path
symlink_resolve() {
cd "$1" && pwd -P
}
symlink_or_die() {
[ -e "${SH_MARKS}/${1}" ] || die "$1 doesn't exist"
[ -h "${SH_MARKS}/${1}" ] || die "$1 doesn't appear to be a symlink..."
[ -d "${SH_MARKS}/${1}" ] || die "$1 doesn't appear to be a directory..."
}
bk_add() {
[ -z "$1" ] && die "No name given"
[ -e "${SH_MARKS}/${1}" ] && die "$1 already exists"
target="${2:-$PWD}"
ln -s "$target" "${SH_MARKS}/${1}"
}
bk_remove() {
[ -z "$1" ] && die "Nothing to remove"
symlink_or_die "$1"
rm -f "${SH_MARKS}/${1}"
}
bk_path() {
[ -z "$1" ] && die "No argument given"
symlink_or_die "$1"
symlink_resolve "${SH_MARKS}/${1}"
}
bk_list() {
[ -z "$1" ] && arrow=" -> "
for file in "$SH_MARKS"/*; do
[ -e "$file" ] && [ -h "$file" ] &&
printf "${col_name}%s${arrow}${col_path}${1}" "${file##*/}" &&
[ -z "$1" ] && symlink_resolve "$file"
done
printf "$col_reset"
}
### main ###
[ -d "$SH_MARKS" ] ||
mkdir -p "$SH_MARKS" ||
die "Cannot create directory $SH_MARKS"
if [ "$1" = "-c" ] || [ "$NO_COLOR" != "" ]; then
[ "$1" = "-c" ] && shift
unset col_error col_name col_path col_reset
fi
case "$1" in
"add")
bk_add "$2" "$3"
;;
"rm")
bk_remove "$2"
;;
"path")
bk_path "$2"
;;
"ls")
[ "$2" = "-n" ] && name_only="\n"
bk_list "$name_only"
;;
*)
die "INVALID COMMAND: $1"
;;
esac