Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Elijahlorden

Get user input without pausing *everything*

Question

I am attempting to write my own operating system.  So far everything is going perfectly fine, except for this extremely annoying issue I am having.  (I am not writing my own core libraries, I copied those from the OpenOS files on GitHub)  For my Shell, I would like tasks running in the background to be able to print to the screen without messing up the user input prompt.  My attempt at doing this:

local Shell = {}

Shell.ShellBacklog = {{Type = "print", Text = "- Init Shell -"}} -- {Type = "print", Text = " "}

Shell.promptOpen = false

Shell.prompt = ">>> "
Shell.history = {}

Shell.step = function()
	if (#Shell.ShellBacklog > 0) and (not Shell.promptOpen) then
		repeat
			local p = Shell.ShellBacklog[1]
			if (p.Type == "print") then
				print(p.Text)
			end
			table.remove(Shell.ShellBacklog, 1)
		until (#Shell.ShellBacklog == 0) or (Shell.promptOpen)
	end
end

Shell.keyDownEventListener = function(event, address, key, code, plr)
	if (Shell.promptOpen) then return end
	Shell.promptOpen = true
	term.write(Shell.prompt)
	local strIn = term.read(Shell.history):gsub("\n", "")
	table.insert(Shell.ShellBacklog, {Type = "print", Text = strIn})
	Shell.promptOpen = false
end

Shell.start = function()
	event.listen("key_down", Shell.keyDownEventListener)
end

Shell.print = function(...)
	table.insert(Shell.ShellBacklog, {Type = "print", Text = ...})
end

The step function is called in the main loop.  My issue is that term.read() pauses everything, including the main loop.  Is there any way around this?  I attempted to use coroutines, but could not get them to function correctly.

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
  • Solution

Nevermind!  It appears that other Lua variants handle concurrent tasks differently than OC Lua.  For anybody who is interested, you can use the following to run a function repeatedly even while event.pull() is holding up the thread:

event.timer(0.05, function() print("stuff") , math.huge)
  --interval between runs, callback function, number of times to be run

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.