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

[Solved] What is the code to exit a program and return to the shell?

Started by AnthonyD98™, 25 January 2013 - 06:31 PM
AnthonyD98™ #1
Posted 25 January 2013 - 07:31 PM
Hello, I have a question.

What is the code to exit a program and return to the shell

I tried using shell.exit() - Nothing happens
I tried os.pullEvent("terminate") - The terminal froze

I am currently working on a GUI / UI / OS - Or whatever you want to call it :)/>

You can find my program here: - Its still in development
http://pastebin.com/4rAzgmCz

If you scroll down to line 104 you will see were I put the os.pullEvent("terminate")

Thanks, AnthonyD98
Zoinky #2
Posted 25 January 2013 - 07:42 PM
Try using error(). You can also simulate someone pressing CTRL + T by using os.queueEvent("terminate").

EDIT: Also, most of the code in your menu isn't necessary. Try using something like this to draw your menu:


local s = 1
local menuOptions = {"Programs", "Options", "Exit", "Reboot", "Shutdown"}

for i = 1, #menuOptions do
 if s == i then
  print("[>"..menuOptions[i].."<]")
 else
  print("[ "..menuOptions[i].." ]")
 end
end
AnthonyD98™ #3
Posted 25 January 2013 - 07:47 PM
Try using error(). You can also simulate someone pressing CTRL + T by using os.queueEvent("terminate").

Thank you the error() worked.

+1 to you
Orwell #4
Posted 25 January 2013 - 07:52 PM
A proper way to exit out of the body of the program is simply using "return". But if you have a complex structure, error() would indeed do just fine. It could only interfere with some wrappers for your program in very specific (and rare) circumstances.
AnthonyD98™ #5
Posted 25 January 2013 - 07:53 PM
A proper way to exit out of the body of the program is simply using "return". But if you have a complex structure, error() would indeed do just fine. It could only interfere with some wrappers for your program in very specific (and rare) circumstances.

I will keep this in mind, thank you.
AnthonyD98™ #6
Posted 25 January 2013 - 07:59 PM
I know its unrelated to this question but do you guys know how I could set the default selection to 'programs' instead of 'shutdown' ?
AnthonyD98™ #7
Posted 25 January 2013 - 08:03 PM
Try using error(). You can also simulate someone pressing CTRL + T by using os.queueEvent("terminate").

EDIT: Also, most of the code in your menu isn't necessary. Try using something like this to draw your menu:


local s = 1
local menuOptions = {"Programs", "Options", "Exit", "Reboot", "Shutdown"}

for i = 1, #menuOptions do
if s == i then
  print("[>"..menuOptions[i].."<]")
else
  print("[ "..menuOptions[i].." ]")
end
end

I would prefer to use the method I am currently using as that is what I am used to.
I really don't mind writing extra lines of code.
LBPHacker #8
Posted 25 January 2013 - 10:09 PM
The problem is you are running selected() a dozen times before any key can affect "s". And "s" is 5. So



local w,h = term.getSize()
local s = 5 -- should be 1
local mainY = 5
local titleY = 1
local maintitleY = 3

Why is it 5 now anyways?
Zoinky #9
Posted 25 January 2013 - 11:11 PM
The problem is you are running selected() a dozen times before any key can affect "s". And "s" is 5. So



local w,h = term.getSize()
local s = 5 -- should be 1
local mainY = 5
local titleY = 1
local maintitleY = 3

Why is it 5 now anyways?
This snippet could also be a problem (Part of the main while loop):


elseif s == 5 then
 os.shutdown()
LBPHacker #10
Posted 25 January 2013 - 11:27 PM
This snippet could also be a problem (Part of the main while loop):


elseif s == 5 then
os.shutdown()

Yup, it shuts down the computer when you move the selection over Shutdown instantly.

BTW
Elseif counts as a block ending
I mean this is much more readable

if key == keys.down then
	s = s+1
	if s >= 5 then s = 5 end
	selected()
elseif key == keys.up then
	s = s-1
	if s <= 1 then s = 1 end
	selected()
elseif key == keys.enter then
	if s == 1 then
		os.run("prog")
		selected()
	elseif s == 2 then
		os.run("options")
		selected()
	elseif s == 3 then
		os.pullEvent("terminate")
		selected()
	elseif s == 4 then
		os.reboot()
		selected()
	elseif s == 5 then
		os.shutdown()
	end
end
AnthonyD98™ #11
Posted 26 January 2013 - 11:36 AM
Okay i've made some changes to my code:
You can find the latest version here:
http://pastebin.com/qLwwRbtn

And 'local s = 5' is now 'local s = 1'

I appreciate all the help you guys are giving me.

I am now making the suggested changes to my code. :)/>
Also I am looking for someone to help me in the process of making my program mouse based aswell as keyboard based - e.g. ( keyboard version of program for non-adv computers &amp; mouse version of program for adv computers )
AnthonyD98™ #12
Posted 26 January 2013 - 11:46 AM
I have Implemented LBPHacker's more 'readable' version of my code into the program which saves alot of space.
Also there are now colours in the menu

SelectGUI Build #006:
http://pastebin.com/z19FzBbD

I have a new question now:

Is anybody willing to help develop SelectGUI further?
Geforce Fan #13
Posted 26 January 2013 - 07:18 PM
I have Implemented LBPHacker's more 'readable' version of my code into the program which saves alot of space.
Also there are now colours in the menu

SelectGUI Build #006:
http://pastebin.com/z19FzBbD

I have a new question now:

Is anybody willing to help develop SelectGUI further?
SURE
*combines with iHomeOS*
Your program is crap now!
LOL, just kidding.



Also, instead of doing
"shell.run("prog") ;
Copy all your code in prog, and at the begining of the app, do

function prog()
(paste prog's code here)
end
Then, replace shell.run('prog') ; with
"prog()"
This will make it so you only have to upload one file to pastebin.
Zambonie #14
Posted 27 January 2013 - 02:33 AM
You could also use:

shell.run("shutdown")
--or
shell.run("reboot")
--or
shell.run("shell")

1+ me if this worked also!
Orwell #15
Posted 27 January 2013 - 03:38 AM
You could also use:

shell.run("shutdown")
--or
shell.run("reboot")
--or
shell.run("shell")

1+ me if this worked also!
How is that even remotely helpful? :P/> He's already using os.shutdown() and os.reboot().
AnthonyD98™ #16
Posted 27 January 2013 - 07:05 PM
I have Implemented LBPHacker's more 'readable' version of my code into the program which saves alot of space.
Also there are now colours in the menu

SelectGUI Build #006:
http://pastebin.com/z19FzBbD

I have a new question now:

Is anybody willing to help develop SelectGUI further?
SURE
*combines with iHomeOS*
Your program is crap now!
LOL, just kidding.



Also, instead of doing
"shell.run("prog") ;
Copy all your code in prog, and at the begining of the app, do

function prog()
(paste prog's code here)
end
Then, replace shell.run('prog') ; with
"prog()"
This will make it so you only have to upload one file to pastebin.

Thank you, It will make Things ALOT eaiser :)/> +1 to you
AnthonyD98™ #17
Posted 28 January 2013 - 01:57 PM
Try using error(). You can also simulate someone pressing CTRL + T by using os.queueEvent("terminate").

EDIT: Also, most of the code in your menu isn't necessary. Try using something like this to draw your menu:


local s = 1
local menuOptions = {"Programs", "Options", "Exit", "Reboot", "Shutdown"}

for i = 1, #menuOptions do
if s == i then
  print("[>"..menuOptions[i].."<]")
else
  print("[ "..menuOptions[i].." ]")
end
end

I now understand that it is a better idea to use the code above instead of my own bloated code.

Thank you! I am now rewriting my entire OS which will now be much more economic in terms of size.

EDIT: Now why do I need selected() ?