Jump to content




Color Specific Word


  • You cannot reply to this topic
5 replies to this topic

#1 PixelFox

  • Members
  • 106 posts

Posted 19 July 2015 - 04:23 PM

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.

#2 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 19 July 2015 - 05:28 PM

Have you looked at the code for the edit program?

#3 PixelFox

  • Members
  • 106 posts

Posted 19 July 2015 - 05:47 PM

View PostHPWebcamAble, on 19 July 2015 - 05:28 PM, said:

Have you looked at the code for the edit program?
Yeah, I still have no idea.

#4 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

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:
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 PixelFox

  • Members
  • 106 posts

Posted 19 July 2015 - 07:19 PM

View PostHPWebcamAble, 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:
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
So In other words, If I do "tryWrite("I like math!", "%s%pmath%s%p", colors.green)" the word math will be green?

Edited by Lightning, 19 July 2015 - 07:25 PM.


#6 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 19 July 2015 - 07:33 PM

View PostLightning, on 19 July 2015 - 07:19 PM, said:

So In other words, If I do "tryWrite("I like math!", "%s%pmath%s%p", colors.green)" the word math will be green?

You'd have to try, like I said, I'm not great with patterns.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users