-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogibar-keyboard
More file actions
executable file
·157 lines (139 loc) · 6.49 KB
/
Copy pathlogibar-keyboard
File metadata and controls
executable file
·157 lines (139 loc) · 6.49 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
#!/bin/bash
# Logitech G915 X TKL Battery Widget for Waybar
#
# Usage: logibar-keyboard [--color-normal HEX] [--color-warning HEX] [--color-critical HEX]
STATE_FILE="${XDG_RUNTIME_DIR:-/run/user/$UID}/logibar/keyboard"
ICON=""
ICON_CHARGING=" ⚡"
TOOLTIP="G915 X TKL"
# --- Parse args ---
CLI_COLOR_NORMAL=""
CLI_COLOR_WARNING=""
CLI_COLOR_CRITICAL=""
FRAME=false
FRAME_FONT="JetBrainsMono Nerd Font Mono"
while [[ $# -gt 0 ]]; do
case "$1" in
--color-normal) [[ $# -ge 2 ]] || { echo '{"text":"⚠","tooltip":"--color-normal requires a value","class":"critical"}'; exit 0; }
CLI_COLOR_NORMAL="$2"; shift 2 ;;
--color-warning) [[ $# -ge 2 ]] || { echo '{"text":"⚠","tooltip":"--color-warning requires a value","class":"critical"}'; exit 0; }
CLI_COLOR_WARNING="$2"; shift 2 ;;
--color-critical) [[ $# -ge 2 ]] || { echo '{"text":"⚠","tooltip":"--color-critical requires a value","class":"critical"}'; exit 0; }
CLI_COLOR_CRITICAL="$2"; shift 2 ;;
--frame) FRAME=true; shift ;;
--frame-font) [[ $# -ge 2 ]] || { echo '{"text":"⚠","tooltip":"--frame-font requires a value","class":"critical"}'; exit 0; }
FRAME_FONT="$2"; shift 2 ;;
*) _o="${1//\\/\\\\}"; _o="${_o//\"/\\\"}"; echo '{"text":"⚠","tooltip":"Unknown option: '"$_o"'","class":"critical"}'; exit 0 ;;
esac
done
# --- Colors: CLI overrides > Omarchy theme > One Dark defaults ---
COLOR_NORMAL="#98c379"
COLOR_WARNING="#e5c07b"
COLOR_CRITICAL="#e06c75"
ACCENT="#61afef"
FG="#abb2bf"
BG="#282c34"
hex_blend() { # average two #RRGGBB colors
local c1="${1#\#}" c2="${2#\#}"
printf '#%02x%02x%02x' \
$(( (16#${c1:0:2} + 16#${c2:0:2}) / 2 )) \
$(( (16#${c1:2:2} + 16#${c2:2:2}) / 2 )) \
$(( (16#${c1:4:2} + 16#${c2:4:2}) / 2 ))
}
_theme="$HOME/.config/omarchy/current/theme/colors.toml"
if [[ -f "$_theme" ]]; then
while IFS= read -r line; do
[[ "$line" =~ ^[[:space:]]*# ]] && continue
[[ -z "${line// /}" ]] && continue
if [[ "$line" =~ ^[[:space:]]*([a-zA-Z_][a-zA-Z0-9_]*)[[:space:]]*=[[:space:]]*\"([^\"]*)\" ]]; then
case "${BASH_REMATCH[1]}" in
color1) COLOR_CRITICAL="${BASH_REMATCH[2]}" ;;
color2) COLOR_NORMAL="${BASH_REMATCH[2]}" ;;
color3) COLOR_WARNING="${BASH_REMATCH[2]}" ;;
accent) ACCENT="${BASH_REMATCH[2]}" ;;
foreground) FG="${BASH_REMATCH[2]}" ;;
background) BG="${BASH_REMATCH[2]}" ;;
esac
fi
done < "$_theme"
fi
[[ -n "$CLI_COLOR_NORMAL" ]] && COLOR_NORMAL="$CLI_COLOR_NORMAL"
[[ -n "$CLI_COLOR_WARNING" ]] && COLOR_WARNING="$CLI_COLOR_WARNING"
[[ -n "$CLI_COLOR_CRITICAL" ]] && COLOR_CRITICAL="$CLI_COLOR_CRITICAL"
DIM=$(hex_blend "$FG" "$BG")
BAR_EMPTY=$(hex_blend "$BG" "$DIM")
# --- Read state ---
if [[ ! -f "$STATE_FILE" ]]; then
echo '{"text": ""}'
exit 0
fi
BATTERY=$(sed -n '1p' "$STATE_FILE")
CONNECTED=$(sed -n '2p' "$STATE_FILE")
CHARGING=$(sed -n '3p' "$STATE_FILE")
if [[ -z "$BATTERY" ]] || [[ "$BATTERY" == "0" ]] || [[ "$CONNECTED" != "1" ]]; then
echo '{"text": ""}'
exit 0
fi
# --- Severity → class + bar color (bar color is always set, for the gauge) ---
if [[ $BATTERY -le 10 ]]; then
CLASS="critical"; COLOR="$COLOR_CRITICAL"; BAR_COLOR="$COLOR_CRITICAL"
elif [[ $BATTERY -le 20 ]]; then
CLASS="warning"; COLOR="$COLOR_WARNING"; BAR_COLOR="$COLOR_WARNING"
else
CLASS="normal"; COLOR=""; BAR_COLOR="$COLOR_NORMAL"
fi
# --- Bar text (the waybar module label) ---
if [[ "$CHARGING" == "1" ]]; then DISPLAY_ICON="$ICON_CHARGING"; else DISPLAY_ICON="$ICON"; fi
if [[ -n "$COLOR" ]]; then
BATTERY_TEXT="<span size='small' foreground='${COLOR}'>$BATTERY%</span>"
else
BATTERY_TEXT="<span size='small'>$BATTERY%</span>"
fi
# --- Tooltip: framed battery card (family style) ---
strip_tags() { sed 's/<[^>]*>//g' <<< "$1"; }
BAR_LEN=14
filled=$(( BATTERY * BAR_LEN / 100 ))
(( filled > BAR_LEN )) && filled=$BAR_LEN
(( filled < 0 )) && filled=0
printf -v _bf '%*s' "$filled" ''
printf -v _be '%*s' "$(( BAR_LEN - filled ))" ''
gauge="<span foreground='${BAR_COLOR}'>${_bf// /█}</span><span foreground='${BAR_EMPTY}'>${_be// /░}</span>"
if [[ "$CHARGING" == "1" ]]; then
status="<span foreground='${COLOR_NORMAL}'>$(printf '\U000F0084') Charging</span>"
else
status="<span foreground='${DIM}'>$(printf '\U000F007E') On battery</span>"
fi
title="<span font_weight='bold' foreground='${ACCENT}'>${ICON} ${TOOLTIP}</span>"
row_bar=" ${gauge} <span font_weight='bold' foreground='${BAR_COLOR}'>${BATTERY}%</span>"
row_status=" ${status}"
INNER=0
for _ln in "$title" "$row_bar" "$row_status"; do
_p=$(strip_tags "$_ln"); (( ${#_p} > INNER )) && INNER=${#_p}
done
INNER=$(( INNER + 1 ))
_pad() { local _p; _p=$(strip_tags "$1"); local n=$(( INNER - ${#_p} )); (( n < 0 )) && n=0; local s; printf -v s '%*s' "$n" ''; printf '%s%s' "$1" "$s"; }
_cpad() { local _p; _p=$(strip_tags "$1"); local n=$(( INNER - ${#_p} )); (( n < 0 )) && n=0; local l=$(( n/2 )) r=$(( n - n/2 )) ls rs; printf -v ls '%*s' "$l" ''; printf -v rs '%*s' "$r" ''; printf '%s%s%s' "$ls" "$1" "$rs"; }
B="<span foreground='${ACCENT}'>"; E="</span>"; NL=$'\n'
printf -v _border '%*s' "$INNER" ''; _border="${_border// /─}"
printf -v _sepd '%*s' "$(( INNER - 2 ))" ''; _sepd="${_sepd// /─}"
if [[ "$FRAME" == "true" ]]; then
# Framed: box pinned to a Mono Nerd Font so text/box-drawing/icons share one
# uniform advance — alignment holds regardless of the user's bar font.
TIP="${B}╭${_border}╮${E}${NL}"
TIP+="${B}│${E}$(_cpad "$title")${B}│${E}${NL}"
TIP+="${B}│${E}$(_pad " <span foreground='${DIM}'>${_sepd}</span>")${B}│${E}${NL}"
TIP+="${B}│${E}$(_pad "$row_bar")${B}│${E}${NL}"
TIP+="${B}│${E}$(_pad "$row_status")${B}│${E}${NL}"
TIP+="${B}╰${_border}╯${E}"
_ff=$(sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/'\''/\'/g' <<< "$FRAME_FONT")
TIP="<span font_family='${_ff}'>${TIP}</span>"
else
# Plain (default): no border, no font pin → renders in the user's font.
# Leading space aligns the title with the rule below it.
TIP=" ${title}${NL}"
TIP+=" <span foreground='${DIM}'>${_sepd}</span>${NL}"
TIP+="${row_bar}${NL}"
TIP+="${row_status}"
fi
jq -nc --arg t "$DISPLAY_ICON ${BATTERY_TEXT}" --arg tip "$TIP" --arg class "$CLASS" --argjson pct "$BATTERY" \
'{text:$t, tooltip:$tip, class:$class, percentage:$pct}'