Posted 06 June 2016 - 01:45 AM
You all know CC > edit (program) has colors assigned to the text of different "KeyWords", so I wonder if I would be able to do some kinds of same function?
—————————————————————————————————————
Program: Edit
NOTE: In edit program you are still in the "console" but it has been assigned functions to act like the editor kind of, if I am right so.
This will of course not work for read() cause of it is already "inside" a function and returning string when enterKey down, which means nothing can be assigned to it WHILE writing?
Sorry if I am wrong, but that is what the question is about, cause I have no idea how to do this.. Any ideas?
—————————————————————————————————————
Program: Edit
Color functions
------------------------------------
-- Colours
local highlightColour, keywordColour, commentColour, textColour, bgColour
if term.isColour() then
bgColour = colours.black
textColour = colours.lime
highlightColour = colours.blue
keywordColour = colours.blue
commentColour = colours.white
stringColour = colours.red
hiddenOSColour = colours.pink
else
bgColour = colours.black
textColour = colours.orange
highlightColour = colours.white
keywordColour = colours.white
commentColour = colours.white
stringColour = colours.white
hiddenOSColour = colours.purple
end
local tKeywords = {
["and"] = true,
["break"] = true,
["do"] = true,
["else"] = true,
["elseif"] = true,
["end"] = true,
["false"] = true,
["for"] = true,
["function"] = true,
["if"] = true,
["in"] = true,
["local"] = true,
["nil"] = true,
["not"] = true,
["or"] = true,
["repeat"] = true,
["return"] = true,
["then"] = true,
["true"] = true,
["until"]= true,
["while"] = true,
}
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
NOTE: In edit program you are still in the "console" but it has been assigned functions to act like the editor kind of, if I am right so.
This will of course not work for read() cause of it is already "inside" a function and returning string when enterKey down, which means nothing can be assigned to it WHILE writing?
Sorry if I am wrong, but that is what the question is about, cause I have no idea how to do this.. Any ideas?