rivo / tview

Terminal UI library with rich, interactive widgets — written in Golang
MIT License
11.17k stars 574 forks source link

Bug in list creation when using Chinese parameter for AddItem #862

Closed nohunt closed 8 months ago

nohunt commented 1 year ago

OS: Windows 11

Description: I modified the list demo in the demos directory by changing the parameter of AddItem from English to Chinese. Here's the modified code:

package main

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

func main() {
    app := tview.NewApplication()
    list := tview.NewList().
        AddItem("列表 1", "Some explanatory text", 'a', nil).
        AddItem("List item 2", "Some explanatory text", 'b', nil).
        AddItem("List item 3", "Some explanatory text", 'c', nil).
        AddItem("List item 4", "Some explanatory text", 'd', nil).
        AddItem("Quit", "Press to exit", 'q', func() {
            app.Stop()
        })

    if err := app.SetRoot(list, true).EnableMouse(true).Run(); err != nil {
        panic(err)
    }
}

After compiling and running the code, the following issue occurs: when selecting a Chinese item and pressing the Enter key, the content disappears, and the arrow keys also don't work properly. The same behavior happens when using the up and down arrow keys.

1 2

nohunt commented 1 year ago

OS: Windows 11

Description: I modified the list demo in the demos directory by changing the parameter of AddItem from English to Chinese. Here's the modified code:

package main

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

func main() {
  app := tview.NewApplication()
  list := tview.NewList().
      AddItem("列表 1", "Some explanatory text", 'a', nil).
      AddItem("List item 2", "Some explanatory text", 'b', nil).
      AddItem("List item 3", "Some explanatory text", 'c', nil).
      AddItem("List item 4", "Some explanatory text", 'd', nil).
      AddItem("Quit", "Press to exit", 'q', func() {
          app.Stop()
      })

  if err := app.SetRoot(list, true).EnableMouse(true).Run(); err != nil {
      panic(err)
  }
}

After compiling and running the code, the following issue occurs: when selecting a Chinese item and pressing the Enter key, the content disappears, and the arrow keys also don't work properly. The same behavior happens when using the up and down arrow keys.

1 2

Apologies, I found a solution to the issue in someone else's reported problem. I added the following code snippet, as mentioned below. However, I'm unsure about the underlying reason for its effectiveness

list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
        app.Sync()
        return event
    })
rivo commented 1 year ago

Isn't this an issue with the Windows terminal? I thought that it cannot display Unicode characters properly. On macOS iTerm 2, it looks like this:

image
nohunt commented 1 year ago

Isn't this an issue with the Windows terminal? I thought that it cannot display Unicode characters properly. On macOS iTerm 2, it looks like this:

I'm not entirely sure if it's an issue with Windows terminal, but after modifying the code, it appears to have resolved the problem. Here's the updated code

package main

import (
    "github.com/gdamore/tcell/v2"
    "github.com/rivo/tview"
)

func main() {
    app := tview.NewApplication()
    list := tview.NewList().
        AddItem("列表 1", "Some explanatory text", 'a', nil).
        AddItem("List item 2", "Some explanatory text", 'b', nil).
        AddItem("List item 3", "Some explanatory text", 'c', nil).
        AddItem("List item 4", "Some explanatory text", 'd', nil).
        AddItem("Quit", "Press to exit", 'q', func() {
            app.Stop()
        })

    list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
        app.Sync()
        return event
    })
    if err := app.SetRoot(list, true).EnableMouse(true).Run(); err != nil {
        panic(err)
    }
}

Dingtalk_20230710104119

YuMao233 commented 1 year ago

Great, it works.

I also encountered the same problem.

Although I think this should be a BUG, but at least there is a solution.

Here comes a short video explaining the bug:

https://github.com/rivo/tview/issues/864

xyaman commented 8 months ago

I have exactly the same problem when using wezterm on Windows 11. Using App.Sync() works, but with every event, it blinks, so it's not ideal.

Bug: https://github.com/rivo/tview/assets/32078353/945c7a4f-9c36-4a0d-9b8c-0a89dc54e38b

With App.Sync()

https://github.com/rivo/tview/assets/32078353/22d5b35d-9e50-4477-bf11-154921618bc2

rivo commented 8 months ago

I ran the code from above (the original comment, without the app.Sync() command) on Windows 11 with no problems. No flickering, no disappearing characters after pressing Enter or otherwise. Here is a screenshot:

image

The flickering on Windows has been resolved a few weeks ago. (Not sure if this was the original problem but I cannot reproduce it as of now.)

I don't have wezterm so I don't know how compatible it is with tcell. It seems to me that this is rather a problem with your terminal's configuration.

In general, I should mention that tview does not deal with terminal configurations or escape characters directly. This all happens on the tcell layer. I would suggest that you create a small program using tcell (not using any tview components, to avoid confusion) which reproduces this and then open an issue there: https://github.com/gdamore/tcell.

So I will close this issue now as I don't see anything that can be solved in this project.