Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local check_evs = {
- ["char"]=function(val)
- return tonumber(val) ~= nil
- end,
- ["key"]=function(val)
- return keys.getName(val):match("%d$") ~= ""
- end
- }
- local function read_number(...)
- local coro = coroutine.create(_G.read)
- local filter
- coroutine.resume(coro,...)
- while coroutine.status(coro) ~= "dead" do
- local ev = table.pack(os.pullEvent())
- local checker = check_evs[ev[1]]
- if (not filter or ev[1] == filter) and (checker and checker(ev[2]) or not checker) then
- local ok,ret = coroutine.resume(coro,table.unpack(ev,1,ev.n))
- if ok then filter = ret end
- if not ok and coroutine.status(coro) == "dead" then
- error("Error: " .. ret,0)
- end
- if coroutine.status(coro) == "dead" and ok then
- return ret
- end
- end
- end
- end
- local function type_input(types,with,...)
- local current_type
- while not type or not types[current_type] do
- for k,v in pairs(types) do
- v.render(v,k)
- end
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.write("pgr> ")
- current_type = with(...)
- term.setBackgroundColor(colors.black)
- if not types[current_type] then
- term.setTextColor(colors.red)
- print("Neplatná moznost")
- end
- end
- return types[current_type].value,current_type
- end
- local scripts = {
- ["1"]={
- title = "palce matika",
- box = {
- ["1"]={
- title = "Palce na cm",
- options = {
- check=function(val) return val ~= nil end,
- {
- registry = "inches",
- process = function(val)
- return tonumber(val)
- end,
- option_name = "Palce"
- }
- },
- code=function(options)
- print("cm: ",options.inches*2.54)
- end
- },
- ["2"]={
- title = "Cm na palce",
- options = {
- check=function(val) return val ~= nil end,
- {
- registry = "cm",
- process = function(val)
- return tonumber(val)
- end,
- option_name = "Cm"
- }
- },
- code=function(options)
- print("Palce: ",options.cm/2.54)
- end
- }
- }
- },
- ["2"]={
- title = "ligma",
- options = {
- check=function(val) return val ~= nil end,
- {
- registry = "what",
- process = function(val)
- return tostring(val)
- end,
- option_name = "Co?"
- }
- },
- code=function(options)
- print("Ligma",options.what)
- end
- }
- }
- local current_tab = scripts
- while true do
- local ask_script = {}
- for k,v in pairs(current_tab) do
- ask_script[k] = {title=v.title,render=function(v,k)
- if current_tab[k] then
- if current_tab[k].box then
- term.setTextColor(colors.blue)
- elseif current_tab[k].code then
- term.setTextColor(colors.green)
- end
- end
- print(k..".",v.title)
- term.setTextColor(colors.white)
- end}
- end
- local v,k = type_input(ask_script,read_number)
- local new_tab = current_tab[k]
- local old_tab = current_tab
- if new_tab.box then
- current_tab = new_tab.box
- new_tab.backtrack = old_tab
- elseif new_tab.code then
- local options_construct = {}
- term.setBackgroundColor(colors.blue)
- term.write(new_tab.title)
- term.setBackgroundColor(colors.black)
- local _,cy = term.getCursorPos()
- term.setCursorPos(cy)
- local current_result
- for k,v in ipairs(new_tab.options) do
- term.write(v.option_name .. ": ")
- while not new_tab.options.check(current_result,k,v) do
- current_result = v.process(read())
- end
- options_construct[v.registry] = current_result
- end
- term.setTextColor(colors.lightGray)
- term.write(" -> ")
- new_tab.code(options_construct)
- term.setTextColor(colors.white)
- print()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement