bar.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // formatDuration limits the rendering of a time.Duration to 2 units
  37. func formatDuration(d time.Duration) string {
  38. if d >= 100*time.Hour {
  39. return "99h+"
  40. }
  41. if d >= time.Hour {
  42. return fmt.Sprintf("%dh%dm", int(d.Hours()), int(d.Minutes())%60)
  43. }
  44. return d.Round(time.Second).String()
  45. }
  46. func (b *Bar) String() string {
  47. termWidth, _, err := term.GetSize(int(os.Stderr.Fd()))
  48. if err != nil {
  49. termWidth = 80
  50. }
  51. var pre, mid, suf strings.Builder
  52. if b.message != "" {
  53. message := strings.TrimSpace(b.message)
  54. if b.messageWidth > 0 && len(message) > b.messageWidth {
  55. message = message[:b.messageWidth]
  56. }
  57. fmt.Fprintf(&pre, "%s", message)
  58. if b.messageWidth-pre.Len() >= 0 {
  59. pre.WriteString(strings.Repeat(" ", b.messageWidth-pre.Len()))
  60. }
  61. pre.WriteString(" ")
  62. }
  63. fmt.Fprintf(&pre, "%3.0f%% ", math.Floor(b.percent()))
  64. fmt.Fprintf(&suf, "(%s/%s", format.HumanBytes(b.currentValue), format.HumanBytes(b.maxValue))
  65. stats := b.Stats()
  66. rate := stats.rate
  67. if stats.value > b.initialValue && stats.value < b.maxValue {
  68. fmt.Fprintf(&suf, ", %s/s", format.HumanBytes(int64(rate)))
  69. }
  70. fmt.Fprintf(&suf, ")")
  71. var timing string
  72. if stats.value > b.initialValue && stats.value < b.maxValue {
  73. timing = fmt.Sprintf("[%s:%s]", formatDuration(time.Since(b.started)), formatDuration(stats.remaining))
  74. }
  75. // 44 is the maximum width for the stats on the right of the progress bar
  76. suf.WriteString(strings.Repeat(" ", 44-suf.Len()-len(timing)))
  77. suf.WriteString(timing)
  78. // add 3 extra spaces: 2 boundary characters and 1 space at the end
  79. f := termWidth - pre.Len() - suf.Len() - 3
  80. n := int(float64(f) * b.percent() / 100)
  81. if f > 0 {
  82. mid.WriteString("▕")
  83. mid.WriteString(strings.Repeat("█", n))
  84. if f-n > 0 {
  85. mid.WriteString(strings.Repeat(" ", f-n))
  86. }
  87. mid.WriteString("▏")
  88. }
  89. return pre.String() + mid.String() + suf.String()
  90. }
  91. func (b *Bar) Set(value int64) {
  92. if value >= b.maxValue {
  93. value = b.maxValue
  94. }
  95. b.currentValue = value
  96. }
  97. func (b *Bar) percent() float64 {
  98. if b.maxValue > 0 {
  99. return float64(b.currentValue) / float64(b.maxValue) * 100
  100. }
  101. return 0
  102. }
  103. func (b *Bar) Stats() Stats {
  104. if time.Since(b.statted) < time.Second {
  105. return b.stats
  106. }
  107. switch {
  108. case b.statted.IsZero():
  109. b.stats = Stats{
  110. value: b.initialValue,
  111. rate: 0,
  112. remaining: 0,
  113. }
  114. case b.currentValue >= b.maxValue:
  115. b.stats = Stats{
  116. value: b.maxValue,
  117. rate: 0,
  118. remaining: 0,
  119. }
  120. default:
  121. rate := b.currentValue - b.stats.value
  122. var remaining time.Duration
  123. if rate > 0 {
  124. remaining = time.Second * time.Duration((float64(b.maxValue-b.currentValue))/(float64(rate)))
  125. } else {
  126. remaining = time.Duration(math.MaxInt64)
  127. }
  128. b.stats = Stats{
  129. value: b.currentValue,
  130. rate: rate,
  131. remaining: remaining,
  132. }
  133. }
  134. b.statted = time.Now()
  135. return b.stats
  136. }