Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Memos app by Miki_Tellurium
- -- Version: 1.0.0
- local library = require("button")
- local memos = {}
- local linesScrolled = 0
- local defaultBackgroundColor = colors.black
- local defaultTextColor = colors.white
- term.setCursorBlink(false)
- term.setPaletteColor(colors.yellow, 0xFFFF00)
- local exit = button:new()
- exit:setLabel("Exit")
- exit:setBackgroundColor(colors.red)
- exit:setTextColor(colors.black)
- exit:setLocation(10, 1)
- --Check if notes file already exist
- local function doNotesExist()
- local notes = io.open("notes.txt", "r")
- if notes ~= nil then
- io.close(notes)
- return true
- else
- return false
- end
- end
- --If file exist read it, else create it
- local function loadMemos()
- memos = {}
- if doNotesExist() then
- local notes = io.open("notes.txt", "r")
- for line in io.lines("notes.txt") do
- table.insert(memos, line)
- end
- io.close(notes)
- else
- local notes = io.open("notes.txt", "w")
- io.close(notes)
- end
- end
- --Update notes.txt with new written memos
- local function updateMemos()
- local notes = io.open("notes.txt", "w")
- for key,line in pairs(memos) do
- io.output(notes)
- io.write(line.."\n")
- end
- io.close(notes)
- end
- --Write memos on screen
- local function showMemos()
- local lineUsed = 0
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- print("Memos:")
- exit:paint()
- print()
- term.setTextColor(defaultTextColor)
- for key,text in pairs(memos) do
- if lineUsed > 16 then
- break
- end
- term.clearLine()
- print("-"..text)
- if #text > 25 then
- lineUsed = lineUsed + math.ceil(#text/25)
- else
- lineUsed = lineUsed + 1
- end
- end
- term.setCursorPos(1, 20)
- term.setTextColor(colors.lightBlue)
- term.write("Up/down arrow to scroll")
- end
- --Draw buttons on screen
- local function makeButtons()
- add = button:new()
- remove = button:new()
- add:setLabel("Add")
- add:setBackgroundColor(colors.green)
- add:setTextColor(colors.white)
- add:setLocation(18, 1)
- remove:setLabel("Remove")
- remove:setBackgroundColor(colors.red)
- remove:setTextColor(colors.white)
- remove:setLocation(21, 1)
- add:paint()
- remove:paint()
- end
- --Function used to add memos
- local function addMemos()
- term.setTextColor(defaultBackgroundColor)
- term.setTextColor(defaultTextColor)
- term.clear()
- term.setCursorPos(1, 1)
- print("Write a new memo:")
- write(">")
- --Read text and add memo
- local function addMemo()
- text = read()
- if text == "" then --TODO: add empty text check
- return
- end
- local notes = io.open("notes.txt", "a")
- io.output(notes)
- io.write(text, "\n")
- io.close(notes)
- table.insert(memos, text)
- linesScrolled = 0
- end
- --Cancel and return to main screen
- local function exit()
- cancel = button:new()
- cancel:setTextColor(colors.black)
- cancel:setBackgroundColor(colors.red)
- cancel:setLocation(26, 1)
- cancel:setLabel("X")
- cancel:paint()
- while true do
- local event = {os.pullEvent("mouse_click")}
- if cancel:waitForClick(event) then
- linesScrolled = 0
- return
- end
- end
- end
- parallel.waitForAny(addMemo, exit)
- term.setCursorBlink(false)
- end
- --Remove memos function
- local function removeMemos()
- local xButtons = {}
- local memosScrolled = 0
- local line = 3
- term.setBackgroundColor(defaultBackgroundColor)
- term.setTextColor(defaultTextColor)
- cancel = button:new()
- cancel:setTextColor(colors.black)
- cancel:setBackgroundColor(colors.red)
- cancel:setLocation(26, 1)
- cancel:setLabel("X")
- for b = 1,8 do
- b = button:new()
- b:setLabel("x")
- b:setLocation(1, line)
- b:setBackgroundColor(colors.red)
- b:setTextColor(colors.black)
- table.insert(xButtons, b)
- end
- local function drawDeleteScreen()
- local line = 3
- term.clear()
- term.setCursorPos(1, 1)
- print("Choose a memo to delete:")
- cancel:paint()
- print()
- for key,text in pairs(memos) do
- if key > 8 then
- break
- end
- xButtons[key]:setLocation(1, line)
- xButtons[key]:paint()
- term.setCursorPos(2, line)
- print(text)
- print()
- line = line + 2
- end
- term.setCursorPos(1, 20)
- term.setTextColor(colors.lightBlue)
- term.write("Up/down arrow to scroll")
- term.setTextColor(colors.white)
- end
- --Make the screen scroll
- local function scrollDelete(key)
- if key == 265 then --Scroll up
- memosScrolled = memosScrolled - 1
- if memosScrolled < 0 then
- memosScrolled = 0
- end
- for i = 1,memosScrolled do
- table.remove(memos, 1)
- end
- elseif key == 264 then --Scroll down
- memosScrolled = memosScrolled + 1
- if memosScrolled > #memos - 8 then
- memosScrolled = #memos - 8
- end
- for i = 1,memosScrolled do
- table.remove(memos, 1)
- end
- end
- end
- drawDeleteScreen()
- while true do
- local event = {os.pullEvent()}
- for key,button in pairs(xButtons) do
- --Remove the memo that was clicked
- if button:waitForClick(event) then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.orange)
- print("Deleted memo:")
- term.setTextColor(colors.white)
- print(memos[key + memosScrolled])
- table.remove(memos, key + memosScrolled)
- sleep(1.15)
- updateMemos()
- return
- --Cancel and beck to main screen
- elseif cancel:waitForClick(event) then
- return
- end
- end
- --Scroll function
- if event[1] == "key" then
- scrollDelete(event[2])
- drawDeleteScreen()
- loadMemos()
- end
- end
- end
- --265 up arrow, 264 down arrow
- local function scroll(key)
- if key == 265 then --Scroll up
- linesScrolled = linesScrolled - 1
- if linesScrolled < 0 then
- linesScrolled = 0
- end
- for i = 1,linesScrolled do
- table.remove(memos, 1)
- end
- showMemos()
- loadMemos()
- elseif key == 264 then --Scroll down
- linesScrolled = linesScrolled + 1
- if linesScrolled > #memos - 17 then
- linesScrolled = #memos - 17
- end
- for i = 1,linesScrolled do
- table.remove(memos, 1)
- end
- showMemos()
- loadMemos()
- end
- end
- --Perform different actions based on button pressed
- local function buttonAction()
- local event = {os.pullEvent()}
- if add:waitForClick(event) then
- addMemos()
- showMemos()
- elseif remove:waitForClick(event) then
- removeMemos()
- showMemos()
- elseif event[1] == "key" then
- scroll(event[2])
- elseif exit:waitForClick(event) then
- os.reboot()
- end
- end
- --Run program
- loadMemos()
- term.clear()
- showMemos()
- while true do
- makeButtons()
- buttonAction()
- end
Advertisement
Add Comment
Please, Sign In to add comment