Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local m = component.modem -- get primary modem component
- m.open(321)
- print(m.isOpen(321)) -- true
- m.setStrength(500)
- -- Send some message.
- if not term.isAvailable() then
- return
- end
- local args, options = shell.parse(...)
- args[1] = "Menu.txt"
- local filename = shell.resolve(args[1])
- local readonly = options.r or fs.get(filename) == nil or fs.get(filename).isReadOnly()
- if fs.isDirectory(filename) or readonly and not fs.exists(filename) then
- print("file not found")
- return
- end
- term.clear()
- term.setCursorBlink(true)
- local running = true
- local buffer = {}
- local scrollX, scrollY = 0, 0
- -------------------------------------------------------------------------------
- local function setStatus(value)
- local w, h = component.gpu.getResolution()
- component.gpu.set(1, h, text.padRight(unicode.sub(value, 1, w - 10), w - 10))
- end
- local function getSize()
- local w, h = component.gpu.getResolution()
- return w, h - 1
- end
- local function getCursor()
- local cx, cy = term.getCursor()
- return cx + scrollX, cy + scrollY
- end
- local function line()
- local cbx, cby = getCursor()
- return buffer[cby]
- end
- local function setCursor(nbx, nby)
- local w, h = getSize()
- nby = math.max(1, math.min(#buffer, nby))
- local ncy = nby - scrollY
- if ncy > h then
- term.setCursorBlink(false)
- local sy = nby - h
- local dy = math.abs(scrollY - sy)
- scrollY = sy
- component.gpu.copy(1, 1 + dy, w, h - dy, 0, -dy)
- for by = nby - (dy - 1), nby do
- local str = text.padRight(unicode.sub(buffer[by], 1 + scrollX), w)
- component.gpu.set(1, by - scrollY, str)
- end
- elseif ncy < 1 then
- term.setCursorBlink(false)
- local sy = nby - 1
- local dy = math.abs(scrollY - sy)
- scrollY = sy
- component.gpu.copy(1, 1, w, h - dy, 0, dy)
- for by = nby, nby + (dy - 1) do
- local str = text.padRight(unicode.sub(buffer[by], 1 + scrollX), w)
- component.gpu.set(1, by - scrollY, str)
- end
- end
- term.setCursor(term.getCursor(), nby - scrollY)
- nbx = math.max(1, math.min(unicode.len(line()) + 1, nbx))
- local ncx = nbx - scrollX
- if ncx > w then
- term.setCursorBlink(false)
- local sx = nbx - w
- local dx = math.abs(scrollX - sx)
- scrollX = sx
- component.gpu.copy(1 + dx, 1, w - dx, h, -dx, 0)
- for by = 1 + scrollY, math.min(h + scrollY, #buffer) do
- local str = unicode.sub(buffer[by], nbx - (dx - 1), nbx)
- str = text.padRight(str, dx)
- component.gpu.set(1 + (w - dx), by - scrollY, str)
- end
- elseif ncx < 1 then
- term.setCursorBlink(false)
- local sx = nbx - 1
- local dx = math.abs(scrollX - sx)
- scrollX = sx
- component.gpu.copy(1, 1, w - dx, h, dx, 0)
- for by = 1 + scrollY, math.min(h + scrollY, #buffer) do
- local str = unicode.sub(buffer[by], nbx, nbx + dx)
- --str = text.padRight(str, dx)
- component.gpu.set(1, by - scrollY, str)
- end
- end
- term.setCursor(nbx - scrollX, nby - scrollY)
- component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10))
- end
- local function up(n)
- n = n or 1
- local cbx, cby = getCursor()
- if cby > 1 then
- setCursor(cbx, cby - n)
- else
- cby = #buffer + 1
- setCursor(cbx, cby - n)
- end
- end
- local function down(n)
- n = n or 1
- local cbx, cby = getCursor()
- if cby < #buffer then
- setCursor(cbx, cby + n)
- else
- cby = 0
- setCursor(cbx, cby + n)
- end
- end
- function trim(s)
- return (s:gsub("^%s*(.-)%s*$", "%1"))
- end
- local function enter()
- term.setCursorBlink(false)
- local cx, cy = term.getCursor()
- local cbx, cby = getCursor()
- local w, h = getSize()
- if cby == #buffer then
- running = false
- end
- m.broadcast(321, trim(buffer[cby]))
- setCursor(1, cby)
- end
- local controlKeyCombos = {[keyboard.keys.s]=true,[keyboard.keys.w]=true,
- [keyboard.keys.c]=true,[keyboard.keys.x]=true}
- local function onKeyDown(char, code)
- if code == keyboard.keys.up then
- up()
- elseif code == keyboard.keys.down then
- down()
- elseif code == keyboard.keys.enter and not readonly then
- enter()
- end
- end
- -------------------------------------------------------------------------------
- do
- local f = io.open(filename)
- if f then
- local w, h = getSize()
- local chars = 0
- for line in f:lines() do
- table.insert(buffer, line)
- chars = chars + unicode.len(line)
- if #buffer <= h then
- component.gpu.set(1, #buffer, line)
- end
- end
- f:close()
- if #buffer == 0 then
- table.insert(buffer, "")
- end
- end
- setCursor(1, 1)
- end
- while running do
- local event, address, arg1, arg2, arg3 = event.pull()
- if type(address) == "string" and component.isPrimary(address) then
- local blink = true
- if event == "key_down" then
- onKeyDown(arg1, arg2)
- else
- blink = false
- end
- if blink then
- term.setCursorBlink(true)
- term.setCursorBlink(true) -- force toggle to caret
- end
- end
- end
- term.clear()
- term.setCursorBlink(false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement