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

如何在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 python动态规划:N根火柴摆出的最大数字
  • ¥20 (标签-excel)
  • ¥200 求idea10和MyEclipse7.1
  • ¥20 vb6.0截取当前窗体保存为jpg文件
  • ¥20 苹果手机不使用大疆sdk怎么获取遥控器控制信息或如何接入大疆sdk并且成功上架sdk
  • ¥20 woocommerce 注册按键重定向
  • ¥100 求书法图像文字切割代码
  • ¥15 同一个波形探测距离不同的目标,为什么fft之后得到的频谱图会发生移动,不应该时移不改变幅度谱吗(标签-matlab)(相关搜索:matlab仿真)
  • ¥15 Proteus仿真程序只能执行一次
  • ¥15 语音识别websocket报错