Jump to content




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


  • You cannot reply to this topic
16 replies to this topic

#1 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

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

#2 Zoinky

  • Members
  • 144 posts
  • LocationWellington, New Zealand

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


#3 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 25 January 2013 - 07:47 PM

View PostZoinky, on 25 January 2013 - 07:42 PM, said:

Try using error(). You can also simulate someone pressing CTRL + T by using os.queueEvent("terminate").

Thank you the error() worked.

+1 to you

#4 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

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.

#5 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 25 January 2013 - 07:53 PM

View PostOrwell, on 25 January 2013 - 07:52 PM, said:

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.

#6 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

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' ?

#7 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 25 January 2013 - 08:03 PM

View PostZoinky, on 25 January 2013 - 07:42 PM, said:

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.

#8 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

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?

#9 Zoinky

  • Members
  • 144 posts
  • LocationWellington, New Zealand

Posted 25 January 2013 - 11:11 PM

View PostLBPHacker, on 25 January 2013 - 10:09 PM, said:

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()


#10 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 25 January 2013 - 11:27 PM

View PostZoinky, on 25 January 2013 - 11:11 PM, said:

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


#11 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

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 & mouse version of program for adv computers )

#12 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

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?

#13 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 26 January 2013 - 07:18 PM

View PostAnthonyD98, on 26 January 2013 - 11:46 AM, said:

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.

#14 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

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!

#15 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 27 January 2013 - 03:38 AM

View PostP4isHere., on 27 January 2013 - 02:33 AM, said:

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().

#16 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 27 January 2013 - 07:05 PM

View PostIcanbreathcode(Bye!), on 26 January 2013 - 07:18 PM, said:

View PostAnthonyD98, on 26 January 2013 - 11:46 AM, said:

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

#17 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 28 January 2013 - 01:57 PM

View PostZoinky, on 25 January 2013 - 07:42 PM, said:

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() ?





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users