Guest User

ALL lua console for Powder Toy

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