func (pbp *ProgressBarPrinter) isTerminal(w io.Writer) bool {
	if pbp.printToTTYAlways {
		return true
	}
	if f, ok := w.(*os.File); ok {
		return terminal.IsTerminal(int(f.Fd()))
	}
	return false
}
isTerminal returns True when w is going to a tty, and false otherwise.
package progressutil
import (
	"fmt"
	"io"
	"os"
	"strings"
	"sync"
	"golang.org/x/crypto/ssh/terminal"
)
var (
	ErrorProgressOutOfBounds = fmt.Errorf("progress is out of bounds (0 to 1)")
	ErrorNoBarsAdded = fmt.Errorf("AddProgressBar hasn't been called yet")
)
type ProgressBar struct {
	lock sync.Mutex
	currentProgress float64
	printBefore     string
	printAfter      string
	done            bool
}
func (pb *ProgressBar) clone() *ProgressBar {
	pb.lock.Lock()
	pbClone := &ProgressBar{
		currentProgress: pb.currentProgress,
		printBefore:     pb.printBefore,
		printAfter:      pb.printAfter,
		done:            pb.done,
	}
	pb.lock.Unlock()
	return pbClone
}
func (pb *ProgressBar) GetCurrentProgress() float64 {
	pb.lock.Lock()
	val := pb.currentProgress
	pb.lock.Unlock()
	return val
}
func (pb *ProgressBar) SetCurrentProgress(progress float64) error {
	if progress < 0 || progress > 1 {
		return ErrorProgressOutOfBounds
	}
	pb.lock.Lock()
	pb.currentProgress = progress
	pb.lock.Unlock()
	return nil
}
func (pb *ProgressBar) GetDone() bool {
	pb.lock.Lock()
	val := pb.done
	pb.lock.Unlock()
	return val
}
func (pb *ProgressBar) SetDone(val bool) {
	pb.lock.Lock()
	pb.done = val
	pb.lock.Unlock()
}
func (pb *ProgressBar) GetPrintBefore() string {
	pb.lock.Lock()
	val := pb.printBefore
	pb.lock.Unlock()
	return val
}
func (pb *ProgressBar) SetPrintBefore(before string) {
	pb.lock.Lock()
	pb.printBefore = before
	pb.lock.Unlock()
}
func (pb *ProgressBar) GetPrintAfter() string {
	pb.lock.Lock()
	val := pb.printAfter
	pb.lock.Unlock()
	return val
}
func (pb *ProgressBar) SetPrintAfter(after string) {
	pb.lock.Lock()
	pb.printAfter = after
	pb.lock.Unlock()
}
type ProgressBarPrinter struct {
	lock sync.Mutex
	DisplayWidth int
	PadToBeEven         bool
	numLinesInLastPrint int
	progressBars        []*ProgressBar
	maxBefore           int
	maxAfter            int
	printToTTYAlways bool
}
func (pbp *ProgressBarPrinter) AddProgressBar() *ProgressBar {
	pb := &ProgressBar{}
	pbp.lock.Lock()
	pbp.progressBars = append(pbp.progressBars, pb)
	pbp.lock.Unlock()
	return pb
}
func (pbp *ProgressBarPrinter) Print(printTo io.Writer) (bool, error) {
	pbp.lock.Lock()
	var bars []*ProgressBar
	for _, bar := range pbp.progressBars {
		bars = append(bars, bar.clone())
	}
	numColumns := pbp.DisplayWidth
	pbp.lock.Unlock()
	if len(bars) == 0 {
		return false, ErrorNoBarsAdded
	}
	if numColumns == 0 {
		numColumns = 80
	}
	if pbp.isTerminal(printTo) {
		moveCursorUp(printTo, pbp.numLinesInLastPrint)
	}
	for _, bar := range bars {
		beforeSize := len(bar.GetPrintBefore())
		afterSize := len(bar.GetPrintAfter())
		if beforeSize > pbp.maxBefore {
			pbp.maxBefore = beforeSize
		}
		if afterSize > pbp.maxAfter {
			pbp.maxAfter = afterSize
		}
	}
	allDone := true
	for _, bar := range bars {
		if pbp.isTerminal(printTo) {
			bar.printToTerminal(printTo, numColumns, pbp.PadToBeEven, pbp.maxBefore, pbp.maxAfter)
		} else {
			bar.printToNonTerminal(printTo)
		}
		allDone = allDone && bar.GetCurrentProgress() == 1
	}
	pbp.numLinesInLastPrint = len(bars)
	return allDone, nil
}
func moveCursorUp(printTo io.Writer, numLines int) {
	if numLines > 0 {
		fmt.Fprintf(printTo, "\033[%dA", numLines)
	}
}
func (pb *ProgressBar) printToTerminal(printTo io.Writer, numColumns int, padding bool, maxBefore, maxAfter int) {
	before := pb.GetPrintBefore()
	after := pb.GetPrintAfter()
	if padding {
		before = before + strings.Repeat(" ", maxBefore-len(before))
		after = strings.Repeat(" ", maxAfter-len(after)) + after
	}
	progressBarSize := numColumns - (len(fmt.Sprintf("%s [] %s", before, after)))
	progressBar := ""
	if progressBarSize > 0 {
		currentProgress := int(pb.GetCurrentProgress() * float64(progressBarSize))
		progressBar = fmt.Sprintf("[%s%s] ",
			strings.Repeat("=", currentProgress),
			strings.Repeat(" ", progressBarSize-currentProgress))
	} else {
		before = pb.GetPrintBefore()
		after = pb.GetPrintAfter()
	}
	fmt.Fprintf(printTo, "%s %s%s\n", before, progressBar, after)
}
func (pb *ProgressBar) printToNonTerminal(printTo io.Writer) {
	if !pb.GetDone() {
		fmt.Fprintf(printTo, "%s %s\n", pb.printBefore, pb.printAfter)
		if pb.GetCurrentProgress() == 1 {
			pb.SetDone(true)
		}
	}
}