Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local GuiH = require("GuiH")
- local gui = GuiH.create_gui(term.current())
- local box = gui.create.inputbox({
- x=2,y=2,width=35,
- name="test",
- background_color=colors.gray,
- text_color=colors.white
- })
- local function fuzzy_match(str, pattern)
- local part = 100/math.max(#str,#pattern)
- local str_len = string.len(str)
- local pattern_len = string.len(pattern)
- local dp = {}
- for i = 0, str_len do
- dp[i] = {}
- dp[i][0] = i
- end
- for j = 0, pattern_len do
- dp[0][j] = j
- end
- for i = 1, str_len do
- for j = 1, pattern_len do
- local cost = 0
- if string.sub(str, i, i) ~= string.sub(pattern, j, j) then
- cost = 1
- end
- dp[i][j] = math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost)
- end
- end
- return 100-dp[str_len][pattern_len]*part
- end
- local function sort_strings(str_array, pattern)
- local result,out = {},{}
- for k, str in pairs(str_array) do
- table.insert(result,{fuzzy_match(k, pattern),k,str})
- end
- table.sort(result, function(a, b) return a[1] > b[1] end)
- local sub = 0
- for k,v in ipairs(result) do
- if v[1] < 20 then
- sub = sub + 1
- else
- out[k-sub] = {match=v[1],str=v[2],data=v[3]}
- end
- end
- return out
- end
- local words = {}
- local display = {"","","","",""}
- for str in io.lines("words") do
- words[str] = 0
- end
- local results = {}
- for i=1,5 do
- results[i] = gui.create.button({
- x=2,y=i+1,width=36,height=1,
- name=tostring(i),
- on_click=function(object)
- box.input = object.text.text or ""
- box.cursor_pos=#box.input
- box.selected = true
- end,
- text=gui.text{
- text="no result",
- blit={
- "000000000",
- "888888888"
- },
- centered=false
- },
- logic_order=2,
- graphic_order=2
- })
- end
- error(gui.execute(function()
- while true do
- local out = sort_strings(words,box.input)
- local cnt = 0
- local increm = 0
- while cnt < 6 do
- increm = increm + 1
- local string = out[increm] or {str=""}
- if #string.str >= #box.input then
- cnt = cnt + 1
- display[cnt] = string
- end
- if increm > #out then break end
- end
- sleep()
- end
- end,function(term)
- local shift = false
- for i=1,5 do
- local text = (display[i].str or ""):gsub(box.input,"")
- local x = ((display[i].str or ""):find(text) or 2)+1
- results[i].positioning.x = x
- if (box.input..text):match("^"..box.input:sub(1,x-2)..text) then shift = true end
- results[i].text = gui.text{
- text=text,
- blit={
- ("0"):rep(#text),
- ("8"):rep(#text)
- },
- centered=false
- }
- results[i].positioning.y = i+1+(shift and 0 or 1)
- results[i].positioning.width = #text
- end
- end),0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement