Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to disable output #44

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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