Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Message
| You can make a simple coloured-line displayer. For example see this thread:
http://www.gammon.com.au/forum/?id=9529
To round that out to actually display them I take the code from reply #8 and add a display function:
-- copied from http://www.gammon.com.au/forum/?id=8947
local BLACK = 1
local RED = 2
local GREEN = 3
local YELLOW = 4
local BLUE = 5
local MAGENTA = 6
local CYAN = 7
local WHITE = 8
-- colour styles (eg. @r is normal red, @R is bold red)
-- @- is shown as ~
-- @@ is shown as @
-- This table uses the colours as defined in the MUSHclient ANSI tab, however the
-- defaults are shown on the right if you prefer to use those.
local colour_conversion = {
k = GetNormalColour (BLACK) , -- 0x000000
r = GetNormalColour (RED) , -- 0x000080
g = GetNormalColour (GREEN) , -- 0x008000
y = GetNormalColour (YELLOW) , -- 0x008080
b = GetNormalColour (BLUE) , -- 0x800000
m = GetNormalColour (MAGENTA) , -- 0x800080
c = GetNormalColour (CYAN) , -- 0x808000
w = GetNormalColour (WHITE) , -- 0xC0C0C0
K = GetBoldColour (BLACK) , -- 0x808080
R = GetBoldColour (RED) , -- 0x0000FF
G = GetBoldColour (GREEN) , -- 0x00FF00
Y = GetBoldColour (YELLOW) , -- 0x00FFFF
B = GetBoldColour (BLUE) , -- 0xFF0000
M = GetBoldColour (MAGENTA) , -- 0xFF00FF
C = GetBoldColour (CYAN) , -- 0xFFFF00
W = GetBoldColour (WHITE) , -- 0xFFFFFF
-- add custom colours here
} -- end conversion table
-----------
-- our code: :)
local P, C, Ct, Cs, Cg, S, Cc = lpeg.P, lpeg.C, lpeg.Ct, lpeg.Cs, lpeg.Cg, lpeg.S, lpeg.Cc
local tmpcolorkeys = {}
for k,v in pairs(colour_conversion) do tmpcolorkeys[#tmpcolorkeys+1] = k end
local Colors = P"@" * C(S( table.concat(tmpcolorkeys, "") ))
tmpcolorkeys = nil -- Clean up on aisle tempvar!
local NotAt = P(1) - P"@"
local NotAColor = P(1) - Colors
local text = Cg(Cs( (NotAt + P"@-"/"~" + P"@@"/"@" + NotAColor )^1 ),"text")
local color = Cg( (Colors + Cc('w')) / colour_conversion ,"textcolour")
local res = Ct( Ct( color * text)^1)
function ShowStyledLine (s)
local styles = res:match(s)
for k, v in ipairs (t) do
ColourTell (RGBColourToName (v.textcolour), "", v.text)
end -- for each style run
print () -- finish off the line
end -- ShowStyledLine
If you put that into your script file (language: Lua) then you can display colours like this using the same syntax that Aardwolf does:
ShowStyledLine ("an @RAardwolf @WAdventurer's Guide@w (type @Rread guide@w)")
Then you just use things like @R for bold red, and @r for normal red, and so on as you can see in the script near the top. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|