Snusmumriken

Terminal stuff

Sep 16th, 2017
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.88 KB | None | 0 0
  1. terminal = {}
  2. terminal._colors = {
  3.     black = 0,
  4.     red = 1,
  5.     green = 2,
  6.     yellow = 3,
  7.     blue = 4,
  8.     magenta = 5,
  9.     cyan = 6,
  10.     white = 7
  11. }
  12.  
  13. terminal._codes = {
  14.     reset = 0,
  15.     bold = 1,
  16.     tiny = 2,
  17.     italic = 3,
  18.     underline = 4,
  19.     blink = function(v) return v + 4 end,
  20.     fblink = 6,
  21.     negate = 7,
  22.     hidden = 8,
  23.     strike = 9,
  24.     font = function(v) return v + 10 end,
  25.     fract = 20,
  26.     fat = 21,
  27.     color = function(v)
  28.         local t = type(v)
  29.         return t == 'table'  and (terminal._colors[v[1]] + 30)..';'..(terminal._colors[v[2]] + 40)
  30.             or t == 'string' and (terminal._colors[t]    + 30)
  31.     end,
  32. }
  33.  
  34.  
  35.  
  36. local concat = table.concat
  37. function terminal:command(mod, ...)
  38.     io.write('\27['..concat({...}, ';')..mod)
  39. end
  40.  
  41. function terminal:s(mod, ...)
  42.     return '\27['..concat({...}, ';')..mod
  43. end
  44.  
  45. function terminal:format(text, opt)
  46.     local res = {}
  47.     for k, v in pairs(opt) do
  48.         local t = type(self._codes[k])
  49.         res[#res+1] = t == 'number' and self._codes[k]
  50.                    or t == 'function' and self._codes[k](v)
  51.                              or nil
  52.     end
  53.     return '\27['..table.concat(res, ';')..'m'..text..'\27[0m' -- сбрасываем форматирование в конце записи
  54. end
  55.  
  56. function terminal:toBlock(text, x, y)
  57.     return self:s('H', x, y)..self:s's' -- set position ans save
  58.          ..text:gsub('\n', self:s'u'..self:s'B'..self:s's') -- replace \n as 'restore' -> 'move down' -> 'save'
  59. end
  60.  
  61. function terminal:move(x, y)
  62.     x = x or 0; y = y or 0
  63.     local cx = x < 0 and 'D' or 'C'
  64.     local cy = y < 0 and 'A' or 'B'
  65.     self:command(cx, x)
  66.     self:command(cy, y)
  67. end
  68.  
  69. function terminal:setTextColor(r, g, b)
  70.     self:command('m', concat({38, 2, r, g, b}, ';'))
  71. end
  72.  
  73. function terminal:moveLine(y)
  74.     y = y or 0
  75.     local cy = y < 0 and 'F' or 'E'
  76.     self:command(cy, y)
  77. end
  78.  
  79. function terminal:setColumn(n)
  80.     self:command('G', n or 0)
  81. end
  82.  
  83. function terminal:setPosition(x, y)
  84.     self:command('H', y, x)
  85. end
  86.  
  87. function terminal:cls(x, y)
  88.     self:setPosition()
  89.     self:command('J')
  90. end
  91.  
  92. function terminal:cLine(x)
  93.     self:setPosition()
  94.     self:command('K', x or 0)
  95. end
  96.  
  97. function terminal:scroll(y)
  98.     y = y or 0
  99.     local cy = y < 0 and 'S' or 'T'
  100.     self:command(cy, y)
  101. end
  102.  
  103. function terminal:saveCurPosition()
  104.     self:command('s')
  105. end
  106.  
  107. function terminal:restoreCurPosition()
  108.     self:command('u')
  109. end
  110.  
  111. function terminal:hideCursor()
  112.     self:command('?25l')
  113. end
  114.  
  115. function terminal:showCursor()
  116.     self:command('?25h')
  117. end
  118.  
  119.  
  120. local fill = {
  121.     [0] = ' ',
  122.     [1] = '-',
  123.     [2] = '-',
  124.     [3] = '-',
  125.     [4] = '+',
  126.     [5] = '-',
  127.     [6] = '-',
  128.     [6] = '¦',
  129.     [7] = '¦',
  130. }
  131.  
  132. local rect = {
  133.     h  = '─',
  134.     v  = '│',
  135.     vp = '¦',
  136.     vl = '├',
  137.     vr = '┤',
  138.     hu = '┴',
  139.     hd = '┬',
  140.     c  = '┼',
  141.     ul = '┌',
  142.     ur = '┐',
  143.     bl = '└',
  144.     br = '┘',
  145. }
  146.  
  147. local rect2 = {
  148.     h  = '=',
  149.     v  = '║',
  150.     vp = '║',
  151.     vl = '╗',
  152.     vr = '╔',
  153.     hu = '╩',
  154.     hd = '╦',
  155.     c  = '╬',
  156.     ul = '╔',
  157.     ur = '╗',
  158.     bl = '╚',
  159.     br = '╝',
  160. }
  161.  
  162.  
  163.  
  164.  
  165. function terminal:line(len, sym, mode)
  166.     local s = self:s's'
  167.     mode = mode or 'h'
  168.    
  169.     len = len or 1
  170.    
  171.     local dir = len > 0
  172.     len = math.abs(len or 1)
  173.    
  174.     sym = sym or '*'
  175.    
  176.     local up, down, left, right = self:s'A', self:s'B', self:s'D', self:s'C'
  177.    
  178.     if mode == 'h' then
  179.         if dir then
  180.             return sym:rep(len)..left
  181.         end
  182.         return left:rep(len)..sym:rep(len)..left:rep(len)
  183.     end
  184.    
  185.     if dir then
  186.         return (sym..down..left):rep(len)..up
  187.     end
  188.     return (sym..up..left):rep(len)..down
  189. end
  190.  
  191. function terminal:rectangle(w, h, line, mode)
  192.     local str = {}
  193.    
  194.     local up, down, left, right = self:s'A', self:s'B', self:s'D', self:s'C'
  195.    
  196.     -- top
  197.     str[1] = rect.ul .. rect.h:rep(w - 2) .. rect.ur
  198.    
  199.     -- right
  200.     str[2] = (left .. down .. rect.v):rep(h - 2) ..  down
  201.    
  202.     -- bottom
  203.    
  204.     str[3] = left:rep(w)..rect.bl .. rect.h:rep(w - 2) .. rect.br..left:rep(w)
  205.    
  206.     --left
  207.     str[4] = (up .. rect.v .. left):rep(h - 2) .. right
  208.    
  209.  
  210.     return table.concat(str)
  211. end
  212.  
  213. return terminal
Advertisement
Add Comment
Please, Sign In to add comment