Skip to content

Instantly share code, notes, and snippets.

@instance-id
Created March 13, 2023 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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)
--| 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'
}
#!/usr/local/bin/cyber
--| Imports and Setup ------------
--|-------------------------------
import os 'os'
args = os.args()
--| Arg Check ---------------
if args.len() < 2:
print "Usage: [larger|smaller|center]"
exit 1
currentDir = "{os.dirName(args[1])}"
adjustment = ""
if args.len() < 3:
adjustment = "center"
if args.len() == 3:
adjustment = args[2].utf8()
--| Load Config ------------------
--|-------------------------------
objCyon = loadConfig(currentDir)
if objCyon == {}:
print "Config is empty"
exit 1
--| Database Path ----------------
--|-------------------------------
dbPath = objCyon.dbpath
if dbPath == "":
print "DB Path is empty"
exit 1
--| Get Active Window ------------
--|-------------------------------
xdt = 'xdotool getactivewindow'
activeWin = runCmd(xdt)
--| Load Data Store --------------
--|-------------------------------
data = {}
loaded = loadDataStore(dbPath, activeWin)
print typeid(loaded)
--| Check if loaded is a map -----
--|-------------------------------
if typeid(loaded) is 11 and loaded.size() != 0:
data = { index: loaded.index, windowid: loaded.windowid }
else:
data = { index: 0, windowid: activeWin }
index = 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 }
--| Get Sizes from Config --------
--|-------------------------------
posx = objCyon.sizes[data.index].x
posy = objCyon.sizes[data.index].y
width = objCyon.sizes[data.index].w
height = objCyon.sizes[data.index].h
--| Run Commands -----------------
--|-------------------------------
doSize = "xdotool windowsize {activeWin} {width} {height}"
doMove = "xdotool windowmove {activeWin} {posx} {posy}"
runCmd(doSize)
runCmd(doMove)
--| Save Data Store --------------
--|-------------------------------
saveDataStore(dbPath, data)
--| Functions --------------------
--| Run Command ------------------
--|-------------------------------
func runCmd(arg):
argStr = arg
cmd = [ '/bin/bash', '-c', '{argStr}| tr -d "\n"' ]
res = execCmd(cmd)
if res.exited != 0:
print res.out
print res.err
else:
return res.out
func isMap(obj):
return obj.type() == #map
--| Load Config ------------------
--|-------------------------------
func loadConfig(cDir):
strCyon = readFile('{cDir}/config/config.cyon')
objCyon = parseCyon(strCyon.utf8())
return objCyon
--| Load Data Store --------------
--|-------------------------------
func loadDataStore(db, winId):
cyonFile = try os.openFile('{db}/{winId}', #read)
print cyonFile
strCyon = cyonFile.readToEnd().utf8()
cyonFile.close()
if strCyon == {} or strCyon == "":
return {}
else:
objCyon = parseCyon(strCyon)
return objCyon
--| Save Data Store --------------
--|-------------------------------
func saveDataStore(db, objCyon):
winId = objCyon.windowid
strCyon = toCyon(objCyon)
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 }
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment