duancan1900 2015-09-02 22:36
浏览 122
已采纳

使用Go制作全屏终端应用程序

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.

  • 写回答

2条回答 默认 最新

  • duanlushen8940 2015-09-02 22:39
    关注

    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("\033[2J") //Clear screen
    fmt.Printf("\033[%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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图