892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 05:57 AM
Hello all! I have been editing the shell file for fun (using copy rom/programs/shell shell; edit shell) trying to get it to use a random color every time on an advanced computer. Recently i have come across this error:
[string "shell"]:15: unexpected symbol
when editing the file. Here is the code from lines 1-21. I only edited lines 15-17, but the rest is included just in case.
local parentShell = shell
local bExit = false
local sDir = (parentShell and parentShell.dir()) or ""
local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
local tAliases = (parentShell and parentShell.aliases()) or {}
local tProgramStack = {}
local shell = {}
local tEnv = {
["shell"] = shell,
}
-- Colours
local promptColour, textColour, bgColour
if term.isColour() then
promptColour = colours.2^math.random(0,15) --Edited lines. Also lines with error. XD
textColour = colours.2^math.random(0,15) --Edited lines. Also lines with error. XD
bgColour = colours.2^math.random(0,15) --Edited lines. Also lines with error. XD
else
promptColour = colours.white
textColour = colours.white
bgColour = colours.black
end
Please help! :)/>
Btw, can an admin change title to: [Lua] Error when editing shell file
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 07:22 AM
So I'm guessing it's impossible? :(/>
2088 posts
Location
South Africa
Posted 17 December 2012 - 09:55 AM
Remove the 'colours.' before the '2^math.random(0,15)' and it should work, and then to set colours:
term.setTextColour(promptColour)
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 10:18 AM
That works, but it doesn't switch colors every command.
7508 posts
Location
Australia
Posted 17 December 2012 - 10:49 AM
shell has its own functionality where it sets it to black. so if shell is running at same time it wont change it.
2088 posts
Location
South Africa
Posted 17 December 2012 - 10:53 AM
What do you mean it doesn't switch colours every command?
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 11:10 AM
The text, once you input a command, does not change colors again until rebooted.
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 11:12 AM
]Screenshot:
[attachment=786:2012-12-16_15.12.33.png
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 11:14 AM
shell has its own functionality where it sets it to black. so if shell is running at same time it wont change it.
I am running an edited version of shell, editable by using: "copy rom/programs/shell; edit shell"
2088 posts
Location
South Africa
Posted 17 December 2012 - 11:14 AM
The text, once you input a command, does not change colors again until rebooted.
Yeah thats because you only change it there and not anywhere else? It will need to be in other places too, like a loop. Show us your code and we can show you where to put it
Edit: Oh that's what you mean, hmm. Yeah well then I'm not entirely sure about that, because it only changes the colour once that program is run…
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 11:17 AM
Ok. But its a long one:
Spoiler
local parentShell = shell
local bExit = false
local sDir = (parentShell and parentShell.dir()) or ""
local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
local tAliases = (parentShell and parentShell.aliases()) or {}
local tProgramStack = {}
local shell = {}
local tEnv = {
["shell"] = shell,
}
– Colours
local promptColour, textColour, bgColour
if term.isColour() then
promptColour = colours.2^math.random(0,15) -Edited lines. Also lines with error. XD
textColour = colours.2^math.random(0,15) -Edited lines. Also lines with error. XD
bgColour = colours.2^math.random(0,15) -Edited lines. Also lines with error. XD
else
promptColour = colours.white
textColour = colours.white
bgColour = colours.black
end
local function run( _sCommand, … )
local sPath = shell.resolveProgram( _sCommand )
if sPath ~= nil then
tProgramStack[#tProgramStack + 1] = sPath
local result = os.run( tEnv, sPath, … )
tProgramStack[#tProgramStack] = nil
return result
else
printError( "No such program" )
return false
end
end
local function runLine( _sLine )
local tWords = {}
for match in string.gmatch( _sLine, "[^ \t]+" ) do
table.insert( tWords, match )
end
local sCommand = tWords[1]
if sCommand then
return run( sCommand, unpack( tWords, 2 ) )
end
return false
end
– Install shell API
function shell.run( … )
return runLine( table.concat( { … }, " " ) )
end
function shell.exit()
bExit = true
end
function shell.dir()
return sDir
end
function shell.setDir( _sDir )
sDir = _sDir
end
function shell.path()
return sPath
end
function shell.setPath( _sPath )
sPath = _sPath
end
function shell.resolve( _sPath )
local sStartChar = string.sub( _sPath, 1, 1 )
if sStartChar == "/" or sStartChar == "\\" then
return fs.combine( "", _sPath )
else
return fs.combine( sDir, _sPath )
end
end
function shell.resolveProgram( _sCommand )
– Substitute aliases firsts
if tAliases[ _sCommand ] ~= nil then
_sCommand = tAliases[ _sCommand ]
end
– If the path is a global path, use it directly
local sStartChar = string.sub( _sCommand, 1, 1 )
if sStartChar == "/" or sStartChar == "\\" then
local sPath = fs.combine( "", _sCommand )
if fs.exists( sPath ) and not fs.isDir( sPath ) then
return sPath
end
return nil
end
– Otherwise, look on the path variable
for sPath in string.gmatch(sPath, "[^:]+") do
sPath = fs.combine( shell.resolve( sPath ), _sCommand )
if fs.exists( sPath ) and not fs.isDir( sPath ) then
return sPath
end
end
– Not found
return nil
end
function shell.programs( _bIncludeHidden )
local tItems = {}
– Add programs from the path
for sPath in string.gmatch(sPath, "[^:]+") do
sPath = shell.resolve( sPath )
if fs.isDir( sPath ) then
local tList = fs.list( sPath )
for n,sFile in pairs( tList ) do
if not fs.isDir( fs.combine( sPath, sFile ) ) and
(_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
tItems[ sFile ] = true
end
end
end
end
– Sort and return
local tItemList = {}
for sItem, b in pairs( tItems ) do
table.insert( tItemList, sItem )
end
table.sort( tItemList )
return tItemList
end
function shell.getRunningProgram()
if #tProgramStack > 0 then
return tProgramStack[#tProgramStack]
end
return nil
end
function shell.setAlias( _sCommand, _sProgram )
tAliases[ _sCommand ] = _sProgram
end
function shell.clearAlias( _sCommand )
tAliases[ _sCommand ] = nil
end
function shell.aliases()
– Add aliases
local tCopy = {}
for sAlias, sCommand in pairs( tAliases ) do
tCopy[sAlias] = sCommand
end
return tCopy
end
term.setBackgroundColor( bgColour )
term.setTextColour( promptColour )
print( os.version() )
term.setTextColour( textColour )
– If this is the toplevel shell, run the startup programs
if parentShell == nil then
– Run the startup from the ROM first
local sRomStartup = shell.resolveProgram( "/rom/startup" )
if sRomStartup then
shell.run( sRomStartup )
end
– Then run the user created startup, from the disks or the root
local sUserStartup = shell.resolveProgram( "/startup" )
for n,sSide in pairs( redstone.getSides() ) do
if disk.isPresent( sSide ) and disk.hasData( sSide ) then
local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
if sDiskStartup then
sUserStartup = sDiskStartup
break
end
end
end
if sUserStartup then
shell.run( sUserStartup )
end
end
– Run any programs passed in as arguments
local tArgs = { … }
if #tArgs > 0 then
shell.run( … )
end
– Read commands and execute them
local tCommandHistory = {}
while not bExit do
term.setBackgroundColor( bgColour )
term.setTextColour( promptColour )
write( shell.dir() .. "> " )
term.setTextColour( textColour )
local sLine = read( nil, tCommandHistory )
table.insert( tCommandHistory, sLine )
runLine( sLine )
end
– If this is the toplevel shell, run the shutdown program
if parentShell == nil then
if shell.resolveProgram( "shutdown" ) then
shell.run( "shutdown" )
end
os.shutdown() – just in case
end
Well I got it to work before, but it shows an error on non-advanced computers.
7508 posts
Location
Australia
Posted 17 December 2012 - 11:20 AM
thats maybe bacause you cant call term.setBackgroundColor() or setTextColor() on a non-advanced computer. A similar error would occur in 1.3 computers too.
2088 posts
Location
South Africa
Posted 17 December 2012 - 11:25 AM
thats maybe bacause you cant call term.setBackgroundColor() or setTextColor() on a non-advanced computer. A similar error would occur in 1.3 computers too.
Lol. You can, but only with black and white (text colours) and only black for background.
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 11:26 AM
That's exactly what I mean. I need it to disable the color functions if the computer is not advanced.
7508 posts
Location
Australia
Posted 17 December 2012 - 11:26 AM
thats maybe bacause you cant call term.setBackgroundColor() or setTextColor() on a non-advanced computer. A similar error would occur in 1.3 computers too.
Lol. You can, but only with black and white (text colours) and only black for background.
Last time I tried it it errored out.
7508 posts
Location
Australia
Posted 17 December 2012 - 11:27 AM
That's exactly what I mean. I need it to disable the color functions if the computer is not advanced.
each time you go to use colour functions you could do this
this will allow it to run on CC1.3 and above
if term.isColor and term.isColor() then
-- do colour functions
else
-- do non-colour functions
end
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 11:28 AM
And I'm not that much of a noobish coder, I just know virtually nothing about functions.
And I just released my first program! :D/>
http://www.computercraft.info/forums2/index.php?/topic/7160-fakeconsole-10-an-almost-unnoticeable-fake-console/
892 posts
Location
Where you'd least expect it.
Posted 17 December 2012 - 11:29 AM
Umm, the thing is, shell is so full of functions and variables my head would explode sifting through them. XD