Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(color): added color.ToStyle() #435

Merged
merged 2 commits into from Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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