Advertisement
9551

Untitled

Oct 18th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. local check_evs = {
  2. ["char"]=function(val)
  3. return tonumber(val) ~= nil
  4. end,
  5. ["key"]=function(val)
  6. return keys.getName(val):match("%d$") ~= ""
  7. end
  8. }
  9.  
  10. local function read_number(...)
  11. local coro = coroutine.create(_G.read)
  12. local filter
  13.  
  14. coroutine.resume(coro,...)
  15.  
  16. while coroutine.status(coro) ~= "dead" do
  17. local ev = table.pack(os.pullEvent())
  18.  
  19. local checker = check_evs[ev[1]]
  20.  
  21. if (not filter or ev[1] == filter) and (checker and checker(ev[2]) or not checker) then
  22. local ok,ret = coroutine.resume(coro,table.unpack(ev,1,ev.n))
  23.  
  24. if ok then filter = ret end
  25.  
  26. if not ok and coroutine.status(coro) == "dead" then
  27. error("Error: " .. ret,0)
  28. end
  29.  
  30. if coroutine.status(coro) == "dead" and ok then
  31. return ret
  32. end
  33. end
  34. end
  35. end
  36.  
  37. local function type_input(types,with,...)
  38. local current_type
  39. while not type or not types[current_type] do
  40. for k,v in pairs(types) do
  41. v.render(v,k)
  42. end
  43. term.setTextColor(colors.yellow)
  44. term.setBackgroundColor(colors.gray)
  45. term.clearLine()
  46. term.write("pgr> ")
  47.  
  48. current_type = with(...)
  49.  
  50. term.setBackgroundColor(colors.black)
  51. if not types[current_type] then
  52. term.setTextColor(colors.red)
  53. print("Neplatná moznost")
  54. end
  55. end
  56. return types[current_type].value,current_type
  57. end
  58.  
  59. local scripts = {
  60. ["1"]={
  61. title = "palce matika",
  62. box = {
  63. ["1"]={
  64. title = "Palce na cm",
  65. options = {
  66. check=function(val) return val ~= nil end,
  67. {
  68. registry = "inches",
  69. process = function(val)
  70. return tonumber(val)
  71. end,
  72. option_name = "Palce"
  73. }
  74. },
  75. code=function(options)
  76. print("cm: ",options.inches*2.54)
  77. end
  78. },
  79. ["2"]={
  80. title = "Cm na palce",
  81. options = {
  82. check=function(val) return val ~= nil end,
  83. {
  84. registry = "cm",
  85. process = function(val)
  86. return tonumber(val)
  87. end,
  88. option_name = "Cm"
  89. }
  90. },
  91. code=function(options)
  92. print("Palce: ",options.cm/2.54)
  93. end
  94. }
  95. }
  96. },
  97. ["2"]={
  98. title = "ligma",
  99. options = {
  100. check=function(val) return val ~= nil end,
  101. {
  102. registry = "what",
  103. process = function(val)
  104. return tostring(val)
  105. end,
  106. option_name = "Co?"
  107. }
  108. },
  109. code=function(options)
  110. print("Ligma",options.what)
  111. end
  112. }
  113. }
  114.  
  115. local current_tab = scripts
  116.  
  117. while true do
  118. local ask_script = {}
  119. for k,v in pairs(current_tab) do
  120. ask_script[k] = {title=v.title,render=function(v,k)
  121. if current_tab[k] then
  122. if current_tab[k].box then
  123. term.setTextColor(colors.blue)
  124. elseif current_tab[k].code then
  125. term.setTextColor(colors.green)
  126. end
  127. end
  128.  
  129. print(k..".",v.title)
  130.  
  131. term.setTextColor(colors.white)
  132. end}
  133. end
  134.  
  135. local v,k = type_input(ask_script,read_number)
  136.  
  137. local new_tab = current_tab[k]
  138. local old_tab = current_tab
  139.  
  140. if new_tab.box then
  141. current_tab = new_tab.box
  142. new_tab.backtrack = old_tab
  143. elseif new_tab.code then
  144. local options_construct = {}
  145.  
  146. term.setBackgroundColor(colors.blue)
  147. term.write(new_tab.title)
  148. term.setBackgroundColor(colors.black)
  149.  
  150. local _,cy = term.getCursorPos()
  151.  
  152. term.setCursorPos(cy)
  153.  
  154. local current_result
  155. for k,v in ipairs(new_tab.options) do
  156.  
  157. term.write(v.option_name .. ": ")
  158.  
  159. while not new_tab.options.check(current_result,k,v) do
  160. current_result = v.process(read())
  161. end
  162.  
  163. options_construct[v.registry] = current_result
  164. end
  165.  
  166. term.setTextColor(colors.lightGray)
  167. term.write(" -> ")
  168. new_tab.code(options_construct)
  169. term.setTextColor(colors.white)
  170. print()
  171. end
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement