Repository: pterm/pterm
Pull request #197 (2021-05-13 22:38:22)

Add `PrintIfError` to `PrefixPrinter`
### Description
Adds the function `PrintIfError` to `PrefixPrinter`, which only prints if the given error is not nil

### Scope
> What is affected by this pull request?

- [ ] Bug Fix
- [x] New Feature
- [ ] Documentation
- [ ] Other

### To-Do Checklist
- [x] I tested my changes
- [x] I have commented every method that I created/changed
- [ ] 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/197?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) Report
> Merging [#197](https://codecov.io/gh/pterm/pterm/pull/197?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) (41a18e6) into [master](https://codecov.io/gh/pterm/pterm/commit/7de1f834ba8244c390670334b62cbdb5a98222b2?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) (7de1f83) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/pterm/pterm/pull/197/graphs/tree.svg?width=650&height=150&src=pr&token=C3EX231V76&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm)](https://codecov.io/gh/pterm/pterm/pull/197?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm)

```diff
@@            Coverage Diff            @@
##            master      #197   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           23        23           
  Lines         1097      1099    +2     
=========================================
+ Hits          1097      1099    +2     
```


| [Impacted Files](https://codecov.io/gh/pterm/pterm/pull/197?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm) | Coverage Δ | |
|---|---|---|
| [prefix\_printer.go](https://codecov.io/gh/pterm/pterm/pull/197/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm#diff-cHJlZml4X3ByaW50ZXIuZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pterm/pterm/pull/197?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pterm/pterm/pull/197?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm). Last update [7de1f83...41a18e6](https://codecov.io/gh/pterm/pterm/pull/197?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pterm).


Patch:
diff --git a/docs/docs/printer/prefix.md b/docs/docs/printer/prefix.md
index 2648b747..7ed1f296 100644
--- a/docs/docs/printer/prefix.md
+++ b/docs/docs/printer/prefix.md
@@ -27,6 +27,7 @@ pterm.Fatal.Println("Hello, World!") // Print Fatal.
 |Function|Description|
 |--------|-----------|
 |[FormattedPrefix](https://pkg.go.dev/github.com/pterm/pterm#PrefixPrinter.GetFormattedPrefix)|Returns the Prefix as a styled text string.|
+|[PrintIfError](https://pkg.go.dev/github.com/pterm/pterm#PrefixPrinter.PrintIfError)|Only prints if the given error is not nil.|
 
 ### Options
 
@@ -79,4 +80,4 @@ pterm.Fatal.WithOptionName(option).WithOptionName2(option2).Println("Hello, Worl
 
 ## Related
 
-- [Override default printers](docs/customizing/override-default-printer.md)
\ No newline at end of file
+- [Override default printers](docs/customizing/override-default-printer.md)
diff --git a/prefix_printer.go b/prefix_printer.go
index aefdff09..67ed8295 100644
--- a/prefix_printer.go
+++ b/prefix_printer.go
@@ -197,6 +197,16 @@ func (p *PrefixPrinter) Sprint(a ...interface{}) string {
 	return Sprint(ret)
 }
 
+// PrintIfError prints if the given error is not nil.
+// This can be used for simple error checking.
+//
+// Note: Use WithFatal(true) or Fatal to panic after printing.
+func (p PrefixPrinter) PrintIfError(err error) {
+	if err != nil {
+		p.Println(p.Sprint(Sprint(err)))
+	}
+}
+
 // Sprintln formats using the default formats for its operands and returns the resulting string.
 // Spaces are always added between operands and a newline is appended.
 func (p PrefixPrinter) Sprintln(a ...interface{}) string {


Test patch:
diff --git a/prefix_printer_test.go b/prefix_printer_test.go
index 895370cc..5af9c100 100644
--- a/prefix_printer_test.go
+++ b/prefix_printer_test.go
@@ -1,6 +1,7 @@
 package pterm
 
 import (
+	"errors"
 	"fmt"
 	"io"
 	"testing"
@@ -83,6 +84,20 @@ func TestPrefixPrinterPrintMethods(t *testing.T) {
 				return p.Sprintln(a)
 			})
 		})
+
+		t.Run("PrintIfError", func(t *testing.T) {
+			result := captureStdout(func(w io.Writer) {
+				p.PrintIfError(errors.New("hello world"))
+			})
+			assert.Contains(t, result, "hello world")
+		})
+
+		t.Run("PrintIfError_WithoutError", func(t *testing.T) {
+			result := captureStdout(func(w io.Writer) {
+				p.PrintIfError(nil)
+			})
+			assert.Empty(t, result)
+		})
 	}
 }