Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- terminal = {}
- terminal._colors = {
- black = 0,
- red = 1,
- green = 2,
- yellow = 3,
- blue = 4,
- magenta = 5,
- cyan = 6,
- white = 7
- }
- terminal._codes = {
- reset = 0,
- bold = 1,
- tiny = 2,
- italic = 3,
- underline = 4,
- blink = function(v) return v + 4 end,
- fblink = 6,
- negate = 7,
- hidden = 8,
- strike = 9,
- font = function(v) return v + 10 end,
- fract = 20,
- fat = 21,
- color = function(v)
- local t = type(v)
- return t == 'table' and (terminal._colors[v[1]] + 30)..';'..(terminal._colors[v[2]] + 40)
- or t == 'string' and (terminal._colors[t] + 30)
- end,
- }
- local concat = table.concat
- function terminal:command(mod, ...)
- io.write('\27['..concat({...}, ';')..mod)
- end
- function terminal:s(mod, ...)
- return '\27['..concat({...}, ';')..mod
- end
- function terminal:format(text, opt)
- local res = {}
- for k, v in pairs(opt) do
- local t = type(self._codes[k])
- res[#res+1] = t == 'number' and self._codes[k]
- or t == 'function' and self._codes[k](v)
- or nil
- end
- return '\27['..table.concat(res, ';')..'m'..text..'\27[0m' -- сбрасываем форматирование в конце записи
- end
- function terminal:toBlock(text, x, y)
- return self:s('H', x, y)..self:s's' -- set position ans save
- ..text:gsub('\n', self:s'u'..self:s'B'..self:s's') -- replace \n as 'restore' -> 'move down' -> 'save'
- end
- function terminal:move(x, y)
- x = x or 0; y = y or 0
- local cx = x < 0 and 'D' or 'C'
- local cy = y < 0 and 'A' or 'B'
- self:command(cx, x)
- self:command(cy, y)
- end
- function terminal:setTextColor(r, g, b)
- self:command('m', concat({38, 2, r, g, b}, ';'))
- end
- function terminal:moveLine(y)
- y = y or 0
- local cy = y < 0 and 'F' or 'E'
- self:command(cy, y)
- end
- function terminal:setColumn(n)
- self:command('G', n or 0)
- end
- function terminal:setPosition(x, y)
- self:command('H', y, x)
- end
- function terminal:cls(x, y)
- self:setPosition()
- self:command('J')
- end
- function terminal:cLine(x)
- self:setPosition()
- self:command('K', x or 0)
- end
- function terminal:scroll(y)
- y = y or 0
- local cy = y < 0 and 'S' or 'T'
- self:command(cy, y)
- end
- function terminal:saveCurPosition()
- self:command('s')
- end
- function terminal:restoreCurPosition()
- self:command('u')
- end
- function terminal:hideCursor()
- self:command('?25l')
- end
- function terminal:showCursor()
- self:command('?25h')
- end
- local fill = {
- [0] = ' ',
- [1] = '-',
- [2] = '-',
- [3] = '-',
- [4] = '+',
- [5] = '-',
- [6] = '-',
- [6] = '¦',
- [7] = '¦',
- }
- local rect = {
- h = '─',
- v = '│',
- vp = '¦',
- vl = '├',
- vr = '┤',
- hu = '┴',
- hd = '┬',
- c = '┼',
- ul = '┌',
- ur = '┐',
- bl = '└',
- br = '┘',
- }
- local rect2 = {
- h = '=',
- v = '║',
- vp = '║',
- vl = '╗',
- vr = '╔',
- hu = '╩',
- hd = '╦',
- c = '╬',
- ul = '╔',
- ur = '╗',
- bl = '╚',
- br = '╝',
- }
- function terminal:line(len, sym, mode)
- local s = self:s's'
- mode = mode or 'h'
- len = len or 1
- local dir = len > 0
- len = math.abs(len or 1)
- sym = sym or '*'
- local up, down, left, right = self:s'A', self:s'B', self:s'D', self:s'C'
- if mode == 'h' then
- if dir then
- return sym:rep(len)..left
- end
- return left:rep(len)..sym:rep(len)..left:rep(len)
- end
- if dir then
- return (sym..down..left):rep(len)..up
- end
- return (sym..up..left):rep(len)..down
- end
- function terminal:rectangle(w, h, line, mode)
- local str = {}
- local up, down, left, right = self:s'A', self:s'B', self:s'D', self:s'C'
- -- top
- str[1] = rect.ul .. rect.h:rep(w - 2) .. rect.ur
- -- right
- str[2] = (left .. down .. rect.v):rep(h - 2) .. down
- -- bottom
- str[3] = left:rep(w)..rect.bl .. rect.h:rep(w - 2) .. rect.br..left:rep(w)
- --left
- str[4] = (up .. rect.v .. left):rep(h - 2) .. right
- return table.concat(str)
- end
- return terminal
Advertisement
Add Comment
Please, Sign In to add comment