简体   繁体   English

如何使用 Golang 清除终端中的最后一行

[英]How to clear last line in terminal with Golang

I'm building CLI app (on Linux) where user has to choose something and then I have to clear last line.我正在构建 CLI 应用程序(在 Linux 上),用户必须在其中选择一些内容,然后我必须清除最后一行。

I tried several things:我尝试了几件事:

  fmt.Print("\r")
  fmt.Print("\033[1")

And the cursor goes back to beginning of line, but do not clear the text from the last line. cursor 回到行首,但不清除最后一行的文本。

What am I missing here?我在这里错过了什么?

You can use it that way你可以那样使用它

fmt.Printf("\033[1A\033[K")

\033[1A - one line up \033[1A - 一行
\033[K - delete the line \033[K - 删除行

For example例如

fmt.Println("hello")
fmt.Println("world")
fmt.Printf("\033[1A\033[K")

will output only hello as the world will be deleted将 output only hello as the world 将被删除

You can read more about ascii escaping here https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html您可以在此处阅读有关 ascii escaping 的更多信息https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM