Repository: pterm/pterm
Pull request #149 (2020-11-17 22:00:46)

fix: disabling output works as expected now
### Description
disabling output works as expected now

### Scope
> What is affected by this pull request?

- [x] Bug Fix
- [ ] New Feature
- [ ] Documentation
- [ ] Other


### To-Do Checklist
- [x] I tested my changes
- [x] I have commented every method that I created/changed
- [x] I updated the examples to fit with my changes
- [x] I have added tests for my newly created methods


codecov[bot]: # [Codecov](https://codecov.io/gh/pterm/pterm/pull/149?src=pr&el=h1) Report
> Merging [#149](https://codecov.io/gh/pterm/pterm/pull/149?src=pr&el=desc) (b7d9766) into [master](https://codecov.io/gh/pterm/pterm/commit/340d4d99e8ae809bb53655ddcfbfb31e6101aff6?el=desc) (340d4d9) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/pterm/pterm/pull/149/graphs/tree.svg?width=650&height=150&src=pr&token=C3EX231V76)](https://codecov.io/gh/pterm/pterm/pull/149?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master      #149   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           19        19           
  Lines          747       747           
=========================================
  Hits           747       747           
```


| [Impacted Files](https://codecov.io/gh/pterm/pterm/pull/149?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pterm.go](https://codecov.io/gh/pterm/pterm/pull/149/diff?src=pr&el=tree#diff-cHRlcm0uZ28=) | `100.00% <ø> (ø)` | |
| [print.go](https://codecov.io/gh/pterm/pterm/pull/149/diff?src=pr&el=tree#diff-cHJpbnQuZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pterm/pterm/pull/149?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pterm/pterm/pull/149?src=pr&el=footer). Last update [340d4d9...b7d9766](https://codecov.io/gh/pterm/pterm/pull/149?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/_examples/disable-output/main.go b/_examples/disable-output/main.go
new file mode 100644
index 00000000..e604abb1
--- /dev/null
+++ b/_examples/disable-output/main.go
@@ -0,0 +1,18 @@
+package main
+
+import "github.com/pterm/pterm"
+
+func main() {
+	for i := 0; i < 15; i++ {
+		switch i {
+		case 5:
+			pterm.Info.Println("Disabled Output!")
+			pterm.DisableOutput()
+		case 10:
+			pterm.EnableOutput()
+			pterm.Info.Println("Enabled Output!")
+		}
+
+		pterm.Printf("Printing something... [%d/%d]\n", i, 15)
+	}
+}
diff --git a/print.go b/print.go
index 557a6374..a9c21def 100644
--- a/print.go
+++ b/print.go
@@ -37,7 +37,7 @@ 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 Output {
+	if !Output {
 		return
 	}
 
@@ -76,7 +76,7 @@ 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 Output {
+	if !Output {
 		return
 	}
 
@@ -97,7 +97,7 @@ func Fprintln(writer io.Writer, a ...interface{}) {
 //   time.Sleep(time.Second)
 //   pterm.Printo("Hello, Earth!")
 func Printo(a ...interface{}) {
-	if Output {
+	if !Output {
 		return
 	}
 
diff --git a/pterm.go b/pterm.go
index b082773d..896c4021 100644
--- a/pterm.go
+++ b/pterm.go
@@ -10,8 +10,8 @@ func init() {
 }
 
 var (
-	// Output completely disables output from pterm. Can be used in CLI application quiet mode.
-	Output = false
+	// Output completely disables output from pterm if set to false. Can be used in CLI application quiet mode.
+	Output = true
 	// PrintDebugMessages sets if messages printed by the DebugPrinter should be printed.
 	PrintDebugMessages = false
 )


Test patch:
diff --git a/internal/test_utils.go b/internal/test_utils.go
index c70b8771..07e26d69 100644
--- a/internal/test_utils.go
+++ b/internal/test_utils.go
@@ -114,7 +114,7 @@ func CaptureStdout(f func(w io.Writer)) string {
 	out, _ := ioutil.ReadAll(r)
 	os.Stdout = originalStdout
 	color.SetOutput(w)
-	r.Close()
+	_ = r.Close()
 
 	return string(out)
 }
diff --git a/print_test.go b/print_test.go
index 30fe4f76..00396662 100644
--- a/print_test.go
+++ b/print_test.go
@@ -39,6 +39,7 @@ func TestSprinto(t *testing.T) {
 
 func TestPrint(t *testing.T) {
 	t.Run("enabled output", func(t *testing.T) {
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Print(randomString)
@@ -48,19 +49,20 @@ func TestPrint(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		Output = true
+		Output = false
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Print(randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		Output = false
+		Output = true
 	})
 }
 
 func TestPrintln(t *testing.T) {
 	t.Run("enabled output", func(t *testing.T) {
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Println(randomString)
@@ -70,19 +72,20 @@ func TestPrintln(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		Output = true
+		Output = false
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Println(randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		Output = false
+		Output = true
 	})
 }
 
 func TestPrintf(t *testing.T) {
 	t.Run("enabled output", func(t *testing.T) {
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Printf(randomString)
@@ -96,7 +99,7 @@ func TestPrintf(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		Output = true
+		Output = false
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Printf(randomString)
@@ -107,12 +110,13 @@ func TestPrintf(t *testing.T) {
 			Printf("Hello, %s!", "World")
 		})
 		assert.Equal(t, "", out)
-		Output = false
+		Output = true
 	})
 }
 
 func TestFprint(t *testing.T) {
 	t.Run("enabled output", func(t *testing.T) {
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprint(w, randomString)
@@ -122,19 +126,20 @@ func TestFprint(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		Output = true
+		Output = false
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprint(w, randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		Output = false
+		Output = true
 	})
 }
 
 func TestFprintln(t *testing.T) {
 	t.Run("enabled output", func(t *testing.T) {
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprintln(w, randomString)
@@ -144,19 +149,20 @@ func TestFprintln(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		Output = true
+		Output = false
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprintln(w, randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		Output = false
+		Output = true
 	})
 }
 
 func TestPrinto(t *testing.T) {
 	t.Run("enabled output", func(t *testing.T) {
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Printo(randomString)
@@ -166,7 +172,7 @@ func TestPrinto(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		Output = true
+		Output = false
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Printo(randomString)
@@ -179,6 +185,7 @@ func TestPrinto(t *testing.T) {
 
 func TestFprinto(t *testing.T) {
 	t.Run("enabled output", func(t *testing.T) {
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprinto(w, randomString)
@@ -188,14 +195,14 @@ func TestFprinto(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		Output = true
+		Output = false
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprinto(w, randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		Output = false
+		Output = true
 	})
 }
 
diff --git a/pterm_test.go b/pterm_test.go
index 2ee95c34..1769ea41 100644
--- a/pterm_test.go
+++ b/pterm_test.go
@@ -16,13 +16,13 @@ func TestEnableDebugMessages(t *testing.T) {
 	assert.True(t, PrintDebugMessages)
 }
 
-func TestEnableOutput(t *testing.T) {
+func TestDisableOutput(t *testing.T) {
 	DisableOutput()
-	EnableOutput()
-	assert.True(t, Output)
+	assert.False(t, Output)
 }
 
-func TestDisableOutput(t *testing.T) {
+func TestEnableOutput(t *testing.T) {
 	DisableOutput()
-	assert.False(t, Output)
+	EnableOutput()
+	assert.True(t, Output)
 }