Skip to content

Commit d423609

Browse files
committed
partial rewrite
1 parent 3bf2341 commit d423609

File tree

9 files changed

+307
-324
lines changed

9 files changed

+307
-324
lines changed

bpm.toml

Whitespace-only changes.

choose

Lines changed: 0 additions & 1 deletion
This file was deleted.

choose.sh

Lines changed: 26 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -6,145 +6,47 @@ set -Eo pipefail
66
DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
77

88
# ------------------------- start ------------------------ #
9-
source "$DIR/lib/util.sh"
9+
source "$DIR/lib/do.sh"
10+
source "$DIR/lib/helper.sh"
1011
source "$DIR/lib/plumbing.sh"
12+
source "$DIR/lib/util.sh"
1113

1214
dbDir="${CHOOSE_DB_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/choose/db}"
1315

1416
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'
3232
fi
3333

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]}"
4335

44-
case "$1" in
36+
case "${argsCommands[0]}" in
4537
set)
46-
shift
47-
48-
local category="$1"
49-
local program="$2"
38+
local category="${argsCommands[1]}"
39+
local program="${argsCommands[2]}"
5040

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"
6442
;;
6543
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]}"
13845

139-
printf "%s" "$program"
46+
do_launch "$category" "$gui"
14047
;;
141-
--help)
142-
util_show_help
48+
print)
14349
;;
144-
*)
145-
notify_die "$gui" "Subcommand not found"
146-
return
147-
;;
14850
esac
14951
}
15052

convert.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
3+
# @brief Fixes the choose 'database' by converting the schema from
4+
# version 1 to 2
5+
6+
set -e
7+
shopt -s nullglob extglob globstar
8+
9+
main() {
10+
local dbDir="${CHOOSE_DB_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/choose/db}"
11+
12+
for categoryDir in "$dbDir"/*; do
13+
if [ ! -d "$categoryDir" ]; then
14+
printf "%s\n" "Warning: Category dir '$categoryDir' is not a directory. Skipping"
15+
continue
16+
fi
17+
18+
categoryHasCurrent=no
19+
for programDir in "$categoryDir"/*; do
20+
# Special case '_.current'
21+
if [ "${programDir##*/}" = "_.current" ]; then
22+
categoryHasCurrent=yes
23+
24+
if [ -d "$programDir" ]; then
25+
printf "%s\n" "Warning: '$programDir' is not a file. Converting"
26+
27+
rmdir "$programDir"
28+
mkdir -p "${programDir%/*}"
29+
touch "$programDir"
30+
31+
fi
32+
33+
continue
34+
fi
35+
36+
# General case
37+
if [ ! -d "$programDir" ]; then
38+
printf "%s\n" "Warning: Program dir '$programDir' is not a directory. Converting"
39+
40+
rm "$programDir"
41+
mkdir -p "$programDir"
42+
43+
continue
44+
fi
45+
46+
if [ ! -f "$programDir/launch.sh" ]; then
47+
touch"$programDir/launch.sh"
48+
fi
49+
50+
for programScript in "$programDir"/*; do
51+
if [ ! -f "$programScript" ]; then
52+
printf "%s\n" "Warning: Program script '$programScript' is not a file. Skipping"
53+
continue
54+
fi
55+
56+
if [[ "$programScript" == *@(\{|\})* ]]; then
57+
printf "%s\n" "Warning: program script '$programScript' has malformated name. Skipping"
58+
continue
59+
fi
60+
done
61+
done
62+
63+
if [ "$categoryHasCurrent" = no ]; then
64+
touch "$categoryDir/_.current"
65+
fi
66+
done
67+
}
68+
69+
main "$@"

docs/details.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Details

lib/do.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# shellcheck shell=bash
2+
3+
do_set() {
4+
local category="$1"
5+
local program="$2"
6+
local gui="$3"
7+
8+
# Get category
9+
if ! helper_get_category "$category" "$gui"; then
10+
log.die "$gui" "Could not get category"
11+
fi
12+
category="$REPLY"
13+
14+
# Get program
15+
if ! helper_get_program "$category" "$program" "$gui"; then
16+
log.die "$gui" "Could not get program"
17+
fi
18+
program="$REPLY"
19+
20+
# Actually set
21+
printf "%s" "$program" >| "$dbDir/$category/_.current"
22+
23+
log.info "Category '$category' defaults to '$program'"
24+
}
25+
26+
do_launch() {
27+
local category="$1"
28+
local gui="$2"
29+
30+
if ! helper_get_category_filter "$category" "$gui"; then
31+
exit 1
32+
fi
33+
category="$REPLY"
34+
35+
# Check to ensure category is set
36+
if [ ! -f "$dbDir/$category/_.current" ]; then
37+
log.die "$gui" "Program for '$category' is not set. Please set with 'choose set'"
38+
fi
39+
40+
program="$(<"$dbDir/$category/_.current")"
41+
42+
# ensure variable (we already use 'ensure_has_dot_current' in
43+
# helper_get_category_filter; this is another safeguard)
44+
if [ -z "$program" ]; then
45+
log.die "$gui" "Program for '$category' is not set. Please set with 'choose set'"
46+
fi
47+
48+
# ------------------------ launch ------------------------ #
49+
# Source category pre
50+
if [ -f "$dbDir/$category/pre.sh" ]; then
51+
source "$dbDir/$category/pre.sh"
52+
fi
53+
54+
# Source program pre
55+
if [ -f "$dbDir/$category/$program/pre.sh" ]; then
56+
source "$dbDir/$category/$program/pre.sh"
57+
fi
58+
59+
# Source launch if it exists. If otherwise, infer
60+
# the launch command from the program name
61+
if [ -f "$dbDir/$category/$program/launch.sh" ]; then
62+
if ! source "$dbDir/$category/$program/launch.sh"; then
63+
log.die "$gui" "Source failed"
64+
fi
65+
else
66+
if ! command -v "$program" &>/dev/null; then
67+
log.die "$gui" "Executable '$program' does not exist or is not in the current environment"
68+
fi
69+
70+
exec "$program"
71+
fi
72+
73+
# Source program post
74+
if [ -f "$dbDir/$category/$program/post.sh" ]; then
75+
source "$dbDir/$category/$program/post.sh"
76+
fi
77+
78+
# Source category post
79+
if [ -f "$dbDir/$category/post.sh" ]; then
80+
source "$dbDir/$category/post.sh"
81+
fi
82+
}

0 commit comments

Comments
 (0)