Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Brodur

Simple Dialog CLI

Recommended Posts

I am working on making another program and needed to port some old code from computer craft so I figured I may as well share. This file conaints 2 types of menus, a vertical list which takes an array and optional header text, and a yes/no style menu that can be customized.

local term = require("term")
local event = require("event")
local methods = {}
 
function methods.list(m, header)
  header = header or "Menu List"
  n=1
 
  while(true) do
    term.clear()
    term.setCursor(1,1)
    term.write(header)
    term.setCursor(1,2)
 
    for i=1, #m, 1 do
      if(i==n) then
        term.write(" [" .. m[i] .. "]\n")
      else
        term.write("  " ..  m[i] .. "\n")
      end
    end
 
    local _,_,_,key = event.pull("key_down")  
 
    if(key==200 and n>1) then --go up
      n = n-1
    end
    if(key==208 and n<#m) then --go down
      n = n+1
    end
    if(key==28) then --exit
      break
    end
  end
 
  term.clear()
  term.setCursor(1,1)
  return n
end
 
function methods.dialog(prompt, lOpt, rOpt)
  prompt = prompt or "Continue"
  lOpt = lOpt or "YES"
  rOpt = rOpt or "NO"
 
  local n = 1
 
  term.write(prompt.."\n")
 
  while true do
    local x, y =term.getCursor()
    term.clearLine()    
   
    if n==1 then
      term.setCursor(x, y)
      term.clearLine()
      term.write ("["..lOpt.."]   "..rOpt)
    else
      term.setCursor(x, y)
      term.clearLine()
      term.write(" "..lOpt.."   ["..rOpt.."]")
    end
 
  term.setCursor(x, y)  
  local _,_,_,key = event.pull("key_down")  
 
  if key==203 then n=1 end
  if key==205 then n=2 end
  if key==28  then term.write("\n") return n end
  end
end
 
return methods

https://pastebin.com/aHa8NnwX

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.