Skip to content

Commit

Permalink
feat: add ability to disable output (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ome committed Oct 13, 2020
1 parent ce30965 commit a1ba5c2
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 43 deletions.
12 changes: 12 additions & 0 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func Sprinto(a ...interface{}) string {
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func Print(a ...interface{}) {
if DisableOutput {
return
}

var ret string
var printed bool

Expand Down Expand Up @@ -72,6 +76,10 @@ func Printf(format string, a ...interface{}) {
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func Fprint(writer io.Writer, a ...interface{}) {
if DisableOutput {
return
}

color.Fprint(writer, Sprint(a...))
}

Expand All @@ -90,6 +98,10 @@ func Fprintln(writer io.Writer, a ...interface{}) {
// time.Sleep(time.Second)
// pterm.Oprint("Hello, Earth!")
func Printo(a ...interface{}) {
if DisableOutput {
return
}

color.Print("\r" + Sprint(a...))
}

Expand Down
5 changes: 5 additions & 0 deletions pterm.go
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
package pterm

var (
// DisableOutput completely disables output from pterm. Can be used in CLI application quiet mode.
DisableOutput = false
)
181 changes: 138 additions & 43 deletions pterm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,70 +41,165 @@ func TestSprinto(t *testing.T) {
// Print functions

func TestPrint(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Print(randomString)
})
assert.Equal(t, randomString, out)
}
t.Run("enabled output", func(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Print(randomString)
})
assert.Equal(t, randomString, out)
}
})

t.Run("disabled output", func(t *testing.T) {
DisableOutput = true
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Print(randomString)
})
assert.Equal(t, "", out)
}
DisableOutput = false
})
}

func TestPrintln(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Println(randomString)
})
assert.Equal(t, randomString+"\n", out)
}
t.Run("enabled output", func(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Println(randomString)
})
assert.Equal(t, randomString+"\n", out)
}
})

t.Run("disabled output", func(t *testing.T) {
DisableOutput = true
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Println(randomString)
})
assert.Equal(t, "", out)
}
DisableOutput = false
})
}

func TestPrintf(t *testing.T) {
for _, randomString := range internal.RandomStrings {
t.Run("enabled output", func(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Printf(randomString)
})
assert.Equal(t, randomString, out)
}
out := captureStdout(func(w io.Writer) {
Printf(randomString)
Printf("Hello, %s!", "World")
})
assert.Equal(t, randomString, out)
}
out := captureStdout(func(w io.Writer) {
Printf("Hello, %s!", "World")
assert.Equal(t, "Hello, World!", out)
})
assert.Equal(t, "Hello, World!", out)
}

func TestFprint(t *testing.T) {
for _, randomString := range internal.RandomStrings {
t.Run("disabled output", func(t *testing.T) {
DisableOutput = true
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Printf(randomString)
})
assert.Equal(t, "", out)
}
out := captureStdout(func(w io.Writer) {
Fprint(w, randomString)
Printf("Hello, %s!", "World")
})
assert.Equal(t, randomString, out)
}
assert.Equal(t, "", out)
DisableOutput = false
})
}

func TestFprint(t *testing.T) {
t.Run("enabled output", func(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprint(w, randomString)
})
assert.Equal(t, randomString, out)
}
})

t.Run("disabled output", func(t *testing.T) {
DisableOutput = true
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprint(w, randomString)
})
assert.Equal(t, "", out)
}
DisableOutput = false
})
}

func TestFprintln(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprintln(w, randomString)
})
assert.Equal(t, randomString+"\n", out)
}
t.Run("enabled output", func(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprintln(w, randomString)
})
assert.Equal(t, randomString+"\n", out)
}
})

t.Run("disabled output", func(t *testing.T) {
DisableOutput = true
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprintln(w, randomString)
})
assert.Equal(t, "", out)
}
DisableOutput = false
})
}

func TestPrinto(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Printo(randomString)
})
assert.Equal(t, "\r"+randomString, out)
}
t.Run("enabled output", func(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Printo(randomString)
})
assert.Equal(t, "\r"+randomString, out)
}
})

t.Run("disabled output", func(t *testing.T) {
DisableOutput = true
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Printo(randomString)
})
assert.Equal(t, "", out)
}
DisableOutput = false
})
}

func TestFprinto(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprinto(w, randomString)
})
assert.Equal(t, "\r"+randomString, out)
}
t.Run("enabled output", func(t *testing.T) {
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprinto(w, randomString)
})
assert.Equal(t, "\r"+randomString, out)
}
})

t.Run("disabled output", func(t *testing.T) {
DisableOutput = true
for _, randomString := range internal.RandomStrings {
out := captureStdout(func(w io.Writer) {
Fprinto(w, randomString)
})
assert.Equal(t, "", out)
}
DisableOutput = false
})
}

func TestRemoveColors(t *testing.T) {
Expand Down

0 comments on commit a1ba5c2

Please sign in to comment.