【问题标题】:Issue with ANSI cursor movement in goroutinegoroutine 中的 ANSI 光标移动问题
【发布时间】:2021-10-18 22:23:23
【问题描述】:

背景

我正在尝试编写一个用于创建终端任务列表的 Go 库,灵感来自 Node 库 listr

我的库 golist 在后台 goroutine 中打印任务列表,并使用 ANSI 转义序列更新文本和状态字符。

问题

存在一个问题,即列表的最终打印偶尔会包含额外的空格,从而导致出现一些空格或重复行。这里有两个例子——一个正确,一个不正确——都来自完全相同的代码here's a link to the code)的运行。

示例

这是一个应该是什么样子的示例:

(Here's a gist of the raw text output for the correct output)

下面是它有时看起来的示例:

(Here's a gist of the raw text output for the incorrect output)

如果您查看lines 184 and 185 in the gist of the incorrect version,有两个空白行不在正确的版本中。

为什么会发生这种情况,为什么只会发生有时

代码

我在以下循环中将列表打印到终端:

go func() {
    defer donePrinting() // Tell the Stop function that we're done printing
    ts := l.getTaskStates()
    l.print(ts)
    for {
        select {
        case <-ctx.Done(): // Check if the print loop should stop

            // Perform a final clear and an optional print depending on `ClearOnComplete`
            ts := l.getTaskStates()
            if l.ClearOnComplete {
                l.clear(ts)
                return
            }

            l.clearThenPrint(ts)
            return

        case s := <-l.printQ: // Check if there's a message to print
            fmt.Fprintln(l.Writer, s)

        default: // Otherwise, print the list
            ts := l.getTaskStates()
            l.clearThenPrint(ts)
            l.StatusIndicator.Next()
            time.Sleep(l.Delay)
        }
    }
}()

列表被格式化为字符串,然后打印。以下函数格式化字符串:

// fmtPrint returns the formatted list of messages
// and statuses, using the supplied TaskStates
func (l *List) fmtPrint(ts []*TaskState) string {
    s := make([]string, 0)
    for _, t := range ts {
        s = append(s, l.formatMessage(t))
    }
    return strings.Join(s, "\n")
}

下面的函数构建 ANSI 转义字符串以清除行:

// fmtClear returns a string of ANSI escape characters
// to clear the `n` lines previously printed.
func (l *List) fmtClear(n int) string {
    s := "\033[1A" // Move up a line
    s += "\033[K"  // Clear the line
    s += "\r"      // Move back to the beginning of the line
    return strings.Repeat(s, n)
}

我使用this site 作为 ANSI 代码的参考。


提前感谢您对为什么会发生这种情况的任何建议!

如果有任何其他可以提供帮助的信息,请告诉我。

【问题讨论】:

    标签: go terminal command-line-interface goroutine cursor-position


    【解决方案1】:

    我认为 ANSI 代码只是一个红鲱鱼。我拉下库并尝试在本地运行它,发现以下部分是造成此问题的原因:

     case s := <-l.printQ: // Check if there's a message to print
         fmt.Fprintln(l.Writer, s)
    

    printQ 频道关闭时,有时会运行此案例,即使没有打印任何内容,它似乎也会将光标向下移动。当我在调用 l.printDone 之后移动调用以关闭频道时,这种行为就消失了。

    ...
    // Wait for the print loop to finish                       
    <-l.printDone                      
                                                                       
    if l.printQ != nil {                     
        close(l.printQ)                                    
    } 
    ...
    

    这样可以确保在通道关闭时循环不再运行,因此s := &lt;-l.printQ 的情况无法运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2013-05-14
      • 2023-03-19
      相关资源
      最近更新 更多