I'm working on a code editor and I want certain words to be colored different colors, how would I do this? I've tried before. Nothing works. I have a table of the words and what color they should be. and Color["String"] would output the color. So, how would I do this? I know it's possible, like the edit program.
Color Specific Word
Started by PixelFox, Jul 19 2015 04:23 PM
5 replies to this topic
#1
Posted 19 July 2015 - 04:23 PM
#2
Posted 19 July 2015 - 05:28 PM
Have you looked at the code for the edit program?
#4
Posted 19 July 2015 - 07:06 PM
Well, first, I'd like to mention that an editor in Computer Craft doesn't make a lot of sense, the screens just aren't big enough.
Plus, they can be difficult to code in the first place.
But if you succeed, you can take away a lot of good Lua knowledge. So consider this project wisely.
So anyway, here are the functions that color text in the default edit program:
The thing that makes it work is patterns ( These things: "^%-%-%[%[.-%]%]" )
Read about them here: http://www.lua.org/pil/20.2.html
They are very powerful, but can be confusing at first.
Basically, the 'trywrite' function looks through some text for a specific pattern.
For example, the first call in 'writeHighlighted' is this:
'writeHighlighted' continues to try to find things that should be highlighting, like "strings", or a keyword
(keywords are defined above, I haven't included that table)
Each time 'tryWrite' finds something to color, it removes it from the line buffer (sLine)
I hope you took the time to read this, and that it helped
Note that I am not an expert with patterns, most of these are gibberish to me too
Plus, they can be difficult to code in the first place.
But if you succeed, you can take away a lot of good Lua knowledge. So consider this project wisely.
So anyway, here are the functions that color text in the default edit program:
local function tryWrite( sLine, regex, colour )
local match = string.match( sLine, regex )
if match then
if type(colour) == "number" then
term.setTextColour( colour )
else
term.setTextColour( colour(match) )
end
term.write( match )
term.setTextColour( textColour )
return string.sub( sLine, string.len(match) + 1 )
end
return nil
end
local function writeHighlighted( sLine )
while string.len(sLine) > 0 do
sLine =
tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
tryWrite( sLine, "^%-%-.*", commentColour ) or
tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
tryWrite( sLine, "^[%w_]+", function( match )
if tKeywords[ match ] then
return keywordColour
end
return textColour
end ) or
tryWrite( sLine, "^[^%w_]", textColour )
end
end
It isn't simple by any stretch of the imagination.The thing that makes it work is patterns ( These things: "^%-%-%[%[.-%]%]" )
Read about them here: http://www.lua.org/pil/20.2.html
They are very powerful, but can be confusing at first.
Basically, the 'trywrite' function looks through some text for a specific pattern.
For example, the first call in 'writeHighlighted' is this:
tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour )This specific pattern looks for comments ( You know, the two dashes ), and prints them to the screen in the 'commentColour'
'writeHighlighted' continues to try to find things that should be highlighting, like "strings", or a keyword
(keywords are defined above, I haven't included that table)
Each time 'tryWrite' finds something to color, it removes it from the line buffer (sLine)
I hope you took the time to read this, and that it helped
Note that I am not an expert with patterns, most of these are gibberish to me too
#5
Posted 19 July 2015 - 07:19 PM
HPWebcamAble, on 19 July 2015 - 07:06 PM, said:
Well, first, I'd like to mention that an editor in Computer Craft doesn't make a lot of sense, the screens just aren't big enough.
Plus, they can be difficult to code in the first place.
But if you succeed, you can take away a lot of good Lua knowledge. So consider this project wisely.
So anyway, here are the functions that color text in the default edit program:
The thing that makes it work is patterns ( These things: "^%-%-%[%[.-%]%]" )
Read about them here: http://www.lua.org/pil/20.2.html
They are very powerful, but can be confusing at first.
Basically, the 'trywrite' function looks through some text for a specific pattern.
For example, the first call in 'writeHighlighted' is this:
'writeHighlighted' continues to try to find things that should be highlighting, like "strings", or a keyword
(keywords are defined above, I haven't included that table)
Each time 'tryWrite' finds something to color, it removes it from the line buffer (sLine)
I hope you took the time to read this, and that it helped
Note that I am not an expert with patterns, most of these are gibberish to me too
Plus, they can be difficult to code in the first place.
But if you succeed, you can take away a lot of good Lua knowledge. So consider this project wisely.
So anyway, here are the functions that color text in the default edit program:
local function tryWrite( sLine, regex, colour ) local match = string.match( sLine, regex ) if match then if type(colour) == "number" then term.setTextColour( colour ) else term.setTextColour( colour(match) ) end term.write( match ) term.setTextColour( textColour ) return string.sub( sLine, string.len(match) + 1 ) end return nil end local function writeHighlighted( sLine ) while string.len(sLine) > 0 do sLine = tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or tryWrite( sLine, "^%-%-.*", commentColour ) or tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or tryWrite( sLine, "^[%w_]+", function( match ) if tKeywords[ match ] then return keywordColour end return textColour end ) or tryWrite( sLine, "^[^%w_]", textColour ) end endIt isn't simple by any stretch of the imagination.
The thing that makes it work is patterns ( These things: "^%-%-%[%[.-%]%]" )
Read about them here: http://www.lua.org/pil/20.2.html
They are very powerful, but can be confusing at first.
Basically, the 'trywrite' function looks through some text for a specific pattern.
For example, the first call in 'writeHighlighted' is this:
tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour )This specific pattern looks for comments ( You know, the two dashes ), and prints them to the screen in the 'commentColour'
'writeHighlighted' continues to try to find things that should be highlighting, like "strings", or a keyword
(keywords are defined above, I haven't included that table)
Each time 'tryWrite' finds something to color, it removes it from the line buffer (sLine)
I hope you took the time to read this, and that it helped
Note that I am not an expert with patterns, most of these are gibberish to me too
Edited by Lightning, 19 July 2015 - 07:25 PM.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











