go版 命令行进度条

161 阅读1分钟

基础版本

image.png 行更新原理:使用\r每次打印都从头开始

const max = 40
func simple() {
    bar := bytes.Repeat([]byte("-"), max)
    for i := 0; i <= max; i++ {
        if i == max {
                fmt.Printf("\rprogress: %3d%%  [%s]   √", 100, bar)
                return
        }
        bar[i] = '>' // #
        fmt.Printf("\rprogress: %3d%% [%s]", i*100/max, bar)
        time.Sleep(time.Millisecond * 200)
    }
}

加上一点颜色

image.png 颜色库使用的是fatih/color,快速入门可以参考Color --给你五彩斑斓的黑

func colorful() {
    bar := bytes.Repeat([]byte("-"), max)
    for i := 0; i <= max; i++ {
        if i == max {
                color.Green("\rprogress: %3d%%  [%s]   √", 100, bar)
                return
        }
        bar[i] = '>' // #
        fmt.Printf("\r%s%s%s",
                color.YellowString("progress: %3d%% [", i*100/max),
                color.GreenString("%s", bar[:i+1]),
                color.YellowString("%s]", bar[i+1:]),
        )
        time.Sleep(time.Millisecond * 200)
    }
}

前方再加一个转圈效果

image.png 这里的转圈效果使用的是unicode盲文

func bar() {
    bar := bytes.Repeat([]byte("-"), max)
    i := 0
    spin := []rune{0x285f, 0x283f, 0x28bb, 0x28f9, 0x28fc, 0x28f6, 0x28e7, 0x28cf}
    exit := make(chan bool)

    go func() {
        v := 0
        for {
            time.Sleep(time.Millisecond * 200)
            select {
            case <-exit:
                return
            default:
                cur := i
                if cur < max {
                    fmt.Printf("\r %s %s%s%s",
                        color.HiMagentaString("%c", spin[v%len(spin)]),
                        color.YellowString("progress: %3d%%  [", i*100/max),
                        color.GreenString("%s", bar[:i+1]),
                        color.YellowString("%s]", bar[i+1:]),
                    )
                }
                v++
            }
        }
    }()
    for ; i <= max; i++ {
        if i == max {
                exit <- true
                color.Green("\r √  progress: %3d%%  [%s]", 100, bar)
                return
        }
        bar[i] = '>'
        time.Sleep(time.Millisecond * 200)
    }
}

```js