Glk: Is there any way to overwrite a character already output to a text buffer?

With a text grid, the sky’s the limit, of course.

In a text buffer, you can turn off echo_line_event for the main window and clobber the just-entered command.

But is there some hack to output a backspace, or any other way to replace a character that has already been output? (I don’t think so, but I thought I’d check.)

Some Glk implementations have an unput function which is used for some obscure Z-Machine edge cases, but it’s not exposed such that a Glulx story file could use it. In theory perhaps it could be, but it’s not something that has been needed before.

What are you trying to do?

1 Like

If you only care about a particular stream and you control all the code touching it, you can do it by buffering output yourself, erasing characters from the buffer before they ever hit the stream.

If you don’t control the code that writes to the stream, you can kinda fake it with respect to the Glulx stream{char,unichar,str,num} opcodes, using the filter I/O system as featured in the latest bubbling beaker award. Keep a ring buffer / double-ended queue of sufficient capacity and install a filter function that first writes each character to this buffer and only writes the oldest character in the ring buffer to the current Glk stream when the ring buffer is already full. Then you can undo output by erasing it from the buffer before its output.

There’s a catch or two, of course. For starters, it doesn’t do anything about output written directly via Glk functions (I don’t know how common that is). You also have to be careful to flush the buffer whenever the current Glk stream changes, as otherwise output will go to the wrong stream. And any buffering of output means you need to flush the buffer at various times: before the underlying stream is closed, before something wants to read from a file stream that was just written to, and before the user is prompted for input.

Overall, not a very practical approach, just barely good enough to technically answer the question :nerd_face:

2 Likes

The underlying rationale here is that once a character is printed to a buffer, the interpreter can ship it out. It might compiled into part of an HTML DOM element, or shoved out over a telnet link to Floyd on IFMud, or saved to a transcript file. Printed on a line printer even.

So reversing that action is not, in general, meaningful. It would run into too many implementation details.

(The interpreter might batch buffer output and ship it all at once, at the end of the turn. But this isn’t a requirement or visible to the game at all.)

(The “unput” feature is something that only exists in specific implementations that are part of Z-machine interpreters. I am very happy to leave that in Dannii’s hands. :)

2 Likes

Trying to learn Glk’s boundaries. :grin:

I was thinking it’d be nice to have a “press a key to continue” message that didn’t remain on the screen (without requiring clearing the screen). I can accomplish it with another window, though.

This is cool, but I was specifically hoping to change something the user has already seen.

Makes sense.

I finally realized the ambiguity in what I’d asked: by “text buffer” I intended, specifically, a Glk text buffer window.

1 Like