Skip to content

Commit

Permalink
remove from area printer due to more complex requirements. SAdd to ba…
Browse files Browse the repository at this point in the history
…sic text printer
  • Loading branch information
Brooke Hatton committed Mar 17, 2022
1 parent 8833eaa commit fd47a90
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
17 changes: 6 additions & 11 deletions area_printer.go
Expand Up @@ -2,23 +2,24 @@ package pterm

import (
"io"
"os"
"strings"

"github.com/atomicgo/cursor"

"github.com/pterm/pterm/internal"
)

// DefaultArea is the default area printer.
var DefaultArea = AreaPrinter{
Writer: os.Stdout,
type Writer interface {
io.Writer
Fd() uintptr
}

// DefaultArea is the default area printer.
var DefaultArea = AreaPrinter{}

// AreaPrinter prints an area which can be updated easily.
// use this printer for live output like charts, algorithm visualizations, simulations and even games.
type AreaPrinter struct {
Writer io.Writer
RemoveWhenDone bool
Fullscreen bool
Center bool
Expand Down Expand Up @@ -52,12 +53,6 @@ func (p AreaPrinter) WithCenter(b ...bool) *AreaPrinter {
return &p
}

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

// Update overwrites the content of the AreaPrinter.
// Can be used live.
func (p *AreaPrinter) Update(text ...interface{}) {
Expand Down
34 changes: 23 additions & 11 deletions basic_text_printer.go
@@ -1,16 +1,23 @@
package pterm

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

var (
// DefaultBasicText returns a default BasicTextPrinter, which can be used to print text as is.
// No default style is present for BasicTextPrinter.
DefaultBasicText = BasicTextPrinter{}
DefaultBasicText = BasicTextPrinter{
Writer: os.Stdout,
}
)

// 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 +26,11 @@ func (p BasicTextPrinter) WithStyle(style *Style) *BasicTextPrinter {
return &p
}

func (p BasicTextPrinter) WithCustomWriter(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 +58,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

0 comments on commit fd47a90

Please sign in to comment.