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

Assign colors to input while 'input=read()'

Started by TechnicalCoding, 05 June 2016 - 11:45 PM
TechnicalCoding #1
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

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?
Emma #2
Posted 06 June 2016 - 02:09 AM
I'm not completely sure what the question is. But from what I can gather, the question you are trying to ask is "How can I perform syntax highlighting?"
The approach I would take is to, as the edit program uses, match strings and then change the color according to what they match as.
Matching strings involves writing patterns. Patterns can be confusing at first, but you get used to them. I will not explain patterns as they are explained much better than I ever could on the linked page. But the basic jist is that patterns allow you to set specific (or not specific) parameters on how to process a string.
The way the edit program does it, is checks if there is a recognized pattern for each of the different keywords/patterns (ex 'end' or '–text' for comments) and colors them accordingly according to which statement matched.

Syntax highlighting is a relatively advanced topic as it involves patterns, I'm sure Bomb Bloke or someone will come and explain this much better, but that is what I have for you.
Bomb Bloke #3
Posted 06 June 2016 - 02:32 AM
The "edit" script, unlike the Lua / shell prompts, doesn't use the read() function at all. It manually pulls and acts on the events associated with user input (within the loop here), meaning it can change the content of the screen whenever it likes.
TechnicalCoding #4
Posted 06 June 2016 - 02:46 AM
Here is my try to get multi colors if it helps.. :


local HiddenOSColor = colors.yellow
local spacerColor = colors.cyan
local HiddenOSAdminColor = colors.red
local textColor = colors.white
local HiddenOSCommandColor = colors.purple
HiddenOSKeyWords = {
["Hidden"]=true,
["OS"]=true,
["Developer"]=true,
--[""]=true,
}
spacerKeyWords = {
[">"]=true,
}
HiddenOSAdminKeyWords = {
["Admin"]=true,
["New"]=true,
["Delete"]=true,
}
HiddenOSCommandKeyWords = {
["edit"]=true,
["help"]=true,
["version"]=true,
["rebootAPI"]=true,
["cls"]=true,
["clear"]=true,
["logout"]=true,
["relog"]=true,
["Exit"]=true,
--[""]=true,
}
regex = HiddenOSKeyWords, spacerKeyWords, HiddenOSAdminKeyWords, HiddenOSCommandKeyWords
cmd = read()
local function tryWrite( cmd, regex, color )
local match = string.match( cmd, regex )
if match then
  if type(color) == "number" then
   term.setTextColor( color )
  else
   term.setTextColor( color(match) )
  end
  term.write( match )
  term.setTextColor( textColor )
  return string.sub( cmd, string.len(match) + 1 )
end
return nil
end
local function writeHighlighted( cmd )
while string.len(cmd) > 0 do
  cmd =
   tryWrite( cmd, "^[%w_]+", function( match )
    if HiddenOSKeyWords[ match ] then
	 return HiddenOsColor
    end
    if spacerKeyWords[ match ] then
	 return spacerColor
    end
    if HiddenOSAdminKeyWords[ match ] then
	 return HiddenOSAdminColor
    end
    if HiddenOSCommandKeyWords[ match ] then
	 return HiddenOSCommandColor
    end
    return textColor
   end ) or
   tryWrite( cmd, "^[^%w_]", textColor )
end
end
writeHighlighted(cmd)
print(cmd)