This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PixelFox's profile picture

Color Specific Word

Started by PixelFox, 19 July 2015 - 03:23 PM
PixelFox #1
Posted 19 July 2015 - 05: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.
HPWebcamAble #2
Posted 19 July 2015 - 06:28 PM
Have you looked at the code for the edit program?
PixelFox #3
Posted 19 July 2015 - 06:47 PM
Have you looked at the code for the edit program?
Yeah, I still have no idea.
HPWebcamAble #4
Posted 19 July 2015 - 08: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
PixelFox #5
Posted 19 July 2015 - 08:19 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
So In other words, If I do "tryWrite("I like math!", "%s%pmath%s%p", colors.green)" the word math will be green?
Edited on 19 July 2015 - 06:25 PM
HPWebcamAble #6
Posted 19 July 2015 - 08:33 PM
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.