second commit

This commit is contained in:
Daylin Morgan 2024-01-24 10:39:58 -06:00
parent e2e5e2162c
commit 51ed2ba6aa
Signed by: daylin
GPG Key ID: C1E52E7DD81DF79F
3 changed files with 91 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin/

2
config.nims Normal file
View File

@ -0,0 +1,2 @@
task build, "build":
selfExec "c -d:release --outdir:bin src/hyprman"

88
src/hyprman.nim Normal file
View File

@ -0,0 +1,88 @@
import std/[os,net, json, sugar, strformat, tables]
const icons = {
"[Running] - Oracle VM VirtualBox": "",
"Visual Studio Code": "",
"vivaldi-stable": "󰖟",
"LibreOffice": "",
"Geneious Prime": "󰚄",
"Firefox": "",
"- NVIM": "",
"Alacritty": "",
"- Wezterm": "",
"org.wezfurlong.wezterm":""
}.toTable
type
HyprlandDefect* = Defect
# Workspace = object
# id, monitorID, windows: int
# name, monitor, lastwindow, lastwindowtitle: string
# hasfullscreen: bool
WorkspaceIcon = object
id: int
icon, class: string
ActiveWorkspace = object
id: int
name: string
Monitor = object
id: int
activeWorkspace: ActiveWorkspace
Client = object
workspace: ActiveWorkspace
class, title: string
proc getData(data: string): string =
let
his = getenv("HYPRLAND_INSTANCE_SIGNATURE")
socketPath = "/tmp/hypr" / his / ".socket.sock"
let socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
try:
socket.connectUnix(socketPath)
except OSError:
raise newException(HyprlandDefect, "Could not connect to Hyprland IPC UNIX path; is Hyprland running?")
socket.send(data)
let response = socket.recv(4096)
socket.close() # is this necessary?
return response
proc getDefaultWorkspaces(): seq[WorkspaceIcon] =
let clients = parseJson(getData("[-j]/clients")).to(seq[Client])
result = collect(for i in 1..9: WorkspaceIcon(id: i, icon:"",class:fmt"ws-button-{i - 1}"))
for client in clients:
let match = icons.getOrDefault(client.class,"")
if client.workspace.id < 0: continue
result[client.workspace.id - 1].icon &= match
for ws in result.mitems:
if ws.icon == "":
ws.icon = ""
proc getActive(m: Monitor): int {.inline.} = m.activeWorkspace.id
proc getState(): seq[seq[WorkspaceIcon]] =
let
monitors = parseJson(getData("[-j]/monitors")).to(seq[Monitor])
defaultWorkspaces = getDefaultWorkspaces()
for m in monitors:
result.add defaultWorkspaces
result[m.id][getActive(m) - 1].class &=
" " & "ws-button-open"
when isMainModule:
while true:
sleep 500
try:
echo (%* getState())
except JsonParsingError:
discard