Advertisement
9551

Untitled

Apr 11th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. local GuiH = require("GuiH")
  2. local gui = GuiH.create_gui(term.current())
  3.  
  4. local box = gui.create.inputbox({
  5.     x=2,y=2,width=20,
  6.     name="test",
  7.     background_color=colors.gray,
  8.     text_color=colors.white
  9. })
  10.  
  11. local function fuzzy_match(str, pattern)
  12.     local part = 100/math.max(#str,#pattern)
  13.     local str_len = string.len(str)
  14.     local pattern_len = string.len(pattern)
  15.     local dp = {}
  16.     for i = 0, str_len do
  17.         dp[i] = {}
  18.         dp[i][0] = i
  19.     end
  20.     for j = 0, pattern_len do
  21.         dp[0][j] = j
  22.     end
  23.     for i = 1, str_len do
  24.         for j = 1, pattern_len do
  25.             local cost = 0
  26.             if string.sub(str, i, i) ~= string.sub(pattern, j, j) then
  27.                 cost = 1
  28.             end
  29.             dp[i][j] = math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost)
  30.         end
  31.     end
  32.     return 100-dp[str_len][pattern_len]*part
  33. end
  34.  
  35. local function sort_strings(str_array, pattern)
  36.     local result,out = {},{}
  37.     for k, str in pairs(str_array) do
  38.         table.insert(result,{fuzzy_match(k, pattern),k,str})
  39.     end
  40.     table.sort(result, function(a, b) return a[1] > b[1] end)
  41.     for k,v in ipairs(result) do
  42.         out[k] = {match=v[1],str=v[2],data=v[3]}
  43.     end
  44.     return out
  45. end
  46.  
  47. local words = {}
  48. local display = {"","","","",""}
  49.  
  50. for str in io.lines("words") do
  51.     words[str] = 0
  52. end
  53.  
  54. local results = {}
  55.  
  56. for i=1,5 do
  57.     results[i] = gui.create.button({
  58.         x=2,y=3+i,width=21,height=1,
  59.         name=tostring(i),
  60.         on_click=function(object)
  61.             box.input = object.text.text or ""
  62.             box.cursor_pos=#box.input
  63.             box.selected = true
  64.         end,
  65.         text=gui.text{
  66.             text="no result",
  67.             blit={
  68.                 "000000000",
  69.                 "888888888"
  70.             },
  71.             centered=false
  72.         },
  73.         background_color=colors.gray,
  74.         logic_order=2
  75.     })
  76. end
  77.  
  78. error(gui.execute(function()
  79.     while true do
  80.         local out = sort_strings(words,box.input)
  81.         local cnt = 0
  82.         local increm = 0
  83.         while cnt < 6 do
  84.             increm = increm + 1
  85.             local string = out[increm] or {str=""}
  86.             if #string.str > #box.input then
  87.                 cnt = cnt + 1
  88.                 display[cnt] = string
  89.             end
  90.             if increm > #out then break end
  91.         end
  92.         sleep()
  93.     end
  94. end,function(term)
  95.     for i=1,5 do
  96.         results[i].text = gui.text{
  97.             text=display[i].str or "",
  98.             blit={
  99.                 ("0"):rep(#display[i].str or ""),
  100.                 ("8"):rep(#display[i].str or "")
  101.             },
  102.             centered=false
  103.         }
  104.     end
  105. end))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement