@@ -11,32 +11,121 @@ 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+
71+ shopt -s dotglob
72+ for de in "$APPS"/*.desktop; do
73+ awk -F= -v name="$1" '
74+ BEGIN { in_section=0; n=0; e=0; nd=0 }
75+
76+ /^\[Desktop Entry\]/ { in_section=1; next }
77+ /^\[/ { in_section=0 }
78+
79+ in_section && /^Name=/ { n = ($2 == name) }
80+ in_section && /^Exec=/ { e = 1 }
81+ in_section && /^NoDisplay=/ { nd = ($2 == "true") }
82+
83+ END { exit !(n && e && !nd) }
84+ ' "$de" && {
85+ APP="$de"
86+ break
87+ }
88+ done
89+ shopt -u dotglob
90+
91+ if [[ -z "$APP" ]]; then
92+ echo "Trying to find application by filename..."
93+ if [[ -f "$APPS/$1.desktop" ]]; then
94+ APP="$APPS/$1.desktop"
95+ fi
96+ fi
97+ if [[ -z "$APP" ]]; then
98+ echo -e "'' ${RED}No application found with 'Name=$1' or filename '$1.desktop' '' ${ENDCOLOR}" >&2
99+ return 1
100+ fi
101+ echo "Found $APP"
102+ exec_cmd=$(extract_exec "$APP")
103+ if [[ -n "$exec_cmd" ]]; then
104+ echo "Starting application from $APP with command: $exec_cmd"
105+ eval "$exec_cmd '' ${*:2}"
106+ return 0
107+ else
108+ echo "Desktop entry $APP has no 'Exec' field" >&2
109+ return 1
110+ fi
111+ }
33112
34- if [ ! -e "$APPS/$1.desktop" ]; then
35- echo "No launcher entry for $1"
36- exit 1
113+ if [[ $# -lt 1 ] ]; then
114+ help_msg
115+ exit 0
37116 fi
38117
39- eval "$(awk '/^Exec=/{sub(/^Exec=/, ""); print}' "$APPS/$1.desktop") '' ${*:2}"
118+ case "$1" in
119+ -l | --list)
120+ list_apps
121+ ;;
122+ -h | --help)
123+ help_msg
124+ ;;
125+ *)
126+ run_app "$1" "'' ${*:2}"
127+ ;;
128+ esac
40129 '' ;
41130 meta = {
42131 description = "Open applications from the command line" ;
0 commit comments