From 234f6d1ecd22e5f89f5770dfff3b24ca643fc5f0 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Thu, 14 Jul 2022 15:51:36 +0200 Subject: [PATCH] Text inputstream: Allow obtaining cursor position --- text.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/text.lua b/text.lua index a7e67a8..5574c0f 100644 --- a/text.lua +++ b/text.lua @@ -62,13 +62,17 @@ function trim_spacing(text) end local inputstream_metatable = { - __index = {read = function(self, count) - local cursor = self.cursor + 1 - self.cursor = self.cursor + count - local text = self.text:sub(cursor, self.cursor) - return text ~= "" and text or nil - end} + __index = { + read = function(self, count) + local cursor = self.cursor + 1 + self.cursor = self.cursor + count + local text = self.text:sub(cursor, self.cursor) + return text ~= "" and text or nil + 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) return setmetatable({text = text, cursor = 0}, inputstream_metatable) end