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

Can't use read() with shell.run()

Started by austinv11, 24 December 2013 - 02:02 PM
austinv11 #1
Posted 24 December 2013 - 03:02 PM

local i = read()
if i == "AML help" then -- demonstrates usages
term.setTextColor(colors.red)
print("Usages: AML help <list or name> ")
term.setTextColor(colors.white)
os.run({},"internal/Anvil_Shell")
else
shell.run(i)
os.reboot()
end
Any custom 'commands' work, but doing shell.run(i) doesn't. I want to do this so that if i didn't specifically code a command, that it goes through the default, CraftOS shell.
Thanks in advance
Lyqyd #2
Posted 24 December 2013 - 03:10 PM
Is this in an API or a program?
austinv11 #3
Posted 24 December 2013 - 03:12 PM
Is this in an API or a program?
program
Lyqyd #4
Posted 24 December 2013 - 03:14 PM
Then it should be fine. Is this the whole program? What kinds of test strings don't work, and what do they do instead of "working"?
austinv11 #5
Posted 24 December 2013 - 03:20 PM

local author = "austinv11"
globalVersion = "0.1.0PR1"
function checkInstallation()
local check = fs.exists("internal")
if check == false then
  return false
  end
end

function sp() --internal system profiler
local ver = "0.1"
term.clear()
local text = string.len("Sytstem Profiler V."..ver)
local x,y = term.getSize()
local l = term.setCursorPos(math.ceil((x / 2) - ( text/ 2)), y)
term.setCursorPos(1,2)
local over = os.version()
if turtle then
  local f = turtle.getFuelLevel()
  print("OS ="..over)
  print("Fuel Level: "..f)
else
  print("OS ="..over)
  end
print("Anvil V."..globalVersion)
local id = os.getComputerID()
print("Computer ID = "..id)
local ll = os.getComputerLabel()
if ll == nil then
  ll = "nil"
  end
print("Computer Label = "..ll)
local internal = os.clock()
print("Operating Time  = "..internal)
local internal = os.time()
print("Current Time = "..internal)
local internal = os.day()
print("Current Day = "..internal)
--[[local internal = disk.isPresent()  CURRENTLY BROKEN
local n = diskgetID()
if internal == true then
  print("Disk: True ("..n..")")
else
  print("Disk: False")
  end ]]--
local check = fs.getFreeSpace("/")
print("Remaining Space: "..check.." bytes")
local check = term.isColor()
if check == true then
  check = "True"
else
  check = "False"
  end
print("Advanced Computer: "..check)
print("Screen Size; X = "..x.." Y = "..y)
end

function main()
term.setTextColor(colors.yellow)
term.write("> ")
term.setTextColor(colors.white)
local i = read()
if i == "AML help" then -- demonstrates usages
  term.setTextColor(colors.red)
  print("Usages: AML help <list or name> <if list, apis or programs")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help list" then
  term.setTextColor(colors.red)
  print("Usages AML help list <apis or programs>")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help list apis" then
  term.setTextColor(colors.yellow)
  print("Available APIs:")
  print("GridLock, austinLib, loginUtil, ggui, goroutine, redirect, ctrlkeys, GameUtils")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help list programs" then
  term.setTextColor(colors.yellow)
  print("Available Programs:")
  print("system profiler, luaIDE, NPaintPro, lightshot, laserblast")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help GridLock" then
  term.setTextColor(colors.yellow)
  print("A button api by me, austinv11, it offers a unique way for declaring and drawing buttons")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help austinLib" then
  term.setTextColor(colors.yellow)
  print("A general/random api by me, austinv11, it offers random, but useful, functions")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help loginUtil" then
  term.setTextColor(colors.yellow)
  print("A security/login api created by me, austinv11, it offers many useful security functions")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help ggui" then
  term.setTextColor(colors.yellow)
  print("A gui api by GopherAtl, it's a BETA build, and is very 'robust'")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help goroutine" then
  term.setTextColor(colors.yellow)
  print("An enhancement API for coroutines by GopherAtl, replace parallels with this!")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help redirect" then
  term.setTextColor(colors.yellow)
  print("A buffer API by GopherAtl, creates buffer objects which can be used by term.redirect")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help ctrlkeys" then
  term.setTextColor(colors.yellow)
  print("A 'button shortcut' API by GopherAtl, it is a flexible API for commands tha involve CTRL + <any key>")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help GameUtils" then
  term.setTextColor(colors.yellow)
  print("A useful game utility library by NitrogenFingers, use either paint or NPaintPro to import and create objects for games!")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help luaIDE" then
  term.setTextColor(colors.yellow)
  print("luaIDE is an extremely in-depth and well-done code editor by GravityScore")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help NPaintPro" then
  term.setTextColor(colors.yellow)
  print("The original (and better) paint program, by NitrogenFingers, export these files to make games (through GameUtils)")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help lightshot" then
  term.setTextColor(colors.yellow)
  print("The fantastic screen recorder (yup, you read that right) by GravityScore")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "AML help laserblast" then
  term.setTextColor(colors.yellow)
  print("The cool game by NitrogenFingers")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "luaIDE" then
  os.run({}, "internal/luaIDE")
  os.reboot()
elseif i == "NPaintPro" then
  os.run({}, "internal/NPaintPro")
  os.reboot()
elseif i == "lightshot" then
  os.run({}, "internal/lightshot")
  os.reboot()
elseif i == "laserblast" then
  os.run({}, "internal/laserblast")
  os.reboot()
elseif i == "AML help system profiler" then
  term.setTextColor(colors.yellow)
  print("Shows general system info")
  term.setTextColor(colors.white)
  os.run({},"internal/Anvil_Shell")
elseif i == "system profiler" then
  sp()
  os.run({},"internal/Anvil_Shell")
else
  shell.run(i)
  os.reboot()
  end
end

local c = checkInstallation()
if c == false then
print("Anvil not correctly installed!")
else
main()
end
if i were to write an input like "system profiler" which is recognized as a "command", it runs, or "NPaintPro", but anything else like, "edit <name>" (where it should go to the shell.run(i) line, it says, "attempt to index ? (a nil value)"
Edited on 24 December 2013 - 02:22 PM
MKlegoman357 #6
Posted 24 December 2013 - 04:20 PM
Your problem is that you are re-running your custom shell after each command. IIRC you should get a java error after entering 256 commands into the shell. shell.run line errors because you re-run your shell using os.run, passing an empty environment. shell is never loaded with os.loadAPI. It means that when your enter one command into your shell, shell API becomes unavailable for your program. You should really use an infinite loop.
Edited on 24 December 2013 - 03:20 PM
austinv11 #7
Posted 24 December 2013 - 04:24 PM
Thanks, can't believe i didn't think of that!