| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 1 | //go:build linux || darwin || freebsd || netbsd || openbsd || solaris || dragonfly || plan9 || aix |
| 2 | // +build linux darwin freebsd netbsd openbsd solaris dragonfly plan9 aix |
| 3 | |
| 4 | package pb |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/cheggaaa/pb/v3/termutil" |
| 12 | ) |
| 13 | |
| 14 | func (p *Pool) print(first bool) bool { |
| 15 | p.m.Lock() |
| 16 | defer p.m.Unlock() |
| 17 | var out string |
| 18 | if !first { |
| 19 | out = fmt.Sprintf("\033[%dA", p.lastBarsCount) |
| 20 | } |
| 21 | isFinished := true |
| 22 | bars := p.bars |
| 23 | rows, cols, err := termutil.TerminalSize() |
| 24 | if err != nil { |
| 25 | cols = defaultBarWidth |
| 26 | } |
| 27 | if rows > 0 && len(bars) > rows { |
| 28 | // we need to hide bars that overflow terminal height |
| 29 | bars = bars[len(bars)-rows:] |
| 30 | } |
| 31 | for _, bar := range bars { |
| 32 | if !bar.IsFinished() { |
| 33 | isFinished = false |
| 34 | } |
| 35 | bar.SetWidth(cols) |
| 36 | result := bar.String() |
| 37 | if r := cols - CellCount(result); r > 0 { |
| 38 | result += strings.Repeat(" ", r) |
| 39 | } |
| 40 | out += fmt.Sprintf("\r%s\n", result) |
| 41 | } |
| 42 | if p.Output != nil { |
| 43 | fmt.Fprint(p.Output, out) |
| 44 | } else { |
| 45 | fmt.Fprint(os.Stderr, out) |
| 46 | } |
| 47 | p.lastBarsCount = len(bars) |
| 48 | return isFinished |
| 49 | } |