mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-07 22:20:24 +00:00
Support colored printf (with non-colored output when not on terminal) in Progress.
This commit is contained in:
@@ -56,6 +56,8 @@ type Progress interface {
|
|||||||
AddBar(count int)
|
AddBar(count int)
|
||||||
// Printf does printf but in safe manner: not overwriting progress bar
|
// Printf does printf but in safe manner: not overwriting progress bar
|
||||||
Printf(msg string, a ...interface{})
|
Printf(msg string, a ...interface{})
|
||||||
|
// ColoredPrintf does printf in colored way + newline
|
||||||
|
ColoredPrintf(msg string, a ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Downloader is parallel HTTP fetcher
|
// Downloader is parallel HTTP fetcher
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/cheggaaa/pb"
|
"github.com/cheggaaa/pb"
|
||||||
"github.com/smira/aptly/aptly"
|
"github.com/smira/aptly/aptly"
|
||||||
|
"github.com/wsxiaoys/terminal/color"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -104,6 +106,34 @@ func (p *Progress) Printf(msg string, a ...interface{}) {
|
|||||||
p.queue <- printTask{code: codePrint, message: fmt.Sprintf(msg, a...)}
|
p.queue <- printTask{code: codePrint, message: fmt.Sprintf(msg, a...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ColoredPrintf does printf in colored way + newline
|
||||||
|
func (p *Progress) ColoredPrintf(msg string, a ...interface{}) {
|
||||||
|
if RunningOnTerminal() {
|
||||||
|
p.queue <- printTask{code: codePrint, message: color.Sprintf(msg, a...) + "\n"}
|
||||||
|
} else {
|
||||||
|
// stip color marks
|
||||||
|
var prev rune
|
||||||
|
msg = strings.Map(func(r rune) rune {
|
||||||
|
if prev == '@' {
|
||||||
|
prev = 0
|
||||||
|
if r == '@' {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
prev = r
|
||||||
|
if r == '@' {
|
||||||
|
return -1
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}, msg)
|
||||||
|
|
||||||
|
p.Printf(msg+"\n", a...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Progress) worker() {
|
func (p *Progress) worker() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|||||||
Reference in New Issue
Block a user