使用Go创建一个全屏终端应用程序

huangapple go评论87阅读模式
英文:

Making a full screen Terminal application with Go

问题

我正在尝试构建一个全屏终端应用程序,我选择使用Go语言。我已经弄清楚了如何从os.Stdin读取输入,但我不清楚如何清除终端窗口并操作光标位置。我还想捕获终端输入,但不要将其打印出来(不回显)。

我的问题是:

  1. 如何有效地使用列/行坐标清除和打印到终端?
  2. 如何阻止终端打印按键输入?

我的意图是:

我想创建一个全屏终端应用程序,它可以渲染自己的用户界面并在内部处理输入(热键/导航等)。

如果有任何涵盖这种用例的库,请随时提供建议。

英文:

I'm trying to build a full screen terminal application. I'm using Go as my language of choice. I've figured out how to read from os.Stdin, but I'm unclear on how to clear the terminal window and manipulate the cursor position. I also want to capture the terminal input without it being printed (echoed back).

My questions are:

  1. How can I effectively clear and print to the terminal with column/row coordinates?
  2. How do I stop the terminal from printing keys pressed

My intent:

I want to create a full screen terminal application that renders it's own UI and handles input internally (hot keys/navigation/etc...).

If there are any libraries that cover this sort of use case please feel free to suggest them.

答案1

得分: 5

清除终端并设置位置的最简单方法是使用 ANSI 转义码。然而,这可能不是最理想的方法,因为终端的差异可能会导致问题。

fmt.Print("3[2J") // 清屏
fmt.Printf("3[%d;%dH", line, col) // 设置光标位置

更好的选择是使用类似 goncursestermbox-go 的库(感谢 Tim Cooper 的评论提供的第二个选择)。

使用这样的库,你可以做如下操作:

import (
    gc "code.google.com/p/goncurses"
)

func main() {
    s, err := gc.Init()
    if err != nil {
        panic(err)
    }
    defer gc.End()
    s.Move(5, 2)
    s.Println("Hello")
    s.GetChar()
}

上面的代码摘自 Rosetta Code

英文:

The easiest way to clear the terminal and set position is via ansi escape codes. However, this may not be the ideal way as variation in terminals may come back to bite you.

fmt.Print("3[2J") //Clear screen
fmt.Printf("3[%d;%dH", line, col) // Set cursor position

A better alternative would be to use a library like goncurses or termbox-go (credit: second is from Tim Cooper's comment).

With such a library you can do things like this:

import (
    gc "code.google.com/p/goncurses"
)

func main() {
    s, err := gc.Init()
    if err != nil {
        panic(err)
    }
    defer gc.End()
    s.Move(5, 2)
    s.Println("Hello")
    s.GetChar()
}

Code above copied from Rosetta Code

答案2

得分: 2

截至2019年12月,我建议使用rivo/tview库。

(@vastlysuperiorman提到的goncurses自2019年6月以来没有更新,termbox-go明确声明为不维护)。

以下是从该项目的README中提取的“hello world”应用程序(为了可读性进行了重新格式化):

package main

import (
	"github.com/rivo/tview"
)

func main() {

	box := tview.NewBox().
        SetBorder(true).
        SetTitle("Hello, world!")

	if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil {
		panic(err)
	}
}

tview提供屏幕截图和示例代码以及标准的godoc参考文档

英文:

As of December 2019, I would recommend using rivo/tview library.

(goncurses mentioned by @vastlysuperiorman has not been updated since June 2019 and termbox-go is explicitly declared unmaintained).

<br/>

Here's the "hello world" app, taken from the project's README (reformatted for readability):

package main

import (
	&quot;github.com/rivo/tview&quot;
)

func main() {

	box := tview.NewBox().
        SetBorder(true).
        SetTitle(&quot;Hello, world!&quot;)

	if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil {
		panic(err)
	}
}

<br/>

tview provides screenshots and example code as well as the standard godoc reference.

答案3

得分: 0

要停止终端打印按键,您可以使用以下代码:

import (
    "fmt"
    "syscall"
    "golang.org/x/crypto/ssh/terminal"
)

func main() {
    fmt.Print("输入值:")
    byteInput, _ := terminal.ReadPassword(int(syscall.Stdin))
    input := string(byteInput)
    fmt.Println() // 用户输入后需要添加一个新行
    fmt.Printf("您的输入是 '%s'", input)
}

请注意,这是一个使用Go语言编写的代码示例。

英文:

To stop the terminal from printing keys pressed you can use the below code:
<pre>
import (
"fmt"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)

func main(){
    fmt.Print(&quot;Enter Value: &quot;)
    byteInput, _ := terminal.ReadPassword(int(syscall.Stdin))
    input:= string(byteInput)
    fmt.Println() // it&#39;s necessary to add a new line after user&#39;s input
    fmt.Printf(&quot;Your input is &#39;%s&#39;&quot;, input)
}

</pre>

huangapple
  • 本文由 发表于 2015年9月3日 06:36:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/32364252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定