初始化项目

This commit is contained in:
sugood
2020-10-09 17:36:23 +08:00
commit 0466ba9565
11 changed files with 729 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
hyper = {"cmd", "ctrl", "alt"}
hyperShift = {"alt", "shift"}
hyperCtrl = {"alt", "ctrl"}
hyperAlt = {"ctrl", "alt", "shift"}
+45
View File
@@ -0,0 +1,45 @@
local hotkey = require "hs.hotkey"
local grid = require "hs.grid"
local window = require "hs.window"
local application = require "hs.application"
local appfinder = require "hs.appfinder"
local fnutils = require "hs.fnutils"
grid.setMargins({0, 0})
applist = {
{shortcut = 'I',appname = 'IntelliJ IDEA'},
{shortcut = 'T',appname = 'Terminal'},
{shortcut = 'Z',appname = 'Calculator'},
{shortcut = 'D',appname = 'Finder'},
{shortcut = 'Q',appname = 'Activity Monitor'},
{shortcut = 'S',appname = 'System Preferences'},
{shortcut = 'C',appname = 'Google Chrome'},
{shortcut = 'A',appname = 'Launchpad'},
}
fnutils.each(applist, function(entry)
hotkey.bind({'ctrl', 'shift'}, entry.shortcut, entry.appname, function()
application.launchOrFocus(entry.appname)
-- toggle_application(applist[i].appname)
end)
end)
-- Toggle an application between being the frontmost app, and being hidden
function toggle_application(_app)
local app = appfinder.appFromName(_app)
if not app then
application.launchOrFocus(_app)
return
end
local mainwin = app:mainWindow()
if mainwin then
if mainwin == window.focusedWindow() then
mainwin:application():hide()
else
mainwin:application():activate(true)
mainwin:application():unhide()
mainwin:focus()
end
end
end
+18
View File
@@ -0,0 +1,18 @@
local pathwatcher = require "hs.pathwatcher"
local alert = require "hs.alert"
-- http://www.hammerspoon.org/go/#fancyreload
function reloadConfig(files)
doReload = false
for _, file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon", reloadConfig):start()
alert.show("Hammerspoon Config Reloaded")
+150
View File
@@ -0,0 +1,150 @@
local chooser = require("hs.chooser")
local console = require("hs.console")
console.clearConsole()
local historyPath= "~/.hammerspoon/history.json"
local maxLength = 1000
local history = {}
function initData()
if hs.json.read(historyPath) ~= nil then
history = hs.json.read(historyPath)
end
end
-- 初始化,读取本地数据
initData()
-- 查重
function duplicate(tablea,keys)
for k,v in ipairs(tablea) do
if v.text == keys then
return true
end
end
return false
end
-- 清除收尾空格
function trim(input)
return (string.gsub(input, "^%s*(.-)%s*$", "%1"))
end
-- 字符串分割
string.split = function(s, p)
local rt= {}
string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
return rt
end
-- 添加片段到记录列表中
function addToHistory()
local item = {}
local str = hs.pasteboard.getContents()
if str == nil then
hs.alert.show("文本为空,请重新选择文本!")
end
local list = string.split(str, "|")
if #list >1 then
item.subText = trim(string.sub(str,#list[1]+2,string.len(str)))
item.text = trim(list[1])
else
item.subText = trim(list[1])
item.text = trim(list[1])
end
if duplicate(history, item.text) then
hs.alert.show("片段已经存在")
return
end
if #history >= maxLength then
hs.alert.show("片段已超过:"..maxLength.."个")
return
end
table.insert(history, 1, item)
-- 排序
table.sort(history,function(a,b) return a.subText<b.subText end )
hs.json.write(history,historyPath, true, true)
hs.alert.show("成功添加片段:"..item.text)
end
-- 按下添加快捷键时映射到复制快捷键
function bindCopyKey()
hs.eventtap.keyStroke({ "cmd" }, "C")
end
-- 粘贴选中的片段
local completionFn = function(result)
if result then
hs.pasteboard.setContents(result.text)
hs.eventtap.keyStroke({ "cmd" }, "V")
print("keywords:"..result.text)
end
end
-- 添加片段到记录列表中
function modifyHistory(text, subText, isText, index)
if text and subText then
if isText and duplicate(history, text) then
hs.alert.show("片段已经存在")
return
end
history[index].text = text
history[index].subText = subText
-- 排序
table.sort(history,function(a,b) return a.subText<b.subText end )
hs.json.write(history,historyPath, true, true)
if isText then
hs.alert.show("成功修改片段内容:"..text)
else
hs.alert.show("成功修改片段说明:"..subText)
end
else
hs.alert.show("参数错误")
return
end
end
-- 右键弹出菜单
local menuFn = function(index)
if index then
menubar = hs.menubar.new(false)
menubar:setTitle("Hidden Menu")
menubar:setMenu( {
{ title = "菜单", fn = function() print("you clicked my menu item!") end },
{ title = "-" },
{ title = "修改片段内容", fn = function()
result,text = hs.dialog.textPrompt("修改片段内容", "请输入新的内容", history[index].text, "确定", "取消")
if result == "确定" then
modifyHistory(text,history[index].subText,true,index)
end
end },
{ title = "修改片段说明", fn = function()
result,subText = hs.dialog.textPrompt("修改片段说明", "请输入新的说明", history[index].subText, "确定", "取消")
if result == "确定" then
modifyHistory(history[index].text,subText,false,index)
end
end },
{ title = "-" },
{ title = "删除当前片段", fn = function()
if hs.dialog.blockAlert("确定删除以下片段?",history[index].text,"确定","取消","informational") == "确定" then
table.remove(history,index)
hs.json.write(history,historyPath, true, true)
hs.alert.show("成功删除片段")
end
end },
})
menubar:popupMenu(hs.mouse.getAbsolutePosition(), true)
else
hs.alert.show("错误")
end
end
-- 选取片段内容(按下快捷键时显示片段列表,点击选中的快捷键将自动粘贴)
hs.hotkey.bind({ "ctrl", "cmd" }, "V", function ()
chooser.new(completionFn)
:choices(history)
:rightClickCallback(menuFn)
:searchSubText(true)
:show()
end)
-- 添加片段(按下快捷键时做一个复制操作,并记录复制的内容到片段列表中)
hs.hotkey.bind({"ctrl", "cmd"}, "A", function ()
bindCopyKey()
if hs.dialog.blockAlert("添加片段:‘|’为分隔符,建议按如下格式","内容|keyword|示例|说明","确定","取消","informational") == "确定" then
addToHistory()
end
end)
+18
View File
@@ -0,0 +1,18 @@
local hotkey = require "hs.hotkey"
local caffeinate = require "hs.caffeinate"
local audiodevice = require "hs.audiodevice"
hotkey.bind(hyper, "L", function()
caffeinate.lockScreen()
-- caffeinate.startScreensaver()
end)
-- mute on sleep
function muteOnWake(eventType)
if (eventType == caffeinate.watcher.systemDidWake) then
local output = audiodevice.defaultOutputDevice()
output:setMuted(true)
end
end
caffeinateWatcher = caffeinate.watcher.new(muteOnWake)
caffeinateWatcher:start()
+169
View File
@@ -0,0 +1,169 @@
-- window management
local application = require "hs.application"
local hotkey = require "hs.hotkey"
local window = require "hs.window"
local layout = require "hs.layout"
local grid = require "hs.grid"
local hints = require "hs.hints"
local screen = require "hs.screen"
local alert = require "hs.alert"
local fnutils = require "hs.fnutils"
local geometry = require "hs.geometry"
local mouse = require "hs.mouse"
-- default 0.2
window.animationDuration = 0
-- left half
hotkey.bind(hyper, "Left", function()
if window.focusedWindow() then
window.focusedWindow():moveToUnit(layout.left50)
else
alert.show("No active window")
end
end)
-- right half
hotkey.bind(hyper, "Right", function()
window.focusedWindow():moveToUnit(layout.right50)
end)
-- top half
hotkey.bind(hyper, "Up", function()
window.focusedWindow():moveToUnit'[0,0,100,50]'
end)
-- bottom half
hotkey.bind(hyper, "Down", function()
window.focusedWindow():moveToUnit'[0,50,100,100]'
end)
-- left top quarter
hotkey.bind(hyperAlt, "Left", function()
window.focusedWindow():moveToUnit'[0,0,50,50]'
end)
-- right bottom quarter
hotkey.bind(hyperAlt, "Right", function()
window.focusedWindow():moveToUnit'[50,50,100,100]'
end)
-- right top quarter
hotkey.bind(hyperAlt, "Up", function()
window.focusedWindow():moveToUnit'[50,0,100,50]'
end)
-- left bottom quarter
hotkey.bind(hyperAlt, "Down", function()
window.focusedWindow():moveToUnit'[0,50,50,100]'
end)
-- full screen
hotkey.bind(hyper, 'F', function()
window.focusedWindow():toggleFullScreen()
end)
-- center window
hotkey.bind(hyper, 'C', function()
window.focusedWindow():centerOnScreen()
end)
-- maximize window
hotkey.bind(hyper, 'M', function() toggle_maximize() end)
-- defines for window maximize toggler
local frameCache = {}
-- toggle a window between its normal size, and being maximized
function toggle_maximize()
local win = window.focusedWindow()
if frameCache[win:id()] then
win:setFrame(frameCache[win:id()])
frameCache[win:id()] = nil
else
frameCache[win:id()] = win:frame()
win:maximize()
end
end
-- display a keyboard hint for switching focus to each window
hotkey.bind(hyperShift, '/', function()
hints.windowHints()
-- Display current application window
-- hints.windowHints(hs.window.focusedWindow():application():allWindows())
end)
-- switch active window
hotkey.bind(hyperShift, "H", function()
window.switcher.nextWindow()
end)
-- move active window to previous monitor
hotkey.bind(hyperShift, "Left", function()
window.focusedWindow():moveOneScreenWest()
end)
-- move active window to next monitor
hotkey.bind(hyperShift, "Right", function()
window.focusedWindow():moveOneScreenEast()
end)
-- move cursor to previous monitor
hotkey.bind(hyperCtrl, "Right", function ()
focusScreen(window.focusedWindow():screen():previous())
end)
-- move cursor to next monitor
hotkey.bind(hyperCtrl, "Left", function ()
focusScreen(window.focusedWindow():screen():next())
end)
--Predicate that checks if a window belongs to a screen
function isInScreen(screen, win)
return win:screen() == screen
end
function focusScreen(screen)
--Get windows within screen, ordered from front to back.
--If no windows exist, bring focus to desktop. Otherwise, set focus on
--front-most application window.
local windows = fnutils.filter(
window.orderedWindows(),
fnutils.partial(isInScreen, screen))
local windowToFocus = #windows > 0 and windows[1] or window.desktop()
windowToFocus:focus()
-- move cursor to center of screen
local pt = geometry.rectMidPoint(screen:fullFrame())
mouse.setAbsolutePosition(pt)
end
-- maximized active window and move to selected monitor
moveto = function(win, n)
local screens = screen.allScreens()
if n > #screens then
alert.show("Only " .. #screens .. " monitors ")
else
local toWin = screen.allScreens()[n]:name()
alert.show("Move " .. win:application():name() .. " to " .. toWin)
layout.apply({{nil, win:title(), toWin, layout.maximized, nil, nil}})
end
end
-- move cursor to monitor 1 and maximize the window
hotkey.bind(hyperShift, "1", function()
local win = window.focusedWindow()
moveto(win, 1)
end)
hotkey.bind(hyperShift, "2", function()
local win = window.focusedWindow()
moveto(win, 2)
end)
hotkey.bind(hyperShift, "3", function()
local win = window.focusedWindow()
moveto(win, 3)
end)