Skip to content

Commit

Permalink
Improvement: expose cecho2ansi and have it work for non-ansi color na…
Browse files Browse the repository at this point in the history
…mes as well (#5672)

#### Brief overview of PR changes/additions
While perusing the ansi conversion functions I realized we were doing all the work of cecho2ansi but had not exposed it as separate functionality. Since we have done so for d and hecho, it made sense to me to perform this refactor.
In the process I also added support for colors which are not named ansi colors but exist in the color table. 
#### Motivation for adding to Mudlet
More complete API, consistency across the c/d/h variants.
  • Loading branch information
demonnic committed Dec 1, 2021
1 parent 6bc1800 commit cef0167
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/mudlet-lua/lua/GUIUtils.lua
Expand Up @@ -1485,12 +1485,20 @@ if rex then
local colorNumber = ctable[fore]
if colorNumber then
result = string.format("%s\27[38:5:%sm", result, colorNumber)
elseif color_table[fore] then
local rgb = color_table[fore]
result = string.format("%s\27[38:2::%s:%s:%sm", result, rgb[1], rgb[2], rgb[3])
end
end
end
if back then
local colorNumber = ctable[back]
result = string.format("%s\27[48:5:%sm", result, colorNumber)
if colorNumber then
result = string.format("%s\27[48:5:%sm", result, colorNumber)
elseif color_table[back] then
local rgb = color_table[back]
result = string.format("%s\27[48:2::%s:%s:%sm", result, rgb[1], rgb[2], rgb[3])
end
end
return result
end
Expand Down Expand Up @@ -1537,6 +1545,18 @@ if rex then
return result
end

function cecho2ansi(text)
local colorPattern = _Echos.Patterns.Color[1]
local result = ""
for str, color in rex.split(text, colorPattern) do
result = result .. str
if color then
result = result .. colorToAnsi(color:match("<(.+)>"))
end
end
return result
end

--- feedTriggers with cecho style color information.
-- Valid colors are black,red,green,yellow,blue,magenta,cyan,white and light_* versions of same
-- Can also pass in a number between 0 and 255 to use the expanded ansi 255 colors. IE <124> will set foreground to the color ANSI124
Expand All @@ -1546,15 +1566,7 @@ if rex then
--@see cecho
--@see cinsertText
function cfeedTriggers(text)
local colorPattern = _Echos.Patterns.Color[1]
local result = ""
for str, color in rex.split(text, colorPattern) do
result = result .. str
if color then
result = result .. colorToAnsi(color:match("<(.+)>"))
end
end
feedTriggers(result .. "\n")
feedTriggers(cecho2ansi(text) .. "\n")
echo("")
end

Expand Down
39 changes: 39 additions & 0 deletions src/mudlet-lua/tests/GUIUtils_spec.lua
Expand Up @@ -198,6 +198,45 @@ describe("Tests the GUI utilities as far as possible without mudlet", function()
end)
end)

describe("Tests the functionality of cecho2ansi()", function()
local simple_original = "<red>This is in red<r> And then reset."
local simple_expected = "\27[38:5:1mThis is in red\27[0m And then reset."

it("should convert a simple cecho string to an equivalent ansi string", function()
local actual = cecho2ansi(simple_original)
assert.equals(simple_expected, actual)
end)

it("should convert a color name which doesn't have a direct ansi named equivalent", function()
local actual = cecho2ansi("<DodgerBlue>")
assert.equals("\27[38:2::30:144:255m", actual)
end)

it("should handle bold", function()
local expected = "\27[1mbold\27[22m"
local actual = cecho2ansi("<b>bold</b>")
assert.equals(expected, actual)
end)

it("should handle underline", function()
local expected = "\27[4munderline\27[24m"
local actual = cecho2ansi("<u>underline</u>")
assert.equals(expected, actual)
end)

it("should handle italics", function()
local expected = "\27[3mitalics\27[23m"
local actual = cecho2ansi("<i>italics</i>")
assert.equals(expected, actual)
end)

it("should handle strikeout", function()
local expected = "\27[9mstrikeout\27[29m"
local actual = cecho2ansi("<s>strikeout</s>")
assert.equals(expected, actual)
end)
end)

describe("Tests the functionality of ansi2string()", function()
it("should return the string fed into it with ansi codes removed", function()
local original = '\27[38;5;179;48;5;230mYou say in a baritone voice, "Test."\27[0;37;40m'
Expand Down

0 comments on commit cef0167

Please sign in to comment.