Skip to content

Commit

Permalink
Merge pull request #435 from pterm/350-add-tostyle-to-colors
Browse files Browse the repository at this point in the history
feat(color): added `color.ToStyle()`
  • Loading branch information
MarvinJWendt committed Jan 5, 2023
2 parents 77b8ea1 + 53a891d commit 113497a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -3,6 +3,14 @@
> This document explains how to participate in the development of PTerm.\
If your goal is to report a bug instead of programming PTerm, you can do so [here](https://github.com/pterm/pterm/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc).

## Best practise

We enforce some best practises, especially made for PTerm, to provide a clean and consistent user experience.

### Styles

Styles should always be consumed as pointers. That way, the user can change the style of printers globally.

## Creating a new printer

> In this chapter we will show you how to create a new printer.
Expand Down
5 changes: 5 additions & 0 deletions color.go
Expand Up @@ -246,6 +246,11 @@ func (c Color) String() string {
return fmt.Sprintf("%d", c)
}

// ToStyle converts the color to a style.
func (c Color) ToStyle() *Style {
return &Style{c}
}

// Style is a collection of colors.
// Can include foreground, background and styling (eg. Bold, Underscore, etc.) colors.
type Style []Color
Expand Down
6 changes: 6 additions & 0 deletions color_test.go
Expand Up @@ -152,7 +152,13 @@ func TestColorPrinterPrintMethods(t *testing.T) {
}

func TestNewStyle(t *testing.T) {
s := pterm.NewStyle(pterm.FgRed, pterm.BgBlue, pterm.Bold)
testza.AssertEqual(t, s, &pterm.Style{pterm.FgRed, pterm.BgBlue, pterm.Bold})
}

func TestColor_ToStyle(t *testing.T) {
s := pterm.FgCyan.ToStyle()
testza.AssertEqual(t, s, &pterm.Style{pterm.FgCyan})
}

func TestStyle_Add(t *testing.T) {
Expand Down

0 comments on commit 113497a

Please sign in to comment.