initial commit for alacritty-forker

This commit is contained in:
Rasmus Moorats 2023-02-25 15:08:46 +02:00
parent e56060e623
commit 27a404cd5c
Signed by: xx
GPG key ID: FE14255A6AE7241C
3 changed files with 27 additions and 115 deletions

View file

@ -1,21 +1,20 @@
# Package
version = "0.1.0"
author = "Rasmus Moorats"
description = "switch quick - sway window focuser / unfocuser"
description = "forks Alacritty from existing windows"
license = "GPL-3.0-or-later"
srcDir = "src"
bin = @["swick"]
bin = @["alacritty_forker"]
# Dependencies
requires "nim >= 1.6.6"
requires "swayipc2 >= 0.1.0"
let outdir = "bin/"
let output = outdir & "swick"
let output = outdir & bin[0]
task release, "build binary for release":
exec "mkdir -p " & outdir
exec "nim c -d:release --passC:-flto --passL:-flto --passL:-s --opt:speed --mm:orc --passC:-ffast-math -o:" & output & " src/swick.nim"
exec "nim c -d:release --passC:-flto --passL:-flto --passL:-s --opt:speed --mm:orc --passC:-ffast-math -o:" & output & " src/" & bin[0] & ".nim"
echo "built " & output
task clean, "clean workspace":

23
src/alacritty_forker.nim Normal file
View file

@ -0,0 +1,23 @@
import os
import posix
import system
import net
import swayipc2/[connection, commands]
proc main() =
let sockpath = "/run/user/" & $geteuid() & "/Alacritty-*.sock"
for sock in walkPattern(sockpath): # we only expect this loop to be executed 0 or 1 times
let s = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM, Protocol.IPPROTO_IP)
s.connectUnix(sock)
s.send("{\"CreateWindow\":{\"terminal_options\":{\"working_directory\":null,\"hold\":false,\"command\":[]},\"window_identity\":{\"title\":null,\"class\":null}}}")
close s
system.quit(0)
let sway = connect()
let ret = sway.run_command("exec alacritty")[0]
sway.close
system.quit(if ret.success: 0 else: 2)
when isMainModule:
main()

View file

@ -1,110 +0,0 @@
import std/parseopt
import system
import swayipc2/[connection, commands, util]
const helpText = """swick - quickly launch or focus/unfocus application
usage: swick -u:use -i:identifier -c:cmd
flags:
u, use (default: class) which qualifier to use (app_id, class)
i, identifier the window's identifier as per the qualifier
c, cmd command to use to launch the application
examples:
# focus or launch spotify
swick -u:class -i:Spotify -c:spotify
# focus or launch obsidian with extra flags, implicitly use class
swick -i:obsidian -c:'/bin/electron18 /usr/lib/obsidian/app.asar --force-device-scale-factor=1'"""
proc optErr(err: string) {.inline.} =
echo err & ", bailing"
echo "run with flag -h for help"
system.quit(1)
proc parseOpts(): (string, string, string) =
var use, identifier, cmd: string
var p = initOptParser()
while true:
p.next()
case p.kind
of cmdEnd: break
of cmdArgument:
optErr("erronous extra argument `" & p.val & "`")
of cmdShortOption, cmdLongOption:
if p.key == "h" or p.key == "help":
echo helpText
system.quit(0)
if p.val == "":
optErr("no value set for flag " & p.key)
case p.key
of "u", "use":
if p.val == "class" or p.val == "app_id":
use = p.val
else:
optErr("unknown use value `" & p.val)
of "i", "identifier":
identifier = p.val
of "c", "cmd":
cmd = p.val
else:
optErr("unknown key and value pair: " & p.key & ", " & p.val)
if identifier == "":
optErr("identifier not specified")
if cmd == "":
optErr("start-up command not specified")
if use == "":
use = "class"
return (use, identifier, cmd)
when defined(async):
import std/asyncdispatch
proc main() {.async.} =
let (use, identifier, cmd) = parseOpts()
let sway = await connect_async()
let tree = await sway.get_tree
let nodes =
if use == "class": tree.filterNodesByClass(identifier, 1)
else: tree.filterNodesByAppID(identifier, 1)
let sway_cmd =
if nodes.len == 0: "exec " & cmd
else:
let selector = "[" & use & "=" & identifier & "] "
if nodes[0].focused: selector & "move scratchpad"
else: selector & "focus"
let ret = (await sway.run_command(sway_cmd))[0]
sway.close
system.quit(if ret.success: 0 else: 2)
else:
proc main() =
let (use, identifier, cmd) = parseOpts()
let sway = connect()
let tree = sway.get_tree
let nodes =
if use == "class": tree.filterNodesByClass(identifier, 1)
else: tree.filterNodesByAppID(identifier, 1)
let sway_cmd =
if nodes.len == 0: "exec " & cmd
else:
let selector = "[" & use & "=" & identifier & "] "
if nodes[0].focused: selector & "move scratchpad"
else: selector & "focus"
let ret = sway.run_command(sway_cmd)[0]
sway.close
system.quit(if ret.success: 0 else: 2)
when isMainModule:
when defined(async):
waitFor main()
else:
main()