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