4

I am trying to learn TUI programming in Go using the TCell API. It is a simple app that print word "hello". However, when I run the program below, nothing happens. Please tell me what I am doing wrong.

package main
import (
         "fmt"
         "github.com/gdamore/tcell"
         "os"
)

func main() {
        scn, err := tcell.NewScreen()
        if err != nil {
                 fmt.Fprintf(os.Stderr, "%v\n", err)
                 os.Exit(1)
         }
         hhh := []rune("hello")
         scn.SetContent(10, 10, rune(' '), hhh, tcell.StyleDefault)
         scn.Show()
}
phage
  • 584
  • 3
  • 17

1 Answers1

2

The creator of this api (https://github.com/gdamore/tcell.git) provided the solution. Here is his respond:

There are three potential issues.

First, you need to initialize the screen. Call scn.Init() after creating the screen.

The second is that your call to SetContent is misguided. The string you are passing is to accommodate combining characters. Instead you need to call SetContent 5 times (one for each letter of "hello") with a different offset, and the appropriate letter of "hello". You probably want to just pass "" for the 4th argument (the string), since none of this is combining characters..

The third problem is that your program just exits. On most terminals this will cause the reset of the terminal to occur, losing your output. (On xterm, for example, tcell uses the alternate screen buffer by default, which leads to exit causing the contents of that screen to be lost, when it switches back to the primary screen buffer at program termination.) The simplest way to prove this is to add a time.Sleep(time.Second * 10) or similar as the last line of your program.

Here is the modified code:


import (
        "fmt"
        "github.com/gdamore/tcell"
        "github.com/gdamore/tcell/encoding"
        "os"
        "time"
)

func main() {
        encoding.Register()
        scn, err := tcell.NewScreen()
        scn.Init()
        if err != nil {
                fmt.Fprintf(os.Stderr, "%v\n", err)
                os.Exit(1)
        }
        scn.Clear()
        scn.SetContent(10, 10, rune('h'), []rune(""), tcell.StyleDefault)
        scn.Show()
        time.Sleep(time.Second * 2)
}

Hope this help.

phage
  • 584
  • 3
  • 17