lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


My opinion is that such tweak is ill-designed. Reading a property should have not the effect of changing it. Here the prompt should be incremented only when a new input prompt is created, not when it is read (possibly multiple times: imagine a screen refresh with no other input submitted). The incrementation should only be done after entering something on the console, just before processing the input when an Enter key is pressed or a submit button is clicked.
The prompt then should only read the current line number and not change it at all. It's up to the console input processor to increment the line number and then read again the PROMPT property to recompute its value (and refresh any caching variable).
The assumption made here (by the compiler) is that this is a read-only property and it is cachable. Lua does not have the concept of "volatile" to enforce the rereading, and even if it had it, this would result in spurious incrementations. So manage the line number elsewhere, on the I/O interface and let your _PROMPT read the line number., but beware of caching its results in local variables whose value is kept inside a loop.
Note that your custom prompt could display other status as well (e.g. CPU load, memory usage, number of non-blocked threads, security tokens or user name on a system, or local system name, or local time)...

Le jeu. 22 oct. 2020 à 06:07, Александр Машин <traditio.ru@gmail.com> a écrit :
Dear all,

Why doesn't interactive Lua takes into account the __tostring metamethod
of the non-string _PROMPT global when it prints the prompt, so that the
below code does not help to increment the prompt on any typed command,
although on any >=_PROMPT the variable is incremented?

|>_PROMPT = {no = 0}; setmetatable (_PROMPT, {__tostring = function
(self) self.no = self.no + 1; return tostring (self.no) .. ' >' end})|

https://stackoverflow.com/a/64474830/6632736


Alexander Mashin