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)