-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolybar.lib
More file actions
executable file
·220 lines (190 loc) · 6.31 KB
/
polybar.lib
File metadata and controls
executable file
·220 lines (190 loc) · 6.31 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env bash
#
# ############################################################################
# Project: polybarctl (vundefined)
# File...: polybar.lib
# Created: Monday, 2023/03/06 - 01:29:51
# Author.: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Sunday, 2023/03/26 - 20:03:29
# Modified By..: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 0.0.17.478
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
_xLIB_POLYBAR_=true
PHOME="$HOME/.config/polybar"
xrequirements mkdir mv rm touch ls cat rev cut killall pgrep wc grep tr
is_installed(){
[ -f "$PHOME/polybarctl" ] && return 0 || return 1
}
pInstall(){
local themes_repo=https://github.com/adi1090x/polybar-themes
local tmp=/tmp/polybar-themes
if [ ! -f "$PHOME/polybarctl" ]; then
echo -e "installing polybarctl"
touch "$PHOME"/polybarctl
echo -e "downloading themes"
git clone "$themes_repo" "$tmp"
ls -d $tmp/simple/* > "$PHOME"/polybarctl
mv $tmp/simple/* "$PHOME"/
mv $tmp/wallpapers/ "$PHOME"/
echo -e "install fonts"
[ ! -d "$HOME/.local/fonts" ] && mkdir -p "$HOME"/.local/fonts
mv $tmp/fonts/* "$HOME/.local/fonts/"
echo -e "remove temporary files"
rm -rf $tmp
else
echo -e "${RED}Error! 'polybar-themes' already installed${RESET}"
fi
}
pList(){
# function to print a formatted msg
print_msg(){
# shellcheck disable=2034 # declare and assing var
local text=$1; local selected_color=${2:-$YELLOW}
echo -e "${selected_color} $1${RESET}"
}
# get last item on a line sepated by '/'
last_item(){
echo "$1" | rev | cut -d '/' -f1 | rev
}
# cmd to list all saved themes
local cmd="cat $PHOME/polybarctl | grep -v 'launch.sh'"
# check if polybarctl is already installed
if is_installed; then
THEMES=$(eval "$cmd" 2> /dev/null)
print_msg "\n${BLUE}-> Available themes: ${GREEN}($( eval "$cmd" | wc -l))${RESET}"
for t in $THEMES; do
_current_theme=$(last_item "$t")
if [ "$_current_theme" != "panels" ]; then
print_msg " * $_current_theme"
else
print_msg " * $_current_theme"
for p in "$PHOME/$_current_theme"/panel/*; do
print_msg " \`-> $(last_item "${p//.ini/}")" "$CYAN"
done
fi
done
else
echo -e "${RED}Error! themes not installed yet.${RESET}"
fi
}
pUninstall(){
if is_installed; then
# shellcheck disable=SC2155,SC2002
# ignore declare and assign
# Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
local toRemove=$(cat "$PHOME/polybarctl" | rev | cut -d '/' -f1 | rev)
for item in $toRemove; do
echo "rm -rf $PHOME/$item"
# shellcheck disable=2115 # Use \"${var:?}\" to ensure this never expands to / .
rm -rf "$PHOME/$item"
done
for item in themes wallpapers polybarctl; do
# shellcheck disable=2115 # Use \"${var:?}\" to ensure this never expands to / .
rm -rf "$PHOME/$item"
done
echo "$APP: themes uninstalled"
else
echo -e "${RED}Error! Unable to uninstall (not installed).${RESET}"
fi
}
pQuit(){
# Terminate already running bar instances
killall -q polybar
# Wait until the processes have been shut down
while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done
}
pRun(){
local theme=$1 # current theme
local theme_bars='' # bars in this theme
local ttype='' # theme type (dir or file) -> panels theme
local subtheme='' # used to run subitems -> panels theme
local config_file='' # polybar config file
_theme_exists(){
if [ -d "$PHOME/$theme" ]; then
ttype=d
true
return 0
else
subtheme="$(echo "$theme" | cut -d '/' -f2)"
if [ -f "$PHOME/panels/panel/$subtheme.ini" ]; then
ttype=f
true
return 0
else
return 1
fi
fi
}
_get_bars(){
# command to get all bars in a polybar configfile
local _cmd="grep 'bar/' <file> | grep -v '=' | cut -d '/' -f2 | tr -d ']'"
# replace '<file>' with theme config
case "$ttype" in
d) _cmd=${_cmd/<file>/$PHOME\/$theme\/config.ini} ;;
f) _cmd=${_cmd/<file>/$PHOME\/panels\/panel\/$subtheme.ini} ;;
esac
eval "$_cmd"
}
local error_msg="";
# themes installed?
if is_installed; then
# this is a valid theme?
if _theme_exists "$theme"; then
# kill all polybar instances
pQuit
theme_bars=$(_get_bars)
echo -e "starting bars:\n'$theme_bars'"
# run all bars defined in theme
for bar in $theme_bars; do
case "$ttype" in
d) config_file="$PHOME"/"$theme"/config.ini ;;
f) config_file="$PHOME/panels/panel/$subtheme.ini" ;;
esac
# start polybar
polybar -q "$bar" -c "$config_file" &
done
else
error_msg+="${RED}Error! theme '$theme' not found.${RESET}"
fi
fi
# is a file? AND had error loading theme?
# try to run polybar
if [ -f "$theme" ] && [ -n "$error_msg" ]; then
# quit all polybar instances
pQuit
# run new config
polybar -c "$theme" "$bar" &
else
# not a valid file either? Write error message
echo -e "${RED}Error! Themes isn't installed. Configfile '$theme' not found.${RESET}"
fi
}
# show polybar status on system
pStatus(){
count_themes(){
pList | grep -vc 'Available'
}
polybar_version(){
polybar -v | head -1 | cut -d ' ' -f2
}
pb_current_config(){
# shellcheck disable=SC2009 # Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
ps -aux | grep polybar | grep -v 'grep\|polybarctl' | aw 13
}
echo -e "${BLUE}
############# POLYBARCTL #############\n\
STATUS
${MAGENTA}polybar version.: ${YELLOW}$(polybar_version)${RESET}
${MAGENTA}current config..: ${YELLOW}$(pb_current_config)${RESET}
${MAGENTA}themes installed: ${YELLOW}$(is_installed && echo -n true; echo " ($(count_themes))" || echo false) ${RESET}
${BLUE}
############# END #############\
${RESET}"
}