commit 0466ba9565cfb170662f98d9aea1f7868d47c91f Author: sugood <15820258199@163.com> Date: Fri Oct 9 17:36:23 2020 +0800 初始化项目 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0bf5dc9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 greyby + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a6516a3 --- /dev/null +++ b/README.md @@ -0,0 +1,77 @@ +# Hammerspoon Configuration + +## Usage + +1. Install [Hammerspoon](http://www.hammerspoon.org/) +2. `git clone https://github.com/sugood/hammerspoon.git ~/.hammerspoon` + +## Modifier keys +| | Key | +| --------- | -------------- | +| | Shift | +| | Control | +| | Option | +| | Command | + +## Features + +### Window Management + +#### Split Screen Actions + +* + Left half +* + Right half +* + Top half +* + Bottom half + +#### Quarter Screen Actions + +* + Left top quarter +* + Right bottom quarter +* + Right top quarter +* + Left bottom quarter + +#### Multiple Monitor + +##### Move Cursor + +* + Move cursor to next monitor +* + Move cursor to previous monitor + +##### Move Windows + +* + Move active window to previous monitor +* + Move active window to next monitor +* + 1 Move active window to monitor 1 and maximize the window +* + 2 Move active window to monitor 2 and maximize the window + + +#### Other + +* + F Full Screen +* + M Maximize Window +* + C Window Center + + +* + H Switch active window +* + / Display a keyboard hint for switching focus to each window + +### System Tools + +* + L Lock Screen + +### Launch Application + +* + I `IntelliJ IDEA` +* + T `Terminal` +* + Z `Calculator` +* + D `Finder` +* + Q `Activity Monitor` +* + S `System Preferences` +* + C `Google Chrome` +* + A `Launchpad` + +### Snippet & Memorandum + +* + A Add a snippet or memorandum +* + V List snippet or memos and use them \ No newline at end of file diff --git a/README_zh-CN.md b/README_zh-CN.md new file mode 100644 index 0000000..f267dfd --- /dev/null +++ b/README_zh-CN.md @@ -0,0 +1,75 @@ +# Hammerspoon 配置 + +## 使用方法 + +1. 安装 [Hammerspoon](http://www.hammerspoon.org/) +2. `git clone https://github.com/greyby/hammerspoon.git ~/.hammerspoon` + +## 快捷键图标 +| | 键位 | +| --------- | -------------- | +| | Shift | +| | Control | +| | Option | +| | Command | + +## 功能 + +### 窗口管理 + +#### 1/2 屏幕 + +* + 将当前窗口移动到左半屏 +* + 将当前窗口移动到右半屏 +* + 将当前窗口移动到上半屏 +* + 将当前窗口移动到下半屏 + +#### 1/4 屏幕 + +* + 将当前窗口移动到左上 1/4 屏 +* + 将当前窗口移动到右下 1/4 屏 +* + 将当前窗口移动到右上 1/4 屏 +* + 将当前窗口移动到左下 1/4 屏 + +#### 多个显示器 + +##### 移动光标 + +* + 把光标移动到下一个显示器 +* + 把光标移动到上一个显示器 + +##### 移动窗口 + +* + 将当前活动窗口移动到上一个显示器 +* + 将当前活动窗口移动到下一个显示器 +* + 1 将当前活动窗口移动到第一个显示器并窗口最大化 +* + 2 将当前活动窗口移动到第二个显示器并窗口最大化 + + +#### 其它 + +* + F 全屏 +* + M 最大化窗口 +* + C 将窗口放到中间 +* + H 切换活动窗口 +* + / 显示窗口切换的快捷键 + +### 系统工具 + +* + L 锁屏 + +### 快速启动 + +* + I `IntelliJ IDEA` +* + T `Terminal` +* + Z `Calculator` +* + D `Finder` +* + Q `Activity Monitor` +* + S `System Preferences` +* + C `Google Chrome` +* + A `Launchpad` + +### 片段和备忘录 + +* + A 添加一个片段或者备忘 +* + V 列出片段或备忘录并使用 \ No newline at end of file diff --git a/history.json b/history.json new file mode 100644 index 0000000..83f9eb8 --- /dev/null +++ b/history.json @@ -0,0 +1,146 @@ +[ + { + "subText" : "cmd|cat filename|显示或连接文件", + "text" : "cat" + }, + { + "subText" : "cmd|cd dirname|打开当前目录", + "text" : "cd" + }, + { + "subText" : "cmd|chgrp staff filename|改变文件或目录的所属组", + "text" : "chgrp" + }, + { + "subText" : "cmd|chmod -R 777 filename|改变文件或目录的权限", + "text" : "chmod" + }, + { + "subText" : "cmd|chown newowner:newowner filename|改变文件或目录的属主", + "text" : "chown" + }, + { + "subText" : "cmd|clear|清屏", + "text" : "clear" + }, + { + "subText" : "cmd|cp file1 file2|复制文件或目录", + "text" : "cp" + }, + { + "subText" : "cmd|date|显示系统的当前日期和时间", + "text" : "date" + }, + { + "subText" : "cmd|diff file1 file2|比较并显示两个文件的差异", + "text" : "diff" + }, + { + "subText" : "cmd|kill -9 pid|终止进程", + "text" : "kill" + }, + { + "subText" : "cmd|ln -s file1 file2|联接文件", + "text" : "ln" + }, + { + "subText" : "cmd|ls -l|显示当前目录的内容", + "text" : "ls" + }, + { + "subText" : "cmd|lsof -i:port|查看端口", + "text" : "lsof" + }, + { + "subText" : "cmd|mkdir dirname|创建一个目录", + "text" : "mkdir" + }, + { + "subText" : "cmd|mv file1 file2|移动文件名或目录、重命名", + "text" : "mv" + }, + { + "subText" : "cmd|passwd|修改用户密码", + "text" : "passwd" + }, + { + "subText" : "cmd|ping ip|给一个网络主机发送 回应请求", + "text" : "ping" + }, + { + "subText" : "cmd|ps u|显示进程当前状态", + "text" : "ps" + }, + { + "subText" : "cmd|reboot|重启电脑", + "text" : "reboot" + }, + { + "subText" : "cmd|rm -rf filename|删除文件或目录", + "text" : "rm" + }, + { + "subText" : "cmd|rmdir dirname|删除一个目录", + "text" : "rmdir" + }, + { + "subText" : "cmd|scp -r \/Users\/test username@ip:\/root|从本地将文件夹传输到服务器", + "text" : "scp -r" + }, + { + "subText" : "cmd|scp -r username@ip:\/data\/test \/Users\/mac\/Desktop|将服务器上的文件夹传输到本地", + "text" : "scp -r username" + }, + { + "subText" : "cmd|scp \/Users\/mac\/test.png username@ip:\/root |从本地将文件传输到服务器", + "text" : "scp" + }, + { + "subText" : "cmd|scp username@ip:\/data\/111.png \/Users\/mac\/Desktop|将服务器上的文件传输到本地", + "text" : "scp username" + }, + { + "subText" : "cmd|source ~\/.bash_profile|使环境变量生效", + "text" : "source ~\/.bash_profile" + }, + { + "subText" : "cmd|ssh username@ip|ssh远程登录", + "text" : "ssh" + }, + { + "subText" : "cmd|tail -f file|显示文件的最后几行、实时查看日志文件", + "text" : "tail" + }, + { + "subText" : "cmd|tar xvf filename.tar|解压格式tar", + "text" : "tar" + }, + { + "subText" : "cmd|telnet ip port|远程连接,需要先安装telnet", + "text" : "telnet" + }, + { + "subText" : "cmd|vi ~\/.bash_profile|修改环境变量", + "text" : "vi ~\/.bash_profile" + }, + { + "subText" : "code|java|author注释模板", + "text" : "\/** \n* @Title: ${file_name} \n* @Package ${package_name} \n* @Description: ${todo}(用一句话描述该文件做什么) \n* @author sugood\n* @date ${date} ${time} \n* @version V1.0 \n*\/" + }, + { + "subText" : "code|java|author注释模板", + "text" : "something" + }, + { + "subText" : "key|Command+Option+C|复制路径", + "text" : "Command+Option+C" + }, + { + "subText" : "key|Command+Q|关闭程序", + "text" : "Command+Q" + }, + { + "subText" : "key|Command+Shift+.|显示隐藏文件", + "text" : "Command+Shift+." + } +] \ No newline at end of file diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..b74fb5a --- /dev/null +++ b/init.lua @@ -0,0 +1,6 @@ +require "modules/reload" +require "modules/hotkey" +require "modules/system" +require "modules/windows" +require "modules/launcher" +require "modules/snippet" diff --git a/modules/hotkey.lua b/modules/hotkey.lua new file mode 100644 index 0000000..6b0f33a --- /dev/null +++ b/modules/hotkey.lua @@ -0,0 +1,4 @@ +hyper = {"cmd", "ctrl", "alt"} +hyperShift = {"alt", "shift"} +hyperCtrl = {"alt", "ctrl"} +hyperAlt = {"ctrl", "alt", "shift"} \ No newline at end of file diff --git a/modules/launcher.lua b/modules/launcher.lua new file mode 100644 index 0000000..7fa2eef --- /dev/null +++ b/modules/launcher.lua @@ -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 diff --git a/modules/reload.lua b/modules/reload.lua new file mode 100644 index 0000000..be2bae1 --- /dev/null +++ b/modules/reload.lua @@ -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") diff --git a/modules/snippet.lua b/modules/snippet.lua new file mode 100644 index 0000000..76b8dd2 --- /dev/null +++ b/modules/snippet.lua @@ -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 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) \ No newline at end of file