Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local event = require('event')
- local term = require('term')
- local gpu = term.gpu()
- local debug = require('component').debug
- local unicode = require('unicode')
- local fs = require('filesystem')
- local hex = '0123456789abcdef'
- local colors = {
- 0x000000, -- 0
- 0x0000aa, -- 1
- 0x00aa00, -- 2
- 0x00aaaa, -- 3
- 0xaa0000, -- 4
- 0xaa00aa, -- 5
- 0xffaa00, -- 6
- 0xaaaaaa, -- 7
- 0x555555, -- 8
- 0x5555ff, -- 9
- 0x55ff55, -- a
- 0x55ffff, -- b
- 0xff5555, -- c
- 0xff55ff, -- d
- 0xffff55, -- e
- 0xffffff -- f
- }
- local ofg = gpu.setForeground(colors[16])
- local obg = gpu.setBackground(colors[1])
- local ow, oh = gpu.getResolution()
- gpu.setResolution(80, 25)
- term.clear()
- for i = 1, 16 do
- if i < 3 then
- gpu.setForeground(0xffffff)
- else
- gpu.setForeground(0x000000)
- end
- gpu.setBackground(colors[i])
- gpu.set((i - 1) * 5 + 1, 4, ' ' .. hex:sub(i, i) .. ' ')
- end
- gpu.set(76, 4, unicode.char(9656))
- gpu.set(80, 4, unicode.char(9666))
- gpu.fill(1, 5, 80, 1, ' ')
- gpu.set(18, 5, 'Quit')
- gpu.set(54, 5, 'Run /tellraw')
- gpu.setForeground(colors[16])
- gpu.setBackground(colors[1])
- local text = ''
- local colr = ''
- local cursor = 1
- local scroll = 0
- local ccolor = 16
- local function waitForEvent()
- local char, fg, bg = gpu.get(cursor - scroll, 1)
- local ofg = gpu.getForeground()
- local obg = gpu.getBackground()
- local isShown = true
- while true do
- if isShown then
- gpu.setForeground(bg)
- gpu.setBackground(fg)
- else
- gpu.setForeground(fg)
- gpu.setBackground(bg)
- end
- isShown = not isShown
- gpu.set(cursor - scroll, 1, char)
- local evt, arg1, arg2, arg3, arg4, arg5, arg6 = event.pull(0.5)
- if evt then
- gpu.setForeground(fg)
- gpu.setBackground(bg)
- gpu.set(cursor - scroll, 1, char)
- gpu.setForeground(ofg)
- gpu.setBackground(obg)
- return evt, arg1, arg2, arg3, arg4, arg5, arg6
- end
- end
- end
- local function getWidth()
- return ({gpu.getResolution()})[1]
- end
- local function setCursor(x)
- cursor = math.max(math.min(x, text:len() + 1), 1)
- if cursor - scroll < 6 then
- scroll = math.max(cursor - 6, 0)
- end
- local width = getWidth()
- if cursor - scroll > width - 7 then
- scroll = cursor - width + 7
- end
- end
- local function setCursorBy(delta)
- setCursor(cursor + delta)
- end
- local function drawLine()
- local width = getWidth()
- local toDraw = text:sub(scroll + 1, scroll + width - 3)
- local toColr = colr:sub(scroll + 1, scroll + width - 3)
- local obg = gpu.setBackground(0x000000)
- local ofg = gpu.setForeground(0xffffff)
- gpu.fill(1, 1, width - 3, 2, ' ')
- for i = 1, width do
- local color = hex:find(toColr:sub(i, i))
- gpu.setForeground(colors[color])
- if color == 1 then
- gpu.setBackground(0xffffff)
- else
- gpu.setBackground(obg)
- end
- gpu.set(i, 1, toDraw:sub(i, i))
- end
- gpu.setForeground(colors[9])
- gpu.setBackground(colors[1])
- gpu.set(1, 2, toColr)
- gpu.setForeground(colors[1])
- gpu.setBackground(colors[5])
- gpu.set(width - 2, 1, ' x ')
- gpu.setBackground(obg)
- gpu.setForeground(ofg)
- end
- local function escape(char)
- local hex = ('%x'):format(string.byte(char))
- return '\\u' .. string.rep('0', 4 - hex:len()) .. hex
- end
- local function runCommand()
- if text:len() > 0 then
- local json = '["'
- local last = nil
- for i = 1, colr:len() do
- local color = colr:sub(i, i)
- if color ~= last then
- json = json .. '\\u00a7' .. color
- last = color
- end
- local char = text:sub(i, i)
- json = json .. escape(char)
- if char == '\n' then
- last = 16
- end
- end
- json = json .. '"]'
- local status, result = debug.runCommand('/tellraw @a ' .. json)
- end
- end
- local function drawSaves()
- local obg = gpu.getBackground()
- local ofg = gpu.setForeground(colors[1])
- local w, h = gpu.getResolution()
- for i = 1, h - 5 do
- local num = tostring(i)
- gpu.setBackground(colors[4])
- gpu.set(1, i + 5, ' ' .. string.rep('0', 3 - num:len()) .. num .. ' ')
- gpu.setBackground(colors[5])
- gpu.set(w - 2, i + 5, ' x ')
- gpu.setBackground(colors[8])
- gpu.fill(6, i + 5, (w / 2) - 4, 1, ' ')
- gpu.set(2 + w / 4, i + 5, 'Save')
- if fs.exists('/home/tellraw.save.' .. num) then
- gpu.setBackground(colors[8])
- else
- gpu.setBackground(colors[9])
- end
- gpu.fill(w / 2 + 2, i + 5, (w / 2) - 4, 1, ' ')
- gpu.set(w / 2 + (w / 4) - 2, i + 5, 'Load')
- end
- gpu.setForeground(ofg)
- gpu.setBackground(obg)
- end
- drawSaves()
- drawLine()
- local function deleteSave(num)
- fs.remove('/home/tellraw.save.' .. tostring(num))
- end
- local function loadSave(num)
- local path = '/home/tellraw.save.' .. tostring(num)
- local handle = fs.open(path, 'r')
- if handle then
- local size = fs.size(path)
- local data = handle:read(size)
- handle:close()
- local match = data:find('(%x+)$')
- local dcolr = data:sub(match)
- local dtext = data:sub(1, match - 2)
- if dcolr:len() == dtext:len() then
- text = dtext
- colr = dcolr
- scroll = 0
- setCursor(text:len() + 1)
- drawLine()
- end
- end
- end
- local function saveTo(num)
- local path = '/home/tellraw.save.' .. tostring(num)
- local handle = fs.open(path, 'w')
- if handle then
- handle:write(text .. '\n' .. colr)
- handle:close()
- end
- end
- while true do
- local oscroll = scroll
- local otext = text
- local evt, _, arg1, arg2 = waitForEvent()
- if evt == 'key_down' then
- if arg1 > 0 and arg1 < 127 then
- local char = unicode.char(arg1)
- if arg1 == 13 then
- char = '\n'
- end
- local cpos = hex:sub(ccolor, ccolor)
- text = text:sub(1, cursor - 1) .. char .. text:sub(cursor)
- colr = colr:sub(1, cursor - 1) .. cpos .. colr:sub(cursor)
- setCursorBy(1)
- elseif arg1 == 127 and cursor > 1 then
- text = text:sub(1, cursor - 2) .. text:sub(cursor)
- colr = colr:sub(1, cursor - 2) .. colr:sub(cursor)
- setCursorBy(-1)
- elseif arg1 == 63234 then -- left arrow
- setCursorBy(-1)
- elseif arg1 == 63235 then -- right arrow
- setCursorBy(1)
- elseif arg1 == 63273 then -- Home
- setCursor(1)
- elseif arg1 == 63275 then -- End
- setCursor(#text + 1)
- end
- elseif evt == 'clipboard' then
- local cpos = hex:sub(ccolor, ccolor)
- text = text:sub(1, cursor - 1) .. arg1 .. text:sub(cursor)
- colr = colr:sub(1, cursor - 1) .. string.rep(cpos, arg1:len()) .. colr:sub(cursor)
- setCursorBy(arg1:len())
- drawLine()
- elseif evt == 'touch' or evt == 'drag' then
- local w = getWidth()
- if arg2 == 1 then
- if arg1 > w - 3 then
- text = ''
- colr = ''
- setCursor(1)
- drawLine()
- else
- setCursor(arg1 + scroll)
- end
- elseif arg2 == 2 then
- if arg1 + scroll <= #colr then
- colr = colr:sub(1, arg1 + scroll - 1) .. hex:sub(ccolor, ccolor) .. colr:sub(arg1 + scroll + 1)
- drawLine()
- end
- elseif arg2 == 4 then
- local ofg = gpu.setForeground(ccolor < 3 and 0xffffff or 0x000000)
- local obg = gpu.setBackground(colors[ccolor])
- gpu.set((ccolor - 1) * 5 + 1, 4, ' ')
- gpu.set((ccolor - 1) * 5 + 5, 4, ' ')
- ccolor = math.floor((arg1 - 1) / 5) + 1
- gpu.setForeground(ccolor < 3 and 0xffffff or 0x000000)
- gpu.setBackground(colors[ccolor])
- gpu.set((ccolor - 1) * 5 + 1, 4, unicode.char(9656))
- gpu.set((ccolor - 1) * 5 + 5, 4, unicode.char(9666))
- gpu.setForeground(ofg)
- gpu.setBackground(obg)
- elseif arg2 == 5 then
- if arg1 < 40 then
- break
- elseif arg1 <= 80 then
- runCommand()
- end
- elseif arg2 > 5 then
- local num = math.floor(arg2 - 5)
- if arg1 > 5 then
- if arg1 > w - 3 then
- deleteSave(num)
- elseif arg1 > w / 2 + 1 then
- loadSave(num)
- elseif arg1 > 5 then
- saveTo(num)
- end
- end
- drawSaves()
- end
- end
- if oscroll ~= scroll or otext ~= text then
- drawLine()
- end
- end
- gpu.setForeground(ofg)
- gpu.setBackground(obg)
- gpu.setResolution(ow, oh)
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement