BruceWplays

Todo V4

Jun 29th, 2022 (edited)
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.24 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.  
  74.  
  75.  
  76.  
  77. local function redrawtime()
  78. redrawTimer = os.startTimer(.83)
  79.     ev, b, xPos, yPos = os.pullEvent()
  80.    
  81.          if ev == "mouse_click" then
  82.          if ev == "mouse_click" then
  83.   if xPos == 2 and yPos == 1 and not Options then
  84.    addTask()
  85.   elseif xPos == 6 and yPos == 1 and not Options then
  86.    delTask()
  87.   elseif xPos >= maxX-12 and xPos <= maxX-4 and yPos == 1 and not Options then
  88.   Options = true
  89.   elseif xPos >=maxX-3 and xPos <= maxX and yPos == 1 and not Options then
  90.   isUsing = false
  91.   elseif xPos == 2 and yPos == maxY and not Options then
  92.    if page > 1 then
  93.     page = page - 1
  94.    end
  95.   elseif xPos == maxX-1 and yPos == maxY and not Options then
  96.    page = page + 1
  97.   elseif xPos > 1 and yPos < maxY and not Options then
  98.    showTask(page, yPos, maxY, maxX)
  99.   elseif xPos >= maxX-6 and xPos <= maxX-1 and yPos == 1 and Options then
  100.    Options = false
  101.    local opfile = fs.open("/.todocfg", "w")
  102.    opfile.writeLine(color1)
  103.    opfile.write(color2)
  104.    opfile.close()
  105.   elseif xPos == 15 and yPos == 3 and Options then
  106.     if choice-1 ~= -1 then
  107.         choice = choice-1
  108.     else
  109.         choice = 15
  110.     end
  111.     redraw(page)
  112.   elseif xPos == 17 and yPos == 3 and Options then
  113.     if choice+1 ~= 16 then
  114.         choice = choice+1
  115.     else
  116.         choice = 0
  117.     end
  118.     redraw(page)
  119.   elseif xPos == 15 and yPos == 7 and Options then
  120.     if choice2-1 ~= -1 then
  121.         choice2 = choice2-1
  122.     else
  123.         choice2 = 15
  124.     end
  125.     redraw(page)
  126.   elseif xPos == 17 and yPos == 7 and Options then
  127.     if choice2+1 ~= 16 then
  128.         choice2 = choice2+1
  129.     else
  130.         choice2 = 0
  131.     end
  132.     redraw(page)
  133.    end
  134.  elseif ev == "mouse_scroll" then
  135.   if b == -1 then
  136.    if page > 1 then page = page - 1 end
  137.   elseif b == 1 then
  138.    page = page + 1
  139.   end
  140.            
  141.        else
  142.        
  143.     if isUsing and not Options then
  144.  
  145.    
  146.     term.setBackgroundColour(color1)
  147.     if color1 ~= color2 and color1 ~= colors.white then
  148.         term.setTextColour(color2)
  149.     elseif color1 == color2 and color1 ~= colors.white then
  150.         term.setTextColor(colors.white)
  151.     else
  152.         term.setTextColor(colors.black)
  153.     end
  154.  
  155.  
  156.                         term.setCursorPos(10,1)
  157. print(textutils.formatTime(os.time(), false))
  158.    else
  159. end
  160. end
  161. end
  162. end
  163.  
  164.  
  165. local function redraw(page)
  166.    
  167.  if isUsing and not Options then
  168.     local tasks = getTasks()
  169.     local nMaxY = maxY - 2
  170.  
  171.      term.clear()
  172.     term.setCursorPos(1,1)    
  173.             term.setBackgroundColour(color1)
  174.     if color1 ~= color2 and color1 ~= colors.white then
  175.         term.setTextColour(color2)
  176.     elseif color1 == color2 and color1 ~= colors.white then
  177.         term.setTextColor(colors.white)
  178.     else
  179.         term.setTextColor(colors.black)
  180.     end
  181.  
  182.  
  183.       term.clearLine()
  184.     print(" + / -  ")
  185.     term.setCursorPos(maxX-12,1)
  186.     print("     Opt Exit")
  187.        
  188.        
  189. redrawtime()
  190.        
  191.        
  192.     for i=1, nMaxY do
  193.         if i%2 == 1 and color1 ~= color2 then
  194.             term.setBackgroundColour(color2)
  195.             term.setTextColour(color1)
  196.         elseif color1 ~= color2 then
  197.             term.setBackgroundColour(color1)
  198.             term.setTextColour(color2)
  199.         elseif color1 == colors.white then
  200.             term.setBackgroundColor(color1)
  201.             term.setTextColor(colors.black)
  202.         else
  203.             term.setBackgroundColor(color1)
  204.             term.setTextColor(colors.black)
  205.         end
  206.  
  207.         term.setCursorPos(1,1+i)
  208.         term.clearLine()
  209.  
  210.         local num = i+(nMaxY*(page-1))
  211.  
  212.         if tasks[num] ~= nil then
  213.             write(num..": "..tasks[num])
  214.         else
  215.             write(num..": ")
  216.         end
  217.     end
  218.  
  219.  if maxY%2 == 0 and color1 ~= color2 then
  220.   term.setBackgroundColour(color2)
  221.   term.setTextColour(color1)
  222.  elseif color1 ~= color2 then
  223.   term.setBackgroundColour(color1)
  224.   term.setTextColour(color2)
  225.  elseif color1 == colors.white then
  226.     term.setBackgroundColor(color1)
  227.     term.setTextColor(colors.black)
  228.  else
  229.     term.setBackgroundColor(color1)
  230.     term.setTextColor(colors.white)
  231.  end
  232. term.setCursorPos(2,maxY)
  233.  term.clearLine()
  234.  write("<")
  235.  term.setCursorPos((maxX/2-#tostring(page)/2)+1,maxY)
  236.  write(page)
  237.  term.setCursorPos(maxX-1,maxY)
  238.  write(">")
  239.        
  240.  
  241.        
  242.  elseif not isUsing and not Options then --Exited program
  243.     term.setBackgroundColor(colors.black)
  244.     term.setTextColor(colors.white)
  245.     term.clear()
  246.     term.setCursorPos(1,1)
  247.     print("Goodbye !")
  248.  elseif isUsing and Options then
  249.     term.setBackgroundColor(colors.gray)
  250.     if color1 ~= colors.white then
  251.         term.setTextColor(colors.white)
  252.     else
  253.         term.setTextColor(colors.black)
  254.     end
  255.     term.clear()
  256.     color1 = numconv[choice]
  257.     color2 = numconv[choice2]
  258.     term.setCursorPos(1,1)
  259.     term.setBackgroundColor(color1)
  260.     term.clearLine()
  261.     term.setCursorPos(maxX-(maxX-2),1)
  262.     print("Options")
  263.     term.setCursorPos(math.ceil(maxX/2)+1,1)
  264.     print("|")
  265.     term.setCursorPos(maxX-6,1)
  266.     print("Return")
  267.     term.setBackgroundColor(colors.gray)
  268.     term.setTextColor(colors.white)
  269.     term.setCursorPos(15,3)
  270.     print("<->")
  271.     paintutils.drawLine(14,4,14+string.len(colnames[choice]),4,numconv[choice])
  272.     term.setCursorPos(3,4)
  273.     if color1 ~= colors.white then
  274.         term.setTextColor(colors.white)
  275.     else
  276.         term.setTextColor(colors.black)
  277.     end
  278.     print("1st color : " .. colnames[choice])
  279.     term.setBackgroundColor(colors.gray)
  280.     paintutils.drawLine(14,6,14+string.len(colnames[choice2]),6,numconv[choice2])
  281.     if color2 ~= colors.white then
  282.         term.setTextColor(colors.white)
  283.     else
  284.         term.setTextColor(colors.black)
  285.     end
  286.     term.setCursorPos(3,6)
  287.     print("2nd color : " .. colnames[choice2])
  288.     term.setBackgroundColor(colors.gray)
  289.     term.setTextColor(colors.white)
  290.     term.setCursorPos(15,7)
  291.     print("<->")
  292.     term.setCursorPos(5,10)
  293.     print("About this program")
  294.     term.setCursorPos(4,12)
  295.     print("HD's Todo list V1.6")
  296.     term.setCursorPos(1,14)
  297.     print("-masterdisasterHD : Orignal program and idea")
  298.     term.setCursorPos(1,17)
  299.     print("-s0r00t : V1.5 enhancements")
  300.    
  301.  end
  302. end
  303.  
  304. local function saveTasks(tbl)
  305.  local A = fs.open(taskFile, "w")
  306.  A.write(textutils.serialize(tbl))
  307.  A.close()
  308. end
  309.  
  310. local function addTask()
  311.  local tasks = getTasks()
  312.  term.setBackgroundColour(colors.black)
  313.  term.setTextColour(colors.white)
  314.  term.clear()
  315.  term.setCursorPos(1,1)
  316.  term.setCursorBlink(true)
  317.  write("Task: ")
  318.  local tsk = read()
  319.  table.insert(tasks, tsk)
  320.  saveTasks(tasks)
  321. end
  322.  
  323. local function delTask()
  324.  local tasks = getTasks()
  325.  term.setBackgroundColour(colors.black)
  326.  term.setTextColour(colors.white)
  327.  term.clear()
  328.  term.setCursorPos(1,1)
  329.  write("Task number: ")
  330.  local tsknmbr = tonumber(read())
  331.  table.remove(tasks, tsknmbr)
  332.  saveTasks(tasks)
  333. end
  334.  
  335. local function showTask(pg, pos, h, w)
  336.  local tasks = getTasks()
  337.  local step1 = (h-2)*(pg-1)
  338.  local step2 = (pos-1)+step1
  339.  term.setBackgroundColour(color2)
  340.  term.setTextColour(color1)
  341.  term.clear()
  342.  term.setCursorPos(1,1)
  343.  if tasks[step2] ~= nil then
  344.   write("Task #"..step2..":")
  345.   local text = tasks[step2]
  346.   local k = 1
  347.   for i=1, h-1 do
  348.    term.setCursorPos(1,i+1)
  349.    for j=1,w do
  350.     write(string.sub(text, k, k))
  351.     k = k + 1
  352.    end
  353.   end
  354.  term.setCursorPos(1,h)
  355.  write("Press any key to continue")
  356.  os.pullEvent("key")
  357.  end
  358. end
  359.  
  360. --Program loop
  361. redraw(page)
  362. local maxX, maxY = term.getSize()
  363. while isUsing do
  364.  sleep(0)
  365.  ev, b, xPos, yPos = os.pullEvent()
  366.  
  367.  if ev == "mouse_click" then
  368.   if xPos == 2 and yPos == 1 and not Options then
  369.    addTask()
  370.   elseif xPos == 6 and yPos == 1 and not Options then
  371.    delTask()
  372.   elseif xPos >= maxX-12 and xPos <= maxX-4 and yPos == 1 and not Options then
  373.   Options = true
  374.   elseif xPos >=maxX-3 and xPos <= maxX and yPos == 1 and not Options then
  375.   isUsing = false
  376.   elseif xPos == 2 and yPos == maxY and not Options then
  377.    if page > 1 then
  378.     page = page - 1
  379.    end
  380.   elseif xPos == maxX-1 and yPos == maxY and not Options then
  381.    page = page + 1
  382.   elseif xPos > 1 and yPos < maxY and not Options then
  383.    showTask(page, yPos, maxY, maxX)
  384.   elseif xPos >= maxX-6 and xPos <= maxX-1 and yPos == 1 and Options then
  385.    Options = false
  386.    local opfile = fs.open("/.todocfg", "w")
  387.    opfile.writeLine(color1)
  388.    opfile.write(color2)
  389.    opfile.close()
  390.   elseif xPos == 15 and yPos == 3 and Options then
  391.     if choice-1 ~= -1 then
  392.         choice = choice-1
  393.     else
  394.         choice = 15
  395.     end
  396.     redraw(page)
  397.   elseif xPos == 17 and yPos == 3 and Options then
  398.     if choice+1 ~= 16 then
  399.         choice = choice+1
  400.     else
  401.         choice = 0
  402.     end
  403.     redraw(page)
  404.   elseif xPos == 15 and yPos == 7 and Options then
  405.     if choice2-1 ~= -1 then
  406.         choice2 = choice2-1
  407.     else
  408.         choice2 = 15
  409.     end
  410.     redraw(page)
  411.   elseif xPos == 17 and yPos == 7 and Options then
  412.     if choice2+1 ~= 16 then
  413.         choice2 = choice2+1
  414.     else
  415.         choice2 = 0
  416.     end
  417.     redraw(page)
  418.    end
  419.  elseif ev == "mouse_scroll" then
  420.   if b == -1 then
  421.    if page > 1 then page = page - 1 end
  422.   elseif b == 1 then
  423.    page = page + 1
  424.   end
  425.  
  426.  elseif ev == "timer" then
  427.      redrawtime()
  428.        
  429.        
  430.        
  431.       end
  432.  
  433.    
  434.  redraw(page)
  435. end
Add Comment
Please, Sign In to add comment