Categories
Development

How to copy a line from terminal

Is there a way to copy the current line of terminal characters in Go? I want to replace what the user is typing in the console with the program output. Then I want to paste what he typed on the next line so he can continue what he was typing. The output will look cleaner. Otherwise the program output comes between what he was typing.

Below are parts of two concurrent functions:

for {
    // print username before typing field in consoles
    fmt.Print(username, ": ")
    // read the message from the user
    message, _ := reader.ReadString('\n')

The above allows user to send messages, remaining code is omitted as it is not part of the problem

The below receives the message from some other source

// set message display format
    msgDisplay := "%s : %s"
    // populate the message to the display format
    msgDisplay = fmt.Sprintf(msgDisplay, t.Username, t.Message)

You can deduce that this is similar to chatting, Imagine a user is typing in the send messages part and there was an incoming message from some other source. eg: user “Jojo” was typing “fabrication”, incoming message from “Renu” was “2019” reached before the “Jojo” finished typing, The console is supposed to look

Renu: 2019
Jojo: fabrication

Current behaviour

Jojo: fabr2019

Jojo was in the midst of typing the message and the incoming message interupted with the input visually. If Jojo continued typing, it’ll look

Jojo: fabr2019cation

The program works correctly, the destination receives “fabrication”, but visually it’s not correct

Leave a Reply

Your email address will not be published. Required fields are marked *