@@ -11,32 +11,106 @@ writeShellApplication {
1111 runtimeInputs = [ gawk ] ;
1212 text = ''
1313 APPS=/run/current-system/sw/share/applications
14+ RED="\e[31m"
15+ ENDCOLOR="\e[0m"
1416
15- function list_apps () {
16- for e in "$APPS"/*.desktop; do
17- [[ -e "$e" ]] || continue # in case of no entries
17+ help_msg () {
18+ cat <<EOF
19+ Usage: $(basename "$0") [OPTIONS] [APPLICATION] [ARGS...]
1820
19- basename "$e" .desktop
20- done
21+ Options:
22+ -l, --list List available applications by their Name field and exit.
23+ -h, --help Show this help message and exit.
24+
25+ Examples:
26+ $(basename "$0") slack
27+ $(basename "$0") --list
28+ EOF
2129 }
2230
23- if [ $# -eq 0 ]; then
24- echo -e "Usage: ghaf-open <-l|application> [args...]\n"
25- echo -e "\t-l\tList available applications"
26- exit 1
27- fi
31+ extract_exec() {
32+ awk -F= '
33+ /^Exec=/ {
34+ cmd=$2
35+ gsub(/ %[fFuUdDnNickvm]/, "", cmd)
36+ print cmd
37+ exit
38+ }
39+ ' "$1"
40+ }
2841
29- if [ "$1" = "-l" ]; then
30- list_apps
31- exit 0
32- fi
42+ list_apps() {
43+ for de in "$APPS"/*.desktop; do
44+ # Skip entries without Exec field
45+ grep -qE '^Exec=.+$' "$de" || continue
46+ # Skip entries with NoDisplay=true
47+ grep -q 'NoDisplay=true' "$de" && continue
48+ # Prefer Name, fall back to filename
49+ name=$(awk -F= '
50+ /^Name=/ {
51+ print $2
52+ found=1
53+ exit
54+ }
55+ END {
56+ if (!found) {
57+ gsub(/^.*\//, "", FILENAME)
58+ gsub(/\.desktop$/, "", FILENAME)
59+ print FILENAME
60+ }
61+ }
62+ ' "$de")
63+ echo "$name"
64+ done
65+ }
66+
67+ run_app() {
68+ APP=
69+ echo "Trying to find application by Name..."
70+ for de in "$APPS"/*.desktop; do
71+ if grep -qiE "^Name=$1$" "$de" && grep -qE "^Exec=.+" "$de"; then
72+ APP="$de"
73+ break
74+ fi
75+ done
76+ if [[ -z "$APP" ]]; then
77+ echo "Trying to find application by filename..."
78+ if [[ -f "$APPS/$1.desktop" ]]; then
79+ APP="$APPS/$1.desktop"
80+ fi
81+ fi
82+ if [[ -z "$APP" ]]; then
83+ echo -e "'' ${RED}No application found with 'Name=$1' or filename '$1.desktop' '' ${ENDCOLOR}" >&2
84+ return 1
85+ fi
86+ echo "Found $APP"
87+ exec_cmd=$(extract_exec "$APP")
88+ if [[ -n "$exec_cmd" ]]; then
89+ echo "Starting application from $APP with command: $exec_cmd"
90+ eval "$exec_cmd '' ${*:2}"
91+ return 0
92+ else
93+ echo "Desktop entry $APP has no 'Exec' field" >&2
94+ return 1
95+ fi
96+ }
3397
34- if [ ! -e "$APPS/$1.desktop" ]; then
35- echo "No launcher entry for $1"
36- exit 1
98+ if [[ $# -lt 1 ] ]; then
99+ help_msg
100+ exit 0
37101 fi
38102
39- eval "$(awk '/^Exec=/{sub(/^Exec=/, ""); print}' "$APPS/$1.desktop") '' ${*:2}"
103+ case "$1" in
104+ -l | --list)
105+ list_apps
106+ ;;
107+ -h | --help)
108+ help_msg
109+ ;;
110+ *)
111+ run_app "$1" "'' ${*:2}"
112+ ;;
113+ esac
40114 '' ;
41115 meta = {
42116 description = "Open applications from the command line" ;
0 commit comments