BruceWplays

todoV3

Jun 17th, 2022 (edited)
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.98 KB | None | 0 0
  1. local isUsing = true
  2. local maxX, maxY = term.getSize()
  3. local color1 = colors.lime
  4. local color2 = colors.white
  5. if fs.exists("/.todocfg") then
  6.   local opfile = fs.open("/.todocfg", "r")
  7.   color1 = tonumber(opfile.readLine())
  8.   color2 = tonumber(opfile.readLine())
  9.   opfile.close()
  10. end
  11.  
  12. local colnames = {[0] = "White",
  13.                 [1] = "Orange",
  14.                 [2] = "Magenta",
  15.                 [3] = "Light Blue",
  16.                 [4] = "Yellow",
  17.                 [5] = "Lime",
  18.                 [6] = "Pink",
  19.                 [7] = "Gray",
  20.                 [8] = "Light Gray",
  21.                 [9] = "Cyan",
  22.                 [10] = "Purple",
  23.                 [11] = "Blue",
  24.                 [12] = "Brown",
  25.                 [13] = "Green",
  26.                 [14] = "Red",
  27.                 [15] = "Black"}
  28.                
  29. local numconv = {[0] = colors.white,
  30.                 [1] = colors.orange,
  31.                 [2] = colors.magenta,
  32.                 [3] = colors.lightBlue,
  33.                 [4] = colors.yellow,
  34.                 [5] = colors.lime,
  35.                 [6] = colors.pink,
  36.                 [7] = colors.gray,
  37.                 [8] = colors.lightGray,
  38.                 [9] = colors.cyan,
  39.                 [10] = colors.purple,
  40.                 [11] = colors.blue,
  41.                 [12] = colors.brown,
  42.                 [13] = colors.green,
  43.                 [14] = colors.red,
  44.                 [15] = colors.black}
  45.  
  46. --Maybe need to rewrite this :P
  47. local choice = 0
  48. local choice2 = 0
  49. while numconv[choice] ~= color1 do
  50.     choice=choice+1
  51. end
  52. while numconv[choice2] ~= color2 do
  53.     choice2=choice2+1
  54. end
  55. local taskFile = "/.tasks"
  56. local page = 1
  57. local Options = false
  58.  
  59. local function getTasks()
  60.  if not fs.exists(taskFile) then
  61.   local A = fs.open(taskFile, "w")
  62.   A.write("{}")
  63.   A.close()
  64.   return {}
  65.  else
  66.   local A = fs.open(taskFile, "r")
  67.   local tmp = textutils.unserialize(A.readAll())
  68.   A.close()
  69.   return tmp
  70.  end
  71. end
  72.  
  73. local function redraw(page)
  74.     redrawTimer = os.startTimer(.83)
  75.  if isUsing and not Options then
  76.     local tasks = getTasks()
  77.     local nMaxY = maxY - 2
  78.  
  79.     term.clear()
  80.     term.setCursorPos(1,1)
  81.     term.setBackgroundColour(color1)
  82.     if color1 ~= color2 and color1 ~= colors.white then
  83.         term.setTextColour(color2)
  84.     elseif color1 == color2 and color1 ~= colors.white then
  85.         term.setTextColor(colors.white)
  86.     else
  87.         term.setTextColor(colors.black)
  88.     end
  89.     term.clearLine()
  90.     print(" + / -  ")
  91.     term.setCursorPos(maxX-12,1)
  92.     print("     Opt Exit")
  93.  
  94.                         term.setCursorPos(10,1)
  95. print(textutils.formatTime(os.time(), false))
  96.        
  97.        
  98.     for i=1, nMaxY do
  99.         if i%2 == 1 and color1 ~= color2 then
  100.             term.setBackgroundColour(color2)
  101.             term.setTextColour(color1)
  102.         elseif color1 ~= color2 then
  103.             term.setBackgroundColour(color1)
  104.             term.setTextColour(color2)
  105.         elseif color1 == colors.white then
  106.             term.setBackgroundColor(color1)
  107.             term.setTextColor(colors.black)
  108.         else
  109.             term.setBackgroundColor(color1)
  110.             term.setTextColor(colors.black)
  111.         end
  112.  
  113.         term.setCursorPos(1,1+i)
  114.         term.clearLine()
  115.  
  116.         local num = i+(nMaxY*(page-1))
  117.  
  118.         if tasks[num] ~= nil then
  119.             write(num..": "..tasks[num])
  120.         else
  121.             write(num..": ")
  122.         end
  123.     end
  124.  
  125.  if maxY%2 == 0 and color1 ~= color2 then
  126.   term.setBackgroundColour(color2)
  127.   term.setTextColour(color1)
  128.  elseif color1 ~= color2 then
  129.   term.setBackgroundColour(color1)
  130.   term.setTextColour(color2)
  131.  elseif color1 == colors.white then
  132.     term.setBackgroundColor(color1)
  133.     term.setTextColor(colors.black)
  134.  else
  135.     term.setBackgroundColor(color1)
  136.     term.setTextColor(colors.white)
  137.  end
  138. term.setCursorPos(2,maxY)
  139.  term.clearLine()
  140.  write("<")
  141.  term.setCursorPos((maxX/2-#tostring(page)/2)+1,maxY)
  142.  write(page)
  143.  term.setCursorPos(maxX-1,maxY)
  144.  write(">")
  145.        
  146.  
  147.        
  148.  elseif not isUsing and not Options then --Exited program
  149.     term.setBackgroundColor(colors.black)
  150.     term.setTextColor(colors.white)
  151.     term.clear()
  152.     term.setCursorPos(1,1)
  153.     print("Goodbye !")
  154.  elseif isUsing and Options then
  155.     term.setBackgroundColor(colors.gray)
  156.     if color1 ~= colors.white then
  157.         term.setTextColor(colors.white)
  158.     else
  159.         term.setTextColor(colors.black)
  160.     end
  161.     term.clear()
  162.     color1 = numconv[choice]
  163.     color2 = numconv[choice2]
  164.     term.setCursorPos(1,1)
  165.     term.setBackgroundColor(color1)
  166.     term.clearLine()
  167.     term.setCursorPos(maxX-(maxX-2),1)
  168.     print("Options")
  169.     term.setCursorPos(math.ceil(maxX/2)+1,1)
  170.     print("|")
  171.     term.setCursorPos(maxX-6,1)
  172.     print("Return")
  173.     term.setBackgroundColor(colors.gray)
  174.     term.setTextColor(colors.white)
  175.     term.setCursorPos(15,3)
  176.     print("<->")
  177.     paintutils.drawLine(14,4,14+string.len(colnames[choice]),4,numconv[choice])
  178.     term.setCursorPos(3,4)
  179.     if color1 ~= colors.white then
  180.         term.setTextColor(colors.white)
  181.     else
  182.         term.setTextColor(colors.black)
  183.     end
  184.     print("1st color : " .. colnames[choice])
  185.     term.setBackgroundColor(colors.gray)
  186.     paintutils.drawLine(14,6,14+string.len(colnames[choice2]),6,numconv[choice2])
  187.     if color2 ~= colors.white then
  188.         term.setTextColor(colors.white)
  189.     else
  190.         term.setTextColor(colors.black)
  191.     end
  192.     term.setCursorPos(3,6)
  193.     print("2nd color : " .. colnames[choice2])
  194.     term.setBackgroundColor(colors.gray)
  195.     term.setTextColor(colors.white)
  196.     term.setCursorPos(15,7)
  197.     print("<->")
  198.     term.setCursorPos(5,10)
  199.     print("About this program")
  200.     term.setCursorPos(4,12)
  201.     print("HD's Todo list V1.6")
  202.     term.setCursorPos(1,14)
  203.     print("-masterdisasterHD : Orignal program and idea")
  204.     term.setCursorPos(1,17)
  205.     print("-s0r00t : V1.5 enhancements")
  206.    
  207.  end
  208. end
  209.  
  210. local function saveTasks(tbl)
  211.  local A = fs.open(taskFile, "w")
  212.  A.write(textutils.serialize(tbl))
  213.  A.close()
  214. end
  215.  
  216. local function addTask()
  217.  local tasks = getTasks()
  218.  term.setBackgroundColour(colors.black)
  219.  term.setTextColour(colors.white)
  220.  term.clear()
  221.  term.setCursorPos(1,1)
  222.  term.setCursorBlink(true)
  223.  write("Task: ")
  224.  local tsk = read()
  225.  table.insert(tasks, tsk)
  226.  saveTasks(tasks)
  227. end
  228.  
  229. local function delTask()
  230.  local tasks = getTasks()
  231.  term.setBackgroundColour(colors.black)
  232.  term.setTextColour(colors.white)
  233.  term.clear()
  234.  term.setCursorPos(1,1)
  235.  write("Task number: ")
  236.  local tsknmbr = tonumber(read())
  237.  table.remove(tasks, tsknmbr)
  238.  saveTasks(tasks)
  239. end
  240.  
  241. local function showTask(pg, pos, h, w)
  242.  local tasks = getTasks()
  243.  local step1 = (h-2)*(pg-1)
  244.  local step2 = (pos-1)+step1
  245.  term.setBackgroundColour(color2)
  246.  term.setTextColour(color1)
  247.  term.clear()
  248.  term.setCursorPos(1,1)
  249.  if tasks[step2] ~= nil then
  250.   write("Task #"..step2..":")
  251.   local text = tasks[step2]
  252.   local k = 1
  253.   for i=1, h-1 do
  254.    term.setCursorPos(1,i+1)
  255.    for j=1,w do
  256.     write(string.sub(text, k, k))
  257.     k = k + 1
  258.    end
  259.   end
  260.  term.setCursorPos(1,h)
  261.  write("Press any key to continue")
  262.  os.pullEvent("key")
  263.  end
  264. end
  265.  
  266. --Program loop
  267. redraw(page)
  268. local maxX, maxY = term.getSize()
  269. while isUsing do
  270.  sleep(0)
  271.  ev, b, xPos, yPos = os.pullEvent()
  272.    
  273.  
  274.   if ev == "mouse_click" then
  275.   if xPos == 2 and yPos == 1 and not Options then
  276.    addTask()
  277.   elseif xPos == 6 and yPos == 1 and not Options then
  278.    delTask()
  279.   elseif xPos >= maxX-12 and xPos <= maxX-4 and yPos == 1 and not Options then
  280.   Options = true
  281.   elseif xPos >=maxX-3 and xPos <= maxX and yPos == 1 and not Options then
  282.   isUsing = false
  283.   elseif xPos == 2 and yPos == maxY and not Options then
  284.    if page > 1 then
  285.     page = page - 1
  286.    end
  287.   elseif xPos == maxX-1 and yPos == maxY and not Options then
  288.    page = page + 1
  289.   elseif xPos > 1 and yPos < maxY and not Options then
  290.    showTask(page, yPos, maxY, maxX)
  291.   elseif xPos >= maxX-6 and xPos <= maxX-1 and yPos == 1 and Options then
  292.    Options = false
  293.    local opfile = fs.open("/.todocfg", "w")
  294.    opfile.writeLine(color1)
  295.    opfile.write(color2)
  296.    opfile.close()
  297.   elseif xPos == 15 and yPos == 3 and Options then
  298.     if choice-1 ~= -1 then
  299.         choice = choice-1
  300.     else
  301.         choice = 15
  302.     end
  303.     redraw(page)
  304.   elseif xPos == 17 and yPos == 3 and Options then
  305.     if choice+1 ~= 16 then
  306.         choice = choice+1
  307.     else
  308.         choice = 0
  309.     end
  310.     redraw(page)
  311.   elseif xPos == 15 and yPos == 7 and Options then
  312.     if choice2-1 ~= -1 then
  313.         choice2 = choice2-1
  314.     else
  315.         choice2 = 15
  316.     end
  317.     redraw(page)
  318.   elseif xPos == 17 and yPos == 7 and Options then
  319.     if choice2+1 ~= 16 then
  320.         choice2 = choice2+1
  321.     else
  322.         choice2 = 0
  323.     end
  324.     redraw(page)
  325.    end
  326.  elseif ev == "mouse_scroll" then
  327.   if b == -1 then
  328.    if page > 1 then page = page - 1 end
  329.   elseif b == 1 then
  330.    page = page + 1
  331.   end
  332.  
  333.  elseif ev == "timer" then
  334.      redraw(page)
  335. end
  336.  redraw(page)
  337. end
Add Comment
Please, Sign In to add comment