Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ This program is free software. It comes without any warranty, to
- the extent permitted by applicable law. You can redistribute it
- and/or modify it under the terms of the Do What The Fuck You Want
- To Public License, Version 2, as published by Sam Hocevar. See
- http://sam.zoy.org/wtfpl/COPYING for more details. --]]
- -- The ALL lua-console v4
- -- by devast8a
- console = {
- -- Current state of console
- -- 0 = Closed
- -- 1 = Opened
- -- 2 = Old console opened
- -- [READONLY]
- active = 0,
- -- Debug state of console, 1 if active, 0 if not
- -- In no way effected by tpt.setdebug
- -- [READONLY]
- debugmode = 0,
- -- Current command line text for console
- currentCommand = "",
- -- Colors for console text
- colors = {
- textr = 200,
- textg = 32,
- textb = 255,
- texta = 255,
- },
- buffer = {}, -- Handle writing output to a larger display than screen can show
- history = {}, -- command history
- keys = {}, -- handle key input
- old = {}, -- support for old console
- quick = {}, -- Quick commands (commands that being with !)
- screen = {}, -- Handle drawing to the screen
- }
- ------------------------------------------------
- -- GLOBAL API ----------------------------------
- ------------------------------------------------
- function print(message)
- console.writeln(message)
- --console.screen.buffer = console.screen.buffer .. "\n" .. message
- end
- function error(message)
- print("[ERROR] " .. message)
- end
- -- Call cmd & automatically call error (w/ error message) upon error
- function ecall(cmd, ...)
- _, result = pcall(cmd, unpack(arg))
- if _ == false then
- error(result)
- end
- return _, result
- end
- -- Execute a commnad
- function runcommand(command)
- -- Handle quick commands
- if string.sub(command, 1, 1) == "!" then
- local l = string.find(command, " ")
- if l == nil then
- command = string.sub(command, 2)
- args = ""
- else
- args = string.trim(string.sub(command, l))
- command = string.sub(command, 2, l - 1)
- end
- cmd = console.quick[command]
- message = "No command by the name of '" .. command .. "' exists"
- -- Short hand for return
- elseif string.sub(command, 1, 1) == "@" then
- command = "return " .. string.sub(command, 2)
- cmd, message = loadstring(command)
- -- Compile the string to a function
- else
- cmd, message = loadstring(command)
- end
- -- Handle errors
- if cmd == nil then
- print(message)
- else
- if args == nil then
- _, result = pcall(cmd)
- else
- _, result = pcall(cmd, args)
- end
- print(result)
- end
- end
- ------------------------------------------------
- -- string --------------------------------------
- ------------------------------------------------
- string.trim = function (str)
- return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
- end
- string.split = function (str, split)
- local output = {}
- lastpos = 1
- while true do
- pos = string.find(str, split, lastpos)
- if pos == nil then
- table.insert(output, string.sub(str, lastpos))
- return output
- end
- table.insert(output, string.sub(str, lastpos, pos-1))
- lastpos = pos + 1
- end
- return { str }
- end
- ------------------------------------------------
- -- console -------------------------------------
- ------------------------------------------------
- console.set = function(value)
- if console.active == 2 then
- if value == 0 then
- console.active = 0
- else
- console.active = 2
- end
- return console.old.set_console(value)
- end
- if value == 0 then
- console.active = 0
- tpt.hud(1)
- else
- console.old.set_pause(1)
- tpt.hud(0)
- console.active = 1
- end
- end
- console.toggle = function()
- console.set(1-console.active)
- end
- ------------------------------------------------
- -- console.old ---------------------------------
- ------------------------------------------------
- console.old = {
- active = 0,
- hud_value = 1,
- }
- console.old.open = function()
- -- Old console quirks
- tpt.hud(1)
- -- Open the old console
- console.active = 2
- console.old.set_console(1)
- end
- ------------------------------------------------
- -- console.old.interop -------------------------
- ------------------------------------------------
- console.old.interop = {}
- -- tpt.set_pause
- console.old.interop.set_pause = function(value)
- if console.active == 0 then
- console.old.set_pause(value)
- end
- end
- console.old.set_pause = tpt.set_pause
- tpt.set_pause = console.old.interop.set_pause
- -- tpt.log
- console.old.interop.log = function(message)
- if console.active == 2 then
- return console.old.log(message)
- end
- return print(message)
- end
- console.old.log = tpt.log
- tpt.log = console.old.interop.log
- -- tpt.hud
- console.old.interop.hud = function(value)
- if value == 0 then
- console.old.hud_value = 0
- else
- console.old.hud_value = 1
- end
- console.old.hud(value)
- end
- console.old.hud = tpt.hud
- tpt.hud = console.old.interop.hud
- -- tpt.set_console
- console.old.set_console = tpt.set_console
- tpt.set_console = console.set
- ------------------------------------------------
- -- console.buffer ------------------------------
- ------------------------------------------------
- console.buffer = {
- y = 1,
- width = 74,
- height = 300,
- lines = {},
- }
- console.buffer.get = function(self, x, y, width, height)
- output = ""
- for i = y+(self.y-height),y+(self.y-1) do
- if i < 1 then
- output = output .. self.lines[self.height + i] .. "\n"
- else
- if i > 300 then
- output = output .. self.lines[i-300] .. "\n"
- else
- output = output .. self.lines[i] .. "\n"
- end
- end
- end
- return output
- end
- console.buffer.write = function(text)
- self = console.buffer
- lastpos = 1
- while true do
- pos = string.find(text, "\n", lastpos)
- if pos == nil then
- text = string.sub(text, lastpos)
- return output
- end
- self.lines[self.y] = self.lines[self.y] .. string.sub(text, lastpos, pos-1)
- lastpos = pos + 1
- self.y = self.y + 1
- self.lines[self.y] = ""
- end
- self.lines[self.y] = self.lines[self.y] .. text
- end
- console.write = console.buffer.write
- console.buffer.writeln = function(text)
- if text == nil then
- return
- end
- console.buffer.write(text .. "\n")
- end
- console.writeln = console.buffer.writeln
- console.buffer.init = function(self)
- for i=1,self.height do
- table.insert(self.lines, "")
- end
- end
- console.buffer:init()
- ------------------------------------------------
- -- console.screen ------------------------------
- ------------------------------------------------
- console.screen = {
- x = 0,
- y = 0,
- height = 30,
- width = 74,
- }
- console.screen.test = function(self)
- for y = 1,self.height do
- str = ""
- for x = 1,self.width do
- str = str .. "M" --(x % 10)
- end
- print(str)
- end
- end
- console.screen.up = function(self)
- self.y = self.y + 1
- if self.y > console.buffer.height - 1 then
- self.y = 0
- end
- end
- console.screen.down = function(self)
- self.y = self.y - 1
- if self.y < 0 then
- self.y = console.buffer.height - 1
- end
- end
- console.screen.draw = function(self)
- tpt.fillrect(5, 5, 600, 570, 0, 0, 0, 178)
- tpt.drawrect(5, 5, 600, 570, 255, 255, 255)
- tpt.drawline(5, 372, 605, 372, 255, 255, 255)
- if console.currentCommand == nil then
- tpt.drawtext(12, 374, "> <nil>", 255, 0, 0)
- else
- tpt.drawtext(12, 374, "> " .. console.currentCommand, console.colors.textr, console.colors.textg, console.colors.textb, console.colors.texta)
- end
- tpt.drawtext(
- 12,
- 12,
- console.buffer:get(self.x, self.y, self.width, self.height),
- console.colors.textr,
- console.colors.textg,
- console.colors.textb,
- console.colors.texta
- )
- end
- ------------------------------------------------
- -- console.keys --------------------------------
- ------------------------------------------------
- console.keys = {
- binds = {},
- numbers = {},
- symbols = {},
- lastkey = 0,
- down = 0,
- downstart = 0,
- repeatDelay = 0.5,
- repeatTimer = 0.05
- }
- console.keys.clear = function(self)
- self.binds = {}
- self.numbers = {}
- self.symbols = {}
- end
- console.keys.default = function(self)
- self:clear()
- -- Shifted Numbers Map
- self.numbers = {")", "!", "@", "#", "$", "%", "^", "&", "*", "("}
- -- Shifted Symbols Map
- self.symbols["-"] = "_"
- self.symbols["="] = "+"
- self.symbols["["] = "{"
- self.symbols["]"] = "}"
- self.symbols[";"] = ":"
- self.symbols["'"] = "\""
- self.symbols[","] = "<"
- self.symbols["."] = ">"
- self.symbols["/"] = "?"
- self.symbols["\\"] = "|"
- -- Set binds
- self.binds[27] = function() console.set(0) end -- Esc
- self.binds[96] = function() console.set(0) end -- ` - Console Key
- self.binds[280] = function() console.screen:up() end -- Page Up
- self.binds[281] = function() console.screen:down() end -- Page Down
- self.binds[273] = function() console.history:next() end -- Up
- self.binds[274] = function() console.history:prev() end -- Down
- end
- -- default keys should work for US style keyboards
- console.keys.US = console.keys.default
- -- Repeat the key if held down for longer than 0.5 seconds
- console.keys.heldkey = function(self)
- if self.down == 1 then
- if os.clock() - self.downstart > self.repeatDelay then
- self.down = 2
- end
- end
- if self.down == 2 and (os.clock() - self.downstart) > self.repeatTimer then
- downstart = os.clock()
- console.keys:process(self.lastks, self.lastkey, self.lastmod, 3)
- -- HACK: down is set within console.keys.process, set it back to 2
- self.down = 2
- end
- end
- console.keys.process = function(self, keyString, key, modifier, event)
- self.lastkey = key
- self.lastks = keyString
- self.lastmod = modifier
- self.down = 2 - event
- self.downstart = os.clock()
- -- Handle keys closing old console
- if console.active == 2 and (key == 96 or key == 27) then
- console.active = 0
- end
- -- Override keys
- if console.active == 0 and event == 1 then
- -- ` - Console Key
- if key == 96 then
- if console.active == 0 and (modifier == 1 or modifier == 2) then
- console.old.open()
- return false
- end
- console.toggle()
- return false
- end
- -- d - Debug Mode
- if key == 100 and event == 1 then
- console.debugmode = 1 - console.debugmode
- end
- -- h - Hud Toggle
- if key == 104 and event == 1 then
- tpt.hud(1 - console.old.hud_value)
- return false
- end
- end
- if console.active == 1 then
- if event == 2 then
- return false
- end
- -- Handle backspace
- if key == 8 then
- console.currentCommand = string.sub(console.currentCommand, 1, string.len(console.currentCommand)-1)
- return false
- end
- -- Process console.currentCommand
- if key == 13 then
- console.history:store(console.currentCommand)
- local temp = console.currentCommand
- console.currentCommand = ""
- runcommand(temp)
- return false
- end
- -- Execute bind for key
- bind = self.binds[key]
- if bind ~= nil then
- ecall(bind, keyString, key, modifier, event)
- return false
- end
- -- Ignore any special keys
- if key >= 256 then
- return false
- end
- -- Mappings for shifted keys
- if modifier == 1 or modifier == 2 then
- if key >= 48 and key <= 57 then
- keyString = self.numbers[key - 47]
- elseif key >= 97 and key <= 122 then
- keyString = string.char(key - 97 + 65)
- else
- local temp = self.symbols[keyString]
- if temp ~= nil then
- keyString = temp
- end
- end
- end
- console.currentCommand = console.currentCommand .. keyString
- return false
- end
- end
- ------------------------------------------------
- -- console.history -----------------------------
- ------------------------------------------------
- console.history = {
- contents = {},
- maxsize = 30,
- size = 0,
- }
- console.history.store = function(self, value)
- self.size = self.size + 1
- table.insert(self.contents, 1, value)
- self.selected = 0
- end
- console.history.next = function(self)
- if self.size == 0 then
- return
- end
- self.selected = self.selected + 1
- if self.selected > self.size then
- self.selected = 1
- end
- console.currentCommand = self.contents[self.selected]
- end
- console.history.prev = function(self)
- if self.size == 0 then
- return
- end
- self.selected = self.selected - 1
- if self.selected <= 0 then
- self.selected = self.size
- end
- console.currentCommand = self.contents[self.selected]
- end
- console.history.select = function(self, n)
- if self.size == 0 then
- return
- end
- n = n%self.size
- print(n)
- self.selected = (self.selected + n)
- print(self.selected)
- console.currentCommand = self.contents[self.selected]
- end
- ------------------------------------------------
- -- console.quick -------------------------------
- ------------------------------------------------
- local function console_quick_help()
- print("!e [text] - Echo text")
- print("!r [file] - Run a file")
- print("!help - Display this")
- end
- console.quick[""] = console_quick_help
- console.quick["help"] = console_quick_help
- console.quick["e"] = function(arguments)
- console.buffer.write(arguments)
- end
- -- Function for running a file named filename or filename.lua
- console.quick["r"] = function(filename)
- if string.sub(filename, -4) ~= ".lua" then
- f, msg = io.open(filename, "r")
- if not f then
- f, msg = io.open(filename .. ".lua", "r")
- if f then
- f:close()
- return dofile(filename .. ".lua")
- end
- else
- f:close()
- end
- end
- return dofile(filename)
- end
- ------------------------------------------------
- -- Initialize Console --------------------------
- ------------------------------------------------
- console.keys:default()
- -- Hook us up to TPT
- local function keyHandler(keyString, key, modifier, event)
- return console.keys:process(keyString, key, modifier, event)
- end
- motd = 1
- motdstart = 0
- local function mouseHandler(mousex, mousey, button, event)
- if motd == 1 then
- motdstart = os.clock()
- motd = 0
- end
- if console.active == 1 then
- return false
- end
- return true
- end
- local function draw()
- if console.active == 1 then
- ecall(console.keys.heldkey, console.keys)
- console.screen:draw()
- end
- if motd == 1 then
- alpha = 1
- else
- alpha = 1 - (os.clock() - motdstart)
- end
- if alpha ~= 0 then
- tpt.drawtext(17, 335, "The all lua-console V4 by devast8a is installed!", 178, 32, 255, 255 * alpha)
- end
- if console.debugmode == 1 then
- x = 16
- y = 27
- if console.old.hud_value == 1 then
- tpt.drawtext(16, 27, "lua-console (V4) (" .. console.active .. ", " .. console.keys.lastkey .. ")", 178, 32, 255)
- end
- if console.active == 1 then
- tpt.drawtext(575, 8, "[" .. console.keys.lastkey .. "]", 178, 32, 255)
- end
- end
- if console.active == 2 then
- tpt.drawtext(5, 228, "Old console [Press escape to close] -- lua-console", 178, 32, 255)
- end
- end
- tpt.register_step(draw)
- tpt.register_keypress(keyHandler)
- tpt.register_mouseclick(mouseHandler)
- console.old.log("lua-console loaded successfully")
- tpt.log("The all lua-console V4")
- tpt.log(" by devast8a")
- tpt.log("")
- tpt.log("Page Up/Down - move console screen up/down")
- tpt.log("Up/Down - cycle through command history")
- tpt.log("")
- tpt.log("Commands starting with ! don't work, instead close this console and press shift + console key to use the old console.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement