-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch
More file actions
executable file
·158 lines (133 loc) · 3.39 KB
/
launch
File metadata and controls
executable file
·158 lines (133 loc) · 3.39 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
TEMP=$(getopt --name 'launch' --options 'ahr' --long 'add,help,remove' -- "$@")
STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/launch"
STATE_FILE="$STATE_DIR/state.json"
eval set -- "$TEMP"
unset TEMP
# if state file doesn't exist, create it
[ ! -e "$STATE_FILE" ] && mkdir -p "$STATE_DIR" && echo '[]' >"$STATE_FILE"
main() {
_parse_options "$@"
local name=$(_select_project)
local options=("Launch" "Exit")
local result
read -r result < <(
printf '%s\0' "${options[@]}" |
fzf --read0 --print0 --reverse --header="Select an option" --header-first
)
case "$result" in
"Launch")
local dir=$(jq -r --arg name "$name" '.[] | select(.name == $name) | .dir' "$STATE_FILE")
[ -d "$dir" ] && code "$dir"
exit 0
;;
"Exit")
exit 0
;;
esac
}
add() {
local start="${1:-$HOME/projects}"
local dir
read -r dir < <(
cd "$start"
ls -1 -d */ |
fzf --reverse --header="Select a project" --header-first |
tr -d "/"
)
local options=("Add to launch manager" "Select sub directory" "Undo" "Exit")
local result
read -r result < <(
printf '%s\0' "${options[@]}" |
fzf --read0 --print0 --reverse --header="Select an option" --header-first
)
local path="$start/$dir"
case "$result" in
"Add to launch manager")
local name="${path#"$HOME/projects/"}/"
# check if the directory already exists in the state file
if jq -e --arg name "$name" '.[] | select(.name == $name)' "$STATE_FILE" &>/dev/null; then
echo "Directory already exists in the state file"
exit 1
fi
jq --arg name "$name" --arg dir "$path" '. += [{"name": $name, "dir": $dir}]' "$STATE_FILE" >"$STATE_FILE.tmp" &&
mv "$STATE_FILE.tmp" "$STATE_FILE"
echo "Added $name to launch manager"
exit 0
;;
"Select sub directory")
add "$path"
;;
"Undo")
add "$start"
;;
"Exit")
exit 0
;;
esac
}
help() {
echo "Usage: launch [OPTION]"
echo " -a, --add Add a project to the launch manager"
echo " -h, --help Show this help message"
echo " -r, --remove Remove a project from the launch manager"
exit 0
}
remove() {
local name=$(_select_project)
local options=("Remove from launch manager" "Exit")
local result
read -r result < <(
printf '%s\0' "${options[@]}" |
fzf --read0 --print0 --reverse --header="Select an option" --header-first
)
if [[ "$result" == "Remove from launch manager" ]]; then
jq --arg name "$name" 'del(.[] | select(.name == $name))' "$STATE_FILE" >"$STATE_FILE.tmp" &&
mv "$STATE_FILE.tmp" "$STATE_FILE"
echo "Removed $name from launch manager"
exit 0
else
exit 0
fi
}
# Check if the required packages are installed on the system.
_check_requirements() {
local packages="$@"
for package in $packages; do
if ! command -v $package &>/dev/null; then
echo "$package is not installed on your system"
echo "Please install it and try again"
exit 1
fi
done
}
_parse_options() {
while true; do
case "$1" in
-a | --add)
add
break
;;
-h | --help)
help
break
;;
-r | --remove)
remove
break
;;
--)
shift
break
;;
*)
break
;;
esac
done
}
_select_project() {
jq -r 'map(.name) | sort | .[]' "$STATE_FILE" | fzf --reverse --header="Select a project" --header-first
}
_check_requirements fzf jq
main "$@"