Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
initial version, with colorize and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
egarcia committed May 12, 2011
1 parent 2966dca commit 523e7f2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 49 deletions.
1 change: 1 addition & 0 deletions COPYING
@@ -1,4 +1,5 @@
Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net>
Copyright (c) 2011 Enrique García Cota <enrique.garcia.cota@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
111 changes: 62 additions & 49 deletions ansicolors.lua
@@ -1,4 +1,5 @@
-- Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net>
-- Copyright (c) 2011 Enrique García Cota <enrique.garcia.cota@gmail.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,65 +19,77 @@
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

local pairs = pairs
local tostring = tostring
local setmetatable = setmetatable
local schar = string.char
local ansicolors = {}

module 'ansicolors'
local codes = {
-- misc
bright = 1,
dim = 2,
underline = 4,
blink = 5,
reverse = 7,
hidden = 8,

local colormt = {}
-- foreground colors
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
white = 37,

function colormt:__tostring()
return self.value
end
-- background colors
blackbg = 40,
redbg = 41,
greenbg = 42,
yellowbg = 43,
bluebg = 44,
magentabg = 45,
cyanbg = 46,
whitebg = 47
}

function colormt:__concat(other)
return tostring(self) .. tostring(other)
end
local codeNames = {}
for name,_ in pairs(codes) do table.insert(codeNames, name) end
table.sort(codeNames, function(a,b) return codes[a] > codes[b] end)

function colormt:__call(s)
return self .. s .. _M.reset
local escapeString = string.char(27) .. '[%dm'
local function escapeNumber(number)
return escapeString:format(number)
end

colormt.__metatable = {}

local function makecolor(value)
return setmetatable({ value = schar(27) .. '[' .. tostring(value) .. 'm' }, colormt)
local function escapeNumbers(numbers)
local buffer = {}
for i=1, #numbers do
table.insert(buffer, escapeNumber(numbers[i]))
end
return table.concat(buffer)
end

local colors = {
-- attributes
reset = 0,
clear = 0,
bright = 1,
dim = 2,
underscore = 4,
blink = 5,
reverse = 7,
hidden = 8,
local reset = escapeNumber(0)

-- foreground
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
white = 37,
local function options2numbers(options)
local numbers, substitutions, codeName = {}, 0
for i = 1, #codeNames do
codeName = codeNames[i]
options, substitutions = options:gsub(codeName, '')
if substitutions > 0 then
table.insert(numbers, codes[codeName])
end
end
return numbers
end

-- background
onblack = 40,
onred = 41,
ongreen = 42,
onyellow = 43,
onblue = 44,
onmagenta = 45,
oncyan = 46,
onwhite = 47,
}
function ansicolors.colorize( text, options )
options = string.lower(options or '')

local numbers = options2numbers(options)

for c, v in pairs(colors) do
_M[c] = makecolor(v)
if #numbers == 0 then return text end

return escapeNumbers(numbers) .. text .. reset
end

return ansicolors
40 changes: 40 additions & 0 deletions specs/ansicolors_spec.lua
@@ -0,0 +1,40 @@
local ansicolors = require 'ansicolors'

local c27 = string.char(27)

describe('ansicolors', function()

describe('.colorize', function()

it('should do nothing with no options', function()
assert_equal(ansicolors.colorize('foo'), 'foo')
end)

it('should add red color to text', function()
assert_equal(ansicolors.colorize('foo', 'red'), c27 .. '[31mfoo' .. c27 .. '[0m')
end)

it('should add red color to text with uppercase', function()
assert_equal(ansicolors.colorize('foo', 'Red'), c27 .. '[31mfoo' .. c27 .. '[0m')
end)

it('should add red underlined text', function()
assert_equal(ansicolors.colorize('foo', 'UnderlineRed'), c27 .. '[31m' .. c27 .. '[4mfoo' .. c27 .. '[0m')
end)

it('should parse the red background before the red foreground', function()
assert_equal(ansicolors.colorize('foo', 'RedBG'), c27 .. '[41mfoo' .. c27 .. '[0m')
end)

it('should allow multiple attributes and spureous chars', function()
assert_equal(ansicolors.colorize('foo', 'underline_redbg_yellow_dim'), c27 .. '[41m' .. c27 .. '[33m' .. c27 .. '[4m' .. c27 .. '[2mfoo' .. c27 .. '[0m')
end)

it('should ignore order', function()
assert_equal(ansicolors.colorize('foo', 'underline_redbg_yellow'), ansicolors.colorize('foo', 'yellowRedBGUnderline'))
end)

end)


end)

0 comments on commit 523e7f2

Please sign in to comment.