text-io icon indicating copy to clipboard operation
text-io copied to clipboard

Add ability to print from another thread

Open asm0dey opened this issue 8 years ago • 3 comments

My usecase: By default I'm waiting for some input from user. But sometimes my program's background thread needs to print some output. If I do this — this outputs is being inserted as answer into console and, of course, it's not correct behaviour. Minimalistic example:

        fun main(args: Array<String>) {
            thread {
                Thread.sleep(3000)
                textio.textTerminal.println("Some text here")
                Thread.sleep(1000)
                textio.textTerminal.println("Some text here")
            }
            textio
                    .newStringInputReader()
                    .withMaxLength(2)
                    .read("some input")
        }

Is it possible to make terminal redraw question if another thread is trying to print something?

asm0dey avatar Jun 24 '17 12:06 asm0dey

I think this would be a nice feature. I'm currently working on another project, which will keep me busy for several months. Therefore, I put the label "up-for-grabs" on this issue, to indicate that this feature has been specifically chosen to be implemented by contributors.

Some guidelines for contributors:

  • Implementing this feature is not a trivial task, because Text-IO has not been designed to be thread-safe.

  • In order to avoid intermingling the messages printed by different threads, TextTerminal's print methods should not be allowed to run in parallel.

  • When a print method runs in parallel with a read method, an ideal implementation will allow specifying the desired behavior:

    1. (as requested by @asm0dey) Print the message and redraw the question. Take care to restore the properties that were in use by the initial call of the read method. Also take care of the partial input available at the moment when the read method has been interrupted.
    2. Postpone the print operation until after the end of the read operation.

    (It is OK to send a PR implementing only one of these two approaches, leaving the other one open for a future contribution.)

In order to prevent that two people work on the same thing, leave a comment here to claim this issue before starting to work on it.

siordache avatar Jul 09 '17 16:07 siordache

Any plans on introducing it? This library is pretty useless without this feature.

ghandhikus avatar Jun 05 '20 00:06 ghandhikus

Sorry, currently I have no time to implement this feature and no one stepped in to do this. Would you be interested perhaps in sending a pull request for implementing this feature?

siordache avatar Jun 08 '20 10:06 siordache