File: progress-bar.go

package info (click to toggle)
kitty 0.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,468 kB
  • sloc: ansic: 84,285; python: 57,992; objc: 5,432; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (63 lines) | stat: -rw-r--r-- 1,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package tui

import (
	"fmt"
	"github.com/kovidgoyal/kitty/tools/cli/markup"
	"strings"
)

var _ = fmt.Print

func RepeatChar(char string, count int) string {
	if count <= 5 {
		return strings.Repeat(char, count)
	}
	return fmt.Sprintf("%s\x1b[%db", char, count-1)
}

func RenderProgressBar(frac float64, width int) string {
	fc := markup.New(true)
	if frac >= 1 {
		return fc.Green(RepeatChar("🬋", width))
	}
	if frac <= 0 {
		return fc.Dim(RepeatChar("🬋", width))
	}
	w := frac * float64(width)
	fl := int(w)
	overhang := w - float64(fl)
	filled := RepeatChar("🬋", fl)
	needs_break := false
	if overhang < 0.2 {
		needs_break = true
	} else if overhang < 0.8 {
		filled += "🬃"
		fl += 1
	} else {
		if fl < width-1 {
			filled += "🬋"
			fl += 1
			needs_break = true
		} else {
			filled += "🬃"
			fl += 1
		}
	}
	ans := fc.Blue(filled)
	unfilled := ""
	ul := 0
	if width > fl && needs_break {
		unfilled = "🬇"
		ul = 1
	}
	filler := width - fl - ul
	if filler > 0 {
		unfilled += RepeatChar("🬋", filler)
	}
	if unfilled != "" {
		ans += fc.Dim(unfilled)
	}
	return ans
}