登录
首页 >  Golang >  Go问答

Goroutine 中的 ANSI 光标移动问题

来源:stackoverflow

时间:2024-04-02 10:39:36 266浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《Goroutine 中的 ANSI 光标移动问题》,聊聊,我们一起来看看吧!

问题内容

背景

受 node 库 listr 的启发,我正在尝试编写一个用于创建终端任务列表的 go 库。

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

问题

存在一个问题,列表的最终打印偶尔会包含额外的空格,从而导致一些空格或重复的行。这里有两个例子——一个正确,一个不正确——都来自完全相同的代码的运行(这里是代码的链接)。

示例

以下是其外观的示例:

(这是正确输出的原始文本输出要点)

这是一个有时看起来像这样的示例:

(这是错误输出的原始文本输出要点)

如果您查看错误版本要点中的第 184 行和第 185 行,会发现有两个空行不在正确版本中。

为什么会发生这种情况以及为什么它只是有时发生?

代码

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

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)
}

我使用此网站作为 ansi 代码的参考。

预先感谢您对发生这种情况的原因提出的任何建议!

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


正确答案


我认为 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 := <-l.printq 案例无法运行。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>