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

如何在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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵