Advertisement
Guest User

The ALL lua-console for powder-toy

a guest
Mar 26th, 2012
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.01 KB | None | 0 0
  1. --[[  This program is free software. It comes without any warranty, to
  2.  the extent permitted by applicable law. You can redistribute it
  3.  and/or modify it under the terms of the Do What The Fuck You Want
  4.  To Public License, Version 2, as published by Sam Hocevar. See
  5.  http://sam.zoy.org/wtfpl/COPYING for more details. --]]
  6.  
  7. -- The ALL lua-console v4
  8. --   by devast8a
  9.  
  10. console = {
  11.     -- Current state of console
  12.     -- 0 = Closed
  13.     -- 1 = Opened
  14.     -- 2 = Old console opened
  15.     -- [READONLY]
  16.     active = 0,
  17.  
  18.     -- Debug state of console, 1 if active, 0 if not
  19.     -- In no way effected by tpt.setdebug
  20.     -- [READONLY]
  21.     debugmode = 0,
  22.  
  23.     -- Current command line text for console
  24.     currentCommand = "",
  25.  
  26.     -- Colors for console text
  27.     colors = {
  28.         textr = 200,
  29.         textg = 32,
  30.         textb = 255,
  31.         texta = 255,
  32.     },
  33.  
  34.     buffer = {},    -- Handle writing output to a larger display than screen can show
  35.     history = {},   -- command history
  36.     keys = {},      -- handle key input
  37.     old = {},       -- support for old console
  38.     quick = {},     -- Quick commands (commands that being with !)
  39.     screen = {},    -- Handle drawing to the screen
  40. }
  41.  
  42. ------------------------------------------------
  43. -- GLOBAL API ----------------------------------
  44. ------------------------------------------------
  45. function print(message)
  46.     console.writeln(message)
  47.     --console.screen.buffer = console.screen.buffer .. "\n" .. message
  48. end
  49.  
  50. function error(message)
  51.     print("[ERROR] " .. message)
  52. end
  53.  
  54. -- Call cmd & automatically call error (w/ error message) upon error
  55. function ecall(cmd, ...)
  56.     _, result = pcall(cmd, unpack(arg))
  57.  
  58.     if _ == false then
  59.         error(result)
  60.     end
  61.  
  62.     return _, result
  63. end
  64.  
  65. -- Execute a commnad
  66. function runcommand(command)
  67.     -- Handle quick commands
  68.     if string.sub(command, 1, 1) == "!" then
  69.         local l = string.find(command, " ")
  70.  
  71.         if l == nil then
  72.             command = string.sub(command, 2)
  73.             args = ""
  74.         else
  75.             args = string.trim(string.sub(command, l))
  76.             command = string.sub(command, 2, l - 1)
  77.         end
  78.        
  79.         cmd = console.quick[command]
  80.         message = "No command by the name of '" .. command .. "' exists"
  81.        
  82.     -- Short hand for return
  83.     elseif string.sub(command, 1, 1) == "@" then
  84.         command = "return " .. string.sub(command, 2)
  85.         cmd, message = loadstring(command)
  86.    
  87.     -- Compile the string to a function
  88.     else
  89.         cmd, message = loadstring(command)
  90.     end
  91.    
  92.     -- Handle errors
  93.     if cmd == nil then
  94.         print(message)
  95.     else
  96.         if args == nil then
  97.             _, result = pcall(cmd)
  98.         else
  99.             _, result = pcall(cmd, args)
  100.         end
  101.  
  102.         print(result)
  103.     end
  104. end
  105.  
  106. ------------------------------------------------
  107. -- string --------------------------------------
  108. ------------------------------------------------
  109. string.trim = function (str)
  110.     return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
  111. end
  112.  
  113. string.split = function (str, split)
  114.     local output = {}
  115.    
  116.     lastpos = 1
  117.     while true do
  118.         pos = string.find(str, split, lastpos)
  119.         if pos == nil then
  120.             table.insert(output, string.sub(str, lastpos))
  121.             return output
  122.         end
  123.  
  124.         table.insert(output, string.sub(str, lastpos, pos-1))
  125.         lastpos = pos + 1
  126.     end
  127.  
  128.     return { str }
  129. end
  130.  
  131. ------------------------------------------------
  132. -- console -------------------------------------
  133. ------------------------------------------------
  134. console.set = function(value)
  135.     if console.active == 2 then
  136.         if value == 0 then
  137.             console.active = 0
  138.         else
  139.             console.active = 2
  140.         end
  141.  
  142.         return console.old.set_console(value)
  143.     end
  144.  
  145.     if value == 0 then
  146.         console.active = 0
  147.         tpt.hud(1)
  148.     else
  149.         console.old.set_pause(1)
  150.         tpt.hud(0)
  151.         console.active = 1
  152.     end
  153. end
  154.  
  155. console.toggle = function()
  156.     console.set(1-console.active)
  157. end
  158.  
  159. ------------------------------------------------
  160. -- console.old ---------------------------------
  161. ------------------------------------------------
  162. console.old = {
  163.     active = 0,
  164.     hud_value = 1,
  165. }
  166.  
  167. console.old.open = function()
  168.     -- Old console quirks
  169.     tpt.hud(1)
  170.  
  171.     -- Open the old console
  172.     console.active = 2
  173.     console.old.set_console(1)
  174. end
  175.  
  176. ------------------------------------------------
  177. -- console.old.interop -------------------------
  178. ------------------------------------------------
  179. console.old.interop = {}
  180.  
  181. -- tpt.set_pause
  182. console.old.interop.set_pause = function(value)
  183.     if console.active == 0 then
  184.         console.old.set_pause(value)
  185.     end
  186. end
  187. console.old.set_pause = tpt.set_pause
  188. tpt.set_pause = console.old.interop.set_pause
  189.  
  190. -- tpt.log
  191. console.old.interop.log = function(message)
  192.     if console.active == 2 then
  193.         return console.old.log(message)
  194.     end
  195.  
  196.     return print(message)
  197. end
  198. console.old.log = tpt.log
  199. tpt.log = console.old.interop.log
  200.  
  201. -- tpt.hud
  202. console.old.interop.hud = function(value)
  203.     if value == 0 then
  204.         console.old.hud_value = 0
  205.     else
  206.         console.old.hud_value = 1
  207.     end
  208.    
  209.     console.old.hud(value)    
  210. end
  211. console.old.hud = tpt.hud
  212. tpt.hud = console.old.interop.hud
  213.  
  214. -- tpt.set_console
  215. console.old.set_console = tpt.set_console
  216. tpt.set_console = console.set
  217.  
  218. ------------------------------------------------
  219. -- console.buffer ------------------------------
  220. ------------------------------------------------
  221. console.buffer = {
  222.     y = 1,
  223.     width = 74,
  224.     height = 300,
  225.     lines = {},
  226. }
  227.  
  228. console.buffer.get = function(self, x, y, width, height)
  229.     output = ""
  230.     for i = y+(self.y-height),y+(self.y-1) do
  231.         if i < 1 then
  232.             output = output .. self.lines[self.height + i] .. "\n"
  233.         else
  234.             if i > 300 then
  235.                 output = output .. self.lines[i-300] .. "\n"
  236.             else
  237.                 output = output .. self.lines[i] .. "\n"
  238.             end
  239.         end
  240.     end
  241.     return output
  242. end
  243.  
  244. console.buffer.write = function(text)
  245.     self = console.buffer
  246.    
  247.     lastpos = 1
  248.     while true do
  249.         pos = string.find(text, "\n", lastpos)
  250.  
  251.         if pos == nil then
  252.             text = string.sub(text, lastpos)
  253.             return output
  254.         end
  255.  
  256.         self.lines[self.y] = self.lines[self.y] .. string.sub(text, lastpos, pos-1)
  257.         lastpos = pos + 1
  258.         self.y = self.y + 1
  259.         self.lines[self.y] = ""
  260.     end
  261.  
  262.     self.lines[self.y] = self.lines[self.y] .. text
  263. end
  264. console.write = console.buffer.write
  265.  
  266. console.buffer.writeln = function(text)
  267.     if text == nil then
  268.         return
  269.     end
  270.  
  271.     console.buffer.write(text .. "\n")
  272. end
  273. console.writeln = console.buffer.writeln
  274.  
  275. console.buffer.init = function(self)
  276.     for i=1,self.height do
  277.         table.insert(self.lines, "")
  278.     end
  279. end
  280. console.buffer:init()
  281.  
  282. ------------------------------------------------
  283. -- console.screen ------------------------------
  284. ------------------------------------------------
  285. console.screen = {
  286.     x = 0,
  287.     y = 0,
  288.     height = 30,
  289.     width  = 74,
  290. }
  291.  
  292. console.screen.test = function(self)
  293.     for y = 1,self.height do
  294.         str = ""
  295.  
  296.         for x = 1,self.width do
  297.             str = str .. "M" --(x % 10)
  298.         end
  299.        
  300.         print(str)
  301.     end
  302. end
  303.  
  304. console.screen.up = function(self)
  305.     self.y = self.y + 1
  306.     if self.y > console.buffer.height - 1 then
  307.         self.y = 0
  308.     end
  309. end
  310.  
  311. console.screen.down = function(self)
  312.     self.y = self.y - 1
  313.     if self.y < 0 then
  314.         self.y = console.buffer.height - 1
  315.     end
  316. end
  317.  
  318. console.screen.draw = function(self)
  319.     tpt.fillrect(5, 5, 600, 570, 0, 0, 0, 178)
  320.     tpt.drawrect(5, 5, 600, 570, 255, 255, 255)
  321.     tpt.drawline(5, 372, 605, 372, 255, 255, 255)
  322.    
  323.     if console.currentCommand == nil then
  324.         tpt.drawtext(12, 374, "> <nil>", 255, 0, 0)
  325.     else
  326.         tpt.drawtext(12, 374, "> " .. console.currentCommand, console.colors.textr, console.colors.textg, console.colors.textb, console.colors.texta)
  327.     end
  328.  
  329.     tpt.drawtext(
  330.         12,
  331.         12,
  332.         console.buffer:get(self.x, self.y, self.width, self.height),
  333.         console.colors.textr,
  334.         console.colors.textg,
  335.         console.colors.textb,
  336.         console.colors.texta
  337.     )
  338. end
  339.  
  340. ------------------------------------------------
  341. -- console.keys --------------------------------
  342. ------------------------------------------------
  343. console.keys = {
  344.     binds = {},
  345.     numbers = {},
  346.     symbols = {},
  347.     lastkey = 0,
  348.     down = 0,
  349.     downstart = 0,
  350.     repeatDelay = 0.5,
  351.     repeatTimer = 0.05
  352. }
  353.  
  354. console.keys.clear = function(self)
  355.     self.binds = {}
  356.     self.numbers = {}
  357.     self.symbols = {}
  358. end
  359.  
  360. console.keys.default = function(self)
  361.     self:clear()
  362.     -- Shifted Numbers Map
  363.     self.numbers = {")", "!", "@", "#", "$", "%", "^", "&", "*", "("}
  364.     -- Shifted Symbols Map
  365.     self.symbols["-"] = "_"
  366.     self.symbols["="] = "+"
  367.     self.symbols["["] = "{"
  368.     self.symbols["]"] = "}"
  369.     self.symbols[";"] = ":"
  370.     self.symbols["'"] = "\""
  371.     self.symbols[","] = "<"
  372.     self.symbols["."] = ">"
  373.     self.symbols["/"] = "?"
  374.     self.symbols["\\"] = "|"
  375.     -- Set binds
  376.     self.binds[27] = function() console.set(0) end -- Esc
  377.     self.binds[96] = function() console.set(0) end -- ` - Console Key
  378.     self.binds[280] = function() console.screen:up() end -- Page Up
  379.     self.binds[281] = function() console.screen:down() end -- Page Down
  380.     self.binds[273] = function() console.history:next() end -- Up
  381.     self.binds[274] = function() console.history:prev() end -- Down
  382. end
  383.  
  384. -- default keys should work for US style keyboards
  385. console.keys.US = console.keys.default
  386.  
  387. -- Repeat the key if held down for longer than 0.5 seconds
  388. console.keys.heldkey = function(self)
  389.     if self.down == 1 then
  390.         if os.clock() - self.downstart > self.repeatDelay then
  391.             self.down = 2
  392.         end
  393.     end
  394.     if self.down == 2 and (os.clock() - self.downstart) > self.repeatTimer then
  395.         downstart = os.clock()
  396.         console.keys:process(self.lastks, self.lastkey, self.lastmod, 3)
  397.  
  398.         -- HACK: down is set within console.keys.process, set it back to 2
  399.         self.down = 2
  400.     end
  401. end
  402.  
  403. console.keys.process = function(self, keyString, key, modifier, event)
  404.     self.lastkey = key
  405.     self.lastks = keyString
  406.     self.lastmod = modifier
  407.    
  408.     self.down = 2 - event
  409.     self.downstart = os.clock()
  410.    
  411.     -- Handle keys closing old console
  412.     if console.active == 2 and (key == 96 or key == 27) then
  413.         console.active = 0
  414.     end
  415.  
  416.     -- Override keys
  417.     if console.active == 0 and event == 1 then
  418.         -- ` - Console Key
  419.         if key == 96 then
  420.             if console.active == 0 and (modifier == 1 or modifier == 2) then
  421.                 console.old.open()
  422.                 return false
  423.             end
  424.  
  425.             console.toggle()        
  426.             return false
  427.         end
  428.  
  429.         -- d - Debug Mode
  430.         if key == 100 and event == 1 then
  431.             console.debugmode = 1 - console.debugmode
  432.         end
  433.  
  434.         -- h - Hud Toggle
  435.         if key == 104 and event == 1 then
  436.             tpt.hud(1 - console.old.hud_value)
  437.             return false
  438.         end
  439.     end
  440.    
  441.     if console.active == 1 then
  442.         if event == 2 then
  443.             return false
  444.         end
  445.        
  446.         -- Handle backspace
  447.         if key == 8 then
  448.             console.currentCommand = string.sub(console.currentCommand, 1, string.len(console.currentCommand)-1)
  449.             return false
  450.         end
  451.        
  452.         -- Process console.currentCommand
  453.         if key == 13 then
  454.             console.history:store(console.currentCommand)
  455.             local temp = console.currentCommand
  456.             console.currentCommand = ""
  457.             runcommand(temp)
  458.             return false
  459.         end
  460.  
  461.         -- Execute bind for key
  462.         bind = self.binds[key]
  463.         if bind ~= nil then
  464.             ecall(bind, keyString, key, modifier, event)
  465.             return false
  466.         end
  467.        
  468.         -- Ignore any special keys
  469.         if key >= 256 then
  470.             return false
  471.         end
  472.  
  473.         -- Mappings for shifted keys
  474.         if modifier == 1 or modifier == 2 then
  475.             if key >= 48 and key <= 57 then
  476.                 keyString = self.numbers[key - 47]
  477.             elseif key >= 97 and key <= 122 then
  478.                 keyString = string.char(key - 97 + 65)
  479.             else
  480.                 local temp = self.symbols[keyString]
  481.                
  482.                 if temp ~= nil then
  483.                     keyString = temp
  484.                 end
  485.             end
  486.         end
  487.        
  488.         console.currentCommand = console.currentCommand .. keyString
  489.         return false
  490.     end
  491. end
  492.  
  493. ------------------------------------------------
  494. -- console.history -----------------------------
  495. ------------------------------------------------
  496. console.history = {
  497.     contents = {},
  498.     maxsize = 30,
  499.     size = 0,
  500. }
  501.  
  502. console.history.store = function(self, value)
  503.     self.size = self.size + 1
  504.     table.insert(self.contents, 1, value)
  505.     self.selected = 0
  506. end
  507.  
  508. console.history.next = function(self)
  509.     if self.size == 0 then
  510.         return
  511.     end
  512.     self.selected = self.selected + 1
  513.  
  514.     if self.selected > self.size then
  515.         self.selected = 1
  516.     end
  517.  
  518.     console.currentCommand = self.contents[self.selected]
  519. end
  520.  
  521. console.history.prev = function(self)
  522.     if self.size == 0 then
  523.         return
  524.     end
  525.     self.selected = self.selected - 1
  526.  
  527.     if self.selected <= 0 then
  528.         self.selected = self.size
  529.     end
  530.  
  531.     console.currentCommand = self.contents[self.selected]
  532. end
  533.  
  534. console.history.select = function(self, n)
  535.     if self.size == 0 then
  536.         return
  537.     end
  538.  
  539.     n = n%self.size
  540.     print(n)
  541.     self.selected = (self.selected + n)
  542.  
  543.     print(self.selected)
  544.     console.currentCommand = self.contents[self.selected]
  545. end
  546.  
  547. ------------------------------------------------
  548. -- console.quick -------------------------------
  549. ------------------------------------------------
  550.  
  551. local function console_quick_help()
  552.     print("!e [text]  - Echo text")
  553.     print("!r [file]  - Run a file")
  554.     print("!help      - Display this")
  555. end
  556.  
  557. console.quick[""] = console_quick_help
  558. console.quick["help"] = console_quick_help
  559.  
  560. console.quick["e"] = function(arguments)
  561.     console.buffer.write(arguments)
  562. end
  563.  
  564. -- Function for running a file named filename or filename.lua
  565. console.quick["r"] = function(filename)
  566.     if string.sub(filename, -4) ~= ".lua" then
  567.         f, msg = io.open(filename, "r")
  568.  
  569.         if not f then
  570.             f, msg = io.open(filename .. ".lua", "r")
  571.             if f then
  572.                 f:close()
  573.                 return dofile(filename .. ".lua")
  574.             end
  575.         else
  576.             f:close()
  577.         end
  578.     end
  579.     return dofile(filename)
  580. end
  581.  
  582. ------------------------------------------------
  583. -- Initialize Console --------------------------
  584. ------------------------------------------------
  585. console.keys:default()
  586.  
  587. -- Hook us up to TPT
  588. local function keyHandler(keyString, key, modifier, event)
  589.     return console.keys:process(keyString, key, modifier, event)
  590. end
  591.  
  592. motd = 1
  593. motdstart = 0
  594.  
  595. local function mouseHandler(mousex, mousey, button, event)
  596.     if motd == 1 then
  597.         motdstart = os.clock()
  598.         motd = 0
  599.     end
  600.  
  601.     if console.active == 1 then
  602.         return false
  603.     end
  604.    
  605.     return true
  606. end
  607.  
  608. local function draw()
  609.     if console.active == 1 then
  610.         ecall(console.keys.heldkey, console.keys)
  611.         console.screen:draw()
  612.     end
  613.  
  614.     if motd == 1 then
  615.         alpha = 1
  616.     else
  617.         alpha = 1 - (os.clock() - motdstart)
  618.     end
  619.    
  620.     if alpha ~= 0 then
  621.         tpt.drawtext(17, 335, "The all lua-console V4 by devast8a is installed!", 178, 32, 255, 255 * alpha)
  622.     end
  623.        
  624.     if console.debugmode == 1 then
  625.         x = 16
  626.         y = 27
  627.  
  628.         if console.old.hud_value == 1 then
  629.             tpt.drawtext(16, 27, "lua-console (V4) (" .. console.active .. ", " .. console.keys.lastkey .. ")", 178, 32, 255)
  630.         end
  631.         if console.active == 1 then
  632.             tpt.drawtext(575, 8, "[" .. console.keys.lastkey .. "]", 178, 32, 255)
  633.         end
  634.     end
  635.  
  636.     if console.active == 2 then
  637.         tpt.drawtext(5, 228, "Old console [Press escape to close] -- lua-console", 178, 32, 255)
  638.     end
  639. end
  640.  
  641. tpt.register_step(draw)
  642. tpt.register_keypress(keyHandler)
  643. tpt.register_mouseclick(mouseHandler)
  644.  
  645. console.old.log("lua-console loaded successfully")
  646.  
  647. tpt.log("The all lua-console V4")
  648. tpt.log("  by devast8a")
  649. tpt.log("")
  650. tpt.log("Page Up/Down - move console screen up/down")
  651. tpt.log("Up/Down - cycle through command history")
  652. tpt.log("")
  653. 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