Repository: pterm/pterm
Pull request #108 (2020-11-01 19:19:31)

feat: add `DisableOutput()` and `EnableOutput()`
BREAKING CHANGE: The global variable `DisableOutput` was renamed to `Output`.

### Description
Describe your work here.

### Scope
> What is affected by this pull request?

- [ ] Bug Fix
- [ ] New Feature
- [ ] Documentation
- [ ] Other

### Related Issue
Fixes #


### To-Do Checklist
- [ ] I tested my changes
- [ ] I have commented every method that I created/changed
- [ ] I updated the examples to fit with my changes
- [ ] I have added tests for my newly created methods


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

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

```diff
@@            Coverage Diff            @@
##            master      #108   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           17        17           
  Lines          647       649    +2     
=========================================
+ Hits           647       649    +2     
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/pterm/pterm/pull/108?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/108?src=pr&el=footer). Last update [64d07f1...f7f388c](https://codecov.io/gh/pterm/pterm/pull/108?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/print.go b/print.go
index f8bb59cf..4b70cc54 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 DisableOutput {
+	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 DisableOutput {
+	if Output {
 		return
 	}
 
@@ -98,7 +98,7 @@ func Fprintln(writer io.Writer, a ...interface{}) {
 // time.Sleep(time.Second)
 // pterm.Oprint("Hello, Earth!")
 func Printo(a ...interface{}) {
-	if DisableOutput {
+	if Output {
 		return
 	}
 
diff --git a/pterm.go b/pterm.go
index 160b6952..9bc984d9 100644
--- a/pterm.go
+++ b/pterm.go
@@ -4,12 +4,22 @@
 package pterm
 
 var (
-	// DisableOutput completely disables output from pterm. Can be used in CLI application quiet mode.
-	DisableOutput = false
+	// Output completely disables output from pterm. Can be used in CLI application quiet mode.
+	Output = false
 	// PrintDebugMessages sets if messages printed by the DebugPrinter should be printed.
 	PrintDebugMessages = false
 )
 
+// EnableOutput enables the output of PTerm.
+func EnableOutput() {
+	Output = true
+}
+
+// DisableOutput disables the output of PTerm.
+func DisableOutput() {
+	Output = false
+}
+
 // EnableDebugMessages enables the output of debug printers.
 func EnableDebugMessages() {
 	PrintDebugMessages = true


Test patch:
diff --git a/print_test.go b/print_test.go
index 875be219..30fe4f76 100644
--- a/print_test.go
+++ b/print_test.go
@@ -48,14 +48,14 @@ func TestPrint(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		DisableOutput = true
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Print(randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		DisableOutput = false
+		Output = false
 	})
 }
 
@@ -70,14 +70,14 @@ func TestPrintln(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		DisableOutput = true
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Println(randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		DisableOutput = false
+		Output = false
 	})
 }
 
@@ -96,7 +96,7 @@ func TestPrintf(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		DisableOutput = true
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Printf(randomString)
@@ -107,7 +107,7 @@ func TestPrintf(t *testing.T) {
 			Printf("Hello, %s!", "World")
 		})
 		assert.Equal(t, "", out)
-		DisableOutput = false
+		Output = false
 	})
 }
 
@@ -122,14 +122,14 @@ func TestFprint(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		DisableOutput = true
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprint(w, randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		DisableOutput = false
+		Output = false
 	})
 }
 
@@ -144,14 +144,14 @@ func TestFprintln(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		DisableOutput = true
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprintln(w, randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		DisableOutput = false
+		Output = false
 	})
 }
 
@@ -166,14 +166,14 @@ func TestPrinto(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		DisableOutput = true
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Printo(randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		DisableOutput = false
+		Output = false
 	})
 }
 
@@ -188,14 +188,14 @@ func TestFprinto(t *testing.T) {
 	})
 
 	t.Run("disabled output", func(t *testing.T) {
-		DisableOutput = true
+		Output = true
 		for _, randomString := range internal.RandomStrings {
 			out := internal.CaptureStdout(func(w io.Writer) {
 				Fprinto(w, randomString)
 			})
 			assert.Equal(t, "", out)
 		}
-		DisableOutput = false
+		Output = false
 	})
 }
 
diff --git a/pterm_test.go b/pterm_test.go
index de93b637..2ee95c34 100644
--- a/pterm_test.go
+++ b/pterm_test.go
@@ -15,3 +15,14 @@ func TestEnableDebugMessages(t *testing.T) {
 	EnableDebugMessages()
 	assert.True(t, PrintDebugMessages)
 }
+
+func TestEnableOutput(t *testing.T) {
+	DisableOutput()
+	EnableOutput()
+	assert.True(t, Output)
+}
+
+func TestDisableOutput(t *testing.T) {
+	DisableOutput()
+	assert.False(t, Output)
+}