personal dotfiles: my Hyprland config
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/sh
# Author: Zach DeCook <zachdecook@librem.one>
# License: MIT
. sxmo_hook_icons.sh

## Data

ID_LIKE="$(sh -c '. /etc/os-release; echo "${ID_LIKE:-$ID}"')"

# Data comes from the apps hook.
# Outputs $icon_name\tbin_name
listApps(){
	grep ^write_line_app /usr/share/sxmo/default_hooks/*apps* | tr -d '"'| awk '{print $3 "\t" $2}'
}
# Categories should have 4-12 apps.
# These are the menu categories, with the icons which go with them.
Menus="$icon_msg Messaging	\(msg\|tgm\)
$icon_glb Browsing	\(ffx\|glb\)
$icon_eml Email	eml
$icon_edt Editing	\(edt\|vim\)
$icon_mus Multimedia	\(cam\|mic\|mus\|mvi\)
$icon_dir Files	dir
$icon_trm Terminal	trm
$icon_map Maps	\(gps\|map\)
$icon_itm Misc	\(and\|bok\|cfg\|chs\|clc\|clk\|img\|inf\|itm\|red\|rld\|rss\)
exit"

## Functions

pkgInfo(){
	pkg="$1"
	case "$ID_LIKE" in
		"alpine")
			apk info -dws "$pkg" | grep -v '^$' | grep -v ' webpage:' | grep -v 'installed size:'
			;;
		"debian")
			# apt has an unstable cli
			# lines starting with a space are additional description lines
			apt info "$pkg" | grep '\(Package\|Version\|Description\| \|\)' | sed 's/: /\t/g' | cut -f2
			;;
		"arch")
			# -Si means remote information
			pacman -Si "$pkg" | grep '^\(Name\|Version\|Description\|URL\)' | sed 's/ : /\t/g' | cut -f2
			;;
		*)
			printf "I don't know how to get package information in '%s' distros\n" "$ID_LIKE"
			;;
	esac
}
pkgInstall(){
	pkg="$1"
	case "$ID_LIKE" in
		"alpine")
			sxmo_terminal.sh doas apk add "$pkg"
			;;
		"debian")
			sxmo_terminal.sh doas apt-get install "$pkg"
			;;
		"arch")
			sxmo_terminal.sh doas pacman -Sy "$pkg"
			;;
		*)
			printf "I don't know how to install packages in '%s'.\n" "$ID_LIKE"
			;;
	esac
}
pkgRemove(){
	pkg="$1"
	case "$ID_LIKE" in
		"alpine")
			sxmo_terminal.sh doas apk del "$pkg"
			;;
		"debian")
			sxmo_terminal.sh doas apt-get remove "$pkg"
			;;
		"arch")
			sxmo_terminal.sh doas pacman -R "$pkg"
			;;
		*)
			printf "I don't know how to install packages in '%s'.\n" "$ID_LIKE"
			;;
	esac
}


# Takes in bin names and returns package names which provide those.
pkgNames(){
	case "$ID_LIKE" in
		"alpine")
			sed 's/^/cmd:/g' | xargs -r apk search -xqa | uniq
			;;
		"debian")
			if command -v apt-file; then
				#in my experience, apt-file is really slow...
				xargs -r -I{} apt-file find -Fl /usr/bin/{}
				# sed s'@^@/usr/bin/@g' | apt-file find -Flf /dev/stdin
			else
				printf "apt-file must be installed (and updated) to find packages which provide a certain bin.\n" > /dev/stderr
			fi
			;;
		"arch")
			sed 's@^@/usr/bin/@g' | xargs -r pacman -Fq | cut -d/ -f2
			;;
		*)
			printf "I don't know how to find package names from commands in '%s'.\n" "$ID_LIKE" > /dev/stderr
			;;
	esac
}


pkgInstalled(){
	case "$ID_LIKE" in
		"alpine")
			tee /tmp/appstoreapps | xargs -r apk info -e |sed 's/.\+/✅ \0/g' | cat - /tmp/appstoreapps |sed 's/^[a-z]/🟩 \0/g'| sort -i |uniq -f1
			;;
		*)
			cat - |sed 's/.\+/❓ \0/g'
			printf "I don't know how to check installed status from commands in '%s'.\n" "$ID_LIKE" > /dev/stderr
			;;
	esac
}

## Menus

topMenu(){
	sel="$(printf '%s' "$Menus" | cut -f1 | sxmo_dmenu.sh -i -p "App Store" |cut -d' ' -f2)"
	if ! test "$sel" || test "$sel" = "exit"; then
		exit 0
	fi
	menuName "$sel"
}

menuName(){
	name="$1"
	typ="$(echo "$Menus" | grep "$name	" | cut -f2)"
	# shellcheck disable=SC2016
	grep='^\$icon_'"$typ"'	'
	pkg="$(listApps | grep "$grep" |cut -f2|pkgNames|pkgInstalled|awk 'BEGIN{print ".."} ($0){print} END{print "exit"}'|sxmo_dmenu.sh -i -p "$name"|cut -d' ' -f2)"
	if test "$pkg" = "exit" || ! test "$pkg"; then
		exit
	elif test "$pkg" = ".."; then
		topMenu
	else
		pkgMenu "$pkg" "$name"
	fi
}

pkgMenu(){
	title="$(printf '%s' "$1"|cut -f1)"
	menu="..
install
uninstall
$(pkgInfo "$1")
exit"
	while true; do
	sel="$(printf '%s\n' "$menu"|sxmo_dmenu.sh -p "$title")"
	case "$sel" in
		"install")
			pkgInstall "$1"
			;;
		"uninstall")
			pkgRemove "$1"
			;;
		"..")
			menuName "$2"
			return
			;;
		"exit"|"")
			exit
			;;
		*"://"*)
			sxmo_open.sh "$sel"
			exit
			;;
		*)
			notify-send "$sel"
			;;
	esac
	done
}

## Execution
if test -n "$1"; then
	$1
else
	topMenu
fi