Text inputstream: Allow obtaining cursor position

This commit is contained in:
Lars Mueller 2022-07-14 15:51:36 +02:00
parent 7eb5f09752
commit 234f6d1ecd

@ -62,13 +62,17 @@ function trim_spacing(text)
end end
local inputstream_metatable = { local inputstream_metatable = {
__index = {read = function(self, count) __index = {
read = function(self, count)
local cursor = self.cursor + 1 local cursor = self.cursor + 1
self.cursor = self.cursor + count self.cursor = self.cursor + count
local text = self.text:sub(cursor, self.cursor) local text = self.text:sub(cursor, self.cursor)
return text ~= "" and text or nil return text ~= "" and text or nil
end} end,
seek = function(self) return self.cursor end
} }
}
--> inputstream "handle"; only allows reading characters (given a count), seeking does not accept any arguments
function inputstream(text) function inputstream(text)
return setmetatable({text = text, cursor = 0}, inputstream_metatable) return setmetatable({text = text, cursor = 0}, inputstream_metatable)
end end