-- Teleport menu. local channel = "username.tp" local store = "tp.names" local list = settings.get(store, { "home" }) local pos, top = 1, 1 local info = "Ready" local w, h, rows local accent = term.isColor() and colors.cyan or colors.white local function valid(name) return type(name) == "string" and #name > 0 and #name <= 32 and name:match("^[A-Za-z0-9_-]+$") ~= nil end if type(list) ~= "table" then error("Invalid tp.names setting", 0) end for _, name in ipairs(list) do if not valid(name) then error("Invalid saved name", 0) end end local function clear() term.setBackgroundColor(colors.black) term.setTextColor(colors.white) term.setCursorBlink(false) term.clear() term.setCursorPos(1, 1) end local function line(y, text, fg, bg) term.setBackgroundColor(bg or colors.black) term.setTextColor(fg or colors.white) term.setCursorPos(1, y) term.clearLine() term.write(text:sub(1, w)) end local function save() settings.set(store, list) if not settings.save("/.settings") then error("Cannot save /.settings", 0) end end local function ask(title) clear() print(title) print("Empty = cancel") write("> ") return read():match("^%s*(.-)%s*$") end local function add() local name = ask("New name (e.g. mine):") if name == "" then return end if not valid(name) then info = "Use A-Z, 0-9, _- (1-32)" return end for _, item in ipairs(list) do if item == name then info = "Name already exists" return end end list[#list + 1] = name save() pos = #list info = "Added: " .. name end local function remove() local name = list[pos] if not name then return end if ask("Delete " .. name .. "? y/N"):lower() ~= "y" then return end table.remove(list, pos) save() info = "Deleted: " .. name end local function send() local name = list[pos] if not name then return end local modem = peripheral.find("modem", function(_, m) return m.isWireless() end) if not modem then info = "No wireless modem!" return end local ok = pcall(function() rednet.open(peripheral.getName(modem)) rednet.broadcast(name, channel) end) info = ok and ("Sent: " .. name) or "Send failed!" end local function draw() w, h = term.getSize() if w < 22 or h < 12 then error("Screen too small", 0) end rows = h - 8 pos = math.max(1, math.min(pos, #list)) top = math.max( 1, math.min( top, math.max(1, #list - rows + 1) ) ) if pos < top then top = pos end if pos >= top + rows then top = pos - rows + 1 end clear() local count = #list > 0 and pos or 0 line( 1, (" TELEPORTS [%d/%d]"):format(count, #list), colors.black, accent ) line(2, "Channel: " .. channel) line(3, string.rep("-", w)) for i = 0, rows - 1 do local n = top + i if list[n] then local selected = n == pos line( 4 + i, (selected and "> " or " ") .. list[n], selected and colors.black or colors.white, selected and accent or colors.black ) end end if #list == 0 then line(4, "No teleports. Press N.") end line(h - 4, string.rep("-", w)) line(h - 3, info, accent) line(h - 2, "[Enter] Go [N] Add") line(h - 1, "[D] Delete [Q] Exit") line(h, "Up/Down / scroll: select") end local function run() while true do draw() local event, a, b, c = os.pullEvent() if event == "key" then if a == keys.up then pos = pos - 1 elseif a == keys.down then pos = pos + 1 elseif a == keys.enter and not b then send() end elseif event == "char" then a = a:lower() if a == "n" then add() elseif a == "d" then remove() elseif a == "q" then break end elseif event == "mouse_scroll" then pos = pos + a elseif event == "mouse_click" and a == 1 then if c >= 4 and c < 4 + rows then local n = top + c - 4 if list[n] then pos = n end elseif c == h - 2 then if b <= 10 then send() elseif b >= 13 and b <= 19 then add() end elseif c == h - 1 then if b <= 10 then remove() elseif b >= 13 and b <= 20 then break end end end end end local ok, err = pcall(run) clear() if not ok then printError(err) end