dongzhangnong2063 2017-04-11 09:04
浏览 161
已采纳

如何在Golang中将终端输入始终保持在底部?

I am trying to create a program which will have live updates from some data source. And I also want to wait for user input just like a normal terminal. Right now, whenever there is update, I will print the content and print the prompt message for input again which create something like this:

Enter command > 
This is a live update message

Enter command > 
This is a multi-line li......
......ve update message

Enter command > quit
Bye bye!

The problem is that for every live message I received, I will print it and the "Enter command >" will be displayed again again and again, which is not desired. I want the live update to be update on the main part of the terminal, while the "Enter command >" always stay at the bottom

The closest package I can found on Github is https://github.com/gizak/termui but most of the examples inside is trying to display text, gauge and graphs. So I am not quite sure how to get started.

Is there any package or example of the termui package to achieve this? Thank you.

  • 写回答

1条回答 默认 最新

  • douwulu2576 2017-04-11 12:29
    关注

    With github.com/gizak/termui you're heading in the correct direction.

    To understand why you can't get that

    I want the live update to be update on the main part of the terminal, while the "Enter command >" always stay at the bottom

    part sorted out, a little excursion to the history of computing is due. ;-)

    The thing is, the mode your teminal emulator¹ works by default originated in how computers would communicate to the operator in the era which predated alphanumeric displays — they would print their responses using a line printer. Now think of it: a line printer works like this: it prints whatever is sent to it on a roll of paper. What was output, was output. The new output always appears physically below the older.

    When alphanumeric displays (screens) came into existence they naturally continued to support this mode: the line text to be output was rendered at the bottom of the screen with the text above it scrolled upwards. That's what you see in your typical terminal emulator all the time when you're working in the command line of a shell (such as bash) running by the emulator window. This, default, work mode of a terminal is called "canonical" or "cooked".

    Then came more advanced displays, for which it was possible to change individual positions on the screen — identified by their column and row numbers. This changed the paradigm of how the information was output: the concept of a so-called "full-screen application" was born. Typical examples of them are text editors such as Vim and Emacs.

    To support full-screen text output, terminals (and terminal emulators) were adapted by implementing certain extensions to their protocols.

    A full-screen application first requests the terminal to switch into another mode called "raw", in which the terminal sends most of what is input by the user directly to the program running on the terminal. The program handles this input and orders the terminal where and what to draw. You can read this good summary of the distinction between the both modes.

    As you are supposedly suspecting by now, to be able to keep some block of information at a certain fixed place of the terminal's text screen, you want your program to be a full-screen program and use the terminal's raw mode and its special commands allowing you to directly modify text at certain character cells.

    Now the problem is that different terminals (and terminal emulators) have different commands to do that, so there exist libraries to isolate the programs from these gory details. They rely on the special "terminal information databases" to figure out what capabilities a terminal has and how to make it do what the program asks. See man terminfo for more background.

    The most widely known such library (written in C) is called ncurses, and there exist native solutions for Go with supposedly the most visible one being github.com/nsf/termbox-go.

    The github.com/gizak/termui makes use of termbox-go but for you it might suffice to use the latter directly.


    ¹ Chances are very high you're not sitting at a real hardware terminal connected to a UNIX® machine but are rather working in a GUI application such as GNOME Terminal or xterm or Termial.app etc. These are not "terminals" per se but are rather terminal emulators — that is, pieces of software emulating a hardware terminal.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模——参会安排怎么做
  • ¥15 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据