add alacritty_forker.nim

This commit is contained in:
Rasmus Moorats 2023-10-03 09:42:40 +03:00
parent e7ab6110ab
commit 65d4cea2e4
Signed by: xx
GPG key ID: FE14255A6AE7241C
2 changed files with 27 additions and 0 deletions

View file

@ -15,6 +15,7 @@ bin = @["b64d",
"urle",
"urle_all",
"urld",
"alacritty_forker",
"bcat"]
binDir = "bin"
@ -22,3 +23,4 @@ binDir = "bin"
# Dependencies
requires "nim >= 2.0.0"
requires "https://git.dog/xx/nim-swayipc.git#head"

25
src/alacritty_forker.nim Normal file
View file

@ -0,0 +1,25 @@
import std/[os, posix, net]
import swayipc2/[connection, commands]
proc main(): int =
let
sockpath = "/run/user/" & $geteuid() & "/Alacritty-*.sock"
s = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM, Protocol.IPPROTO_IP)
command = "{\"CreateWindow\":{\"terminal_options\":{\"working_directory\":\"" & getHomeDir() & "\",\"hold\":false,\"command\":[]},\"window_identity\":{\"title\":null,\"class\":null}}}"
defer: close s
for sock in walkPattern sockpath:
try:
s.connectUnix sock
s.send command
return 0
except:
continue
let
sway = connect()
ret = sway.run_command("exec alacritty")[0]
close sway
return if ret.success: 0 else: 2
quit main()