Guest User

ALL lua console for Powder Toy

a guest
Mar 29th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.57 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 v6
  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.     buffer = {},    -- Handle writing output to a larger display than screen can show
  27.     colors = {},    -- store console colors & resolve text colors to RGB
  28.     command = {},   -- current command
  29.     history = {},   -- command history
  30.     keys = {},      -- handle key input
  31.     old = {},       -- support for old console
  32.     quick = {},     -- Quick commands (commands that being with !)
  33.     screen = {},    -- handle the drawing of the console to the screen
  34. }
  35.  
  36. ------------------------------------------------
  37. -- GLOBAL API ----------------------------------
  38. ------------------------------------------------
  39. function print(message)
  40.     console.writeln(message)
  41.     --console.screen.buffer = console.screen.buffer .. "\n" .. message
  42. end
  43.  
  44. function error(message)
  45.     console.writeln(message, "error", "errorbg")
  46. end
  47.  
  48. -- Call cmd & automatically call error (w/ error message) upon error
  49. function ecall(cmd, ...)
  50.     _, result = pcall(cmd, unpack(arg))
  51.  
  52.     if _ == false then
  53.         error(result)
  54.     end
  55.  
  56.     return _, result
  57. end
  58.  
  59. -- Execute a commnad
  60. function runcommand(command)
  61.     -- Handle quick commands
  62.     if string.sub(command, 1, 1) == "!" then
  63.         local l = string.find(command, " ")
  64.  
  65.         if l == nil then
  66.             command = string.sub(command, 2)
  67.             args = ""
  68.         else
  69.             args = string.trim(string.sub(command, l))
  70.             command = string.sub(command, 2, l - 1)
  71.         end
  72.        
  73.         cmd = console.quick[command]
  74.         message = "No command by the name of '" .. command .. "' exists"
  75.        
  76.     -- Short hand for return
  77.     elseif string.sub(command, 1, 1) == "@" then
  78.         command = "return " .. string.sub(command, 2)
  79.         cmd, message = loadstring(command)
  80.    
  81.     -- Compile the string to a function
  82.     else
  83.         cmd, message = loadstring(command)
  84.     end
  85.    
  86.     -- Handle errors
  87.     if cmd == nil then
  88.         error(message)
  89.     else
  90.         if args == nil then
  91.             _, result = pcall(cmd)
  92.         else
  93.             _, result = pcall(cmd, args)
  94.         end
  95.  
  96.         if _ == true then
  97.             print(result)
  98.         else
  99.             error(result)
  100.         end
  101.     end
  102. end
  103.  
  104. ------------------------------------------------
  105. -- string --------------------------------------
  106. ------------------------------------------------
  107. string.trim = function (str)
  108.     return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
  109. end
  110.  
  111. string.split = function (str, split)
  112.     local output = {}
  113.    
  114.     lastpos = 1
  115.     while true do
  116.         pos = string.find(str, split, lastpos)
  117.         if pos == nil then
  118.             table.insert(output, string.sub(str, lastpos))
  119.             return output
  120.         end
  121.  
  122.         table.insert(output, string.sub(str, lastpos, pos-1))
  123.         lastpos = pos + 1
  124.     end
  125.  
  126.     return { str }
  127. end
  128.  
  129. ------------------------------------------------
  130. -- console -------------------------------------
  131. ------------------------------------------------
  132. console.set = function(value)
  133.     if console.active == 2 then
  134.         if value == 0 then
  135.             console.active = 0
  136.         else
  137.             console.active = 2
  138.         end
  139.  
  140.         return console.old.set_console(value)
  141.     end
  142.  
  143.     if value == 0 then
  144.         console.active = 0
  145.         tpt.hud(1)
  146.     else
  147.         console.old.set_pause(1)
  148.         tpt.hud(0)
  149.         console.active = 1
  150.     end
  151. end
  152.  
  153. console.toggle = function()
  154.     console.set(1-console.active)
  155. end
  156.  
  157. ------------------------------------------------
  158. -- console.old ---------------------------------
  159. ------------------------------------------------
  160. console.old = {
  161.     active = 0,
  162.     hud_value = 1,
  163. }
  164.  
  165. console.old.open = function()
  166.     -- Old console quirks
  167.     tpt.hud(1)
  168.  
  169.     -- Open the old console
  170.     console.active = 2
  171.     console.old.set_console(1)
  172. end
  173.  
  174. ------------------------------------------------
  175. -- console.old.interop -------------------------
  176. ------------------------------------------------
  177. console.old.interop = {}
  178.  
  179. -- tpt.set_pause
  180. console.old.interop.set_pause = function(value)
  181.     if console.active == 0 then
  182.         console.old.set_pause(value)
  183.     end
  184. end
  185. console.old.set_pause = tpt.set_pause
  186. tpt.set_pause = console.old.interop.set_pause
  187.  
  188. -- tpt.log
  189. console.old.interop.log = function(message)
  190.     if console.active == 2 then
  191.         return console.old.log(message)
  192.     end
  193.  
  194.     return print(message)
  195. end
  196. console.old.log = tpt.log
  197. tpt.log = console.old.interop.log
  198.  
  199. -- tpt.hud
  200. console.old.interop.hud = function(value)
  201.     if value == 0 then
  202.         console.old.hud_value = 0
  203.     else
  204.         console.old.hud_value = 1
  205.     end
  206.    
  207.     console.old.hud(value)    
  208. end
  209. console.old.hud = tpt.hud
  210. tpt.hud = console.old.interop.hud
  211.  
  212. -- tpt.set_console
  213. console.old.set_console = tpt.set_console
  214. tpt.set_console = console.set
  215.  
  216. ------------------------------------------------
  217. -- console.command -----------------------------
  218. ------------------------------------------------
  219. console.command = {
  220.     current = {},
  221.     cursor = 0,
  222. }
  223.  
  224. console.command.writechar = function(self, char, foreground, background)
  225.     self.cursor = self.cursor + 1
  226.     table.insert(self.current, self.cursor, {char, foreground, background})
  227. end
  228.  
  229. console.command.clear = function(self)
  230.     self.cursor = 0
  231.     self.current = {}
  232. end
  233.  
  234. console.command.set = function(self, value)
  235.     self.cursor = #value
  236.     self.current = value
  237. end
  238.  
  239. console.command.left = function(self)
  240.     if self.cursor > 0 then
  241.         self.cursor = self.cursor - 1
  242.     end
  243. end
  244.  
  245. console.command.right = function(self)
  246.     if self.cursor < #self.current then
  247.         self.cursor = self.cursor + 1
  248.     end
  249. end
  250.  
  251. console.command.deleteback = function(self)
  252.     if self.cursor > 0 then
  253.         table.remove(self.current, self.cursor)
  254.         self.cursor = self.cursor - 1
  255.     end
  256. end
  257.  
  258. console.command.deleteforward = function(self)
  259.     if self.cursor < #self.current then
  260.         table.remove(self.current, self.cursor + 1)
  261.     end
  262. end
  263.  
  264. console.command.get = function(self)
  265.     local t = { }
  266.    
  267.     for i = 1,#self.current do
  268.         t[i] = self.current[i][1]
  269.     end
  270.  
  271.     return table.concat(t)
  272. end
  273.  
  274. console.command.run = function(self)
  275.     console.history:store(self.current)
  276.     local temp = console.command:get()
  277.     self:clear()
  278.     runcommand(temp)
  279. end
  280.  
  281. ------------------------------------------------
  282. -- console.buffer ------------------------------
  283. ------------------------------------------------
  284. console.buffer = {
  285.     x = 1,
  286.     y = 1,
  287.     newline = 0,
  288.     width = 85,
  289.     height = 500,
  290.     lines = {},
  291.  
  292.     more = {
  293.         enabled = false,
  294.     }
  295. }
  296.  
  297. console.buffer.iter = function(self, selection, func)
  298.     y = 0
  299.     for i = selection.y+(1+self.y-selection.height), selection.y+self.y do
  300.         if i < 1 then
  301.             line = self.lines[self.height + i]
  302.         else
  303.             if i > self.height then
  304.                 line = self.lines[i-self.height]
  305.             else
  306.                 line = self.lines[i]
  307.             end
  308.         end
  309.  
  310.         if line ~= nil then
  311.             x = 0
  312.             for ch = selection.x + 1, selection.x+selection.width do
  313.                 char = line[ch]
  314.                
  315.                 if char == 0 then
  316.                     break
  317.                 end
  318.  
  319.                 if char ~= nil then
  320.                     func(x, y, char)
  321.                 end
  322.  
  323.                 x = x + 1
  324.             end
  325.         end
  326.  
  327.         y = y + 1
  328.     end
  329. end
  330.  
  331. console.buffer._write = function(char)
  332.     self = console.buffer
  333.  
  334.     if self.newline == true then
  335.         self.y = self.y + 1
  336.         self.newline = false
  337.     end
  338.  
  339.     if char == "\n" then
  340.         self.newline = true
  341.         self.x = 1
  342.     elseif char == "\r" then
  343.         self.x = 1
  344.     else
  345.         line = self.lines[self.y]
  346.        
  347.         if line == nil then line = {} self.lines[self.y] = line end
  348.    
  349.         line[self.x] = {char, self.cf, self.cb}
  350.        
  351.         self.x = self.x + 1    
  352.         if self.x > self.width then
  353.             self.y = self.y + 1
  354.             self.x = 1
  355.             self.lines[self.y] = {}
  356.         end
  357.     end
  358.  
  359.     if self.y > self.height then
  360.         self.y = 1
  361.     end
  362. end
  363.  
  364. console.buffer.write = function(text, colorF, colorB)
  365.     if text == nil then
  366.         return
  367.     end
  368.  
  369.     self = console.buffer
  370.  
  371.     tpt.drawtext(0, 0, "test")
  372.  
  373.     self.cf = colorF
  374.     self.cb = colorB
  375.  
  376.     string.gsub(text, ".", console.buffer._write)
  377. end
  378. console.write = console.buffer.write
  379.  
  380. console.buffer.clear = function()
  381.     console.buffer.lines = {}
  382. end
  383. console.clear = console.buffer.clear
  384.  
  385. console.buffer.writeln = function(text, colorF, colorB)
  386.     if text == nil then
  387.         return
  388.     end
  389.  
  390.     console.buffer.write(text .. "\n", colorF, colorB)
  391. end
  392. console.writeln = console.buffer.writeln
  393.  
  394. ------------------------------------------------
  395. -- console.screen ------------------------------
  396. ------------------------------------------------
  397. console.screen = {
  398.     x = 0,
  399.     y = 0,
  400.     height = 40,
  401.     width  = 85,
  402.     fontx = 7,
  403.     fonty = 9,
  404.     offsetx = 8,
  405.     offsety = 10,
  406.     cursors = {},
  407. }
  408.  
  409. console.screen.right = function(self)
  410.     self.x = self.x + 1
  411.     if self.x > console.buffer.width - self.width then
  412.         self.x = 0
  413.     end
  414. end
  415.  
  416. console.screen.left = function(self)
  417.     self.x = self.x - 1
  418.     if self.x < 0 then
  419.         self.x = 0
  420.     end
  421. end
  422.  
  423. console.screen.down = function(self)
  424.     self.y = self.y + 1
  425.     if self.y > console.buffer.height - 1 then
  426.         self.y = 0
  427.     end
  428. end
  429.  
  430. console.screen.up = function(self)
  431.     self.y = self.y - 1
  432.     if self.y < 0 then
  433.         self.y = console.buffer.height - 1
  434.     end
  435. end
  436.  
  437. function g_drawchar(x, y, w, h, char, fg, bg)
  438.     self = console.screen
  439.  
  440.     if bg ~= 0 then
  441.         tpt.fillrect(x - 2, y - 2, w, h, bg[1], bg[2], bg[3], bg[4])
  442.     end
  443.  
  444.     tpt.drawtext(x, y, char, fg[1], fg[2], fg[3], fg[4])
  445. end
  446.  
  447. -- Draw each character to the console
  448. console.screen._draw = function(x, y, char)
  449.     self = console.screen
  450.  
  451.     rx = self.offset
  452.  
  453.     g_drawchar(
  454.         self.offsetx + x * self.fontx,
  455.         self.offsety + y * self.fonty,
  456.         self.fontx + 2,
  457.         self.fonty + 2,        
  458.         char[1],
  459.         console.colors.get(char[2], console.colors.foreground),
  460.         console.colors.get(char[3], console.colors.background)
  461.     )
  462. end
  463.  
  464. -- Set cursor type
  465. console.screen.setcursor = function(self, cursor)
  466.     if cursor == nil then
  467.         cursor = self
  468.         self = console.screen
  469.     end
  470.  
  471.     self.cursor = self.cursors[cursor]
  472. end
  473.  
  474. -- Draw the entire console
  475. console.screen.draw = function(self)
  476.     cbk = console.colors.backdrop
  477.     cbr = console.colors.border
  478.     cln = console.colors.line
  479.  
  480.     tpt.fillrect(5,   5, 600, 570, cbk[1], cbk[2], cbk[3], cbk[4])
  481.     tpt.drawline(5, 372, 605, 372, cln[1], cln[2], cln[3], cln[4])
  482.     tpt.drawrect(5,   5, 600, 570, cbr[1], cbr[2], cbr[3], cbr[4])
  483.  
  484.     res, error_message = pcall(console.buffer.iter, console.buffer, console.screen, console.screen._draw)
  485.     tpt.drawtext(0, 0, error_message)
  486.  
  487.     if self.cursor.stage == 0 then
  488.         self.cursor:run(self)
  489.     end
  490.  
  491.     if console.command.current == nil then
  492.         tpt.drawtext(8, 375, "current command is nil", 255, 0, 0)
  493.     else
  494.         g_drawchar(
  495.             8,
  496.             375,
  497.             self.fontx + 2,
  498.             self.fonty + 2,
  499.             '>',
  500.             console.colors.command,
  501.             console.colors.commandbg
  502.         )
  503.  
  504.         for i = 1,#console.command.current+1 do
  505.             char = console.command.current[i]
  506.  
  507.             if char ~= nil then
  508.                 g_drawchar(
  509.                     12 + self.fontx * i,
  510.                     375,
  511.                     self.fontx + 2,
  512.                     self.fonty + 2,
  513.                     char[1],
  514.                     console.colors.get(char[2], console.colors.command),
  515.                     console.colors.get(char[3], console.colors.commandbg)
  516.                 )
  517.             end
  518.         end
  519.     end
  520.  
  521.     if self.cursor.stage == 1 then
  522.         self.cursor:run(self)
  523.     end
  524. end
  525.  
  526. ------------------------------------------------
  527. -- console.screen.cursors ----------------------
  528. ------------------------------------------------
  529. console.screen.cursors = {
  530. }
  531.  
  532. console.screen.cursors.line = {
  533.     stage = 0,
  534.  
  535.     run = function(self, screen)
  536.         if os.clock() % 0.8 <= 0.4 then
  537.             c = console.colors.cursor
  538.             x = 19 + screen.fontx * console.command.cursor
  539.             tpt.drawline(
  540.                 x,
  541.                 375,
  542.                 x,
  543.                 382,
  544.                 c[1],
  545.                 c[2],
  546.                 c[3],
  547.                 c[4]
  548.             )
  549.         end
  550.     end,
  551. }
  552.  
  553. console.screen.cursors.block = {
  554.     stage = 0,
  555.  
  556.     run = function(self, screen)
  557.         if os.clock() % 0.8 <= 0.4 then
  558.             c = console.colors.cursor
  559.  
  560.             tpt.fillrect(
  561.                 17 + screen.fontx * console.command.cursor,
  562.                 373,
  563.                 screen.fontx + 1,
  564.                 screen.fonty + 1,
  565.                 c[1],
  566.                 c[2],
  567.                 c[3],
  568.                 c[4])
  569.         end
  570.     end,
  571. }
  572.  
  573. console.screen.cursor = console.screen.cursors.block
  574.  
  575. ------------------------------------------------
  576. -- console.colors ------------------------------
  577. ------------------------------------------------
  578. console.colors = {
  579.     black       = {  0,   0,   0, 255},
  580.     blue        = {  0,   0, 255, 255},
  581.     cyan        = {  0, 255, 255, 255},
  582.     darkblue    = {  0,   0, 178, 255},
  583.     darkcyan    = {  0, 178, 178, 255},
  584.     darkgreen   = {  0, 178,   0, 255},
  585.     darkpurple  = {178,   0, 178, 255},
  586.     darkred     = {178,   0,   0, 255},
  587.     darkyellow  = {178, 178,   0, 255},
  588.     default     = {178,  32, 255, 255},
  589.     gray        = {178, 178, 178, 255},
  590.     green       = {  0, 255,   0, 255},
  591.     purple      = {255,   0, 255, 255},
  592.     red         = {255,   0,   0, 255},
  593.     white       = {255, 255, 255, 255},
  594.     yellow      = {255, 255,   0, 255},
  595.     cursorg     = {  0, 255,  32, 120},
  596. }
  597.  
  598. -- bound colors
  599. console.colors.command    = console.colors.default -- foreground for current command
  600. console.colors.commandbg  = 0                      -- background for current command
  601. console.colors.cursor     = console.colors.cursorg -- color for the cursor
  602. console.colors.foreground = console.colors.default -- default foreground color
  603. console.colors.background = 0                      -- background of nil won't draw anything
  604. console.colors.error      = console.colors.red     -- default foreground for errors
  605. console.colors.errorbg    = 0                      -- default background for errors
  606. console.colors.backdrop   = {0,0,0,178}            -- backdrop, the color behind the text
  607. console.colors.border     = console.colors.white   -- border to the console
  608. console.colors.line       = console.colors.white   -- line which seperates console output from input
  609. console.colors.hud_motd   = console.colors.default -- message of the day display
  610. console.colors.hud_debug  = console.colors.default -- debug display
  611. console.colors.hud_close  = console.colors.default -- close message when old console is opened
  612.  
  613. console.colors.get = function(color, default)
  614.     if color == nil then
  615.         return default
  616.     end
  617.  
  618.     if type(color) == "table" then
  619.         if #color == 3 then
  620.             table.insert(color, 255)
  621.         elseif #color ~= 4 then
  622.             error("Color must be a table of 3 or 4 numbers")
  623.             return default
  624.         end
  625.        
  626.         return color
  627.     end
  628.  
  629.     c = console.colors[color]
  630.  
  631.     if c == nil then
  632.         error("Unknown color '" .. color .. "'")
  633.         return default
  634.     end
  635.  
  636.     return c
  637. end
  638.  
  639. ------------------------------------------------
  640. -- console.keys --------------------------------
  641. ------------------------------------------------
  642. console.keys = {
  643.     binds = {},
  644.     numbers = {},
  645.     symbols = {},
  646.     lastkey = 0,
  647.     down = 0,
  648.     downstart = 0,
  649.     repeatDelay = 0.5,
  650.     repeatTimer = 0.05
  651. }
  652.  
  653. console.keys.clear = function(self)
  654.     self.binds = {}
  655.     self.numbers = {}
  656.     self.symbols = {}
  657. end
  658.  
  659. console.keys.default = function(self)
  660.     self:clear()
  661.     -- Shifted Numbers Map
  662.     self.numbers = {")", "!", "@", "#", "$", "%", "^", "&", "*", "("}
  663.     -- Shifted Symbols Map
  664.     self.symbols["-"] = "_"
  665.     self.symbols["="] = "+"
  666.     self.symbols["["] = "{"
  667.     self.symbols["]"] = "}"
  668.     self.symbols[";"] = ":"
  669.     self.symbols["'"] = "\""
  670.     self.symbols[","] = "<"
  671.     self.symbols["."] = ">"
  672.     self.symbols["/"] = "?"
  673.     self.symbols["\\"] = "|"
  674.     -- Set binds
  675.     self.binds[8] = function() console.command:deleteback() end -- Backspace
  676.     self.binds[13] = function() console.command:run() end   -- Enter
  677.     self.binds[27] = function() console.set(0) end          -- Esc
  678.     self.binds[96] = function() console.set(0) end          -- ` - Console Key
  679.     self.binds[127] = function() console.command:deleteforward() end -- Delete
  680.     self.binds[273] = function() console.history:next() end -- Up
  681.     self.binds[274] = function() console.history:prev() end -- Down
  682.     self.binds[275] = function() console.command:right() end -- Right
  683.     self.binds[276] = function() console.command:left() end -- Left
  684.     self.binds[278] = function() console.screen:left() end  -- Home
  685.     self.binds[279] = function() console.screen:right() end -- End
  686.     self.binds[280] = function() console.screen:up() end    -- Page Up
  687.     self.binds[281] = function() console.screen:down() end  -- Page Down
  688. end
  689.  
  690. -- default keys should work for US style keyboards
  691. console.keys.us = console.keys.default
  692.  
  693. -- Repeat the key if held down for longer than 0.5 seconds
  694. console.keys.heldkey = function(self)
  695.     if self.down == 1 then
  696.         if os.clock() - self.downstart > self.repeatDelay then
  697.             self.down = 2
  698.         end
  699.     end
  700.     if self.down == 2 and (os.clock() - self.downstart) > self.repeatTimer then
  701.         downstart = os.clock()
  702.         console.keys:process(self.lastks, self.lastkey, self.lastmod, 3)
  703.  
  704.         -- HACK: down is set within console.keys.process, set it back to 2
  705.         self.down = 2
  706.     end
  707. end
  708.  
  709. console.keys.process = function(self, keyString, key, modifier, event)
  710.     self.lastkey = key
  711.     self.lastks = keyString
  712.     self.lastmod = modifier
  713.    
  714.     self.down = 2 - event
  715.     self.downstart = os.clock()
  716.    
  717.     -- Handle keys closing old console
  718.     if console.active == 2 and (key == 96 or key == 27) then
  719.         console.active = 0
  720.     end
  721.  
  722.     -- Override keys
  723.     if console.active == 0 and event == 1 then
  724.         -- ` - Console Key
  725.         if key == 96 then
  726.             if console.active == 0 and (modifier == 1 or modifier == 2) then
  727.                 console.old.open()
  728.                 return false
  729.             end
  730.  
  731.             console.toggle()        
  732.             return false
  733.         end
  734.  
  735.         -- d - Debug Mode
  736.         if key == 100 and event == 1 then
  737.             console.debugmode = 1 - console.debugmode
  738.         end
  739.  
  740.         -- h - Hud Toggle
  741.         if key == 104 and event == 1 then
  742.             tpt.hud(1 - console.old.hud_value)
  743.             return false
  744.         end
  745.     end
  746.    
  747.     if console.active == 1 then
  748.         if event == 2 then
  749.             return false
  750.         end
  751.  
  752.         -- Execute bind for key
  753.         bind = self.binds[key]
  754.         if bind ~= nil then
  755.             ecall(bind, keyString, key, modifier, event)
  756.             return false
  757.         end
  758.        
  759.         -- Ignore any special keys
  760.         if key >= 256 then
  761.             return false
  762.         end
  763.  
  764.         -- Mappings for shifted keys
  765.         if modifier == 1 or modifier == 2 then
  766.             if key >= 48 and key <= 57 then
  767.                 keyString = self.numbers[key - 47]
  768.             elseif key >= 97 and key <= 122 then
  769.                 keyString = string.char(key - 97 + 65)
  770.             else
  771.                 local temp = self.symbols[keyString]
  772.                
  773.                 if temp ~= nil then
  774.                     keyString = temp
  775.                 end
  776.             end
  777.         end
  778.        
  779.         console.command:writechar(keyString)
  780.         return false
  781.     end
  782. end
  783.  
  784. ------------------------------------------------
  785. -- console.history -----------------------------
  786. ------------------------------------------------
  787. console.history = {
  788.     contents = {},
  789.     maxsize = 30,
  790.     size = 0,
  791. }
  792.  
  793. console.history.store = function(self, value)
  794.     self.size = self.size + 1
  795.     table.insert(self.contents, 1, value)
  796.     self.selected = 0
  797. end
  798.  
  799. console.history.next = function(self)
  800.     if self.size == 0 then
  801.         return
  802.     end
  803.     self.selected = self.selected + 1
  804.  
  805.     if self.selected > self.size then
  806.         self.selected = 1
  807.     end
  808.  
  809.     console.command:set(self.contents[self.selected])
  810. end
  811.  
  812. console.history.prev = function(self)
  813.     if self.size == 0 then
  814.         return
  815.     end
  816.     self.selected = self.selected - 1
  817.  
  818.     if self.selected <= 0 then
  819.         self.selected = self.size
  820.     end
  821.  
  822.     console.command:set(self.contents[self.selected])
  823. end
  824.  
  825. console.history.select = function(self, n)
  826.     if self.size == 0 then
  827.         return
  828.     end
  829.  
  830.     n = n%self.size
  831.     print(n)
  832.     self.selected = (self.selected + n)
  833.  
  834.     print(self.selected)
  835.     console.command:set(self.contents[self.selected])
  836. end
  837.  
  838. ------------------------------------------------
  839. -- console.quick -------------------------------
  840. ------------------------------------------------
  841. local function console_quick_help()
  842.     print("!cls                   - Clear screen")
  843.     print("!color [name] <color>  - Set color class to specified color")
  844.     print("!cursor [name]         - set current cursor to cursor specified")
  845.     print("!e <text>              - Echo text")
  846.     print("!r <file>              - Run a file")
  847.     print("!help                  - Display this text")
  848. end
  849.  
  850. console.quick[""] = console_quick_help
  851. console.quick["help"] = console_quick_help
  852.  
  853. console.quick["e"] = console.buffer.writeln
  854. console.quick["cls"] = console.clear
  855. console.quick["cursor"] = console.screen.setcursor
  856.  
  857. console.quick["color"] = function(arguments)
  858.     args = string.split(arguments, " ")
  859.  
  860.     if #args == 1 then
  861.         name = "foreground"
  862.         color = args[1]
  863.     elseif #args == 2 then
  864.         name = args[1]
  865.         color = args[2]
  866.     else
  867.         error("!color [name] <color>")
  868.         return
  869.     end
  870.  
  871.     c = string.split(color,",")
  872.    
  873.     if #c == 1 then
  874.         if color == "nil" then
  875.             console.colors[name] = 0
  876.             return
  877.         end
  878.  
  879.         c = console.colors.get(color)
  880.        
  881.         if c == nil then
  882.             return
  883.         end
  884.     elseif #c == 3 or #c == 4 then
  885.         alpha = 255
  886.  
  887.         if #c == 4 then
  888.             alpha = tonumber(c[4])
  889.         end
  890.        
  891.         c = {
  892.             tonumber(c[1]),
  893.             tonumber(c[2]),
  894.             tonumber(c[3]),
  895.             alpha
  896.         }
  897.     end
  898.  
  899.     console.colors[name] = c
  900. end
  901.  
  902. -- Function for running a file named filename or filename.lua
  903. console.quick["r"] = function(filename)
  904.     if string.sub(filename, -4) ~= ".lua" then
  905.         f, msg = io.open(filename, "r")
  906.  
  907.         if not f then
  908.             f, msg = io.open(filename .. ".lua", "r")
  909.             if f then
  910.                 f:close()
  911.                 return dofile(filename .. ".lua")
  912.             end
  913.         else
  914.             f:close()
  915.         end
  916.     end
  917.     return dofile(filename)
  918. end
  919.  
  920. ------------------------------------------------
  921. -- LETS DO THIS! -------------------------------
  922. ------------------------------------------------
  923. local function keyHandler(keyString, key, modifier, event)
  924.     return console.keys:process(keyString, key, modifier, event)
  925. end
  926.  
  927. motd = 1
  928. motdstart = 0
  929.  
  930. local function mouseHandler(mousex, mousey, button, event)
  931.     if motd == 1 then
  932.         motdstart = os.clock()
  933.         motd = 0
  934.     end
  935.  
  936.     if console.active == 1 then
  937.         return false
  938.     end
  939.    
  940.     return true
  941. end
  942.  
  943. local function draw()
  944.     if console.active == 1 then
  945.         console.keys:heldkey()
  946.         console.screen:draw()
  947.     end
  948.  
  949.     if motd == 1 then
  950.         alpha = 1
  951.     else
  952.         alpha = 1 - (os.clock() - motdstart)
  953.     end
  954.    
  955.     if alpha ~= 0 then
  956.         c = console.colors.hud_motd
  957.         tpt.drawtext(15, 335, "The all lua-console V6 by devast8a is installed!", c[1], c[2], c[3], c[4] * alpha)
  958.     end
  959.        
  960.     if console.debugmode == 1 then
  961.         x = 16
  962.         y = 27
  963.  
  964.         c = console.colors.hud_debug
  965.         if console.old.hud_value == 1 then
  966.             tpt.drawtext(16, 27, "lua-console (V6) (" .. console.active .. ", " .. console.keys.lastkey .. ")", c[1], c[2], c[3], c[4])
  967.         end
  968.         if console.active == 1 then
  969.             tpt.drawtext(575, 8, "[" .. console.keys.lastkey .. "]", c[1], c[2], c[3], c[4])
  970.         end
  971.     end
  972.  
  973.     if console.active == 2 then
  974.         c = console.colors.hud_close
  975.         tpt.drawtext(5, 228, "Old console [Press escape to close] -- lua-console", c[1], c[2], c[3], c[4])
  976.     end
  977. end
  978.  
  979. tpt.register_step(draw)
  980. tpt.register_keypress(keyHandler)
  981. tpt.register_mouseclick(mouseHandler)
  982.  
  983. console.old.log("lua-console loaded successfully")
  984.  
  985. tpt.log("The all lua-console V6")
  986. tpt.log("  by devast8a")
  987. tpt.log("")
  988. tpt.log("Page Up/Down - move console screen up/down")
  989. -- Home/End - move console left/right (Only useful when buffer is larger than screen)
  990. tpt.log("Up/Down - cycle through command history")
  991. tpt.log("")
  992. tpt.log("Commands starting with ! don't work, instead close this console")
  993. tpt.log(" and press shift + console key to use the old console.")
  994.  
  995. ------------------------------------------------
  996. -- Last initialization steps -------------------
  997. ------------------------------------------------
  998. console.keys:us() -- keyboard mapping
Advertisement
Add Comment
Please, Sign In to add comment