Skip to content

Commit

Permalink
Merge pull request #324 from Brookke/issue-261-Add_ability_to_set_cus…
Browse files Browse the repository at this point in the history
…tom_io_writter_for_each_of_the_different_output_levels
  • Loading branch information
MarvinJWendt committed Mar 28, 2022
2 parents 091ef5d + 48050e3 commit dc10c49
Show file tree
Hide file tree
Showing 44 changed files with 1,681 additions and 349 deletions.
10 changes: 9 additions & 1 deletion barchart.go
@@ -1,6 +1,7 @@
package pterm

import (
"io"
"strconv"
"strings"

Expand All @@ -11,6 +12,7 @@ import (

// BarChartPrinter is used to print bar charts.
type BarChartPrinter struct {
Writer io.Writer
Bars Bars
Horizontal bool
ShowValue bool
Expand Down Expand Up @@ -81,6 +83,12 @@ func (p BarChartPrinter) WithShowValue(b ...bool) *BarChartPrinter {
return &p
}

// WithWriter sets the custom Writer.
func (p BarChartPrinter) WithWriter(writer io.Writer) *BarChartPrinter {
p.Writer = writer
return &p
}

func (p BarChartPrinter) getRawOutput() string {
var ret string

Expand Down Expand Up @@ -410,7 +418,7 @@ func (p BarChartPrinter) Srender() (string, error) {
// Render prints the Template to the terminal.
func (p BarChartPrinter) Render() error {
s, _ := p.Srender()
Println(s)
Fprintln(p.Writer, s)

return nil
}
10 changes: 10 additions & 0 deletions barchart_test.go
@@ -1,6 +1,7 @@
package pterm_test

import (
"os"
"testing"

"github.com/MarvinJWendt/testza"
Expand Down Expand Up @@ -437,3 +438,12 @@ func TestBarChartPrinter_WithWidth(t *testing.T) {
testza.AssertEqual(t, s, p2.Width)
testza.AssertZero(t, p.Width)
}

func TestBarChartPrinter_WithWriter(t *testing.T) {
p := pterm.BarChartPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)

testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}
29 changes: 19 additions & 10 deletions basic_text_printer.go
@@ -1,6 +1,9 @@
package pterm

import "fmt"
import (
"fmt"
"io"
)

var (
// DefaultBasicText returns a default BasicTextPrinter, which can be used to print text as is.
Expand All @@ -10,7 +13,8 @@ var (

// BasicTextPrinter is the printer used to print the input as-is or as specified by user formatting.
type BasicTextPrinter struct {
Style *Style
Style *Style
Writer io.Writer
}

// WithStyle adds a style to the printer.
Expand All @@ -19,6 +23,11 @@ func (p BasicTextPrinter) WithStyle(style *Style) *BasicTextPrinter {
return &p
}

func (p BasicTextPrinter) WithWriter(writer io.Writer) *BasicTextPrinter {
p.Writer = writer
return &p
}

// 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 BasicTextPrinter) Sprint(a ...interface{}) string {
Expand Down Expand Up @@ -46,37 +55,37 @@ func (p BasicTextPrinter) Sprintfln(format string, a ...interface{}) string {
return p.Sprintf(format, a...) + "\n"
}

// Print formats using the default formats for its operands and writes to standard output.
// Print formats using the default formats for its operands and writes to provided writer.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func (p *BasicTextPrinter) Print(a ...interface{}) *TextPrinter {
Print(p.Sprint(a...))
Fprint(p.Writer, p.Sprint(a...))
tp := TextPrinter(p)
return &tp
}

// Println formats using the default formats for its operands and writes to standard output.
// Println formats using the default formats for its operands and writes to provided writer.
// 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 *BasicTextPrinter) Println(a ...interface{}) *TextPrinter {
Print(p.Sprintln(a...))
Fprint(p.Writer, p.Sprintln(a...))
tp := TextPrinter(p)
return &tp
}

// Printf formats according to a format specifier and writes to standard output.
// Printf formats according to a format specifier and writes to provided writer.
// It returns the number of bytes written and any write error encountered.
func (p *BasicTextPrinter) Printf(format string, a ...interface{}) *TextPrinter {
Print(p.Sprintf(format, a...))
Fprint(p.Writer, p.Sprintf(format, a...))
tp := TextPrinter(p)
return &tp
}

// Printfln formats according to a format specifier and writes to standard output.
// Printfln formats according to a format specifier and writes to provided writer.
// 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 *BasicTextPrinter) Printfln(format string, a ...interface{}) *TextPrinter {
Print(p.Sprintfln(format, a...))
Fprint(p.Writer, p.Sprintfln(format, a...))
tp := TextPrinter(p)
return &tp
}
Expand Down
10 changes: 10 additions & 0 deletions basic_text_printer_test.go
Expand Up @@ -3,6 +3,7 @@ package pterm_test
import (
"errors"
"io"
"os"
"testing"

"github.com/MarvinJWendt/testza"
Expand Down Expand Up @@ -103,3 +104,12 @@ func TestBasicTextPrinter_WithStyle(t *testing.T) {

testza.AssertEqual(t, s, p2.Style)
}

func TestBasicTextPrinter_WithWriter(t *testing.T) {
p := pterm.BasicTextPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)

testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}
13 changes: 11 additions & 2 deletions bigtext_printer.go
@@ -1,9 +1,11 @@
package pterm

import (
"github.com/gookit/color"
"io"
"strings"

"github.com/gookit/color"

"github.com/mattn/go-runewidth"

"github.com/pterm/pterm/internal"
Expand Down Expand Up @@ -80,6 +82,7 @@ type BigTextPrinter struct {
// BigCharacters holds the map from a normal character to it's big version.
BigCharacters map[string]string
Letters Letters
Writer io.Writer
}

// WithBigCharacters returns a new BigTextPrinter with specific BigCharacters.
Expand All @@ -98,6 +101,12 @@ func (p BigTextPrinter) WithLetters(letters ...Letters) *BigTextPrinter {
return &p
}

// WithWriter sets the custom Writer.
func (p BigTextPrinter) WithWriter(writer io.Writer) *BigTextPrinter {
p.Writer = writer
return &p
}

// Srender renders the BigText as a string.
func (p BigTextPrinter) Srender() (string, error) {
var ret string
Expand Down Expand Up @@ -157,7 +166,7 @@ func (p BigTextPrinter) Srender() (string, error) {
// Render prints the BigText to the terminal.
func (p BigTextPrinter) Render() error {
s, _ := p.Srender()
Println(s)
Fprintln(p.Writer, s)

return nil
}
Expand Down
10 changes: 10 additions & 0 deletions bigtext_printer_test.go
Expand Up @@ -2,6 +2,7 @@ package pterm_test

import (
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -154,3 +155,12 @@ func TestDefaultLettersMaxHeight(t *testing.T) {
testza.AssertTrue(t, h <= maxHeight, fmt.Sprintf("'%s' is too high", s))
}
}

func TestBigTextPrinter_WithWriter(t *testing.T) {
p := pterm.BigTextPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)

testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}
16 changes: 12 additions & 4 deletions box_printer.go
Expand Up @@ -2,6 +2,7 @@ package pterm

import (
"fmt"
"io"
"strings"

"github.com/mattn/go-runewidth"
Expand Down Expand Up @@ -30,6 +31,7 @@ type BoxPrinter struct {
BottomPadding int
RightPadding int
LeftPadding int
Writer io.Writer
}

// DefaultBox is the default BoxPrinter.
Expand Down Expand Up @@ -211,6 +213,12 @@ func (p BoxPrinter) WithLeftPadding(padding int) *BoxPrinter {
return &p
}

// WithWriter sets the custom Writer.
func (p BoxPrinter) WithWriter(writer io.Writer) *BoxPrinter {
p.Writer = writer
return &p
}

// 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 BoxPrinter) Sprint(a ...interface{}) string {
Expand Down Expand Up @@ -299,7 +307,7 @@ func (p BoxPrinter) Sprintfln(format string, 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 (p BoxPrinter) Print(a ...interface{}) *TextPrinter {
Print(p.Sprint(a...))
Fprint(p.Writer, p.Sprint(a...))
tp := TextPrinter(p)
return &tp
}
Expand All @@ -308,15 +316,15 @@ func (p BoxPrinter) Print(a ...interface{}) *TextPrinter {
// 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 BoxPrinter) Println(a ...interface{}) *TextPrinter {
Print(p.Sprintln(a...))
Fprint(p.Writer, p.Sprintln(a...))
tp := TextPrinter(p)
return &tp
}

// 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 BoxPrinter) Printf(format string, a ...interface{}) *TextPrinter {
Print(p.Sprintf(format, a...))
Fprint(p.Writer, p.Sprintf(format, a...))
tp := TextPrinter(p)
return &tp
}
Expand All @@ -325,7 +333,7 @@ func (p BoxPrinter) Printf(format string, a ...interface{}) *TextPrinter {
// 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 BoxPrinter) Printfln(format string, a ...interface{}) *TextPrinter {
Print(p.Sprintfln(format, a...))
Fprint(p.Writer, p.Sprintfln(format, a...))
tp := TextPrinter(p)
return &tp
}
Expand Down
10 changes: 10 additions & 0 deletions box_printer_test.go
Expand Up @@ -3,6 +3,7 @@ package pterm_test
import (
"errors"
"io"
"os"
"testing"

"github.com/MarvinJWendt/testza"
Expand Down Expand Up @@ -334,3 +335,12 @@ func TestBoxPrinter_WithHorizontalString(t *testing.T) {
testza.AssertEqual(t, "-", p2.HorizontalString)
testza.AssertZero(t, p.HorizontalString)
}

func TestBoxPrinter_WithWriter(t *testing.T) {
p := pterm.BoxPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)

testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}
10 changes: 9 additions & 1 deletion bulletlist_printer.go
@@ -1,6 +1,7 @@
package pterm

import (
"io"
"strings"

"github.com/pterm/pterm/internal"
Expand Down Expand Up @@ -81,6 +82,7 @@ type BulletListPrinter struct {
TextStyle *Style
Bullet string
BulletStyle *Style
Writer io.Writer
}

// WithItems returns a new list with specific Items.
Expand All @@ -107,10 +109,16 @@ func (l BulletListPrinter) WithBulletStyle(style *Style) *BulletListPrinter {
return &l
}

// WithWriter sets the custom Writer.
func (l BulletListPrinter) WithWriter(writer io.Writer) *BulletListPrinter {
l.Writer = writer
return &l
}

// Render prints the list to the terminal.
func (l BulletListPrinter) Render() error {
s, _ := l.Srender()
Println(s)
Fprintln(l.Writer, s)

return nil
}
Expand Down
10 changes: 10 additions & 0 deletions bulletlist_printer_test.go
Expand Up @@ -3,6 +3,7 @@ package pterm_test
import (
"fmt"
"io"
"os"
"testing"

"github.com/MarvinJWendt/testza"
Expand Down Expand Up @@ -153,3 +154,12 @@ func TestNewBulletListFromString(t *testing.T) {

testza.AssertEqual(t, p, p2)
}

func TestBulletListPrinter_WithWriter(t *testing.T) {
p := pterm.BulletListPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)

testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}

0 comments on commit dc10c49

Please sign in to comment.