Hyprscript in python
This commit is contained in:
parent
44b91c735b
commit
f058d240bd
@ -218,7 +218,6 @@ bind = $mainMod, A, exec, firefox
|
|||||||
bind = $mainMod SHIFT, C, killactive,
|
bind = $mainMod SHIFT, C, killactive,
|
||||||
bind = $mainMod, V, togglefloating,
|
bind = $mainMod, V, togglefloating,
|
||||||
bind = $mainMod, R, exec, $menu
|
bind = $mainMod, R, exec, $menu
|
||||||
bind = $mainMod, H, exec, $hyprscript focusnextmonitor
|
|
||||||
bind = $mainMod, J, exec, $hyprscript focusnextmonitor
|
bind = $mainMod, J, exec, $hyprscript focusnextmonitor
|
||||||
|
|
||||||
bind = $mainMod, O, exec, $hyprscript movewindow
|
bind = $mainMod, O, exec, $hyprscript movewindow
|
||||||
@ -243,8 +242,8 @@ bind = $mainMod SHIFT, code:48, exec, sleep 0.5s && wtype "thomas@polymny.studio
|
|||||||
# bind = $mainMod, up, movefocus, u
|
# bind = $mainMod, up, movefocus, u
|
||||||
# bind = $mainMod, down, movefocus, d
|
# bind = $mainMod, down, movefocus, d
|
||||||
|
|
||||||
bind = $mainMod, left, exec, $hyprscript previous
|
bind = $mainMod, left, exec, $hyprscript workspace previous
|
||||||
bind = $mainMod, right, exec, $hyprscript next
|
bind = $mainMod, right, exec, $hyprscript workspace next
|
||||||
|
|
||||||
# Switch workspaces with mainMod + [0-9]
|
# Switch workspaces with mainMod + [0-9]
|
||||||
bind = $mainMod, code:10, exec, $hyprscript workspace 1
|
bind = $mainMod, code:10, exec, $hyprscript workspace 1
|
||||||
@ -256,7 +255,7 @@ bind = $mainMod, code:15, exec, $hyprscript workspace 6
|
|||||||
bind = $mainMod, code:16, exec, $hyprscript workspace 7
|
bind = $mainMod, code:16, exec, $hyprscript workspace 7
|
||||||
bind = $mainMod, code:17, exec, $hyprscript workspace 8
|
bind = $mainMod, code:17, exec, $hyprscript workspace 8
|
||||||
bind = $mainMod, code:18, exec, $hyprscript workspace 9
|
bind = $mainMod, code:18, exec, $hyprscript workspace 9
|
||||||
bind = $mainMod, code:19, exec, $hyprscript workspace 0
|
bind = $mainMod, code:19, exec, $hyprscript workspace 10
|
||||||
|
|
||||||
# Move active window to a workspace with mainMod + SHIFT + [0-9]
|
# Move active window to a workspace with mainMod + SHIFT + [0-9]
|
||||||
bind = $mainMod SHIFT, code:10, exec, $hyprscript movetoworkspace 1
|
bind = $mainMod SHIFT, code:10, exec, $hyprscript movetoworkspace 1
|
||||||
@ -268,7 +267,7 @@ bind = $mainMod SHIFT, code:15, exec, $hyprscript movetoworkspace 6
|
|||||||
bind = $mainMod SHIFT, code:16, exec, $hyprscript movetoworkspace 7
|
bind = $mainMod SHIFT, code:16, exec, $hyprscript movetoworkspace 7
|
||||||
bind = $mainMod SHIFT, code:17, exec, $hyprscript movetoworkspace 8
|
bind = $mainMod SHIFT, code:17, exec, $hyprscript movetoworkspace 8
|
||||||
bind = $mainMod SHIFT, code:18, exec, $hyprscript movetoworkspace 9
|
bind = $mainMod SHIFT, code:18, exec, $hyprscript movetoworkspace 9
|
||||||
bind = $mainMod SHIFT, code:19, exec, $hyprscript movetoworkspace 0
|
bind = $mainMod SHIFT, code:19, exec, $hyprscript movetoworkspace 10
|
||||||
|
|
||||||
# Screenshot
|
# Screenshot
|
||||||
bind = , Print, exec, grim -g "$(slurp -d)" - | wl-copy
|
bind = , Print, exec, grim -g "$(slurp -d)" - | wl-copy
|
||||||
|
254
hypr/hyprscript
254
hypr/hyprscript
@ -1,103 +1,207 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env python
|
||||||
|
|
||||||
get_current_workspace() {
|
import json
|
||||||
hyprctl activeworkspace | head -n 1 | cut -d ' ' -f 3
|
import os
|
||||||
}
|
import os.path
|
||||||
|
import psutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
get_current_monitor() {
|
os.chdir(os.path.expanduser('~/.config/dotfiles/hypr'))
|
||||||
hyprctl activeworkspace | head -n 1 | rev | cut -d ' ' -f 1 | rev | tr -d ':'
|
bars = "_▂▃▄▅▆▇█"
|
||||||
}
|
|
||||||
|
|
||||||
get_next_monitor() {
|
class Monitor:
|
||||||
current_monitor=$(get_current_monitor)
|
"""
|
||||||
next_monitor=$(hyprctl monitors | grep Monitor | grep " $current_monitor " -A 2 | sed '2q;d' | cut -d ' ' -f 2)
|
A monitor on which many workspaces can appear.
|
||||||
|
"""
|
||||||
|
def __init__(self, id: int, name: str):
|
||||||
|
self.id = id
|
||||||
|
self.name = name
|
||||||
|
|
||||||
if [ -z $next_monitor ]; then
|
def __eq__(self, other):
|
||||||
next_monitor=$(hyprctl monitors | grep Monitor | head -n 1 | cut -d ' ' -f 2)
|
"""
|
||||||
fi
|
Returns true if the monitors are the same.
|
||||||
|
"""
|
||||||
|
self.id == other.id
|
||||||
|
|
||||||
echo $next_monitor
|
@staticmethod
|
||||||
}
|
def active(monitors: list['Monitor']) -> 'Monitor':
|
||||||
|
"""
|
||||||
|
Returns the active monitor.
|
||||||
|
"""
|
||||||
|
proc = subprocess.run(['hyprctl', 'activeworkspace', '-j'], capture_output=True)
|
||||||
|
result = json.loads(proc.stdout)
|
||||||
|
return next((monitor for monitor in monitors if monitor.id == result['monitorID']))
|
||||||
|
|
||||||
get_previous_monitor() {
|
@staticmethod
|
||||||
current_monitor=$(get_current_monitor)
|
def all() -> list['Monitor']:
|
||||||
previous_monitor=$(hyprctl monitors | grep Monitor | grep " $current_monitor " -B 2 | sed '2q;d' | cut -d ' ' -f 2)
|
"""
|
||||||
|
Returns the list of available monitors.
|
||||||
|
"""
|
||||||
|
monitors: list['Monitor'] = []
|
||||||
|
|
||||||
if [ -z $previous_monitor ]; then
|
proc = subprocess.run(['hyprctl', 'monitors', '-j'], capture_output=True)
|
||||||
previous_monitor=$(hyprctl monitors | grep Monitor | tail -n 1 | cut -d ' ' -f 2)
|
result = json.loads(proc.stdout)
|
||||||
fi
|
|
||||||
|
|
||||||
echo $previous_monitor
|
for mon in result:
|
||||||
}
|
monitors.append(Monitor(mon['id'], mon['name']))
|
||||||
|
|
||||||
get_nth_workspace() {
|
return monitors
|
||||||
current_workspace=$1
|
|
||||||
|
|
||||||
if [[ "$current_workspace" == *0 ]]; then
|
def previous(self, monitors: list['Monitor'], move: bool = False):
|
||||||
current_workspace=$(($current_workspace - 10))
|
"""
|
||||||
fi
|
Moves the focus to the previous monitor.
|
||||||
|
|
||||||
workspace=$(echo $current_workspace | cut -c 1)$2
|
Params:
|
||||||
|
move: whether you want to move the active window to the new monitor.
|
||||||
|
"""
|
||||||
|
mon = monitors[(monitors.index(self) - 1) % len(monitors)]
|
||||||
|
|
||||||
if [[ "$workspace" == *0 ]]; then
|
if move:
|
||||||
workspace=$(($workspace + 10))
|
subprocess.run(['hyprctl', 'dispatch', 'movewindow', 'mon:' + str(mon.id)])
|
||||||
fi
|
else:
|
||||||
|
subprocess.run(['hyprctl', 'dispatch', 'focusmonitor', str(mon.id)])
|
||||||
|
|
||||||
echo $workspace
|
def next(self, monitors: list['Monitor'], move: bool = False):
|
||||||
}
|
"""
|
||||||
|
Moves the focus to the next monitor.
|
||||||
|
|
||||||
case "$1" in
|
Params:
|
||||||
"cpustat")
|
move: whether you want to move the active window to the new monitor.
|
||||||
head -n 1 ~/.config/dotfiles/hypr/.stat.txt;;
|
"""
|
||||||
|
mon = monitors[(monitors.index(self) + 1) % len(monitors)]
|
||||||
|
|
||||||
"memstat")
|
if move:
|
||||||
tail -n 1 ~/.config/dotfiles/hypr/.stat.txt;;
|
subprocess.run(['hyprctl', 'dispatch', 'movewindow', 'mon:' + str(mon.name)])
|
||||||
|
else:
|
||||||
|
subprocess.run(['hyprctl', 'dispatch', 'focusmonitor', str(mon.name)])
|
||||||
|
|
||||||
"workspace")
|
|
||||||
current_workspace=$(get_current_workspace)
|
|
||||||
workspace=$(get_nth_workspace $current_workspace $2)
|
|
||||||
hyprctl dispatch workspace $workspace;;
|
|
||||||
|
|
||||||
"movetoworkspace")
|
class Workspace:
|
||||||
current_workspace=$(get_current_workspace)
|
"""
|
||||||
workspace=$(get_nth_workspace $current_workspace $2)
|
A workspace bound to a monitor.
|
||||||
hyprctl dispatch movetoworkspace $workspace;;
|
|
||||||
|
|
||||||
"previous")
|
Workspaces with id from 10 * n + k belongs to the same monitor for k in [1, 10].
|
||||||
current_monitor=$(get_current_monitor)
|
"""
|
||||||
current_workspace=$(get_current_workspace)
|
def __init__(self, id: int):
|
||||||
previous_workspace=$(($current_workspace - 1))
|
self.id = id
|
||||||
|
|
||||||
if [[ "$previous_workspace" == *0 ]]; then
|
@staticmethod
|
||||||
previous_workspace=$(($previous_workspace + 10))
|
def active() -> 'Workspace':
|
||||||
fi
|
"""
|
||||||
|
Returns the active workspace.
|
||||||
|
"""
|
||||||
|
proc = subprocess.run(['hyprctl', 'activeworkspace', '-j'], capture_output=True)
|
||||||
|
result = json.loads(proc.stdout)
|
||||||
|
return Workspace(result['id'])
|
||||||
|
|
||||||
hyprctl dispatch workspace $previous_workspace;;
|
def nth(self, n: int, move: bool = False):
|
||||||
|
"""
|
||||||
|
Goes to the nth workspace on the same monitor as the current workspace.
|
||||||
|
|
||||||
"next")
|
Params:
|
||||||
current_monitor=$(get_current_monitor)
|
n: workspace to go to, between 1 and 10.
|
||||||
current_workspace=$(get_current_workspace)
|
move: whether you want to move the active window to the new workspace.
|
||||||
next_workspace=$(($current_workspace + 1))
|
"""
|
||||||
|
new_id = (self.id - 1) // 10 * 10 + n
|
||||||
|
subprocess.run(['hyprctl', 'dispatch', 'movetoworkspace' if move else 'workspace', str(new_id)])
|
||||||
|
|
||||||
if [[ "$next_workspace" == *1 ]]; then
|
def previous(self, move: bool = False):
|
||||||
next_workspace=$(($next_workspace - 10))
|
"""
|
||||||
fi
|
Goes to the previous workspace on the same monitor, or the last if we're on the first.
|
||||||
|
|
||||||
hyprctl dispatch workspace $next_workspace;;
|
Params:
|
||||||
|
move: whether you want to move the active window to the new workspace.
|
||||||
|
"""
|
||||||
|
new_id = self.id - 1
|
||||||
|
|
||||||
"movewindow")
|
if new_id % 10 == 0:
|
||||||
next_monitor=$(get_next_monitor)
|
new_id += 10
|
||||||
hyprctl dispatch movewindow mon:$next_monitor;;
|
|
||||||
|
|
||||||
"focuspreviousmonitor")
|
subprocess.run(['hyprctl', 'dispatch', 'movetoworkspace' if move else 'workspace', str(new_id)])
|
||||||
previous_monitor=$(get_previous_monitor)
|
|
||||||
hyprctl dispatch focusmonitor $previous_monitor;;
|
|
||||||
|
|
||||||
"focusnextmonitor")
|
def next(self, move: bool = False):
|
||||||
next_monitor=$(get_next_monitor)
|
"""
|
||||||
hyprctl dispatch focusmonitor $next_monitor;;
|
Goes to the next workspace on the same monitor, or the first if we're on the last.
|
||||||
|
|
||||||
"reload")
|
Params:
|
||||||
systemctl restart waybar --user;;
|
move: whether you want to move the active window to the new workspace.
|
||||||
|
"""
|
||||||
|
new_id = self.id + 1
|
||||||
|
|
||||||
esac
|
if new_id % 10 == 1:
|
||||||
|
new_id -= 10
|
||||||
|
|
||||||
|
subprocess.run(['hyprctl', 'dispatch', 'movetoworkspace' if move else 'workspace', str(new_id)])
|
||||||
|
|
||||||
|
|
||||||
|
def to_bar(x):
|
||||||
|
return bars[round((len(bars) - 1) * x / 100)]
|
||||||
|
|
||||||
|
|
||||||
|
def monitor_stats():
|
||||||
|
"""
|
||||||
|
Monitors CPU and MEM usage and prints info in file.
|
||||||
|
"""
|
||||||
|
cpu_values = [0] * 10
|
||||||
|
mem_values = [0] * 10
|
||||||
|
|
||||||
|
while True:
|
||||||
|
cpu_percent = psutil.cpu_percent(interval=2)
|
||||||
|
mem_percent = psutil.virtual_memory().percent
|
||||||
|
|
||||||
|
for i in range(len(cpu_values) - 1):
|
||||||
|
cpu_values[i] = cpu_values[i+1]
|
||||||
|
mem_values[i] = mem_values[i+1]
|
||||||
|
|
||||||
|
cpu_values[-1] = cpu_percent
|
||||||
|
mem_values[-1] = mem_percent
|
||||||
|
|
||||||
|
with open('.stat.txt', 'w') as f:
|
||||||
|
f.write(
|
||||||
|
' ' + ''.join([to_bar(x) for x in cpu_values]) + ' ' + "% 5.1f"%cpu_percent + '%\n' +
|
||||||
|
' ' + ''.join([to_bar(x) for x in mem_values]) + ' ' + "% 5.1f"%mem_percent + '%'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
return
|
||||||
|
|
||||||
|
if sys.argv[1] == 'workspace' or sys.argv[1] == 'movetoworkspace':
|
||||||
|
workspace = Workspace.active()
|
||||||
|
move = sys.argv[1] == 'movetoworkspace'
|
||||||
|
|
||||||
|
if sys.argv[2] == 'next':
|
||||||
|
workspace.next(move)
|
||||||
|
|
||||||
|
elif sys.argv[2] == 'previous':
|
||||||
|
workspace.previous(move)
|
||||||
|
|
||||||
|
else:
|
||||||
|
new_workspace = int(sys.argv[2])
|
||||||
|
workspace.nth(new_workspace, move)
|
||||||
|
|
||||||
|
elif sys.argv[1] in ['movewindow', 'focusnextmonitor', 'focuspreviousmonitor']:
|
||||||
|
monitors = Monitor.all()
|
||||||
|
monitor = Monitor.active(monitors)
|
||||||
|
|
||||||
|
if sys.argv[1] == 'movewindow':
|
||||||
|
monitor.next(monitors, True)
|
||||||
|
elif sys.argv[1] == 'focusnextmonitor':
|
||||||
|
monitor.next(monitors)
|
||||||
|
elif sys.argv[1] == 'focuspreviousmonitor':
|
||||||
|
monitor.previous(monitors)
|
||||||
|
|
||||||
|
elif sys.argv[1] == 'reload':
|
||||||
|
subprocess.run(['systemctl', 'restart', 'waybar', '--user'])
|
||||||
|
|
||||||
|
elif sys.argv[1] == 'stat':
|
||||||
|
monitor_stats()
|
||||||
|
|
||||||
|
else:
|
||||||
|
print(f'Command not found: {sys.argv[1]}')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
31
hypr/stat.py
31
hypr/stat.py
@ -1,31 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
import os
|
|
||||||
from os.path import expanduser
|
|
||||||
import psutil
|
|
||||||
|
|
||||||
os.chdir(os.path.expanduser('~/.config/dotfiles/hypr'))
|
|
||||||
|
|
||||||
bars = "_▂▃▄▅▆▇█"
|
|
||||||
cpu_values = [0] * 10
|
|
||||||
mem_values = [0] * 10
|
|
||||||
|
|
||||||
def to_bar(x):
|
|
||||||
return bars[round((len(bars) - 1) * x / 100)]
|
|
||||||
|
|
||||||
while True:
|
|
||||||
cpu_percent = psutil.cpu_percent(interval=2)
|
|
||||||
mem_percent = psutil.virtual_memory().percent
|
|
||||||
|
|
||||||
for i in range(len(cpu_values) - 1):
|
|
||||||
cpu_values[i] = cpu_values[i+1]
|
|
||||||
mem_values[i] = mem_values[i+1]
|
|
||||||
|
|
||||||
cpu_values[-1] = cpu_percent
|
|
||||||
mem_values[-1] = mem_percent
|
|
||||||
|
|
||||||
with open('.stat.txt', 'w') as f:
|
|
||||||
f.write(
|
|
||||||
' ' + ''.join([to_bar(x) for x in cpu_values]) + ' ' + "% 5.1f"%cpu_percent + '%\n' +
|
|
||||||
' ' + ''.join([to_bar(x) for x in mem_values]) + ' ' + "% 5.1f"%mem_percent + '%'
|
|
||||||
)
|
|
@ -136,11 +136,11 @@
|
|||||||
"spacing": 5
|
"spacing": 5
|
||||||
},
|
},
|
||||||
"custom/cpu": {
|
"custom/cpu": {
|
||||||
"exec": "~/.config/dotfiles/hypr/hyprscript cpustat",
|
"exec": "head -n 1 ~/.config/dotfiles/hypr/.stat.txt",
|
||||||
"restart-interval": 2
|
"restart-interval": 2
|
||||||
},
|
},
|
||||||
"custom/mem": {
|
"custom/mem": {
|
||||||
"exec": "~/.config/dotfiles/hypr/hyprscript memstat",
|
"exec": "tail -n 1 ~/.config/dotfiles/hypr/.stat.txt",
|
||||||
"restart-interval": 2
|
"restart-interval": 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user