diff --git a/.idea/.hammerspoon.iml b/.idea/.hammerspoon.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/.hammerspoon.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e632461 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..dae6639 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,878 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + SubStringGetByteCount + historyPath + translation + hs.json + setState + isEm + webview + console.clearConsole() + menubar + config + menubar.new(true) + searchResult + keywords + hs.hotkey.bind + hs.hotkey + displayIdle + syntaxHighlight + hs.alert.show + 切换 + isSyncTime + hs.dialog + else if + elseif + colorDialog + appid + mChooser + baiduAppSecret + baiduAppid + hs.dialog.blockAlert + ydAppKey + + + self.menubar + macMenubar + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+                
+            
+        
+        
+        
+ +
+ + + + + ]] + + return html +end + + +--- KSheet:hide() +--- Method +--- Hide the cheatsheet view. +function hide(time) + sheetView:hide(time) +end + +function init() + local cscreen = hs.screen.mainScreen() + local cres = cscreen:fullFrame() + sheetView = hs.webview.newBrowser({ + x = cres.x+cres.w*0.15/2, + y = cres.y+cres.h*0.25/2, + w = cres.w*0.85, + h = cres.h*0.75 + }) + sheetView:windowTitle("CheatSheets") + sheetView:windowStyle("utility") + sheetView:titleVisibility("hidden") + sheetView:allowGestures(true) + sheetView:allowNewWindows(false) + sheetView:allowTextEntry(true) + sheetView:closeOnEscape(true) + sheetView:bringToFront(false) + sheetView:darkMode(true) + sheetView:reload(false) + sheetView:level(hs.drawing.windowLevels.mainMenu) +end + +--- KSheet:show() +--- Method +--- Show current application's keybindings in a view. +function show(time) + local webcontent = generateHtml() + sheetView:html(webcontent, "http://localhost") + sheetView:show(time) +end + +function toggle() + if sheetView and sheetView:hswindow() and sheetView:hswindow():isVisible() then + hide(2) + else + show(2) + end +end + +init() + +-- 添加片段(按下快捷键时做一个复制操作,并记录复制的内容到片段列表中) +hs.hotkey.bind(hyperCmd, "G", function () + toggle() +end) diff --git a/modules/snippet.lua b/modules/snippet.lua index 2fd703e..59def20 100644 --- a/modules/snippet.lua +++ b/modules/snippet.lua @@ -1,7 +1,5 @@ local chooser = require("hs.chooser") -local console = require("hs.console") -console.clearConsole() local historyPath= "~/.hammerspoon/data/history.json" local maxLength = 1000 local history = {} @@ -24,10 +22,7 @@ function duplicate(table,keys) end return false end --- 字符串判空 -function isEmpty(str) - return str == nil or str == '' -end + -- 查询text是否存在并返回索引index -- 等于0为没有查询到,大于0为查询到 function searchByText(table,text) @@ -120,7 +115,7 @@ end local rightClickCallbackFn = function(index) if index and index > 0 then local selectResult = mChooser:selectedRowContents(index) - if selectResult == nil or isEmpty(selectResult.text) then + if selectResult == nil or stringIsEmpty(selectResult.text) then return end index = searchByText(history,selectResult.text) @@ -160,7 +155,7 @@ local rightClickCallbackFn = function(index) end end -- 选取片段内容(按下快捷键时显示片段列表,点击选中的快捷键将自动粘贴) -hs.hotkey.bind({ "ctrl", "cmd" }, "V", function () +hs.hotkey.bind(hyperCmd, "V", function () mChooser = chooser.new(completionFn) :choices(history) :rightClickCallback(rightClickCallbackFn) @@ -168,7 +163,7 @@ hs.hotkey.bind({ "ctrl", "cmd" }, "V", function () :show() end) -- 添加片段(按下快捷键时做一个复制操作,并记录复制的内容到片段列表中) -hs.hotkey.bind({"ctrl", "cmd"}, "A", function () +hs.hotkey.bind(hyperCmd, "X", function () bindCopyKey() if hs.dialog.blockAlert("添加片段:‘|’为分隔符,建议按如下格式","内容|keyword|示例|说明","确定","取消","informational") == "确定" then addToHistory() diff --git a/modules/timesync.lua b/modules/timesync.lua new file mode 100644 index 0000000..d0a7054 --- /dev/null +++ b/modules/timesync.lua @@ -0,0 +1,59 @@ +--- +--- 时间同步,开启后在系统或屏幕唤醒后自动同步一次时间 +--- Created by sugood(https://github.com/sugood). +--- DateTime: 2020/10/23 11:12 +--- + +function execTimeSync() + local shell_command = 'expect ~/.hammerspoon/timesync/timesync.sh '.. config[1].rootPassword + result = table.pack(hs.execute(shell_command,true)) + print(result[1]) +end +function caffeinateCallback(eventType) + if (eventType == hs.caffeinate.watcher.screensDidSleep) then + print("screensDidSleep") + elseif (eventType == hs.caffeinate.watcher.screensDidWake) then + execTimeSync() + hs.reload() + print("screensDidWake") + elseif (eventType == hs.caffeinate.watcher.systemDidWake) then + execTimeSync() + hs.reload() + print("systemDidWake") + elseif (eventType == hs.caffeinate.watcher.screensDidLock) then + print("screensDidLock") + elseif (eventType == hs.caffeinate.watcher.screensDidUnlock) then + print("screensDidUnlock") + end +end + +function initData() + if config ~=nil and config[1].isSyncTime == 'on' then + caffeinateWatcher = hs.caffeinate.watcher.new(caffeinateCallback) + caffeinateWatcher:start() + + -- 添加片段(按下快捷键时做一个复制操作,并记录复制的内容到片段列表中) + hs.hotkey.bind(hyperCmd, "T", function () + if hs.json.read(configPath) ~= nil then + config = hs.json.read(configPath) + end + if(stringIsEmpty(config[1].rootPassword)) then + local result,subText = hs.dialog.textPrompt("输入开机密码", "时间同步操作需要开机密码", '', "确定", "取消", true) + if result == "确定" then + if(stringIsEmpty(subText) == false) then + config[1].rootPassword = subText + hs.json.write(config,configPath, true, true) + execTimeSync() + hs.alert.show('同步时间') + end + end + else + execTimeSync() + hs.alert.show('同步时间') + end + end) + end +end +-- 初始化,读取本地数据 +initData() + diff --git a/timesync/timesync.sh b/timesync/timesync.sh new file mode 100755 index 0000000..210efaf --- /dev/null +++ b/timesync/timesync.sh @@ -0,0 +1,15 @@ +#timesyne.sh +#expect的安装路径 +#/usr/local/bin/expect这个路径安装完expect之后 which expect可以查看 +#!/usr/local/bin/expect + +set timeout 10 +set passwd [lindex $argv 0] + +spawn sudo sntp -sS ntp.aliyun.com +#根据输出传递数据,在这里是等待密码提示显示 +expect "*assword*" +send "$passwd\n" +#interact +expect eof +exit