Advertisement
9551

Untitled

Apr 11th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 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=35,
  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. local sub = 0
  42. for k,v in ipairs(result) do
  43. if v[1] < 20 then
  44. sub = sub + 1
  45. else
  46. out[k-sub] = {match=v[1],str=v[2],data=v[3]}
  47. end
  48. end
  49. return out
  50. end
  51.  
  52. local words = {}
  53. local display = {"","","","",""}
  54.  
  55. for str in io.lines("words") do
  56. words[str] = 0
  57. end
  58.  
  59. local results = {}
  60.  
  61. for i=1,5 do
  62. results[i] = gui.create.button({
  63. x=2,y=i+1,width=36,height=1,
  64. name=tostring(i),
  65. on_click=function(object)
  66. box.input = object.text.text or ""
  67. box.cursor_pos=#box.input
  68. box.selected = true
  69. end,
  70. text=gui.text{
  71. text="no result",
  72. blit={
  73. "000000000",
  74. "888888888"
  75. },
  76. centered=false
  77. },
  78. logic_order=2,
  79. graphic_order=2
  80. })
  81. end
  82.  
  83. error(gui.execute(function()
  84. while true do
  85. local out = sort_strings(words,box.input)
  86. local cnt = 0
  87. local increm = 0
  88. while cnt < 6 do
  89. increm = increm + 1
  90. local string = out[increm] or {str=""}
  91. if #string.str >= #box.input then
  92. cnt = cnt + 1
  93. display[cnt] = string
  94. end
  95. if increm > #out then break end
  96. end
  97. sleep()
  98. end
  99. end,function(term)
  100. local shift = false
  101. for i=1,5 do
  102. local text = (display[i].str or ""):gsub(box.input,"")
  103. local x = ((display[i].str or ""):find(text) or 2)+1
  104. results[i].positioning.x = x
  105. if (box.input..text):match("^"..box.input:sub(1,x-2)..text) then shift = true end
  106. results[i].text = gui.text{
  107. text=text,
  108. blit={
  109. ("0"):rep(#text),
  110. ("8"):rep(#text)
  111. },
  112. centered=false
  113. }
  114. results[i].positioning.y = i+1+(shift and 0 or 1)
  115. results[i].positioning.width = #text
  116. end
  117. end),0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement