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] < 10 then
- sub = sub + 1
- else
- out[k-sub] = {match=v[1],str=v[2],data=v[3]}
- end
- end
- return out
- end
- local function keys(tbl)
- local keys = {}
- for k,_ in pairs(tbl) do
- table.insert(keys,k)
- end
- return keys
- end
- local function iterate_order(tbl)
- local indice = 0
- local keys = keys(tbl)
- table.sort(keys, function(a, b) return a>b end)
- return function()
- indice = indice + 1
- if tbl[keys[indice]] then return keys[indice],tbl[keys[indice]]
- else return end
- end
- end
- local words = {}
- local display = {}
- for str in io.lines("words") do
- words[str] = 0
- end
- local results = {}
- for i=1,10 do
- display[i] = {}
- results[i] = gui.create.button({
- x=2,y=i+1,width=36,height=1,
- name=tostring(i),
- on_click=function(object)
- box.input = object.full or ""
- box.cursor_pos=#box.input
- box.selected = true
- end,
- text=gui.text{
- text="",
- blit={
- "",
- ""
- },
- 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 < 11 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
- local lists = {}
- for i=1,10 do
- local text = (display[i].str or ""):gsub(box.input,"")
- local _,x = (display[i].str or ""):find("^.+("..text..")")
- x = x and x-#text+2 or 2
- if not lists[x] then lists[x] = {} end
- table.insert(lists[x],function(y)
- 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].full = display[i].str
- results[i].positioning.y = y+1+(shift and 0 or 1)
- results[i].positioning.width = #text
- end)
- end
- local nth = 0
- for k,v in iterate_order(lists) do
- for k,v in ipairs(v) do
- nth = nth + 1
- v(nth)
- end
- end
- end),0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement