Skip to content

Commit

Permalink
pterm#296 NewDefaultBarChart() constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiaszCudnik committed Nov 15, 2021
1 parent d673e99 commit bf3aea8
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func main() {
},
}

_ = pterm.DefaultBarChart.WithBars(bars).Render()
_ = pterm.DefaultBarChart.WithHorizontal().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithHorizontal().WithBars(bars).Render()
}

```
Expand Down
4 changes: 2 additions & 2 deletions _examples/barchart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func main() {
},
}

_ = pterm.DefaultBarChart.WithBars(bars).Render()
_ = pterm.DefaultBarChart.WithHorizontal().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithHorizontal().WithBars(bars).Render()
}

```
4 changes: 2 additions & 2 deletions _examples/barchart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func main() {
},
}

_ = pterm.DefaultBarChart.WithBars(bars).Render()
_ = pterm.DefaultBarChart.WithHorizontal().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithHorizontal().WithBars(bars).Render()
}
7 changes: 3 additions & 4 deletions barchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ type BarChartPrinter struct {
HorizontalBarCharacter string
}

var (
// DefaultBarChart is the default BarChartPrinter.
DefaultBarChart = BarChartPrinter{
func NewDefaultBarChart() BarChartPrinter {
return BarChartPrinter{
Horizontal: false,
VerticalBarCharacter: "██",
HorizontalBarCharacter: "█",
Height: GetTerminalHeight() * 2 / 3,
Width: GetTerminalWidth() * 2 / 3,
}
)
}

// WithBars returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithBars(bars Bars) *BarChartPrinter {
Expand Down
20 changes: 10 additions & 10 deletions barchart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBarChartPrinterNilStylePrint(t *testing.T) {
},
}

printer := pterm.DefaultBarChart.WithBars(bars)
printer := pterm.NewDefaultBarChart().WithBars(bars)
err := printer.Render()
if err != nil {
panic(err)
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestBarChartPrinter_RenderExample(t *testing.T) {
},
}

printer := pterm.DefaultBarChart.WithBars(bars)
printer := pterm.NewDefaultBarChart().WithBars(bars)
printer.Render()
})
testza.SnapshotCreateOrValidate(t, t.Name(), content)
Expand All @@ -90,15 +90,15 @@ func TestBarChartPrinter_RenderExampleRawOutput(t *testing.T) {
},
}

printer := pterm.DefaultBarChart.WithBars(bars)
printer := pterm.NewDefaultBarChart().WithBars(bars)
printer.Render()
})
testza.SnapshotCreateOrValidate(t, t.Name(), content)
pterm.EnableStyling()
}

func TestBarChartPrinter_RenderMultipleLineLabel(t *testing.T) {
printer := pterm.DefaultBarChart.WithShowValue().WithBars(pterm.Bars{
printer := pterm.NewDefaultBarChart().WithShowValue().WithBars(pterm.Bars{
pterm.Bar{
Label: "Test",
Value: -1337,
Expand All @@ -123,7 +123,7 @@ func TestBarChartPrinter_RenderMultipleLineLabel(t *testing.T) {

func TestBarChartPrinter_RenderNegativeBarValues(t *testing.T) {
content := captureStdout(func(w io.Writer) {
printer := pterm.DefaultBarChart.WithShowValue().WithBars(pterm.Bars{
printer := pterm.NewDefaultBarChart().WithShowValue().WithBars(pterm.Bars{
pterm.Bar{
Label: "Test",
Value: -1337,
Expand All @@ -141,7 +141,7 @@ func TestBarChartPrinter_RenderNegativeBarValues(t *testing.T) {
}

func TestBarChartPrinter_RenderZeroBarValuesHorizontal(t *testing.T) {
printer := pterm.DefaultBarChart.WithShowValue().WithHorizontal().WithBars(pterm.Bars{
printer := pterm.NewDefaultBarChart().WithShowValue().WithHorizontal().WithBars(pterm.Bars{
pterm.Bar{
Label: "Test",
Value: 0,
Expand All @@ -159,7 +159,7 @@ func TestBarChartPrinter_RenderZeroBarValuesHorizontal(t *testing.T) {
}

func TestBarChartPrinter_RenderZeroBarValues(t *testing.T) {
printer := pterm.DefaultBarChart.WithShowValue().WithBars(pterm.Bars{
printer := pterm.NewDefaultBarChart().WithShowValue().WithBars(pterm.Bars{
pterm.Bar{
Label: "Test",
Value: 0,
Expand All @@ -178,7 +178,7 @@ func TestBarChartPrinter_RenderZeroBarValues(t *testing.T) {

func TestBarChartPrinter_RenderLowBarValues(t *testing.T) {
content := captureStdout(func(w io.Writer) {
printer := pterm.DefaultBarChart.WithShowValue().WithBars(pterm.Bars{
printer := pterm.NewDefaultBarChart().WithShowValue().WithBars(pterm.Bars{
pterm.Bar{
Label: "Test",
Value: 1,
Expand All @@ -197,7 +197,7 @@ func TestBarChartPrinter_RenderLowBarValues(t *testing.T) {

func TestBarChartPrinter_Render(t *testing.T) {
content := captureStdout(func(w io.Writer) {
printer := pterm.DefaultBarChart.WithShowValue().WithBars(pterm.Bars{
printer := pterm.NewDefaultBarChart().WithShowValue().WithBars(pterm.Bars{
pterm.Bar{
Label: "Test",
Value: 1337,
Expand All @@ -216,7 +216,7 @@ func TestBarChartPrinter_Render(t *testing.T) {

func TestBarChartPrinter_RenderHorizonzal(t *testing.T) {
content := captureStdout(func(w io.Writer) {
printer := pterm.DefaultBarChart.WithShowValue().WithHorizontal().WithBars(pterm.Bars{
printer := pterm.NewDefaultBarChart().WithShowValue().WithHorizontal().WithBars(pterm.Bars{
pterm.Bar{
Label: "Test",
Value: 1337,
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func main() {
},
}

_ = pterm.DefaultBarChart.WithBars(bars).Render()
_ = pterm.DefaultBarChart.WithHorizontal().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithHorizontal().WithBars(bars).Render()
}

```
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/printer/barchart.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ bars := pterm.Bars{
},
}

_ = pterm.DefaultBarChart.WithBars(bars).Render()
_ = pterm.NewDefaultBarChart().WithBars(bars).Render()
```

### Options

> To make a copy with modified options you can use:
> `pterm.DefaultBarChart.WithOptionName(option)`
> `pterm.NewDefaultBarChart().WithOptionName(option)`
>
> To change multiple options at once, you can chain the functions:
> `pterm.DefaultBarChart.WithOptionName(option).WithOptionName2(option2)...`
> `pterm.NewDefaultBarChart().WithOptionName(option).WithOptionName2(option2)...`
> [!TIP]
> Click the options and types to show the documentation on _pkg.go.dev_
Expand Down
2 changes: 1 addition & 1 deletion pterm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestInterfaceImplementation(t *testing.T) {
// If a printer doesn't fit into the slice, the printer doesn't has the right interface anymore.
_ = []pterm.TextPrinter{&pterm.DefaultBasicText, pterm.DefaultBox, pterm.DefaultCenter, &pterm.DefaultHeader, &pterm.DefaultParagraph, &pterm.Info, &pterm.DefaultSection, pterm.FgRed, pterm.NewRGB(0, 0, 0)}
_ = []pterm.LivePrinter{pterm.DefaultProgressbar, &pterm.DefaultSpinner}
_ = []pterm.RenderPrinter{pterm.DefaultBarChart, pterm.DefaultBigText, pterm.DefaultBulletList, pterm.DefaultPanel, pterm.DefaultTable, pterm.DefaultTree}
_ = []pterm.RenderPrinter{pterm.NewDefaultBarChart(), pterm.DefaultBigText, pterm.DefaultBulletList, pterm.DefaultPanel, pterm.DefaultTable, pterm.DefaultTree}
}

// Acceptance test
Expand Down
10 changes: 8 additions & 2 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ var FallbackTerminalWidth = 80
// You can override that value if necessary.
var FallbackTerminalHeight = 10

// forcedTerminalWidth, when set along with forcedTerminalHeight, forces the terminal width value for GetTerminalSize().
// forcedTerminalWidth, when set along with forcedTerminalHeight, forces the terminal width value.
var forcedTerminalWidth int = 0

// forcedTerminalHeight, when set along with forcedTerminalWidth, forces the terminal height value for GetTerminalSize().
// forcedTerminalHeight, when set along with forcedTerminalWidth, forces the terminal height value.
var forcedTerminalHeight int = 0

// GetTerminalWidth returns the terminal width of the active terminal.
func GetTerminalWidth() int {
if forcedTerminalWidth > 0 {
return forcedTerminalWidth
}
width, _, _ := GetTerminalSize()
return width
}

// GetTerminalHeight returns the terminal height of the active terminal.
func GetTerminalHeight() int {
if forcedTerminalHeight > 0 {
return forcedTerminalHeight
}
_, height, _ := GetTerminalSize()
return height
}
Expand Down

0 comments on commit bf3aea8

Please sign in to comment.