Advertisement
BruceWplays

Todo V7

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