Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Based on LDDestroier's dvdlogo
- local function dvdlogo()
- -- adjusts walls of screen so that it will bounce further/closer to the boundries of the screen
- local xMargin, yMargin = 0, 0
- local redrawDelay = nil
- local scr_x, scr_y = term.getSize()
- local max, min = math.max, math.min
- local floor, ceil = math.floor, math.ceil
- if scr_x >= 60 and scr_y >= 25 then
- redrawDelay = redrawDelay or 0.05
- else
- redrawDelay = redrawDelay or 0.1
- end
- local getSize = function(image)
- local x, y = 0, #image[1]
- for y = 1, #image[1] do
- x = max(x, #image[1][y])
- end
- return x, y
- end
- local drawImage = function(image, x, y, terminal)
- terminal = terminal or term.current()
- local cx, cy = terminal.getCursorPos()
- for iy = 0, #image[1] + 1 do
- terminal.setCursorPos(x - 1, y + (iy - 1))
- if image[1][iy] then
- terminal.blit(
- " " .. image[1][iy] .. " ",
- "f" .. image[2][iy] .. "f",
- "f" .. image[3][iy] .. "f"
- )
- else
- terminal.clearLine()
- end
- end
- terminal.setCursorPos(cx,cy)
- end
- local colorSwap = function(image, text, back)
- local output = {{},{},{}}
- for y = 1, #image[1] do
- output[1][y] = image[1][y]
- output[2][y] = image[2][y]:gsub(".", text)
- output[3][y] = image[3][y]:gsub(".", back or text)
- end
- return output
- end
- local logo = {
- xvel = (math.random(0, 1) * 2) - 1,
- yvel = (math.random(0, 1) * 2) - 1,
- x = floor(scr_x / 2),
- y = floor(scr_y / 2),
- img = {
- {
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- }, {
- "00ffffffffff000000fffffffffff",
- "000000f0f00000000f000000000f0",
- "0f00fff0000f000ff000ff0000f00",
- "0f00fff0000000f00000f0000ff00",
- "f00ff0000000f0000000f0fff0000",
- "00000000000000000000000000000",
- "000000fffffffffffffff00000000",
- "0fff0000000000000000000fff000",
- "f000000f0ffffffffff0000000000",
- "000f000000ffffff0000000000000",
- "00000000000000000000000000000",
- }, {
- "ff0000000000ffffff000000000ff",
- "ffffff000ff00ffff000ffffff00f",
- "f00ffff00ff00ff000fff00fff00f",
- "f00fff00ffff00000fff00fff000f",
- "0000000fffff000fffff000000fff",
- "fffffffffffff0fffffffffffffff",
- "ffffff000000000000000ffffffff",
- "f000000000ffffff0000000000fff",
- "00000000ffffffffff00000000fff",
- "fff00000000000000000000ffffff",
- "fffffffffffffffffffffffffffff",
- }
- }
- }
- local imgXsize, imgYsize = getSize(logo.img)
- local xWall, yWall
- local render = function(colorReplace)
- if colorReplace then
- drawImage(
- colorSwap(logo.img, {["0"] = colorReplace}, {["0"] = colorReplace}),
- floor(logo.x),
- floor(logo.y)
- )
- else
- drawImage(
- logo.img,
- floor(logo.x),
- floor(logo.y)
- )
- end
- end
- local color = math.random(1, 15)
- local tick = function()
- scr_x, scr_y = term.getSize()
- xWall = scr_x - imgXsize + 1 - xMargin
- yWall = scr_y - imgYsize + 1 - yMargin
- logo.x = min(max(logo.x + logo.xvel, 1 + xMargin), xWall)
- logo.y = min(max(logo.y + logo.yvel, 1 + yMargin), yWall)
- if floor(logo.x) == (1 + xMargin) or floor(logo.x) == xWall then
- logo.xvel = -logo.xvel
- color = math.random(1, 15)
- end
- if floor(logo.y) == (1 + yMargin) or floor(logo.y) == yWall then
- logo.yvel = -logo.yvel
- color = math.random(1, 15)
- end
- render(string.sub("0123456789abcdef", color, color))
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- local evt
- local tID = os.startTimer(redrawDelay)
- while true do
- evt = {os.pullEventRaw()}
- if evt[1] == "timer" and evt[2] == tID then
- tick()
- tID = os.startTimer(redrawDelay)
- elseif evt[1] == "key" or evt[1] == "key_up" or evt[1] == "mouse_click" or evt[1] == "mouse_up" or evt[1] == "mouse_drag" then
- render("8")
- sleep(0.05)
- render("7")
- sleep(0.05)
- term.clear()
- break
- end
- end
- return 0
- end
- function _G.read( _sReplaceChar, _tHistory, _fnComplete, _sDefault )
- if _sReplaceChar ~= nil and type( _sReplaceChar ) ~= "string" then
- error( "bad argument #1 (expected string, got " .. type( _sReplaceChar ) .. ")", 2 )
- end
- if _tHistory ~= nil and type( _tHistory ) ~= "table" then
- error( "bad argument #2 (expected table, got " .. type( _tHistory ) .. ")", 2 )
- end
- if _fnComplete ~= nil and type( _fnComplete ) ~= "function" then
- error( "bad argument #3 (expected function, got " .. type( _fnComplete ) .. ")", 2 )
- end
- if _sDefault ~= nil and type( _sDefault ) ~= "string" then
- error( "bad argument #4 (expected string, got " .. type( _sDefault ) .. ")", 2 )
- end
- term.setCursorBlink( true )
- local sLine
- if type( _sDefault ) == "string" then
- sLine = _sDefault
- else
- sLine = ""
- end
- local nHistoryPos
- local nPos, nScroll = #sLine, 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local tCompletions
- local nCompletion
- local function recomplete()
- if _fnComplete and nPos == string.len(sLine) then
- tCompletions = _fnComplete( sLine )
- if tCompletions and #tCompletions > 0 then
- nCompletion = 1
- else
- nCompletion = nil
- end
- else
- tCompletions = nil
- nCompletion = nil
- end
- end
- local function uncomplete()
- tCompletions = nil
- nCompletion = nil
- end
- local w = term.getSize()
- local sx = term.getCursorPos()
- local function redraw( _bClear )
- local cursor_pos = nPos - nScroll
- if sx + cursor_pos >= w then
- nScroll = (sx + nPos) - w
- elseif cursor_pos < 0 then
- nScroll = nPos
- end
- local cx,cy = term.getCursorPos()
- term.setCursorPos( sx, cy )
- local sReplace = (_bClear and " ") or _sReplaceChar
- if sReplace then
- term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- if nCompletion then
- local sCompletion = tCompletions[ nCompletion ]
- local oldText, oldBg
- if not _bClear then
- oldText = term.getTextColor()
- oldBg = term.getBackgroundColor()
- term.setTextColor( colors.white )
- term.setBackgroundColor( colors.gray )
- end
- if sReplace then
- term.write( string.rep( sReplace, string.len( sCompletion ) ) )
- else
- term.write( sCompletion )
- end
- if not _bClear then
- term.setTextColor( oldText )
- term.setBackgroundColor( oldBg )
- end
- end
- term.setCursorPos( sx + nPos - nScroll, cy )
- end
- local function clear()
- redraw( true )
- end
- recomplete()
- redraw()
- local function acceptCompletion()
- if nCompletion then
- -- Clear
- clear()
- -- Find the common prefix of all the other suggestions which start with the same letter as the current one
- local sCompletion = tCompletions[ nCompletion ]
- sLine = sLine .. sCompletion
- nPos = string.len( sLine )
- -- Redraw
- recomplete()
- redraw()
- end
- end
- local tm
- while true do
- if tm ~= nil then os.cancelTimer(tm) end
- tm = os.startTimer(60)
- local sEvent, param, cursorx, cursory = os.pullEvent()
- if sEvent == "char" then
- -- Typed key
- clear()
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- recomplete()
- redraw()
- elseif sEvent == "paste" then
- -- Pasted text
- clear()
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + string.len( param )
- recomplete()
- redraw()
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- if nCompletion then
- clear()
- uncomplete()
- redraw()
- end
- break
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- clear()
- nPos = nPos - 1
- recomplete()
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLine) then
- -- Move right
- clear()
- nPos = nPos + 1
- recomplete()
- redraw()
- else
- -- Accept autocomplete
- acceptCompletion()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if nCompletion then
- -- Cycle completions
- clear()
- if param == keys.up then
- nCompletion = nCompletion - 1
- if nCompletion < 1 then
- nCompletion = #tCompletions
- end
- elseif param == keys.down then
- nCompletion = nCompletion + 1
- if nCompletion > #tCompletions then
- nCompletion = 1
- end
- end
- redraw()
- elseif _tHistory then
- -- Cycle history
- clear()
- if param == keys.up then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos, nScroll = string.len( sLine ), 0
- else
- sLine = ""
- nPos, nScroll = 0, 0
- end
- uncomplete()
- redraw()
- end
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- clear()
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- if nScroll > 0 then nScroll = nScroll - 1 end
- recomplete()
- redraw()
- end
- elseif param == keys.home then
- -- Home
- if nPos > 0 then
- clear()
- nPos = 0
- recomplete()
- redraw()
- end
- elseif param == keys.delete then
- -- Delete
- if nPos < string.len(sLine) then
- clear()
- sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
- recomplete()
- redraw()
- end
- elseif param == keys["end"] then
- -- End
- if nPos < string.len(sLine ) then
- clear()
- nPos = string.len(sLine)
- recomplete()
- redraw()
- end
- elseif param == keys.tab then
- -- Tab (accept autocomplete)
- acceptCompletion()
- end
- -- Borrowed from [CC: Tweaked](https://github.com/SquidDev/CC-Tweaked)
- elseif sEvent == "mouse_click" or sEvent == "mouse_drag" and param == 1 then
- local _, cy = term.getCursorPos()
- if cursorx >= sx and cursorx <= w and cursory == cy then
- -- Ensure we don't scroll beyond the current line
- nPos = math.min(math.max(nScroll + cursorx - sx, 0), #sLine)
- redraw()
- end
- elseif sEvent == "term_resize" then
- -- Terminal resized
- w = term.getSize()
- redraw()
- elseif sEvent == "timer" and param == tm then
- local oldblink, oldfg, oldbg = term.getCursorBlink(), term.getTextColor(), term.getBackgroundColor()
- term.setCursorBlink(false)
- dvdlogo()
- term.setCursorBlink(oldblink)
- term.setTextColor(oldfg)
- term.setBackgroundColor(oldbg)
- redraw()
- end
- end
- local cx, cy = term.getCursorPos()
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, cy )
- print()
- return sLine
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement