Miki_Tellurium

notepad-memos

Jan 2nd, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.33 KB | Gaming | 0 0
  1. -- Memos app by Miki_Tellurium
  2. -- Version: 1.0.0
  3.  
  4. local library = require("button")
  5.  
  6. local memos = {}
  7. local linesScrolled = 0
  8. local defaultBackgroundColor = colors.black
  9. local defaultTextColor = colors.white
  10. term.setCursorBlink(false)
  11. term.setPaletteColor(colors.yellow, 0xFFFF00)
  12.  
  13. local exit = button:new()
  14. exit:setLabel("Exit")
  15. exit:setBackgroundColor(colors.red)
  16. exit:setTextColor(colors.black)
  17. exit:setLocation(10, 1)
  18.  
  19. --Check if notes file already exist
  20. local function doNotesExist()
  21.  local notes = io.open("notes.txt", "r")
  22.  
  23.  if notes ~= nil then
  24.   io.close(notes)
  25.   return true
  26.  else
  27.   return false
  28.  end
  29. end
  30.  
  31. --If file exist read it, else create it
  32. local function loadMemos()
  33.  memos = {}
  34.  if doNotesExist() then
  35.   local notes = io.open("notes.txt", "r")
  36.   for line in io.lines("notes.txt") do
  37.    table.insert(memos, line)
  38.   end
  39.   io.close(notes)
  40.  else
  41.   local notes = io.open("notes.txt", "w")
  42.   io.close(notes)
  43.  end
  44. end
  45. --Update notes.txt with new written memos
  46. local function updateMemos()
  47.  local notes = io.open("notes.txt", "w")
  48.  for key,line in pairs(memos) do
  49.   io.output(notes)
  50.   io.write(line.."\n")
  51.  end
  52.  io.close(notes)
  53. end
  54.  
  55. --Write memos on screen
  56. local function showMemos()
  57.  local lineUsed = 0
  58.  term.clear()
  59.  term.setCursorPos(1, 1)
  60.  term.setTextColor(colors.yellow)
  61.  print("Memos:")
  62.  exit:paint()
  63.  print()
  64.  term.setTextColor(defaultTextColor)
  65.  for key,text in pairs(memos) do
  66.   if lineUsed > 16 then
  67.    break
  68.   end
  69.   term.clearLine()
  70.   print("-"..text)
  71.   if #text > 25 then
  72.    lineUsed = lineUsed + math.ceil(#text/25)
  73.   else
  74.    lineUsed = lineUsed + 1
  75.   end
  76.  end
  77.  term.setCursorPos(1, 20)
  78.  term.setTextColor(colors.lightBlue)
  79.  term.write("Up/down arrow to scroll")
  80. end
  81. --Draw buttons on screen
  82. local function makeButtons()
  83.  add = button:new()
  84.  remove = button:new()
  85.  
  86.  add:setLabel("Add")
  87.  add:setBackgroundColor(colors.green)
  88.  add:setTextColor(colors.white)
  89.  add:setLocation(18, 1)
  90.  remove:setLabel("Remove")
  91.  remove:setBackgroundColor(colors.red)
  92.  remove:setTextColor(colors.white)
  93.  remove:setLocation(21, 1)
  94.  
  95.  add:paint()
  96.  remove:paint()
  97. end
  98. --Function used to add memos
  99. local function addMemos()
  100.  term.setTextColor(defaultBackgroundColor)
  101.  term.setTextColor(defaultTextColor)
  102.  term.clear()
  103.  term.setCursorPos(1, 1)
  104.  print("Write a new memo:")
  105.  write(">")
  106.  
  107.  --Read text and add memo
  108.  local function addMemo()
  109.   text = read()
  110.   if text == "" then --TODO: add empty text check
  111.    return
  112.   end
  113.   local notes = io.open("notes.txt", "a")
  114.   io.output(notes)
  115.   io.write(text, "\n")
  116.   io.close(notes)
  117.   table.insert(memos, text)
  118.   linesScrolled = 0
  119.  end
  120.  
  121.  --Cancel and return to main screen
  122.  local function exit()
  123.   cancel = button:new()
  124.   cancel:setTextColor(colors.black)
  125.   cancel:setBackgroundColor(colors.red)
  126.   cancel:setLocation(26, 1)
  127.   cancel:setLabel("X")
  128.   cancel:paint()
  129.   while true do
  130.    local event = {os.pullEvent("mouse_click")}
  131.    if cancel:waitForClick(event) then
  132.     linesScrolled = 0
  133.     return
  134.    end
  135.   end
  136.  end
  137.  
  138.  parallel.waitForAny(addMemo, exit)
  139.  term.setCursorBlink(false)
  140. end
  141.  
  142. --Remove memos function
  143. local function removeMemos()
  144.  local xButtons = {}
  145.  local memosScrolled = 0
  146.  local line = 3
  147.  term.setBackgroundColor(defaultBackgroundColor)
  148.  term.setTextColor(defaultTextColor)
  149.  cancel = button:new()
  150.  cancel:setTextColor(colors.black)
  151.  cancel:setBackgroundColor(colors.red)
  152.  cancel:setLocation(26, 1)
  153.  cancel:setLabel("X")
  154.  for b = 1,8 do
  155.   b = button:new()
  156.   b:setLabel("x")
  157.   b:setLocation(1, line)
  158.   b:setBackgroundColor(colors.red)
  159.   b:setTextColor(colors.black)
  160.   table.insert(xButtons, b)
  161.  end
  162.  local function drawDeleteScreen()
  163.   local line = 3
  164.   term.clear()
  165.   term.setCursorPos(1, 1)
  166.   print("Choose a memo to delete:")
  167.   cancel:paint()
  168.   print()
  169.   for key,text in pairs(memos) do
  170.    if key > 8 then
  171.     break
  172.    end
  173.    xButtons[key]:setLocation(1, line)
  174.    xButtons[key]:paint()
  175.    term.setCursorPos(2, line)
  176.    print(text)
  177.    print()
  178.    line = line + 2
  179.   end
  180.   term.setCursorPos(1, 20)
  181.   term.setTextColor(colors.lightBlue)
  182.   term.write("Up/down arrow to scroll")
  183.   term.setTextColor(colors.white)
  184.  end
  185.  --Make the screen scroll
  186.  local function scrollDelete(key)
  187.  if key == 265 then  --Scroll up
  188.    memosScrolled = memosScrolled - 1
  189.    if memosScrolled < 0 then
  190.     memosScrolled = 0
  191.    end
  192.    for i = 1,memosScrolled do
  193.     table.remove(memos, 1)
  194.    end
  195.   elseif key == 264 then --Scroll down
  196.    memosScrolled = memosScrolled + 1
  197.    if memosScrolled > #memos - 8 then
  198.     memosScrolled = #memos - 8
  199.    end
  200.    for i = 1,memosScrolled do
  201.     table.remove(memos, 1)
  202.    end
  203.   end
  204.  end
  205.  
  206.  drawDeleteScreen()
  207.  while true do
  208.   local event = {os.pullEvent()}
  209.   for key,button in pairs(xButtons) do
  210.    --Remove the memo that was clicked
  211.    if button:waitForClick(event) then
  212.     term.clear()
  213.     term.setCursorPos(1, 1)
  214.     term.setTextColor(colors.orange)
  215.     print("Deleted memo:")
  216.     term.setTextColor(colors.white)
  217.     print(memos[key + memosScrolled])
  218.     table.remove(memos, key + memosScrolled)
  219.     sleep(1.15)
  220.     updateMemos()
  221.     return
  222.    --Cancel and beck to main screen
  223.    elseif cancel:waitForClick(event) then
  224.     return
  225.    end
  226.   end
  227.   --Scroll function
  228.   if event[1] == "key" then
  229.    scrollDelete(event[2])
  230.    drawDeleteScreen()
  231.    loadMemos()
  232.   end
  233.  end
  234. end
  235. --265 up arrow, 264 down arrow
  236. local function scroll(key)
  237.  if key == 265 then  --Scroll up
  238.   linesScrolled = linesScrolled - 1
  239.   if linesScrolled < 0 then
  240.    linesScrolled = 0
  241.   end
  242.   for i = 1,linesScrolled do
  243.    table.remove(memos, 1)
  244.   end
  245.   showMemos()
  246.   loadMemos()
  247.  elseif key == 264 then --Scroll down
  248.   linesScrolled = linesScrolled + 1
  249.   if linesScrolled > #memos - 17 then
  250.    linesScrolled = #memos - 17
  251.   end
  252.   for i = 1,linesScrolled do
  253.    table.remove(memos, 1)
  254.   end
  255.   showMemos()
  256.   loadMemos()
  257.  end
  258. end
  259. --Perform different actions based on button pressed
  260. local function buttonAction()
  261.   local event = {os.pullEvent()}
  262.  if add:waitForClick(event) then
  263.   addMemos()
  264.   showMemos()
  265.  elseif remove:waitForClick(event) then
  266.   removeMemos()
  267.   showMemos()
  268.  elseif event[1] == "key" then
  269.   scroll(event[2])
  270.  elseif exit:waitForClick(event) then
  271.   os.reboot()
  272.  end
  273. end
  274.  
  275. --Run program
  276. loadMemos()
  277. term.clear()
  278. showMemos()
  279. while true do
  280.  makeButtons()
  281.  buttonAction()
  282. end
Advertisement
Add Comment
Please, Sign In to add comment