Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pocket Computer Address Manager
- -- Full edit mode: name, address (grid), notes
- -- Address: 7 digits chosen from grid (0–38), no duplicates, undo supported
- local addresses = {}
- local currentTab = "Home"
- local w, h = term.getSize()
- local selectedIndex = nil
- local editMode = nil -- "name", "address", "notes"
- local function clear()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
- local function drawTabs()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1) term.write("Home")
- term.setCursorPos(7,1) term.write("List")
- term.setCursorPos(12,1) term.write("Add")
- end
- ----------------------------------------------------
- -- HOME TAB
- ----------------------------------------------------
- local function drawHome()
- clear()
- drawTabs()
- term.setCursorPos(1,3) term.write("Address Manager")
- term.setCursorPos(1,5) term.write("Tabs:")
- term.setCursorPos(2,6) term.write("- Home")
- term.setCursorPos(2,7) term.write("- List")
- term.setCursorPos(2,8) term.write("- Add")
- end
- ----------------------------------------------------
- -- LIST TAB (Clickable names)
- ----------------------------------------------------
- local function drawList()
- clear()
- drawTabs()
- term.setCursorPos(1,3) term.write("Saved Addresses:")
- local y = 5
- for i, a in ipairs(addresses) do
- term.setCursorPos(1,y)
- term.write(i..": "..a.name)
- y = y + 1
- end
- end
- local function handleListClick(mx,my)
- local y = 5
- for i,_ in ipairs(addresses) do
- if my == y then
- selectedIndex = i
- currentTab = "Details"
- editMode = nil
- return
- end
- y = y + 1
- end
- end
- ----------------------------------------------------
- -- DETAILS SCREEN
- ----------------------------------------------------
- local function drawDetails()
- clear()
- drawTabs()
- local a = addresses[selectedIndex]
- if not a then currentTab="List" return end
- term.setCursorPos(1,3)
- term.write("Name: " .. a.name)
- term.setCursorPos(1,4)
- term.write("Address: ")
- for _,d in ipairs(a.addr) do term.write(d.." ") end
- term.setCursorPos(1,6)
- term.write("Notes:")
- term.setCursorPos(1,7)
- term.write(a.notes or "")
- -- Buttons
- term.setCursorPos(1,9)
- term.setBackgroundColor(colors.blue)
- term.write("Edit Name")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,11)
- term.setBackgroundColor(colors.blue)
- term.write("Edit Address")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,13)
- term.setBackgroundColor(colors.blue)
- term.write("Edit Notes")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,15)
- term.setBackgroundColor(colors.red)
- term.write("Delete")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,17)
- term.setBackgroundColor(colors.gray)
- term.write("Back")
- term.setBackgroundColor(colors.black)
- end
- local function handleDetailsClick(mx,my)
- -- Edit Name
- if my == 9 then
- editMode = "name"
- currentTab = "Edit"
- return
- end
- -- Edit Address
- if my == 11 then
- editMode = "address"
- currentTab = "Edit"
- return
- end
- -- Edit Notes
- if my == 13 then
- editMode = "notes"
- currentTab = "Edit"
- return
- end
- -- Delete
- if my == 15 then
- table.remove(addresses, selectedIndex)
- selectedIndex = nil
- currentTab = "List"
- return
- end
- -- Back
- if my == 17 then
- selectedIndex = nil
- currentTab = "List"
- return
- end
- end
- ----------------------------------------------------
- -- EDIT MODE SCREEN
- ----------------------------------------------------
- local editName = ""
- local editAddr = {}
- local editUsed = {}
- local editNotes = ""
- local function drawEdit()
- clear()
- drawTabs()
- local a = addresses[selectedIndex]
- if editMode == "name" then
- term.setCursorPos(1,3) term.write("Edit Name:")
- term.setCursorPos(1,4) term.write("> "..editName)
- term.setCursorPos(1,6)
- term.setBackgroundColor(colors.green)
- term.write("Save")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,8)
- term.setBackgroundColor(colors.gray)
- term.write("Cancel")
- term.setBackgroundColor(colors.black)
- elseif editMode == "notes" then
- term.setCursorPos(1,3) term.write("Edit Notes:")
- term.setCursorPos(1,5) term.write(editNotes)
- term.setCursorPos(1,7)
- term.setBackgroundColor(colors.green)
- term.write("Save")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,9)
- term.setBackgroundColor(colors.gray)
- term.write("Cancel")
- term.setBackgroundColor(colors.black)
- elseif editMode == "address" then
- term.setCursorPos(1,3) term.write("Edit Address:")
- term.setCursorPos(1,4)
- term.write("Addr: ")
- for _,d in ipairs(editAddr) do term.write(d.." ") end
- -- Undo button
- term.setCursorPos(1,5)
- term.setBackgroundColor(colors.blue)
- term.write("Undo")
- term.setBackgroundColor(colors.black)
- -- Grid
- local x,y = 1,7
- for i=0,38 do
- term.setCursorPos(x,y)
- if editUsed[i] then
- term.setTextColor(colors.red)
- term.write("X")
- term.setTextColor(colors.white)
- else
- term.write(i)
- end
- x = x + 3
- if x > w-2 then x = 1 y = y + 1 end
- end
- end
- end
- local function handleEditClick(mx,my)
- local a = addresses[selectedIndex]
- if editMode == "name" then
- if my == 6 then
- a.name = editName
- currentTab = "Details"
- return
- elseif my == 8 then
- currentTab = "Details"
- return
- end
- elseif editMode == "notes" then
- if my == 7 then
- a.notes = editNotes
- currentTab = "Details"
- return
- elseif my == 9 then
- currentTab = "Details"
- return
- end
- elseif editMode == "address" then
- -- Undo
- if my == 5 and mx >= 1 and mx <= 4 then
- if #editAddr > 0 then
- local last = editAddr[#editAddr]
- editUsed[last] = nil
- table.remove(editAddr)
- drawEdit()
- end
- return
- end
- -- Grid
- local x,y = 1,7
- for i=0,38 do
- if mx >= x and mx <= x+1 and my == y then
- if editUsed[i] then
- term.setCursorPos(1,h)
- term.setTextColor(colors.red)
- term.write("Digit already used!")
- term.setTextColor(colors.white)
- return
- end
- table.insert(editAddr, i)
- editUsed[i] = true
- drawEdit()
- if #editAddr == 7 then
- a.addr = editAddr
- currentTab = "Details"
- end
- return
- end
- x = x + 3
- if x > w-2 then x = 1 y = y + 1 end
- end
- end
- end
- ----------------------------------------------------
- -- ADD TAB (unchanged)
- ----------------------------------------------------
- local addState = "name"
- local newName = ""
- local newAddr = {}
- local usedDigits = {}
- local function drawAdd()
- clear()
- drawTabs()
- if addState == "name" then
- term.setCursorPos(1,3) term.write("Type Name:")
- term.setCursorPos(1,4) term.write("> "..newName)
- term.setCursorPos(1,6) term.write("[Enter to confirm]")
- elseif addState == "grid" then
- term.setCursorPos(1,3)
- term.write("Pick 7 digits:")
- term.setCursorPos(1,4)
- term.write("Addr: ")
- for _,d in ipairs(newAddr) do term.write(d.." ") end
- term.setCursorPos(1,5)
- term.setBackgroundColor(colors.blue)
- term.write("Undo")
- term.setBackgroundColor(colors.black)
- local x,y = 1,7
- for i=0,38 do
- term.setCursorPos(x,y)
- if usedDigits[i] then
- term.setTextColor(colors.red)
- term.write("X")
- term.setTextColor(colors.white)
- else
- term.write(i)
- end
- x = x + 3
- if x > w-2 then x = 1 y = y + 1 end
- end
- end
- end
- local function handleGridClick(mx,my)
- if mx >= 1 and mx <= 4 and my == 5 then
- if #newAddr > 0 then
- local last = newAddr[#newAddr]
- usedDigits[last] = nil
- table.remove(newAddr)
- drawAdd()
- end
- return
- end
- local x,y = 1,7
- for i=0,38 do
- if mx >= x and mx <= x+1 and my == y then
- if usedDigits[i] then
- term.setCursorPos(1,h)
- term.setTextColor(colors.red)
- term.write("Digit already used!")
- term.setTextColor(colors.white)
- return
- end
- table.insert(newAddr, i)
- usedDigits[i] = true
- drawAdd()
- if #newAddr == 7 then
- table.insert(addresses, {name=newName, addr=newAddr, notes=""})
- newName = ""
- newAddr = {}
- usedDigits = {}
- addState = "name"
- currentTab = "List"
- drawList()
- end
- return
- end
- x = x + 3
- if x > w-2 then x = 1 y = y + 1 end
- end
- end
- ----------------------------------------------------
- -- REDRAW
- ----------------------------------------------------
- local function redraw()
- if currentTab=="Home" then drawHome()
- elseif currentTab=="List" then drawList()
- elseif currentTab=="Add" then drawAdd()
- elseif currentTab=="Details" then drawDetails()
- elseif currentTab=="Edit" then drawEdit()
- end
- end
- redraw()
- ----------------------------------------------------
- -- MAIN LOOP
- ----------------------------------------------------
- while true do
- local e, p1, p2, p3 = os.pullEvent()
- if e == "mouse_click" then
- local mx,my = p2,p3
- if my == 1 then
- if mx <= 4 then currentTab="Home"
- elseif mx <= 10 then currentTab="List"
- else currentTab="Add" end
- redraw()
- elseif currentTab=="List" then
- handleListClick(mx,my)
- elseif currentTab=="Details" then
- handleDetailsClick(mx,my)
- redraw()
- elseif currentTab=="Edit" then
- handleEditClick(mx,my)
- redraw()
- elseif currentTab=="Add" and addState=="grid" then
- handleGridClick(mx,my)
- redraw()
- end
- elseif e == "char" then
- if currentTab=="Add" and addState=="name" then
- newName = newName .. p1
- drawAdd()
- elseif currentTab=="Edit" and editMode=="name" then
- editName = editName .. p1
- drawEdit()
- elseif currentTab=="Edit" and editMode=="notes" then
- editNotes = editNotes .. p1
- drawEdit()
- end
- elseif e == "key" then
- if currentTab=="Add" and addState=="name" then
- if p1 == keys.backspace then
- newName = newName:sub(1,-2)
- drawAdd()
- elseif p1 == keys.enter then
- if #newName > 0 then
- addState = "grid"
- drawAdd()
- end
- end
- elseif currentTab=="Edit" and editMode=="name" then
- if p1 == keys.backspace then
- editName = editName:sub(1,-2)
- drawEdit()
- elseif p1 == keys.enter then
- addresses[selectedIndex].name = editName
- currentTab = "Details"
- drawDetails()
- end
- elseif currentTab=="Edit" and editMode=="notes" then
- if p1 == keys.backspace then
- editNotes = editNotes:sub(1,-2)
- drawEdit()
- elseif p1 == keys.enter then
- addresses[selectedIndex].notes = editNotes
- currentTab = "Details"
- drawDetails()
- end
- end
- end
- end
Advertisement