forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#169791315] Co-authored-by: Timothy Hitchener <[email protected]>
- Loading branch information
Showing
13 changed files
with
681 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package scribe | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/cheggaaa/pb" | ||
) | ||
|
||
type Bar struct { | ||
w io.Writer | ||
p *pb.ProgressBar | ||
} | ||
|
||
func NewBar(w io.Writer) *Bar { | ||
return &Bar{ | ||
w: w, | ||
p: pb.New(100), | ||
} | ||
} | ||
|
||
func (b *Bar) Start() { | ||
b.p.Start() | ||
b.p.SetWriter(b.w) | ||
b.p.SetWidth(100) | ||
b.p.SetTemplateString(`{{bar . "[" "-" ">" " " "]"}} {{percent .}} {{etime .}}`) | ||
} | ||
|
||
func (b *Bar) Increment() { | ||
b.p.Increment() | ||
} | ||
|
||
func (b *Bar) Finish() { | ||
b.p.Finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package scribe_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/cloudfoundry/packit/scribe" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testBar(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
|
||
buffer *bytes.Buffer | ||
bar *scribe.Bar | ||
) | ||
|
||
it.Before(func() { | ||
buffer = bytes.NewBuffer(nil) | ||
|
||
bar = scribe.NewBar(buffer) | ||
}) | ||
|
||
it("renders a progress bar to the writer", func() { | ||
bar.Start() | ||
for i := 0; i < 40; i++ { | ||
bar.Increment() | ||
} | ||
bar.Finish() | ||
|
||
Expect(buffer.String()).To(Equal("[-----------------------------------> ] 40.00% 0s")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package scribe | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
var ( | ||
BlackColor = NewColor(false, 0, -1) | ||
RedColor = NewColor(false, 1, -1) | ||
GreenColor = NewColor(false, 2, -1) | ||
YellowColor = NewColor(false, 3, -1) | ||
BlueColor = NewColor(false, 4, -1) | ||
MagentaColor = NewColor(false, 5, -1) | ||
CyanColor = NewColor(false, 6, -1) | ||
WhiteColor = NewColor(false, 7, -1) | ||
GrayColor = NewColor(false, 244, -1) | ||
) | ||
|
||
type Color func(message string) string | ||
|
||
func NewColor(bold bool, fg, bg int) Color { | ||
return func(message string) string { | ||
prefix := "\x1b[" | ||
if bold { | ||
prefix = prefix + "1" | ||
} else { | ||
prefix = prefix + "0" | ||
} | ||
|
||
if fg >= 0 { | ||
prefix = prefix + ";38;5;" + strconv.Itoa(fg) | ||
} | ||
|
||
if bg >= 0 { | ||
prefix = prefix + ";48;5;" + strconv.Itoa(bg) | ||
} | ||
|
||
prefix = prefix + "m" | ||
suffix := "\x1b[0m" | ||
|
||
return prefix + message + suffix | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package scribe_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudfoundry/packit/scribe" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testColor(t *testing.T, context spec.G, it spec.S) { | ||
var Expect = NewWithT(t).Expect | ||
|
||
it("returns a function that wraps a string in color codes", func() { | ||
redFgColor := scribe.NewColor(false, 1, -1) | ||
Expect(redFgColor("some-text")).To(Equal("\x1b[0;38;5;1msome-text\x1b[0m")) | ||
|
||
blueBgColor := scribe.NewColor(false, -1, 4) | ||
Expect(blueBgColor("some-text")).To(Equal("\x1b[0;48;5;4msome-text\x1b[0m")) | ||
|
||
magentaBoldFgColor := scribe.NewColor(true, 5, -1) | ||
Expect(magentaBoldFgColor("some-text")).To(Equal("\x1b[1;38;5;5msome-text\x1b[0m")) | ||
|
||
mixedFgBgColor := scribe.NewColor(false, 3, 244) | ||
Expect(mixedFgBgColor("some-text")).To(Equal("\x1b[0;38;5;3;48;5;244msome-text\x1b[0m")) | ||
}) | ||
|
||
context("BlackColor", func() { | ||
it("returns a function that wraps a string in black color codes", func() { | ||
Expect(scribe.BlackColor("some-text")).To(Equal("\x1b[0;38;5;0msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("RedColor", func() { | ||
it("returns a function that wraps a string in red color codes", func() { | ||
Expect(scribe.RedColor("some-text")).To(Equal("\x1b[0;38;5;1msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("GreenColor", func() { | ||
it("returns a function that wraps a string in green color codes", func() { | ||
Expect(scribe.GreenColor("some-text")).To(Equal("\x1b[0;38;5;2msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("YellowColor", func() { | ||
it("returns a function that wraps a string in yellow color codes", func() { | ||
Expect(scribe.YellowColor("some-text")).To(Equal("\x1b[0;38;5;3msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("BlueColor", func() { | ||
it("returns a function that wraps a string in blue color codes", func() { | ||
Expect(scribe.BlueColor("some-text")).To(Equal("\x1b[0;38;5;4msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("MagentaColor", func() { | ||
it("returns a function that wraps a string in magenta color codes", func() { | ||
Expect(scribe.MagentaColor("some-text")).To(Equal("\x1b[0;38;5;5msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("CyanColor", func() { | ||
it("returns a function that wraps a string in cyan color codes", func() { | ||
Expect(scribe.CyanColor("some-text")).To(Equal("\x1b[0;38;5;6msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("WhiteColor", func() { | ||
it("returns a function that wraps a string in white color codes", func() { | ||
Expect(scribe.WhiteColor("some-text")).To(Equal("\x1b[0;38;5;7msome-text\x1b[0m")) | ||
}) | ||
}) | ||
|
||
context("GrayColor", func() { | ||
it("returns a function that wraps a string in gray color codes", func() { | ||
Expect(scribe.GrayColor("some-text")).To(Equal("\x1b[0;38;5;244msome-text\x1b[0m")) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package scribe | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type FormattedList []string | ||
|
||
func (l FormattedList) String() string { | ||
sort.Strings(l) | ||
|
||
var builder strings.Builder | ||
for i, elem := range l { | ||
builder.WriteString(elem) | ||
if i < len(l)-1 { | ||
builder.WriteRune('\n') | ||
} | ||
} | ||
|
||
return builder.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package scribe_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudfoundry/packit/scribe" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testFormattedList(t *testing.T, context spec.G, it spec.S) { | ||
var Expect = NewWithT(t).Expect | ||
|
||
context("String", func() { | ||
it("returns a formatted string representation of the list", func() { | ||
Expect(scribe.FormattedList{ | ||
"third", | ||
"first", | ||
"second", | ||
}.String()).To(Equal("first\nsecond\nthird")) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package scribe | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type FormattedMap map[string]interface{} | ||
|
||
func (m FormattedMap) String() string { | ||
var ( | ||
keys []string | ||
padding int | ||
) | ||
for key := range m { | ||
if len(key) > padding { | ||
padding = len(key) | ||
} | ||
keys = append(keys, key) | ||
} | ||
|
||
sort.Strings(keys) | ||
|
||
var builder strings.Builder | ||
for _, key := range keys { | ||
value := m[key] | ||
if value == nil { | ||
value = "<empty>" | ||
} | ||
|
||
for i := len(key); i < padding; i++ { | ||
key = key + " " | ||
} | ||
|
||
builder.WriteString(fmt.Sprintf("%s -> %v\n", key, value)) | ||
} | ||
|
||
return strings.TrimSpace(builder.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package scribe_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudfoundry/packit/scribe" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testFormattedMap(t *testing.T, context spec.G, it spec.S) { | ||
var Expect = NewWithT(t).Expect | ||
|
||
context("String", func() { | ||
it("returns a formatted string representation of the map", func() { | ||
Expect(scribe.FormattedMap{ | ||
"third": 3, | ||
"first": 1, | ||
"second": 2, | ||
}.String()).To(Equal("first -> 1\nsecond -> 2\nthird -> 3")) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.