feat: world time command

This commit is contained in:
Klesh Wong 2021-08-05 00:47:38 +08:00
parent 7189b9795c
commit de43385680

View File

@ -2,14 +2,13 @@
Spotlight() { Spotlight() {
global global
SPOTLIGHT_ITEMS := 10 SPOTLIGHT_FONT_NAME := "Sarasa Mono SC"
SPOTLIGHT_FONT_NAME := "Sarasa Mono SC Nerd"
SPOTLIGHT_ITEM_FONT_STYLE := "s14 ceeeeee" SPOTLIGHT_ITEM_FONT_STYLE := "s14 ceeeeee"
SPOTLIGHT_SELECTED_ITEM_FONT_STYLE := "s14 c007acc" SPOTLIGHT_SELECTED_ITEM_FONT_STYLE := "s14 c007acc"
spotlightSelectedIndex := 1 SPOTLIGHT_ITEMS := 10
keyword := "" SPOTLIGHT_SELECTED_INDEX := 1
windows := GetWindowList() SPOTLIGHT_SELECTED_ITEM := null
; window position ; window position
GetCursorMonGeometry(x, y, w, h) GetCursorMonGeometry(x, y, w, h)
@ -18,16 +17,16 @@ Spotlight() {
wx := x + w / 2 - ww / 2 wx := x + w / 2 - ww / 2
wy := y + h / 2 - wh / 2 wy := y + h / 2 - wh / 2
Gui, New, +Theme -Border +AlwaysOnTop, Spotlight Gui, New, +Theme -Border +AlwaysOnTop -0xc00000, Spotlight
Gui, Font, %SPOTLIGHT_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME% Gui, Font, %SPOTLIGHT_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
Gui, Color, 252526 Gui, Color, 252526
; search box ; search box
keywordX := 10 userInputX := 10
keywordY := 10 userInputY := 10
keywordW := ww - 20 userInputW := ww - 20
keywordH := 30 userInputH := 30
Gui, Add, Edit, x%keywordX% y%keywordY% w%keywordW% h%keywordH% c252525 gOnChange vKeywordEdit Gui, Add, Edit, x%userInputX% y%userInputY% w%userInputW% h%userInputH% c252525 gOnChange vUserInputEdit
; ok button ; ok button
Gui, Add, Button, Default Hidden gOnEnter, Ok Gui, Add, Button, Default Hidden gOnEnter, Ok
@ -35,27 +34,24 @@ Spotlight() {
; result items ; result items
Loop %SPOTLIGHT_ITEMS% { Loop %SPOTLIGHT_ITEMS% {
ItemEdit%A_Index% := null ItemEdit%A_Index% := null
Gui, Add, Text, w%keywordW% vItemEdit%A_Index% Gui, Add, Text, w%userInputW% vItemEdit%A_Index%
} }
; load windows ; parse keyword
FilterWindowList() SpotlightProcessUserInput()
; show window on center ; show window on center
Gui, Show, X %wx% Y %wy% W %ww% H %wh% Gui, Show, X %wx% Y %wy% W %ww% H %wh%
spotlightSelectedIndex := spotlightSelectedIndex + 1
FilterWindowList()
Return Return
OnChange: OnChange:
GuiControlGet, keyword, , KeywordEdit local userInput := ""
FilterWindowList() GuiControlGet, userInput, , UserInputEdit
SpotlightProcessUserInput(userInput)
Return Return
OnEnter: OnEnter:
WinActivate, ahk_id %spotlightSelectedWindowId% SpotlightProcessUserInput(userInput, 1)
SetCursorToCenterOfActiveWin()
Gui, Destroy Gui, Destroy
Return Return
@ -64,55 +60,118 @@ Spotlight() {
Return Return
} }
FilterWindowList() { SpotlightProcessUserInput(userInput := "", stage := 0) {
global global SPOTLIGHT_SELECTED_INDEX := 1
local index := 1 mode := ""
idx := 0
kw := keyword ; extract "<mode-sign> " as mode, current only '>' as command mode
sl := 1 idx := RegExMatch(userInput, ">")
idx := RegExMatch(keyword, " \d")
if (idx) { if (idx) {
kw := SubStr(keyword, 1, idx-1) mode := SubStr(userInput, 1, 1)
sl := SubStr(keyword, idx+1) userInput := SubStr(userInput, 2)
} }
LogDebug("keyword {1}, selected {2}", kw, sl)
for winIndex, win in windows { ; extract " <number>" as selected index
name := win[1] idx := RegExMatch(userInput, " \d")
id := win[2] if (idx) {
if (name and InStr(name, kw)) { SPOTLIGHT_SELECTED_INDEX := SubStr(userInput, idx+1)
suffix := "" userInput := SubStr(userInput, 1, idx-1)
if (sl = index) {
Gui, Font, %SPOTLIGHT_SELECTED_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
spotlightSelectedWindowId := id
suffix := " <<< "
} else {
Gui, Font, %SPOTLIGHT_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
}
name := Format("{1} {2} {3}", index, name, suffix)
GuiControl, Font, ItemEdit%index%
GuiControl, , ItemEdit%index%, %name%
index := index + 1
}
} }
While index < SPOTLIGHT_ITEMS {
GuiControl, , ItemEdit%index% LogDebug("SPOTLIGHT >>> mode {1} input {2} stage {3}", mode, userInput, stage)
index := index + 1 ; enter mode subroutine
if (mode = ">") {
SpotlightRunCommand(userInput, stage)
} else {
SpotlightSwitchWindow(userInput, stage)
} }
} }
GetWindowList() { SpotlightUpdateItems(items) {
global
LogDebug("SPOTLIGHT >>> update ui items, count {1}, first text {2}", items.Length(), items[1][1])
; make sure items within limit
while (items.Length() > SPOTLIGHT_ITEMS) {
items.Pop()
}
; update first n lines
for itemIndex, item in items {
itemText := item[1]
if (itemIndex = SPOTLIGHT_SELECTED_INDEX) {
Gui, Font, %SPOTLIGHT_SELECTED_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
itemText := itemText . " <<< "
SPOTLIGHT_SELECTED_ITEM := item
} else {
Gui, Font, %SPOTLIGHT_ITEM_FONT_STYLE%, %SPOTLIGHT_FONT_NAME%
}
lineText := Format("{1} {2}", itemIndex, itemText)
LogDebug("SPOTLIGHT >>> line {1}", lineText)
GuiControl, Font, ItemEdit%itemIndex%
GuiControl, , ItemEdit%itemIndex%, %lineText%
}
; clear contents of rest lines
While itemIndex <= SPOTLIGHT_ITEMS {
itemIndex := itemIndex + 1
GuiControl, , ItemEdit%itemIndex%
}
}
SpotlightRunCommand(command, stage) {
funcName := Format("cmd_{1}", command)
if (IsFunc(funcName)) {
%funcName%()
}
}
SpotlightSwitchWindow(keyword, stage) {
global SPOTLIGHT_SELECTED_ITEM
LogDebug("SPOTLIGHT >>> switch window mode, keyword {1}, stage {2}", keyword, stage)
if (stage = 1) {
winId := SPOTLIGHT_SELECTED_ITEM[2]
LogDebug("SPOTLIGHT >>> switch to window {1} item length {2}", winId, SPOTLIGHT_SELECTED_ITEM.Length())
WinActivate, ahk_id %winId%
SetCursorToCenterOfActiveWin()
return
}
items := Array()
WinGet windows, List WinGet windows, List
wins := Array()
LogDebug("A_Gui: {1}", A_Gui)
Loop %windows% { Loop %windows% {
id := windows%A_Index% winId := windows%A_Index%
WinGetTitle wt, ahk_id %id% if (A_DefaultGui = winId) {
if (id = A_Gui or !wt) { LogDebug("SPOTLIGHT >>> skip default gui: {2}", winId, A_DefaultGui)
Continue Continue
} }
WinGet wp, ProcessName, ahk_id %id% WinGetTitle winTitle, ahk_id %winId%
name := Format("{1:-20} {2}", wp, wt) if (!winTitle) {
wins.Push(Array(name, id)) Continue
}
WinGet procName, ProcessName, ahk_id %winId%
itemText := Format("{1:-20} {2}", procName, winTitle)
if (itemText and !InStr(itemText, keyword)) {
Continue
}
items.Push(Array(itemText, winId))
} }
return wins SpotlightUpdateItems(items)
}
cmd_wt() {
TIMEZONES := { "Atlantic": -3 }
; dt := A_NowUTC
; dt += -3, Hours
; FormatTime, timeText, %A_Now%, dd HH:mm
; LogDebug("SPOTLIGHT >>> now {1}", timeText)
items := Array()
for zone, offset in TIMEZONES {
dt = %A_NowUTC%
dt += offset, Hours
FormatTime, timeText, %dt%, dd HH:mm
items.Push(Array(Format("{1:-20} {2}", zone, timeText)))
}
SpotlightUpdateItems(items)
} }