Advertisement
9551

Untitled

Apr 11th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 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] < 10 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 function keys(tbl)
  53. local keys = {}
  54. for k,_ in pairs(tbl) do
  55. table.insert(keys,k)
  56. end
  57. return keys
  58. end
  59.  
  60. local function iterate_order(tbl)
  61. local indice = 0
  62. local keys = keys(tbl)
  63. table.sort(keys, function(a, b) return a>b end)
  64. return function()
  65. indice = indice + 1
  66. if tbl[keys[indice]] then return keys[indice],tbl[keys[indice]]
  67. else return end
  68. end
  69. end
  70.  
  71. local words = {}
  72. local display = {}
  73.  
  74. for str in io.lines("words") do
  75. words[str] = 0
  76. end
  77.  
  78. local results = {}
  79.  
  80. for i=1,10 do
  81. display[i] = {}
  82. results[i] = gui.create.button({
  83. x=2,y=i+1,width=36,height=1,
  84. name=tostring(i),
  85. on_click=function(object)
  86. box.input = object.full or ""
  87. box.cursor_pos=#box.input
  88. box.selected = true
  89. end,
  90. text=gui.text{
  91. text="",
  92. blit={
  93. "",
  94. ""
  95. },
  96. centered=false
  97. },
  98. logic_order=2,
  99. graphic_order=2
  100. })
  101. end
  102.  
  103. error(gui.execute(function()
  104. while true do
  105. local out = sort_strings(words,box.input)
  106. local cnt = 0
  107. local increm = 0
  108. while cnt < 11 do
  109. increm = increm + 1
  110. local string = out[increm] or {str=""}
  111. if #string.str >= #box.input then
  112. cnt = cnt + 1
  113. display[cnt] = string
  114. end
  115. if increm > #out then break end
  116. end
  117. sleep()
  118. end
  119. end,function(term)
  120. local shift = false
  121. local lists = {}
  122. for i=1,10 do
  123. local text = (display[i].str or ""):gsub(box.input,"")
  124. local _,x = (display[i].str or ""):find("^.+("..text..")")
  125. x = x and x-#text+2 or 2
  126. if not lists[x] then lists[x] = {} end
  127. table.insert(lists[x],function(y)
  128. results[i].positioning.x = x
  129. if (box.input..text):match("^"..box.input:sub(1,x-2)..text) then shift = true end
  130. results[i].text = gui.text{
  131. text=text,
  132. blit={
  133. ("0"):rep(#text),
  134. ("8"):rep(#text)
  135. },
  136. centered=false
  137. }
  138. results[i].full = display[i].str
  139. results[i].positioning.y = y+1+(shift and 0 or 1)
  140. results[i].positioning.width = #text
  141. end)
  142. end
  143. local nth = 0
  144. for k,v in iterate_order(lists) do
  145. for k,v in ipairs(v) do
  146. nth = nth + 1
  147. v(nth)
  148. end
  149. end
  150. end),0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement