Initial commit

This commit is contained in:
ItsDrike 2022-10-29 20:25:42 +02:00
parent b912871070
commit a3e01caebf
No known key found for this signature in database
GPG key ID: B014E761034AF742
157 changed files with 9696 additions and 0 deletions

View file

@ -0,0 +1,11 @@
[flake8]
max-line-length=119
extend-ignore=E203
extend-select=B902,B904
exclude=.venv,.git,.cache
ignore=
ANN002, # *args annotation
ANN003, # **kwargs annotation
ANN101, # self param annotation
ANN102, # cls param annotation
ANN204, # return type annotation for special methods

View file

@ -0,0 +1,79 @@
#!/bin/bash
# shellcheck source=include
source "./scripts/include"
# $BATTERY and $ADAPTER env vars can be set manually, being the names of the
# devices (in /sys/class/power_supply/) i.e. BATTERY=BAT0 ADAPTER=ADP0
# or, if left unset, they will be automatically picked.
CAPACITY_ICONS=("" "" "" "" "")
CHARGING_ICON=""
DISCHARGING_ICON=""
FULL_ICON="" # Plugged in, but no longer charging (fully charged)
CRITICAL_ICON=""
CRITICAL_PERCENTAGE=15
if [ -z "$BATTERY" ]; then
# shellcheck disable=SC2010
BATTERY="$(ls -t /sys/class/power_supply | grep "BAT" | head -n 1)"
fi
if [ -z "$ADAPTER" ]; then
# shellcheck disable=SC2010
ADAPTER="$(ls -t /sys/class/power_supply | grep "ADP" | head -n 1)"
fi
get_bat_info() {
cat /sys/class/power_supply/"$BATTERY"/"$1"
}
get_adp_info() {
cat /sys/class/power_supply/"$ADAPTER"/"$1"
}
manufacturer="$(get_bat_info manufacturer)"
model_name="$(get_bat_info model_name)"
technology="$(get_bat_info technology)"
energy_now="$(get_bat_info energy_now)"
energy_full="$(get_bat_info energy_full)"
energy_full_design="$(get_bat_info energy_full_design)"
cycle_count="$(get_bat_info cycle_count)"
capacity="$(get_bat_info capacity)"
status="$(get_bat_info status)"
[ "$(get_adp_info online)" -eq 1 ] && adp_connected="true" || adp_connected="false"
full="false"
capacity_icon="$(pick_icon "$capacity" 0 100 "${CAPACITY_ICONS[@]}")"
if [ "$status" = "Not charging" ] || [ "$status" = "Full" ] && [ "$adp_connected" = "true" ]; then
extra_icon="$FULL_ICON"
full="true"
elif [ "$status" = "Discharging" ] && [ "$capacity" -le "$CRITICAL_PERCENTAGE" ]; then
extra_icon="$CRITICAL_ICON"
elif [ "$status" = "Discharging" ]; then
extra_icon="$DISCHARGING_ICON"
elif [ "$status" = "Charging" ]; then
extra_icon="$CHARGING_ICON"
fi
[ "$capacity" -le "$CRITICAL_PERCENTAGE" ] && critical="true" || critical="false"
jq -n -c --monochrome-output \
--arg percent "$capacity" \
--arg plugged "$adp_connected" \
--arg status "$status" \
--arg capacity_icon "$capacity_icon" \
--arg extra_icon "$extra_icon" \
--arg manufacturer "$manufacturer" \
--arg model_name "$model_name" \
--arg technology "$technology" \
--arg energy_now "$energy_now" \
--arg energy_full "$energy_full" \
--arg energy_full_design "$energy_full_design" \
--arg cycle_count "$cycle_count" \
--arg critical "$critical" \
--arg full "$full" \
'$ARGS.named'

View file

@ -0,0 +1,49 @@
#!/bin/bash
# $1: Current number
# $2: Range minimum
# $3: Range maximum
# $4-: Icons as individual arguments
pick_icon() {
cur="$1"
min="$2"
max="$3"
shift 3
icons=( "$@" )
index="$(echo "($cur-$min)/(($max-$min)/${#icons[@]})" | bc)"
# Print the picked icon, handling overflows/underflows, i.e. if our index is <0 or >len(icons)
if [ "$index" -ge "${#icons[@]}" ]; then
index=-1
elif [ "$index" -lt 0 ]; then
index=0
fi
echo "${icons[index]}"
}
# Will block and listen to the hyprland socket messages and output them
# Generally used like: hyprland_ipc | while read line; do handle $line; done
# Read <https://wiki.hyprland.org/IPC/> for output format and available events
# Note: requires openbsd version of netcat.
# $1 - Optional event to listen for (no event filtering will be done if not provided)
hyprland_ipc() {
if [ -z "$HYPRLAND_INSTANCE_SIGNATURE" ]; then
>&2 echo "Hyprland is not running, IPC not available"
exit 1
fi
SOCKET_PATH="/tmp/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
if [ -z "$1" ]; then
nc -U "$SOCKET_PATH" | while read -r test; do
echo "$test"
done
else
nc -U "$SOCKET_PATH" | while read -r test; do
# shellcheck disable=SC2016
echo "$test" | grep --line-buffered -E "^$1>>" | stdbuf -oL awk -F '>>' '{print $2}'
done
fi
}

View file

@ -0,0 +1,13 @@
#!/bin/bash
# shellcheck source=include
source "./scripts/include"
# Consider usning a file as a flag for whether nightlight is on or off
# as we might be in transition state and just killing the program might
# not be enough.
if [ "$1" = "toggle" ]; then
gammastep -x
fi

View file

@ -0,0 +1,12 @@
[tool.black]
line-length = 119
extend-exclude = "^/.cache"
[tool.isort]
profile = "black"
line_length = 119
atomic = true
order_by_type = false
case_sensitive = true
combine_as_imports = true
skip = [".venv", ".git", ".cache"]

View file

@ -0,0 +1,35 @@
#!/bin/bash
MOUNTPOINTS=("/" "/mnt/ext")
data="$(df -H)"
as_json() {
mountpoint="$1"
res="$2"
arr_res=($res)
jq -n -c --monochrome-output \
--arg mountpoint "$mountpoint" \
--arg size "${arr_res[0]}" \
--arg used "${arr_res[1]}" \
--arg avail "${arr_res[2]}" \
--arg percent "${arr_res[3]}" \
'$ARGS.named'
}
output_json="[]"
for mountpoint in "${MOUNTPOINTS[@]}"; do
res="$(echo "$data" | awk -v m="$mountpoint" '$6 == m {print $2 " " $3 " " $4 " " $5}')"
out="$(as_json "$mountpoint" "$res")"
# echo "$output_json $out" | jq -c -s
jq --argjson arr1 "$output_json" --argjson arr2 "[$out]" -n \
'$arr1 + $arr2'
# mount_data+=("$mountpoint" $res)
# echo "${mount_data[@]}"
done
# echo "${mount_data[@]}"

6
home/.config/eww/scripts/temp Executable file
View file

@ -0,0 +1,6 @@
#!/bin/bash
# shellcheck source=include
source "./scripts/include"
hyprland_ipc "workspace|createworkspace|destroyworkspace"

114
home/.config/eww/scripts/volume Executable file
View file

@ -0,0 +1,114 @@
#!/bin/bash
# Define some icons
SPEAKER_ICONS=("" "" "")
SPEAKER_MUTED_ICON=""
MIC_ICON=""
MIC_MUTED_ICON=""
# Define some helper functions for getting/setting audio data using wireplumber (wpctl)
# $1 can either be "SINK" (speaker) or "SOURCE" (microphone)
get_vol() {
wpctl get-volume "@DEFAULT_AUDIO_${1}@" | awk '{print int($2*100)}'
}
# $1 can either be "SINK" (speaker) or "SOURCE" (microphone)
# #2 is the voulme (as percentage) to set the volume to
# $3 is optional, if set, it can be '+' or '-', which then adds/decreases volume, instead of setting
set_vol() {
wpctl set-volume "@DEFAULT_AUDIO_${1}@" "$(awk -v n="$2" 'BEGIN{print (n / 100)}')$3"
}
# $1 can either be "SINK" (speaker) or "SOURCE" (microphone)
check_mute() {
wpctl get-volume "@DEFAULT_AUDIO_${1}@" | grep -i muted >/dev/null
echo $?
}
# $1 can either be "SINK" (speaker) or "SOURCE" (microphone)
toggle_mute() {
wpctl set-mute "@DEFAULT_AUDIO_${1}@" toggle
}
get_report() {
spkr_vol="$(get_vol "SINK")"
mic_vol="$(get_vol "SOURCE")"
if [ "$(check_mute "SINK")" == "0" ]; then
spkr_mute="true"
spkr_icon="$SPEAKER_MUTED_ICON"
else
spkr_mute="false"
index="$(awk -v n="$spkr_vol" -v m="${#SPEAKER_ICONS[@]}" 'BEGIN{print int(n/(100/m))}')"
# We might end up with an higher than the length of icons, if the volume is over 100%
# in this case, set the index to last icon
if [ "$index" -ge "${#SPEAKER_ICONS[@]}" ]; then
spkr_icon="${SPEAKER_ICONS[-1]}"
else
spkr_icon="${SPEAKER_ICONS[$index]}"
fi
fi
if [ "$(check_mute "SOURCE")" = "0" ]; then
mic_mute="true"
mic_icon="$MIC_MUTED_ICON"
else
mic_mute="false"
mic_icon="$MIC_ICON"
fi
echo "{ \"speaker_vol\": \"$spkr_vol\", \"speaker_mute\": $spkr_mute, \"speaker_icon\": \"$spkr_icon\", \"microphone_mute\": $mic_mute, \"microphone_vol\": \"$mic_vol\", \"microphone_icon\": \"$mic_icon\" }"
}
# Continually run and report every volume change (into stdout)
loop() {
pactl subscribe | grep --line-buffered "change" | while read -r _; do
get_report
done
}
case "$1" in
"loop")
get_report
loop
;;
"once") get_report ;;
"togglemute")
if [ "$2" != "SOURCE" ] && [ "$2" != "SINK" ]; then
>&2 echo "Invalid usage, expected second argument to be 'SINK' or 'SOURCE', got '$2'"
exit 1
fi
toggle_mute "$2"
;;
"setvol")
if [ "$2" != "SOURCE" ] && [ "$2" != "SINK" ]; then
>&2 echo "Invalid usage, expected second argument to be 'SINK' or 'SOURCE', got '$2'"
exit 1
fi
if [[ "$3" =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]; then
case "$4" in
"") set_vol "$2" "$3" ;;
up|+) set_vol "$2" "$3" "+" ;;
down|-) set_vol "$2" "$3" "-" ;;
*)
>&2 echo "Invalid usage, expected fourth argument to be up/down or +/-, got '$4'"
exit 1
;;
esac
else
>&2 echo "Invalid usage, exepcted third argument to be a number, got '$3'"
exit 1
fi
;;
*)
>&2 echo "Invalid usage, argument '$1' not recognized."
exit 1
;;
esac

View file

@ -0,0 +1,6 @@
#!/bin/bash
# shellcheck source=include
source "./scripts/include"
hyprland_ipc "activewindow" | ./scripts/window_name.py

View file

@ -0,0 +1,90 @@
#!/usr/bin/env python3
"""This is a utility script for regex remaps on window names.
This is done in python, because of the complex regex logic, which would be pretty hard to
recreate in bash. This python script is expected to be called from the bash script controlling
the window names. Window name and class are obtained from piped stdin, to prevent having to
needlessly keep restarting this program, which takes a while due to the interpreter starting
overhead.
"""
import re
import json
import sys
from typing import Iterator, Optional
class RemapRule:
__slots__ = ("name_pattern", "output_pattern", "class_pattern")
def __init__(self, name_pattern: str, output_pattern: str, class_pattern: Optional[str] = None):
self.name_pattern = re.compile(name_pattern)
self.output_pattern = output_pattern
self.class_pattern = re.compile(class_pattern) if class_pattern else None
def apply(self, window_name: str, window_class: str) -> str:
"""Returns new name after this remap rule was applied."""
if self.class_pattern is not None:
if not self.class_pattern.fullmatch(window_class):
# Rule doesn't apply, class mismatch, return original name
return window_name
res = self.name_pattern.fullmatch(window_name)
if not res:
# Rule doesn't apply, name mismatch, return original name
return window_name
# NOTE: This is potentially unsafe, since output_pattern might be say {0.__class__}, etc.
# meaning this allows arbitrary attribute access, however it doesn't allow running functions
# here. That said, code could still end up being run if there's some descriptor defined,
# and generally I wouldn't trust this in production. However, this code is for my personal
# use here, and all of the output patterns are hard-coded in this file, so in this case, it's
# fine. But if you see this code and you'd like to use it in your production code, maybe don't
return self.output_pattern.format(*res.groups())
# Rules will be applied in specified order
REMAP_RULES: list[RemapRule] = [
RemapRule(r"", "", ""),
RemapRule(r"(.*) — Mozilla Firefox", "{}", "firefox"),
RemapRule(r"Mozilla Firefox", " Mozilla Firefox", "firefox"),
RemapRule(r"Alacritty", " Alacritty", "Alacritty"),
RemapRule(r"zsh;#toggleterm#1 - \(term:\/\/(.+)\/\/(\d+):(.+)\) - N?VIM", " Terminal: {0}"),
RemapRule(r"(.+) \+ \((.+)\) - N?VIM", "{0} ({1}) [MODIFIED]"),
RemapRule(r"(.+) \((.+)\) - N?VIM", "{0} ({1})"),
RemapRule(r"(?:\[\d+\] )?\*?WebCord - (.+)", "{}", "WebCord"),
RemapRule(r"(.+) - mpv", "{}", "mpv"),
RemapRule(r"Stremio - (.+)", " Stremio - {}", "com.stremio.stremio"),
RemapRule(r"Spotify", " Spotify", "Spotify"),
RemapRule(r"pulsemixer", " Pulsemixer"),
RemapRule(r"(.*)", "{}", "Pcmanfm"),
]
MAX_LENGTH = 65
def iter_window() -> Iterator[tuple[str, str]]:
"""Listen for the window parameters from stdin/pipe, yields (window_name, window_class)."""
for line in sys.stdin:
line = line.removesuffix("\n")
els = line.split(",", maxsplit=1)
if len(els) != 2:
raise ValueError(f"Expected 2 arguments from stdin line (name, class), but got {len(els)}")
yield els[1], els[0]
def main() -> None:
for window_name, window_class in iter_window():
formatted_name = window_name
for remap_rule in REMAP_RULES:
formatted_name = remap_rule.apply(formatted_name, window_class)
if len(formatted_name) > MAX_LENGTH:
formatted_name = formatted_name[:MAX_LENGTH - 3] + "..."
ret = json.dumps({"name": window_name, "class": window_class, "formatted_name": formatted_name})
print(ret)
sys.stdout.flush()
if __name__ == "__main__":
main()

View file

@ -0,0 +1,12 @@
#!/bin/bash
# shellcheck source=include
source "./scripts/include"
if [ "$1" = "--switch" ]; then
hyprctl dispatch workspace "$2" >/dev/null
elif [ "$1" = "--loop" ]; then
hyprland_ipc "workspace|createworkspace|destroyworkspace" | ./scripts/workspaces.py "$@"
else
./scripts/workspaces.py "$@"
fi

View file

@ -0,0 +1,171 @@
#!/usr/bin/env python3
import argparse
import json
import subprocess
import sys
from typing import TypedDict, TYPE_CHECKING
if TYPE_CHECKING:
from _typeshed import SupportsRichComparison
class WorkspaceInfo(TypedDict):
id: int
name: str
monitor: str
windows: int
hasfullscreen: bool
lastwindow: str
lastwindowtitle: str
class ActiveWorkspaceInfo(TypedDict):
id: int
name: str
class MonitorInfo(TypedDict):
id: int
name: str
description: str
width: int
height: int
refreshRate: float
x: int
y: int
activeWorkspace: ActiveWorkspaceInfo
reserved: list[int]
scale: float
transform: int
focused: bool
dpmsStatus: bool
class OutputWorkspaceInfo(WorkspaceInfo):
format_name: str
active: bool
# workspace id -> remapped name
REMAPS = {
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: "7",
8: "8",
9: "9",
}
# Skip the special (scratchpad) workspace
SKIP = {-99}
def workspace_sort(obj: OutputWorkspaceInfo) -> "SupportsRichComparison":
"""Returns a key to sort by, given the current element."""
return obj["id"]
def fill_blank_workspaces(open: list[OutputWorkspaceInfo]) -> list[OutputWorkspaceInfo]:
"""Add in the rest of the workspaces which don't have any open windows on them.
This is needed because hyprland deletes workspaces with nothing in them.
Note that this assumes all available workspaces were listed in REMAPS, and will
only fill those. These blank workspaces will have most string values set to "N/A",
and most int values set to 0.
"""
# Work on a copy, we don't want to alter the original list
lst = open.copy()
for remap_id, format_name in REMAPS.items():
# Skip for already present workspaces
if any(ws_info["id"] == remap_id for ws_info in lst):
continue
blank_ws: OutputWorkspaceInfo = {
"id": remap_id,
"name": str(remap_id),
"monitor": "N/A",
"windows": 0,
"hasfullscreen": False,
"lastwindow": "N/A",
"lastwindowtitle": "N/A",
"active": False,
"format_name": format_name,
}
lst.append(blank_ws)
return lst
def get_workspaces() -> list[OutputWorkspaceInfo]:
"""Obtain workspaces from hyprctl, sort them and add format_name arg."""
proc = subprocess.run(["hyprctl", "workspaces", "-j"], stdout=subprocess.PIPE)
proc.check_returncode()
workspaces: list[WorkspaceInfo] = json.loads(proc.stdout)
proc = subprocess.run(["hyprctl", "monitors", "-j"], stdout=subprocess.PIPE)
proc.check_returncode()
monitors: list[MonitorInfo] = json.loads(proc.stdout)
active_workspaces = {monitor["activeWorkspace"]["id"] for monitor in monitors}
out: list[OutputWorkspaceInfo] = []
for workspace in workspaces:
if workspace["id"] in SKIP:
continue
format_name = REMAPS.get(workspace["id"], workspace["name"])
active = workspace["id"] in active_workspaces
out.append({**workspace, "format_name": format_name, "active": active})
out = fill_blank_workspaces(out)
out.sort(key=workspace_sort)
return out
def print_workspaces() -> None:
wks = get_workspaces()
ret = json.dumps(wks)
print(ret)
sys.stdout.flush()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--oneshot",
action="store_true",
help="Don't listen to stdout for updates, only run once and quit",
)
parser.add_argument(
"--loop",
action="store_true",
help="Listen to stdout input, once something is received, re-print workspaces"
)
args = parser.parse_args()
if args.loop and args.oneshot:
print("Can't use both --oneshot and --loop", file=sys.stdout)
sys.exit(1)
if args.loop is None and args.oneshot is None:
print("No option specified!", file=sys.stdout)
sys.exit(1)
# Print workspaces here immediately, we don't want to have to wait for the first
# update from stdin as we only receive those on actual workspace change.
print_workspaces()
if args.oneshot:
# We've already printed the workspaces once, we can exit now
return
# Reprint workspaces on each stdin update (flush)
for _ in sys.stdin:
print_workspaces()
if __name__ == "__main__":
main()