Skip to content

Instantly share code, notes, and snippets.

@instance-id
Last active October 20, 2024 02:17
Show Gist options
  • Save instance-id/e6938cf9671cd337c4066f89aa98fe88 to your computer and use it in GitHub Desktop.
Save instance-id/e6938cf9671cd337c4066f89aa98fe88 to your computer and use it in GitHub Desktop.
Converting a Window size cycling acript from PowerShell (linux) to Cyber (scripting language built with Zig)
#!/usr/local/bin/cyber
--| Imports and Setup ------------
--|-------------------------------
use os
use cy
var args = os.args()
--| Arg Check ---------------
if args.len() < 2:
print 'Usage: [larger|smaller|center]'
os.exit(1)
for args -> arg:
print arg
var currentDir = os.dirName(#modUri).?
-- var currentDir = "$(os.dirName(args[1]).?)"
print "Current Dir: $(currentDir)"
var adjustment = ""
if args.len() < 3:
adjustment = 'center'
if args.len() == 3:
adjustment = args[2]
--| Load Config ------------------
--|-------------------------------
var objCyon = loadConfig(currentDir) as Map
if objCyon == Map{}:
print 'Config is empty'
os.exit(1)
print "Config: $(objCyon)"
--| Database Path ----------------
--|-------------------------------
-- var dbPath = objCyon.dbpath
var dbPath = objCyon['dbpath']
print "DB Path: $(dbPath)"
if dbPath == "":
print 'DB Path is empty'
os.exit(1)
--| Get Active Window ------------
--|-------------------------------
var xdt = 'xdotool getactivewindow'
var activeWin = runCmd(xdt)
print "Active Window: $(activeWin)"
--| Load Data Store --------------
--|-------------------------------
-- var data = [:]
var data = {}
var loaded = loadDataStore(dbPath, activeWin) as Map
--| Check if loaded is a map -----
if typeOf(loaded) == Map and loaded != Map{}:
data = { index=loaded['index'], windowid=loaded['windowid'] }
else:
data = { index=0, windowid=activeWin }
var index int = data.index
--| Adjust Window ----------------
if adjustment == 'larger':
index += 1
if index > objCyon['sizes'].len() - 1:
index = 0
else adjustment == 'smaller':
index -= 1
if index < 0:
index = objCyon['sizes'].len() - 1
data = { index=index, windowid=activeWin }
print data.index
print objCyon['sizes'][data.index]['x']
--| Get Sizes from Config --------
var posx = objCyon['sizes'][data.index]['x']
var posy = objCyon['sizes'][data.index]['y']
var width = objCyon['sizes'][data.index]['w']
var height = objCyon['sizes'][data.index]['h']
--| Run Commands -----------------
var doSize = "xdotool windowsize $(activeWin) $(width) $(height)"
var doMove = "xdotool windowmove $(activeWin) $(posx) $(posy)"
runCmd(doSize)
runCmd(doMove)
--| Save Data Store --------------
saveDataStore(dbPath, data)
--| Functions --------------------
--| Run Command ------------------
func runCmd(arg String) String:
var argStr = arg
var cmd = List[String]{ '/bin/bash', '-c', "$(argStr)| tr -d \"\n\"" }
var res = os.execCmd(cmd)
if res['exited'] != 0:
print res['out']
print res['err']
else:
return res['out']
func isMap(obj dyn) bool:
return typeOf(obj) == Map
--| Load Config ------------------
--|-------------------------------
func loadConfig(cDir String) any:
var strCyon = os.readFile("$(cDir)/config/z_config.cyon")
print "Config: $(strCyon)"
var objCyon = cy.parseCyon(strCyon)
-- var objCyon = parseCyon(strCyon.utf8())
return objCyon
--| Load Data Store --------------
--|-------------------------------
func loadDataStore(db String, winId String) any:
print "Loading: $(db)/$(winId)"
var cyonFile = try os.readFile("$(db)/$(winId)")
print cyonFile
if cyonFile == error.FileNotFound:
print 'File not found'
return Map{}
var strCyon = String(cyonFile)
if strCyon == Map{} or strCyon == '':
return Map{}
else:
print "Loaded: $(strCyon)"
var objCyon = cy.parseCyon(String(strCyon))
return objCyon
--| Save Data Store --------------
--|-------------------------------
func saveDataStore(db String, objCyon Table):
var winId = objCyon['windowid']
var strCyon = cy.toCyon(objCyon)
os.writeFile("$(db)/$(winId)", strCyon)
{
$toolDest = "org.gnome.Shell"
$toolPath = "/id/instance/gnomeshell/tool"
$toolMethod = "id.instance.gnomeshell.tool.UnsafeMode"
$toolCommand = "gdbus call --session --dest $toolDest --object-path $toolPath --method $toolMethod true"
try { RunBashCommand $toolCommand }
catch { echo "Error: ${_}"; kvset $unsec "false"; exit 1 }
kvset $unsec "true"
}
if ($(kvget $unsec) -match 'false'){ SetUnsafeMode }
try {
$xWindowId = xdotool getactivewindow
$currentSize.windowId = $xWindowId
$currentSize.windowPath = "windows/${xWindowId}"
}
catch { echo "Error: ${_}"; exit 1 }
if(kvexists $currentSize.windowPath) {
$current = kvget $currentSize.windowPath
echo "Current: $current"
echo $current
} else {
$current = $null
}
if ($null -ne $current) {
$currentJson = ($current | ConvertFrom-Json)
$currentSize.index = $currentJson.index
}
# --| PositionWindow -------------------
# --|----------------------------------
if ($xWindowId) {
if ($larger) {
$currentSize.index++
if ($currentSize.index -gt 4) { $currentSize.index = 0 }
}
if ($smaller) {
$currentSize.index--
if ($currentSize.index -lt 0) { $currentSize.index = 4 }
}
$sizeTable = $sizePosLookup[$currentSize.index]
$posX = $sizeTable.x
$posY = $sizeTable.y
$width = $sizeTable.w
$height = $sizeTable.h
try { xdotool windowsize $xWindowId $width $height }
catch { echo "Error: ${_}"; exit 1 }
try { xdotool windowmove $xWindowId $posX $posY }
catch { echo "Error: ${_}"; exit 1 }
xdotool windowsize $xWindowId $width $height
xdotool windowmove $xWindowId $posX $posY
# gdbus call --session --dest org.gnome.Shell `
# --object-path /id/instance/gnomeshell/tool `
# --method id.instance.gnomeshell.tool.MoveResize `
# $windowId $posX $posY $width $height
kvset $currentSize.windowPath ($currentSize | ConvertTo-Json )
}
@{
sizes = @(
@{ x = 2790; y = 860; w = 820; h = 468 }
@{ x = 2678; y = 797; w = 1044; h = 594 }
@{ x = 2590; y = 748; w = 1220; h = 693 }
@{ x = 2390; y = 635; w = 1620; h = 918 }
@{ x = 2060; y = 577; w = 2280; h = 1034 }
)
}
--| Data Store -----------------------------
--|-----------------------------------------
{
sizes= {
{ x=2790, y=860, w=820, h=468 },
{ x=2678, y=797, w=1044, h=594 },
{ x=2590, y=748, w=1220, h=693 },
{ x=2390, y=635, w=1620, h=918 },
{ x=2060, y=577, w=2280, h=1034 }
},
dbpath = '/mnt/ramdisk/.kv/windows/'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment