Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = nil
- local serverName = ""
- local serverID = 0
- local clientID = 0
- local side = "top"
- local selected = 1
- local w, h = term.getSize()
- local pos = 0
- local color = colors.white
- local infoText = ""
- local index = 1
- local cmds = {}
- local originID = ""
- local currentID = ""
- local isText = false
- local toOpen = ""
- local cancel = false
- local name = ""
- os.loadAPI("menu")
- function ask(request)
- modem.transmit(serverID, clientID, request)
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- return message
- end
- function info(text, isCenter)
- if text == nil then
- term.setCursorPos(1, y+2)
- elseif isCenter then
- x, y = term.getCursorPos()
- x = (w - #text) / 2
- term.setCursorPos(x, y)
- write(text)
- term.setCursorPos(1, y+1)
- else
- print(text)
- end
- end
- function longInfo(text, isCenter)
- line = ""
- for k,s in pairs(split(text, " ")) do
- length = string.len(line) + string.len(s) + 1
- if w >= length then
- a = line .. " " .. s
- line = a
- else
- info(line, isCenter)
- line = " "..s
- end
- end
- info(line, isCenter)
- end
- function hr()
- x, y = term.getCursorPos()
- for i = 1, w, 1 do
- write("-")
- end
- term.setCursorPos(1, y+1)
- end
- function cmdLine()
- flag = true
- count = 0
- while not cancel do
- term.setCursorPos(1, h)
- term.setBackgroundColor(colors.black)
- term.setTextColor(color)
- term.clearLine()
- textLine = "> "..infoText
- term.write(textLine)
- term.setCursorPos(#textLine + 1, h)
- if flag then
- term.setBackgroundColor(color)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- flag = false
- else
- term.setBackgroundColor(colors.black)
- term.write(" ")
- term.setBackgroundColor(color)
- flag = true
- end
- sleep(0.5)
- count = count + 1
- if count >= 10 then
- infoText = ""
- count = 0
- end
- end
- end
- function buttons()
- while true do
- local event, key, isHeld = os.pullEvent("key")
- if not isHeld then
- if isText then
- if not isEmpty(originID) then
- toOpen = originID
- isText = false
- currentID = ""
- originID = ""
- return
- end
- end
- if key == keys.up then
- if selected > 1 then
- selected = selected - 1
- else
- if index > 1 then
- index = index - 1
- end
- end
- elseif key == keys.down then
- if selected < table.getn(cmds) then
- if selected + pos < h - 1 then
- selected = selected + 1
- else
- if index < (h - pos) then
- index = index + 1
- end
- end
- end
- elseif key == keys.enter then
- cmd = cmds[(selected + index - 1)]
- if string.starts(cmd[1], "toggle") then
- numb = tonumber(split(cmd[1], " ")[2])
- flag = ask("check "..numb)
- if cmd[3] then flag = not flag end
- if flag then
- infoText = "Disabling "..cmd[2]
- else
- infoText = "Enabling "..cmd[2]
- end
- ask("toggle "..numb)
- elseif string.starts(cmd[1], "trigger") then
- numb = tonumber(split(cmd[1], " ")[2])
- infoText = "Activating "..cmd[2]
- ask("pulse "..numb)
- elseif string.starts(cmd[1], "open") then
- id = split(cmd[1], " ")[2]
- toOpen = id
- isText = false
- return
- elseif string.starts(cmd[1], "read") then
- id = split(cmd[1], " ")[2]
- toOpen = id
- isText = true
- return
- end
- elseif key == keys.backspace then
- if not isEmpty(originID) then
- toOpen = originID
- currentID = ""
- originID = ""
- return
- end
- end
- options(index)
- end
- end
- end
- function options(index)
- term.setCursorPos(x,pos)
- local ind = index
- if cmds == nil then
- return
- end
- for a = pos+1, h-1, 1 do
- cmd = cmds[ind]
- if cmd == nil then
- else
- if ind == (selected + index - 1) then
- term.setTextColor(colors.black)
- term.setBackgroundColor(color)
- else
- term.setTextColor(color)
- term.setBackgroundColor(colors.black)
- end
- term.clearLine()
- if string.starts(cmd[1], "toggle") then
- numb = tonumber(split(cmd[1], " ")[2])
- info("> "..cmd[2].." "..hasColor(numb, cmd[3]))
- elseif string.starts(cmd[1], "trigger") then
- info("> "..cmd[2])
- elseif string.starts(cmd[1], "open") then
- info("> "..cmd[2])
- elseif string.starts(cmd[1], "read") then
- info("> "..cmd[2])
- end
- end
- ind = ind + 1
- end
- end
- function renderMenu(m)
- isText = false
- if isEmpty(currentID) then
- currentID = m.id
- else
- originID = currentID
- currentID = m.id
- end
- term.setTextColor(color)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
- info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
- info("-Server "..serverID.."-", true)
- info(nil, false)
- info(serverName, false)
- info("======================", false)
- info(m.title, false)
- hr()
- x,y = term.getCursorPos()
- pos = (h - y)
- selected = 1
- cmds = m.cmds
- options(index)
- parallel.waitForAny(buttons, cmdLine)
- if not isEmpty(toOpen) then
- if isText then
- renderText(menu.getText(toOpen))
- else
- renderMenu(menu.getMenu(toOpen))
- end
- end
- end
- function renderText(text)
- isText = true
- if isEmpty(currentID) then
- currentID = text.id
- else
- originID = currentID
- currentID = text.id
- end
- term.setTextColor(color)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
- info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
- info("-Server 1-", true)
- info(nil, false)
- info(serverName, false)
- info("======================", false)
- info(text.title, false)
- hr()
- x,y = term.getCursorPos()
- pos = (h - y)
- selected = 1
- cmds = {}
- longInfo(text.txt, false)
- parallel.waitForAny(buttons, cmdLine)
- if not isEmpty(toOpen) then
- if isText then
- renderText(menu.getText(toOpen))
- else
- renderMenu(menu.getMenu(toOpen))
- end
- end
- end
- function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- function string.starts(String,Start)
- return string.sub(String,1,string.len(Start))==Start
- end
- function isEmpty(s)
- return s == nil or s == ''
- end
- function hasColor(c, flag)
- f = ask("check "..c)
- if flag then f = not f end
- if f then
- return "[On]"
- else
- return "[Off]"
- end
- end
- function init(c, port, id, n, s)
- side = s
- color = c
- modem = peripheral.wrap(side)
- serverID = port
- clientID = id
- name = n
- modem.open(clientID)
- term.setTextColor(color)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- serverName = ask("name:"..name)
- renderMenu(menu.getMenu("start"))
- end
Add Comment
Please, Sign In to add comment