登录
首页 >  Golang >  Go问答

使用 Golang 的克隆命令时,如何在终端中实时显示动画微调器来展示克隆 git 存储库的进度?

来源:stackoverflow

时间:2024-02-06 21:39:23 415浏览 收藏

大家好,我们又见面了啊~本文《使用 Golang 的克隆命令时,如何在终端中实时显示动画微调器来展示克隆 git 存储库的进度?》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~

问题内容

我正在创建一个项目管理 cli 工具来帮助我组织项目并格式化它们。我正在尝试使用 go-git 将远程存储库(将来我计划从配置文件中获取它,但现在我对存储库进行硬编码)克隆到指定目录。在克隆此存储库时,我希望能够在上面显示一个动画微调器,并在其下面显示克隆函数的输出。一个简单的例子来说明我希望它的外观:

⠸ cloning remote
enumerating objects: 13122, done.
counting objects: 100% (66/66), done.
compressing objects: 100% (55/55), done.

顶部旋转器部分会不断更新,而底部将是克隆命令的实时输出。我尝试设置微调器的消息,但 git.plainclone 的输出需要 write() 函数。我正在使用 yacspin (github.com/theckman/yacspin) 来制作动画旋转器,但我真的不知道该去哪里。

我是 golang 新手,所以我还不确定该怎么做!感谢您的帮助!

我用来克隆通用存储库的代码:

_, err := git.PlainClone("./clone/", false, &git.CloneOptions{
    URL:      "https://github.com/go-git/go-git",
    Progress: os.Stdout,
})

我尝试创建一个 io.writer 自定义事物,但我不确定如何准确地做到这一点。我找到了一些文章,但没有一篇文章可以帮助我理解这个问题。我尝试实现的解决方案是在开头添加消息,然后每次调用 write 函数时,附加一个换行符,后跟该消息的内容,在 writer 函数中,该消息是一个字节数组。

我是 golang 新手,所以我还不确定该怎么做!感谢您的帮助!


正确答案


我认为您尝试创建 io.writer 的路径是正确的,但事情变得有点混乱,因为 git 输出包含用于移动光标的终端控制代码。您可以稍微检查一下 git 的输出,以查找换行符和控制代码以将光标向上移动一行,然后使用它们来修改您设置微调器显示的消息以包括适当的行数:

// implements io.writer; changes the spinner's message to match whatever
// is sent to write().
type spinnerupdater struct {
    s *yacspin.spinner
    // the base message you want to write next to the spinner.
    base string
    // how many net lines of output from git are there,
    // taking control terminal codes into account?
    lines int
    // raw output from git so far.
    output string 
}

// terminal control code to move the cursor up one line.
const moveup = "\033[1a"

func (su *spinnerupdater) write(b []byte) (int, error) {
    su.output += string(b)

    // add one line for each newline we see, but subtract one
    // for each "move cursor up" control sequence.
    su.lines += strings.count(string(b), "\n")
    su.lines -= strings.count(string(b), moveup)

    // tack on some control codes to the end of the message to move
    // the cursor back up to the starting line because that's where
    // the spinner expects the cursor to be when it outputs an update.
    move := strings.repeat(moveup, su.lines + 1)
    su.s.message(fmt.sprintf("%s\n%s%s", su.base, su.output, move))

    return len(b), nil
}

然后,您可以将其作为 progress 值传递到 git 操作中,更新将写入其中:

func cloneWithSpinner(target, url string) error {
    cfg := yacspin.Config{
        Frequency:     100 * time.Millisecond,
        CharSet:       yacspin.CharSets[59],
        Message: "Cloning repository",
        StopCharacter: "✓",
        StopMessage: "Done cloning repository",
        StopColors:    []string{"fgGreen"},
        StopFailCharacter: "✗",
        StopFailMessage: "Error cloning repository",
        StopFailColors: []string{"fgRed"},
    }
    spinner, err := yacspin.New(cfg)
    if err != nil {
        return err
    }
    if err := spinner.Start(); err != nil {
        return err
    }
    updater := &spinnerUpdater{s: spinner, base: cfg.Message}
    _, err = git.PlainClone(target, false, &git.CloneOptions{
        URL:      url,
        Progress: updater,
    })
    if err != nil {
        spinner.StopFail()
    } else {
        spinner.Stop()
    }
    // Print one more time to get the cursor down to where it's supposed to be.
    fmt.Print(updater.output)
    return err
}

yacspin.config 中的设置可以更改为您想要的任何内容;我只是举一些例子。

请注意,这段代码对 git 操作如何写入输出做出了一个关键假设:它假设它一次只将终端光标向上移动一行。这在实践中似乎成立,但我怀疑 git 代码是否能保证这一点。

如果您想制作一些一般适用于此类任务的东西,您想在旋转器下面编写一些东西,那么您可能最终不得不更广泛地模拟终端行为。

理论要掌握,实操不能落!以上关于《使用 Golang 的克隆命令时,如何在终端中实时显示动画微调器来展示克隆 git 存储库的进度?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

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