Skip to content

Commit

Permalink
feat: Add hotkey to show/hide log while running (#29)
Browse files Browse the repository at this point in the history
* style: Use WinInfo in Workspace.ToString

* refactor(logger): Add method to show console while running

* feat: Set hotkey (Alt-F1) to toggle console
  • Loading branch information
imawizard authored Jul 13, 2024
1 parent 8a9ff54 commit 8da3a62
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
5 changes: 5 additions & 0 deletions lib/miguru/utils.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@ WinInfo(hwnd) {
try {
info := info " T=`"" WinGetTitle("ahk_id" hwnd) "`""
}
try {
left := 0, top := 0, width := 0, height := 0
RunDpiAware(() => WinGetPos(&left, &top, &width, &height, "ahk_id" hwnd))
info := info " (" width "x" height " @ " left "/" top ")"
}
return info
}

Expand Down
41 changes: 31 additions & 10 deletions lib/miguru/utils/Logger.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,11 @@ class Logger {
Logger.NO_COLOR := true
}
}
} else {
Logger.NO_COLOR := true
}

opts := StrLower(EnvGet("AHK_LOG"))
if (!attached && opts == "") || opts == "disable" {
Logger.Disabled := true
return
}

if !attached {
;; If AHK_LOG was set but there's no attached console.
DllCall("AllocConsole", "Int")
}

for part in StrSplit("debug," opts, ",") {
if !part {
continue
Expand All @@ -107,6 +99,13 @@ class Logger {
Logger.Levels[module] := 0
}
}

if (!attached && opts == "") || opts == "disable" {
Logger.Disabled := true
} else if !attached {
;; If AHK_LOG was set but there's no attached console.
DllCall("AllocConsole", "Int")
}
}

static Log(level, fmt, args*) {
Expand Down Expand Up @@ -148,6 +147,28 @@ class Logger {
FileAppend(t "." A_MSec m " [" l "] " s "`n", "*")
}
}

static ToggleConsole() {
if !DllCall("GetConsoleWindow", "Ptr") {
Logger.OpenConsole()
} else {
Logger.CloseConsole()
}
}

static OpenConsole() {
if !DllCall("GetConsoleWindow", "Ptr") {
DllCall("AllocConsole", "Int")
Logger.Disabled := false
}
}

static CloseConsole() {
if DllCall("GetConsoleWindow", "Ptr") {
Logger.Disabled := true
DllCall("FreeConsole", "Int")
}
}
}

Logger.Init()
Expand Down
30 changes: 29 additions & 1 deletion lib/miguru/workspaces.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,35 @@ class WorkspaceList {
MruTile => this._mruTile

ToString() {
return Stringify(this)
tiles := []
current := this._tiled.First

loop {
tiles.Push(WinInfo(current.data))
current := current.next
} until current == this._tiled.First

floating := []
for hwnd in this._floating {
floating.Push(WinInfo(hwnd))
}

return Type(this) "(" SubStr(Stringify({
Index: this._index,
Monitor: this._monitor.Index,
Windows: {
Active: {
Got: WinInfo(this._active),
Actual: WinInfo(WinExist("A")),
},
Tiles: {
All: tiles,
Mru: WinInfo(this._mruTile.data),
},
Floating: floating,
Mru: WinInfo(this._mruHwnd),
},
}), 2, -1) ")"
}

Hwnds {
Expand Down
4 changes: 4 additions & 0 deletions mwm.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Alt::return
*vk01::MoveActiveWindow()
*vk02::ResizeActiveWindow()

*F1::Logger.ToggleConsole()
*F2::mwm.Do("get-workspace-info")
*F3::mwm.Do("get-monitor-info")

#Hotif GetKeyState(mod1, "P") and GetKeyState("Shift", "P")

*1::mwm.Do("send-to-workspace", { workspace: 1 })
Expand Down

0 comments on commit 8da3a62

Please sign in to comment.