From 7352b399f48c087a878d0756ef93fb4fbb2c9a8d Mon Sep 17 00:00:00 2001 From: sugood <15820258199@163.com> Date: Sun, 11 Oct 2020 16:29:33 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E5=A4=8D=E5=8F=B3=E9=94=AE?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=AE=9A=E4=BD=8D=E4=B8=8D=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B2=E3=80=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=86=E6=9C=AC=E5=9C=B0=E7=89=87=E6=AE=B5=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=96=87=E4=BB=B6=E5=90=8E=E8=87=AA=E5=8A=A8Reload=20?= =?UTF-8?q?config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- history.json => data/history.json | 8 +++++-- init.lua | 3 +++ modules/snippet.lua | 40 +++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 9 deletions(-) rename history.json => data/history.json (94%) diff --git a/history.json b/data/history.json similarity index 94% rename from history.json rename to data/history.json index 83f9eb8..1c9ead8 100644 --- a/history.json +++ b/data/history.json @@ -59,6 +59,10 @@ "subText" : "cmd|mv file1 file2|移动文件名或目录、重命名", "text" : "mv" }, + { + "subText" : "cmd|npm install |使用npm安装软件", + "text" : "npm install " + }, { "subText" : "cmd|passwd|修改用户密码", "text" : "passwd" @@ -128,8 +132,8 @@ "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" : "ins|brew install node|安装node", + "text" : "brew install node" }, { "subText" : "key|Command+Option+C|复制路径", diff --git a/init.lua b/init.lua index b74fb5a..eed58aa 100644 --- a/init.lua +++ b/init.lua @@ -4,3 +4,6 @@ require "modules/system" require "modules/windows" require "modules/launcher" require "modules/snippet" + +-- 监听~./hammerspoon/data/中的文件,如果有改变就自动更新 +hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/data", hs.reload):start() diff --git a/modules/snippet.lua b/modules/snippet.lua index 76b8dd2..2fd703e 100644 --- a/modules/snippet.lua +++ b/modules/snippet.lua @@ -2,26 +2,43 @@ local chooser = require("hs.chooser") local console = require("hs.console") console.clearConsole() -local historyPath= "~/.hammerspoon/history.json" +local historyPath= "~/.hammerspoon/data/history.json" local maxLength = 1000 local history = {} +local mChooser function initData() if hs.json.read(historyPath) ~= nil then history = hs.json.read(historyPath) end + isSearck = false end -- 初始化,读取本地数据 initData() -- 查重 -function duplicate(tablea,keys) - for k,v in ipairs(tablea) do +function duplicate(table,keys) + for k,v in ipairs(table) do if v.text == keys then return true end end return false end +-- 字符串判空 +function isEmpty(str) + return str == nil or str == '' +end +-- 查询text是否存在并返回索引index +-- 等于0为没有查询到,大于0为查询到 +function searchByText(table,text) + for k,v in ipairs(table) do + if v.text == text then + print("结果:"..k) + return k + end + end + return 0 +end -- 清除收尾空格 function trim(input) return (string.gsub(input, "^%s*(.-)%s*$", "%1")) @@ -100,8 +117,17 @@ function modifyHistory(text, subText, isText, index) end end -- 右键弹出菜单 -local menuFn = function(index) - if index then +local rightClickCallbackFn = function(index) + if index and index > 0 then + local selectResult = mChooser:selectedRowContents(index) + if selectResult == nil or isEmpty(selectResult.text) then + return + end + index = searchByText(history,selectResult.text) + if index == 0 then + hs.alert.show("找不到片段") + return + end menubar = hs.menubar.new(false) menubar:setTitle("Hidden Menu") menubar:setMenu( { @@ -135,9 +161,9 @@ local menuFn = function(index) end -- 选取片段内容(按下快捷键时显示片段列表,点击选中的快捷键将自动粘贴) hs.hotkey.bind({ "ctrl", "cmd" }, "V", function () - chooser.new(completionFn) + mChooser = chooser.new(completionFn) :choices(history) - :rightClickCallback(menuFn) + :rightClickCallback(rightClickCallbackFn) :searchSubText(true) :show() end)