dongmi5015 2016-10-03 20:14
浏览 133

在Go中等待用户输入时如何打印到控制台?

I am writing an interactive application that accepts user inputs in a loop and prints out responses in the background. The reader part of my code looks like:

scanner := bufio.NewReader(os.Stdin)
for {
    fmt.Print(":: ")                // prompt for user to enter stuff
    in,_:=scanner.ReadString('
')
}

However, while I am waiting for the user input, I want to print some asynchronous data I got over the network like so:

>> Some text
>> :: Hi I'm the user and I'm

Now some background data arrives:

>> Some text
>> This data was printed while user was inputting
>> :: Hi I'm the user and I'm

And now the user can finish their input:

>> Some text
>> This data was printed while user was inputting
>> :: Hi I'm the user and I'm entering some text
>> :: 

I'm thinking that the background routine would need to scan stdin for text, erase it somehow, print its own data, and restore the original text. I do not know how to scan text that has not been input via the enter key, or how to clear a line. How do I go about it?

  • 写回答

1条回答 默认 最新

  • dq23171 2016-10-04 09:55
    关注

    To clear a line you can use to return the cursor to the beginning of line and then replace all the characters on the line with whitespace and then issue another to return to the beginning of line again. For example (add more spaces if needed):

    fmt.Printf("                                                        ")
    

    Now you can print your background data on top of the data user had inputted.

    Then how do you reprint the data user had inputted? You can't read it from terminal itself so you have to save it somewhere while it's being input. One way to do this would be to disable input buffering on your terminal so each character you type immediately gets flushed to stdin where you can read it. For example:

    var input []byte
    reader := bufio.NewReader(os.Stdin)
    // disables input buffering
    exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
    // append each character that gets typed to input slice
    for {
            b, err := reader.ReadByte()
            if err != nil {
                    panic(err)
            }
            input = append(input, b)
    }
    

    So when you need to insert background data, first clear the line, then print your background data and finally print the contents of the input variable on the next line.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类