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

Archived

This topic is now archived and is closed to further replies.

  • 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.

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

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

 

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

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