Repository: pterm/pterm
Pull request #21 (2020-10-06 14:58:42)

Add section printer
### Description
Add section printer.

### Scope
> What is affected by this pull request?

- [x] Bug Fix
- [x] New Feature
- [ ] Documentation
- [x] Test
- [ ] Other



Patch:
diff --git a/_examples/section-default/main.go b/_examples/section-default/main.go
new file mode 100644
index 00000000..52ca74b7
--- /dev/null
+++ b/_examples/section-default/main.go
@@ -0,0 +1,10 @@
+package main
+
+import "github.com/pterm/pterm"
+
+func main() {
+	pterm.DefaultSection.Println("This is a section!")
+	pterm.Info.Println("And here is some text.\nThis text could be anything.\nBasically it's just a placeholder")
+	pterm.DefaultSection.Println("This is another section!")
+	pterm.Info.Println("And this is\nmore placeholder text")
+}
diff --git a/header_printer.go b/header_printer.go
index 4e1a7b56..20d71824 100644
--- a/header_printer.go
+++ b/header_printer.go
@@ -83,7 +83,7 @@ func (p HeaderPrinter) Sprintln(a ...interface{}) string {
 
 // Sprintf formats according to a format specifier and returns the resulting string.
 func (p HeaderPrinter) Sprintf(format string, a ...interface{}) string {
-	panic("implement me")
+	return p.Sprint(Sprintf(format, a...))
 }
 
 // Print formats using the default formats for its operands and writes to standard output.
@@ -91,7 +91,7 @@ func (p HeaderPrinter) Sprintf(format string, a ...interface{}) string {
 // It returns the number of bytes written and any write error encountered.
 func (p HeaderPrinter) Print(a ...interface{}) GenericPrinter {
 	Print(p.Sprint(a...))
-	return p
+	return &p
 }
 
 // Println formats using the default formats for its operands and writes to standard output.
@@ -99,12 +99,12 @@ func (p HeaderPrinter) Print(a ...interface{}) GenericPrinter {
 // It returns the number of bytes written and any write error encountered.
 func (p HeaderPrinter) Println(a ...interface{}) GenericPrinter {
 	Println(p.Sprint(a...))
-	return p
+	return &p
 }
 
 // Printf formats according to a format specifier and writes to standard output.
 // It returns the number of bytes written and any write error encountered.
 func (p HeaderPrinter) Printf(format string, a ...interface{}) GenericPrinter {
-	p.Print(Sprintf(format, a...))
-	return p
+	Print(p.Sprintf(format, a...))
+	return &p
 }
diff --git a/section_printer.go b/section_printer.go
new file mode 100644
index 00000000..d8b049c3
--- /dev/null
+++ b/section_printer.go
@@ -0,0 +1,63 @@
+package pterm
+
+// DefaultSection is the default section printer.
+var DefaultSection = SectionPrinter{
+	Style:      NewStyle(FgLightYellow, Underscore),
+	Level:      1,
+	TopPadding: 1,
+}
+
+// SectionPrinter can print errors of the default Error type.
+type SectionPrinter struct {
+	Style      Style
+	Level      int
+	TopPadding int
+}
+
+// Sprint formats using the default formats for its operands and returns the resulting string.
+// Spaces are added between operands when neither is a string.
+func (p SectionPrinter) Sprint(a ...interface{}) string {
+	var ret string
+
+	for i := 0; i < p.TopPadding; i++ {
+		ret += "\n"
+	}
+
+	ret += p.Style.Sprint(a...)
+
+	return ret
+}
+
+// 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 SectionPrinter) Sprintln(a ...interface{}) string {
+	return Sprintln(p.Sprint(a...))
+}
+
+// Sprintf formats according to a format specifier and returns the resulting string.
+func (p SectionPrinter) Sprintf(format string, a ...interface{}) string {
+	return p.Sprint(Sprintf(format, a...))
+}
+
+// Print formats using the default formats for its operands and writes to standard output.
+// Spaces are added between operands when neither is a string.
+// It returns the number of bytes written and any write error encountered.
+func (p SectionPrinter) Print(a ...interface{}) GenericPrinter {
+	Print(p.Sprint(a...))
+	return &p
+}
+
+// Println formats using the default formats for its operands and writes to standard output.
+// Spaces are always added between operands and a newline is appended.
+// It returns the number of bytes written and any write error encountered.
+func (p SectionPrinter) Println(a ...interface{}) GenericPrinter {
+	Println(p.Sprint(a...))
+	return &p
+}
+
+// Printf formats according to a format specifier and writes to standard output.
+// It returns the number of bytes written and any write error encountered.
+func (p SectionPrinter) Printf(format string, a ...interface{}) GenericPrinter {
+	Print(p.Sprintf(format, a...))
+	return &p
+}


Test patch:
diff --git a/pterm_test.go b/pterm_test.go
index b085c6b1..6c6fe758 100644
--- a/pterm_test.go
+++ b/pterm_test.go
@@ -115,7 +115,7 @@ func TestRemoveColors(t *testing.T) {
 }
 
 func TestGenericPrinter(t *testing.T) {
-	var genericPrinters []GenericPrinter
+	var genericPrinters = []GenericPrinter{DefaultSection, DefaultHeader}
 
 	prefixPrinter := []PrefixPrinter{Info, Success, Warning, Error, *Fatal.WithFatal(false)}
 	for _, pp := range prefixPrinter {