Last active
December 11, 2022 18:33
-
-
Save truemedian/775d0a4447d25ec772c793ae2e456b86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local uv = require 'uv' | |
local buffer = "" | |
local waiting = {} | |
process.stdin:on("data", function(data) | |
if not data then return end | |
buffer = buffer .. data | |
local n = string.find(buffer, '\n', 1, true) | |
while n and waiting[1] do | |
local data = string.sub(buffer, 1, n - 1) | |
buffer = buffer:sub(n + 1) | |
assert(coroutine.resume(table.remove(waiting, 1), data) | |
n = string.find(buffer, '\n', 1, true) | |
end | |
if not waiting[1] then | |
process.stdin:pause() | |
end | |
end) | |
return function(prompt) | |
if prompt then | |
process.stdout:write(prompt) | |
end | |
local n = string.find(buffer, '\n', 1, true) | |
if n then | |
local data = string.sub(buffer, 1, n - 1) | |
buffer = buffer:sub(n + 1) | |
return data | |
end | |
process.stdin:resume() | |
table.insert(waiting, coroutine.running()) | |
return coroutine.yield() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment