diff --git a/hypr/hyprland.conf b/hypr/hyprland.conf index 2f1fbc3..3dd73fd 100644 --- a/hypr/hyprland.conf +++ b/hypr/hyprland.conf @@ -218,7 +218,6 @@ bind = $mainMod, A, exec, firefox bind = $mainMod SHIFT, C, killactive, bind = $mainMod, V, togglefloating, bind = $mainMod, R, exec, $menu -bind = $mainMod, H, exec, $hyprscript focusnextmonitor bind = $mainMod, J, exec, $hyprscript focusnextmonitor 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, down, movefocus, d -bind = $mainMod, left, exec, $hyprscript previous -bind = $mainMod, right, exec, $hyprscript next +bind = $mainMod, left, exec, $hyprscript workspace previous +bind = $mainMod, right, exec, $hyprscript workspace next # Switch workspaces with mainMod + [0-9] 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:17, exec, $hyprscript workspace 8 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] 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:17, exec, $hyprscript movetoworkspace 8 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 bind = , Print, exec, grim -g "$(slurp -d)" - | wl-copy diff --git a/hypr/hyprscript b/hypr/hyprscript index 3f795bb..04ed126 100755 --- a/hypr/hyprscript +++ b/hypr/hyprscript @@ -1,103 +1,207 @@ -#!/usr/bin/env bash +#!/usr/bin/env python -get_current_workspace() { - hyprctl activeworkspace | head -n 1 | cut -d ' ' -f 3 -} +import json +import os +import os.path +import psutil +import subprocess +import sys -get_current_monitor() { - hyprctl activeworkspace | head -n 1 | rev | cut -d ' ' -f 1 | rev | tr -d ':' -} +os.chdir(os.path.expanduser('~/.config/dotfiles/hypr')) +bars = "_▂▃▄▅▆▇█" -get_next_monitor() { - current_monitor=$(get_current_monitor) - next_monitor=$(hyprctl monitors | grep Monitor | grep " $current_monitor " -A 2 | sed '2q;d' | cut -d ' ' -f 2) +class Monitor: + """ + 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 - next_monitor=$(hyprctl monitors | grep Monitor | head -n 1 | cut -d ' ' -f 2) - fi + def __eq__(self, other): + """ + 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() { - current_monitor=$(get_current_monitor) - previous_monitor=$(hyprctl monitors | grep Monitor | grep " $current_monitor " -B 2 | sed '2q;d' | cut -d ' ' -f 2) + @staticmethod + def all() -> list['Monitor']: + """ + Returns the list of available monitors. + """ + monitors: list['Monitor'] = [] - if [ -z $previous_monitor ]; then - previous_monitor=$(hyprctl monitors | grep Monitor | tail -n 1 | cut -d ' ' -f 2) - fi + proc = subprocess.run(['hyprctl', 'monitors', '-j'], capture_output=True) + result = json.loads(proc.stdout) - echo $previous_monitor -} + for mon in result: + monitors.append(Monitor(mon['id'], mon['name'])) -get_nth_workspace() { - current_workspace=$1 + return monitors - if [[ "$current_workspace" == *0 ]]; then - current_workspace=$(($current_workspace - 10)) - fi + def previous(self, monitors: list['Monitor'], move: bool = False): + """ + 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 - workspace=$(($workspace + 10)) - fi + if move: + subprocess.run(['hyprctl', 'dispatch', 'movewindow', 'mon:' + str(mon.id)]) + 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 - "cpustat") - head -n 1 ~/.config/dotfiles/hypr/.stat.txt;; + Params: + move: whether you want to move the active window to the new monitor. + """ + mon = monitors[(monitors.index(self) + 1) % len(monitors)] - "memstat") - tail -n 1 ~/.config/dotfiles/hypr/.stat.txt;; + if move: + 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") - current_workspace=$(get_current_workspace) - workspace=$(get_nth_workspace $current_workspace $2) - hyprctl dispatch movetoworkspace $workspace;; +class Workspace: + """ + A workspace bound to a monitor. - "previous") - current_monitor=$(get_current_monitor) - current_workspace=$(get_current_workspace) - previous_workspace=$(($current_workspace - 1)) + Workspaces with id from 10 * n + k belongs to the same monitor for k in [1, 10]. + """ + def __init__(self, id: int): + self.id = id - if [[ "$previous_workspace" == *0 ]]; then - previous_workspace=$(($previous_workspace + 10)) - fi + @staticmethod + def active() -> 'Workspace': + """ + 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") - current_monitor=$(get_current_monitor) - current_workspace=$(get_current_workspace) - next_workspace=$(($current_workspace + 1)) + Params: + n: workspace to go to, between 1 and 10. + move: whether you want to move the active window to the new workspace. + """ + 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 - next_workspace=$(($next_workspace - 10)) - fi + def previous(self, move: bool = False): + """ + 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") - next_monitor=$(get_next_monitor) - hyprctl dispatch movewindow mon:$next_monitor;; + if new_id % 10 == 0: + new_id += 10 - "focuspreviousmonitor") - previous_monitor=$(get_previous_monitor) - hyprctl dispatch focusmonitor $previous_monitor;; + subprocess.run(['hyprctl', 'dispatch', 'movetoworkspace' if move else 'workspace', str(new_id)]) - "focusnextmonitor") - next_monitor=$(get_next_monitor) - hyprctl dispatch focusmonitor $next_monitor;; + def next(self, move: bool = False): + """ + Goes to the next workspace on the same monitor, or the first if we're on the last. - "reload") - systemctl restart waybar --user;; + Params: + 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() diff --git a/hypr/stat.py b/hypr/stat.py deleted file mode 100755 index 0d7fcac..0000000 --- a/hypr/stat.py +++ /dev/null @@ -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 + '%' - ) diff --git a/waybar/config b/waybar/config index b1298cd..d26dbb0 100644 --- a/waybar/config +++ b/waybar/config @@ -136,11 +136,11 @@ "spacing": 5 }, "custom/cpu": { - "exec": "~/.config/dotfiles/hypr/hyprscript cpustat", + "exec": "head -n 1 ~/.config/dotfiles/hypr/.stat.txt", "restart-interval": 2 }, "custom/mem": { - "exec": "~/.config/dotfiles/hypr/hyprscript memstat", + "exec": "tail -n 1 ~/.config/dotfiles/hypr/.stat.txt", "restart-interval": 2 } }