@@ -6,145 +6,47 @@ set -Eo pipefail
6
6
DIR=" $( dirname " $( readlink -f " ${BASH_SOURCE[0]} " ) " ) "
7
7
8
8
# ------------------------- start ------------------------ #
9
- source " $DIR /lib/util.sh"
9
+ source " $DIR /lib/do.sh"
10
+ source " $DIR /lib/helper.sh"
10
11
source " $DIR /lib/plumbing.sh"
12
+ source " $DIR /lib/util.sh"
11
13
12
14
dbDir=" ${CHOOSE_DB_DIR:- ${XDG_CONFIG_HOME:- $HOME / .config} / choose/ db} "
13
15
14
16
main () {
15
- [ -z " $1 " ] && {
16
- util_show_help
17
- notify_die " $gui " ' No subcommand found'
18
- return
19
- }
20
-
21
- # TODO: cleanup
22
- # whether or not we are launching GUI selection interfaces
23
- local gui=no
24
- local ignoreErrors=no
25
- if [ " $1 " = " --gui" ] || [ " $2 " = " --gui" ]; then
26
- gui=yes
27
- shift
28
- fi
29
- if [ " $1 " = " --ignore-errors" ] || [ " $2 " = " --ignore-errors" ]; then
30
- ignoreErrors=yes
31
- shift
17
+ declare -A args=()
18
+ declare -a argsCommands=()
19
+
20
+ # shellcheck disable=SC1091
21
+ source bash-args parse " $@ " << -"EOF "
22
+ @arg launch - (category) Launches the default program in a particular category
23
+ @arg set - (category) (application) Sets the default program in a particular category
24
+ @flag [gui] - Whether to open a GUI
25
+ @flag [help.h] - Show help menu
26
+ @flag [version.v] - Show version
27
+ EOF
28
+
29
+ if [ -z " ${argsCommands[0]} " ]; then
30
+ printf " %s\n" " $argsHelpText "
31
+ log.die " $gui " ' No subcommand found'
32
32
fi
33
33
34
- case " $* " in
35
- * --gui* )
36
- notify_die " $gui " " Must place '--gui' as first arg"
37
- return ;;
38
- * --ignore-errors* )
39
- notify_die " $gui " " Must place '--ignore-errors' as first arg"
40
- return
41
- esac
42
-
34
+ local gui=" ${args[gui]} "
43
35
44
- case " $1 " in
36
+ case " ${argsCommands[0]} " in
45
37
set)
46
- shift
47
-
48
- local category=" $1 "
49
- local program=" $2 "
38
+ local category=" ${argsCommands[1]} "
39
+ local program=" ${argsCommands[2]} "
50
40
51
- category=" $( util_get_category " $category " " $gui " ) "
52
- ifCmdFailed " $? " && return
53
-
54
- program=" $( util_get_program " $category " " $program " " $gui " ) "
55
- ifCmdFailed " $? " && return
56
-
57
- # source pre-exec
58
- if [ -s " $dbDir /$category /set.sh" ]; then
59
- source " $dbDir /$category /set.sh"
60
- fi
61
-
62
- echo " $program " > | " $dbDir /$category /_.current"
63
- notify_info " $gui " " Category '$category ' defaults to '$program '"
41
+ do_set " $category " " $program " " $gui "
64
42
;;
65
43
launch)
66
- shift
67
-
68
- local category=" $1 "
69
-
70
- category=" $( util_get_category_filter " $category " " $gui " ) "
71
- ifCmdFailed " $? " && return
72
-
73
- # get variable
74
- [ ! -f " $dbDir /$category /_.current" ] && {
75
- notify_die " $gui " " Program for '$category ' is not set. Please set with 'choose set'"
76
- }
77
-
78
- program=" $( < " $dbDir /$category /_.current" ) "
79
-
80
- # ensure variable (we already use 'ensure_has_dot_current' in
81
- # util_get_category_filter; this is another safeguard)
82
- [ -z " $program " ] && {
83
- notify_die " $gui " " Program for '$category ' is not set. Please set with 'choose set'"
84
- return
85
- }
86
-
87
- # ------------------------ launch ------------------------ #
88
- # source pre-exec
89
- if [ -s " $dbDir /$category /launch.sh" ]; then
90
- source " $dbDir /$category /launch.sh"
91
- fi
92
-
93
- # if program file has content, it means
94
- # we manually set an execute command. source it
95
- if [ -s " $dbDir /$category /$program /launch.sh" ]; then
96
- source " $dbDir /$category /$program /launch.sh" || {
97
- die
98
- return
99
- }
100
- # if file does not have content, we raw exec it
101
- else
102
- # ensure variable is in the environment
103
- command -v " $program " & > /dev/null || {
104
- notify_die " $gui " " Executable '$program ' does not exist or is not in the current environment"
105
- return
106
- }
107
-
108
- exec " $program "
109
- fi
110
- ;;
111
- get)
112
- shift
113
-
114
- local category=" $1 "
115
-
116
- category=" $( util_get_category_filter " $category " " $gui " ) "
117
- ifCmdFailed " $? " && return
118
-
119
- # get variable
120
- # TODO: cleanup
121
- [ ! -f " $dbDir /$category /_.current" ] && [ " $ignoreErrors " = no ] && {
122
- notify_die " $gui " " Program for '$category ' is not set. Please set with 'choose set'"
123
- }
124
-
125
- program=" $( < " $dbDir /$category /_.current" ) "
126
-
127
- # ensure variable (we already use 'ensure_has_dot_current' in
128
- # util_get_category_filter; this is another safeguard)
129
- [ -z " $program " ] && [ " $ignoreErrors " = no ] && {
130
- notify_die " $gui " " Program for '$category ' is not set. Please set with 'choose set'"
131
- return
132
- }
133
-
134
- # source pre-exec
135
- if [ -s " $dbDir /$category /get.sh" ]; then
136
- source " $dbDir /$category /get.sh"
137
- fi
44
+ local category=" ${argsCommands[1]} "
138
45
139
- printf " %s " " $program "
46
+ do_launch " $category " " $gui "
140
47
;;
141
- --help)
142
- util_show_help
48
+ print)
143
49
;;
144
- * )
145
- notify_die " $gui " " Subcommand not found"
146
- return
147
- ;;
148
50
esac
149
51
}
150
52
0 commit comments