Advertisement
Alakazard12

List API

Dec 11th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. local stx, sty = term.getSize()
  2.  
  3. function clear()
  4.     term.clear()
  5.     term.setCursorPos(1, 1)
  6. end
  7.  
  8. function drawlist(title, listr, num)
  9.     clear()
  10.     term.setTextColor(colors.lime)
  11.     local list = {}
  12.     print(title.."\n")
  13.     for i,v in pairs(listr) do
  14.         list[i] = v
  15.     end
  16.     local start = 1
  17.     local last = #list
  18.     if num > sty - 4 then
  19.         start = num - (sty - 4)
  20.     end
  21.     local cnt = 0
  22.     for i = start, #list do
  23.         if cnt > sty - 4 then
  24.             last = last - 1
  25.             cnt = cnt - 1
  26.         end
  27.         cnt = cnt + 1
  28.     end
  29.     for m = start, last do
  30.         local tor = true
  31.         if tor == true then
  32.             if m == num then
  33.                 term.setTextColor(colors.white)
  34.                 write("[")
  35.                 term.setTextColor(colors.lime)
  36.                 write("-")
  37.                 term.setTextColor(colors.white)
  38.                 write("]")
  39.                 term.setTextColor(colors.lime)
  40.                 print(" "..list[m])
  41.             else
  42.                 term.setTextColor(colors.white)
  43.                 print("[ ] "..list[m])
  44.             end
  45.         end
  46.     end
  47. end
  48.  
  49. function getKey()
  50.     local rt = nil
  51.     function lop()
  52.         local e, a1 = os.pullEvent("key")
  53.         if a1 == 200 then
  54.             rt = "up"
  55.         elseif a1 == 208 then
  56.             rt = "down"
  57.         elseif a1 == 28 then
  58.             rt = "enter"
  59.         else
  60.             lop()
  61.         end
  62.     end
  63.     lop()
  64.     repeat sleep(0) until rt ~= nil
  65.     return rt
  66. end
  67.  
  68. function ch(title, list)
  69.     clear()
  70.     local ind = 1
  71.     local lcopy = list
  72.     drawlist(title, lcopy, ind)
  73.     local rt = nil
  74.     cycle = function()
  75.         local ke = getKey()
  76.         if ke == "up" then
  77.             if ind > 1 then
  78.                 ind = ind - 1
  79.             else
  80.                 ind = #list
  81.             end
  82.             drawlist(title, lcopy, ind)
  83.             cycle()
  84.         elseif ke == "down" then
  85.             if ind < #list then
  86.                 ind = ind + 1
  87.             else
  88.                 ind = 1
  89.             end
  90.             drawlist(title, lcopy, ind)
  91.             cycle()
  92.         elseif ke == "enter" then
  93.             rt = ind
  94.         end
  95.     end
  96.     cycle()
  97.     repeat sleep(0) until rt ~= nil
  98.     return ind
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement