bar.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package progress
  2. import (
  3. "fmt"
  4. "math"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/jmorganca/ollama/format"
  9. "golang.org/x/term"
  10. )
  11. type Stats struct {
  12. rate int64
  13. value int64
  14. remaining time.Duration
  15. }
  16. type Bar struct {
  17. message string
  18. messageWidth int
  19. maxValue int64
  20. initialValue int64
  21. currentValue int64
  22. started time.Time
  23. stats Stats
  24. statted time.Time
  25. }
  26. func NewBar(message string, maxValue, initialValue int64) *Bar {
  27. return &Bar{
  28. message: message,
  29. messageWidth: -1,
  30. maxValue: maxValue,
  31. initialValue: initialValue,
  32. currentValue: initialValue,
  33. started: time.Now(),
  34. }
  35. }
  36. func (b *Bar) String() string {
  37. termWidth, _, err := term.GetSize(int(os.Stderr.Fd()))
  38. if err != nil {
  39. termWidth = 80
  40. }
  41. var pre, mid, suf strings.Builder
  42. if b.message != "" {
  43. message := strings.TrimSpace(b.message)
  44. if b.messageWidth > 0 && len(message) > b.messageWidth {
  45. message = message[:b.messageWidth]
  46. }
  47. fmt.Fprintf(&pre, "%s", message)
  48. if b.messageWidth-pre.Len() >= 0 {
  49. pre.WriteString(strings.Repeat(" ", b.messageWidth-pre.Len()))
  50. }
  51. pre.WriteString(" ")
  52. }
  53. fmt.Fprintf(&pre, "%3.0f%% ", math.Floor(b.percent()))
  54. fmt.Fprintf(&suf, "(%s/%s", format.HumanBytes(b.currentValue), format.HumanBytes(b.maxValue))
  55. stats := b.Stats()
  56. rate := int64(stats.rate)
  57. if rate > 0 {
  58. fmt.Fprintf(&suf, ", %s/s", format.HumanBytes(rate))
  59. }
  60. fmt.Fprintf(&suf, ")")
  61. elapsed := time.Since(b.started)
  62. if b.percent() < 100 && rate > 0 {
  63. fmt.Fprintf(&suf, " [%s:%s]", elapsed.Round(time.Second), stats.remaining)
  64. } else {
  65. fmt.Fprintf(&suf, " ")
  66. }
  67. mid.WriteString("▕")
  68. // add 3 extra spaces: 2 boundary characters and 1 space at the end
  69. f := termWidth - pre.Len() - suf.Len() - 3
  70. n := int(float64(f) * b.percent() / 100)
  71. if n > 0 {
  72. mid.WriteString(strings.Repeat("█", n))
  73. }
  74. if f-n > 0 {
  75. mid.WriteString(strings.Repeat(" ", f-n))
  76. }
  77. mid.WriteString("▏")
  78. return pre.String() + mid.String() + suf.String()
  79. }
  80. func (b *Bar) Set(value int64) {
  81. if value >= b.maxValue {
  82. value = b.maxValue
  83. }
  84. b.currentValue = value
  85. }
  86. func (b *Bar) percent() float64 {
  87. if b.maxValue > 0 {
  88. return float64(b.currentValue) / float64(b.maxValue) * 100
  89. }
  90. return 0
  91. }
  92. func (b *Bar) Stats() Stats {
  93. if time.Since(b.statted) < time.Second {
  94. return b.stats
  95. }
  96. switch {
  97. case b.statted.IsZero():
  98. b.stats = Stats{
  99. value: b.initialValue,
  100. rate: 0,
  101. remaining: 0,
  102. }
  103. case b.currentValue >= b.maxValue:
  104. b.stats = Stats{
  105. value: b.maxValue,
  106. rate: 0,
  107. remaining: 0,
  108. }
  109. default:
  110. rate := b.currentValue - b.stats.value
  111. var remaining time.Duration
  112. if rate > 0 {
  113. remaining = time.Second * time.Duration((float64(b.maxValue-b.currentValue))/(float64(rate)))
  114. }
  115. b.stats = Stats{
  116. value: b.currentValue,
  117. rate: rate,
  118. remaining: remaining,
  119. }
  120. }
  121. b.statted = time.Now()
  122. return b.stats
  123. }