Skip to content

Commit

Permalink
Merge pull request #370 from pterm/366-move-ptermnewrgbfromhex-to-putils
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jun 14, 2022
2 parents d5300ae + 42b2af9 commit e493906
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 123 deletions.
10 changes: 5 additions & 5 deletions _examples/bigtext/demo/main.go
Expand Up @@ -8,16 +8,16 @@ import (
func main() {
// Print a large text with the LetterStyle from the standard theme.
// Useful for title screens.
pterm.DefaultBigText.WithLetters(putils.NewLettersFromString("PTerm")).Render()
pterm.DefaultBigText.WithLetters(putils.LettersFromString("PTerm")).Render()

// Print a large text with differently colored letters.
pterm.DefaultBigText.WithLetters(
putils.NewLettersFromStringWithStyle("P", pterm.NewStyle(pterm.FgCyan)),
putils.NewLettersFromStringWithStyle("Term", pterm.NewStyle(pterm.FgLightMagenta))).
putils.LettersFromStringWithStyle("P", pterm.NewStyle(pterm.FgCyan)),
putils.LettersFromStringWithStyle("Term", pterm.NewStyle(pterm.FgLightMagenta))).
Render()

// NewLettersFromStringWithRGB can be used to create a large text with a specific RGB color.
// LettersFromStringWithRGB can be used to create a large text with a specific RGB color.
pterm.DefaultBigText.WithLetters(
putils.NewLettersFromStringWithRGB("PTerm", pterm.NewRGB(255, 215, 0))).
putils.LettersFromStringWithRGB("PTerm", pterm.NewRGB(255, 215, 0))).
Render()
}
2 changes: 1 addition & 1 deletion _examples/bulletlist/demo/main.go
Expand Up @@ -15,7 +15,7 @@ func main() {
}).Render()

// Convert a text to a list and print it.
putils.NewBulletListFromString(`0
putils.BulletListFromString(`0
1
2
3`, " ").Render()
Expand Down
8 changes: 4 additions & 4 deletions _examples/demo/demo/main.go
Expand Up @@ -67,8 +67,8 @@ func main() {
pterm.Println()
area, _ := pterm.DefaultArea.WithCenter().Start() // Start the Area printer, with the Center option.
for i := 0; i < 10; i++ {
str, _ := pterm.DefaultBigText.WithLetters(putils.NewLettersFromString(time.Now().Format("15:04:05"))).Srender() // Save current time in str.
area.Update(str) // Update Area contents.
str, _ := pterm.DefaultBigText.WithLetters(putils.LettersFromString(time.Now().Format("15:04:05"))).Srender() // Save current time in str.
area.Update(str) // Update Area contents.
time.Sleep(time.Second)
}
area.Stop()
Expand Down Expand Up @@ -216,8 +216,8 @@ func setup() {

func introScreen() {
ptermLogo, _ := pterm.DefaultBigText.WithLetters(
putils.NewLettersFromStringWithStyle("P", pterm.NewStyle(pterm.FgLightCyan)),
putils.NewLettersFromStringWithStyle("Term", pterm.NewStyle(pterm.FgLightMagenta))).
putils.LettersFromStringWithStyle("P", pterm.NewStyle(pterm.FgLightCyan)),
putils.LettersFromStringWithStyle("Term", pterm.NewStyle(pterm.FgLightMagenta))).
Srender()

pterm.DefaultCenter.Print(ptermLogo)
Expand Down
2 changes: 1 addition & 1 deletion _examples/tree/demo/main.go
Expand Up @@ -33,7 +33,7 @@ func main() {
}

// Generate tree from LeveledList.
root := putils.NewTreeFromLeveledList(leveledList)
root := putils.TreeFromLeveledList(leveledList)

// Render TreePrinter
pterm.DefaultTree.WithRoot(root).Render()
Expand Down
43 changes: 36 additions & 7 deletions deprecated.go
@@ -1,6 +1,7 @@
package pterm

import (
"strconv"
"strings"

"github.com/pterm/pterm/internal"
Expand All @@ -9,14 +10,14 @@ import (
// NewLettersFromString creates a Letters object from a string, which is prefilled with the LetterStyle from ThemeDefault.
// You can override the ThemeDefault LetterStyle if you want to.
//
// Deprecated: use putils.NewLettersFromString instead.
// Deprecated: use putils.LettersFromString instead.
func NewLettersFromString(text string) Letters {
return NewLettersFromStringWithStyle(text, &ThemeDefault.LetterStyle)
}

// NewLettersFromStringWithStyle creates a Letters object from a string and applies a Style to it.
//
// Deprecated: use putils.NewLettersFromStringWithStyle instead.
// Deprecated: use putils.LettersFromStringWithStyle instead.
func NewLettersFromStringWithStyle(text string, style *Style) Letters {
s := strings.Split(text, "")
l := Letters{}
Expand All @@ -33,7 +34,7 @@ func NewLettersFromStringWithStyle(text string, style *Style) Letters {

// NewLettersFromStringWithRGB creates a Letters object from a string and applies an RGB color to it (overwrites style).
//
// Deprecated: use putils.NewLettersFromStringWithRGB instead.
// Deprecated: use putils.LettersFromStringWithRGB instead.
func NewLettersFromStringWithRGB(text string, rgb RGB) Letters {
s := strings.Split(text, "")
l := Letters{}
Expand All @@ -51,7 +52,7 @@ func NewLettersFromStringWithRGB(text string, rgb RGB) Letters {

// NewBulletListFromStrings returns a BulletListPrinter with Text using the NewTreeListItemFromString method.
//
// Deprecated: use putils.NewBulletListFromStrings instead.
// Deprecated: use putils.BulletListFromStrings instead.
func NewBulletListFromStrings(s []string, padding string) BulletListPrinter {
var lis []BulletListItem
for _, line := range s {
Expand All @@ -62,7 +63,7 @@ func NewBulletListFromStrings(s []string, padding string) BulletListPrinter {

// NewBulletListItemFromString returns a BulletListItem with a Text. The padding is counted in the Text to define the Level of the ListItem.
//
// Deprecated: use putils.NewBulletListItemFromString instead.
// Deprecated: use putils.BulletListItemFromString instead.
func NewBulletListItemFromString(text string, padding string) BulletListItem {
s, l := internal.RemoveAndCountPrefix(text, padding)
return BulletListItem{
Expand All @@ -73,14 +74,14 @@ func NewBulletListItemFromString(text string, padding string) BulletListItem {

// NewBulletListFromString returns a BulletListPrinter with Text using the NewTreeListItemFromString method, splitting after return (\n).
//
// Deprecated: use putils.NewBulletListFromString instead.
// Deprecated: use putils.BulletListFromString instead.
func NewBulletListFromString(s string, padding string) BulletListPrinter {
return NewBulletListFromStrings(strings.Split(s, "\n"), padding)
}

// NewTreeFromLeveledList converts a TreeItems list to a TreeNode and returns it.
//
// Deprecated: use putils.NewTreeFromLeveledList instead.
// Deprecated: use putils.TreeFromLeveledList instead.
func NewTreeFromLeveledList(leveledListItems LeveledList) TreeNode {
if len(leveledListItems) == 0 {
return TreeNode{}
Expand Down Expand Up @@ -117,3 +118,31 @@ func NewTreeFromLeveledList(leveledListItems LeveledList) TreeNode {

return *root
}

// NewRGBFromHEX converts a HEX and returns a new RGB.
//
// Deprecated: use putils.RGBFromHEX instead.
func NewRGBFromHEX(hex string) (RGB, error) {
hex = strings.ToLower(hex)
hex = strings.ReplaceAll(hex, "#", "")
hex = strings.ReplaceAll(hex, "0x", "")

if len(hex) == 3 {
hex = string([]byte{hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]})
}
if len(hex) != 6 {
return RGB{}, ErrHexCodeIsInvalid
}

i64, err := strconv.ParseInt(hex, 16, 32)
if err != nil {
return RGB{}, err
}
c := int(i64)

return RGB{
R: uint8(c >> 16),
G: uint8((c & 0x00FF00) >> 8),
B: uint8(c & 0x0000FF),
}, nil
}
31 changes: 31 additions & 0 deletions putils/bullet_list.go
@@ -0,0 +1,31 @@
package putils

import (
"strings"

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

// BulletListFromStrings returns a BulletListPrinter with Text using the NewTreeListItemFromString method.
func BulletListFromStrings(s []string, padding string) pterm.BulletListPrinter {
var lis []pterm.BulletListItem
for _, line := range s {
lis = append(lis, BulletListItemFromString(line, padding))
}
return *pterm.DefaultBulletList.WithItems(lis)
}

// BulletListItemFromString returns a BulletListItem with a Text. The padding is counted in the Text to define the Level of the ListItem.
func BulletListItemFromString(text string, padding string) pterm.BulletListItem {
s, l := internal.RemoveAndCountPrefix(text, padding)
return pterm.BulletListItem{
Level: l,
Text: s,
}
}

// BulletListFromString returns a BulletListPrinter with Text using the NewTreeListItemFromString method, splitting after return (\n).
func BulletListFromString(s string, padding string) pterm.BulletListPrinter {
return BulletListFromStrings(strings.Split(s, "\n"), padding)
}
44 changes: 44 additions & 0 deletions putils/letters_from_string.go
@@ -0,0 +1,44 @@
package putils

import (
"strings"

"github.com/pterm/pterm"
)

// LettersFromString creates a Letters object from a string, which is prefilled with the LetterStyle from ThemeDefault.
// You can override the ThemeDefault LetterStyle if you want to.
func LettersFromString(text string) pterm.Letters {
return LettersFromStringWithStyle(text, &pterm.ThemeDefault.LetterStyle)
}

// LettersFromStringWithStyle creates a Letters object from a string and applies a Style to it.
func LettersFromStringWithStyle(text string, style *pterm.Style) pterm.Letters {
s := strings.Split(text, "")
l := pterm.Letters{}

for _, s2 := range s {
l = append(l, pterm.Letter{
String: s2,
Style: style,
})
}

return l
}

// LettersFromStringWithRGB creates a Letters object from a string and applies an RGB color to it (overwrites style).
func LettersFromStringWithRGB(text string, rgb pterm.RGB) pterm.Letters {
s := strings.Split(text, "")
l := pterm.Letters{}

for _, s2 := range s {
l = append(l, pterm.Letter{
String: s2,
Style: &pterm.Style{},
RGB: rgb,
})
}

return l
}
31 changes: 0 additions & 31 deletions putils/new_bullet_list.go

This file was deleted.

44 changes: 0 additions & 44 deletions putils/new_letters_from_string.go

This file was deleted.

35 changes: 35 additions & 0 deletions putils/rgb_from_hex.go
@@ -0,0 +1,35 @@
package putils

import (
"strconv"
"strings"

"github.com/pterm/pterm"
)

// RGBFromHEX converts a HEX and returns a new RGB.
// If the hex code is not valid it returns pterm.ErrHexCodeIsInvalid.
func RGBFromHEX(hex string) (pterm.RGB, error) {
hex = strings.ToLower(hex)
hex = strings.ReplaceAll(hex, "#", "")
hex = strings.ReplaceAll(hex, "0x", "")

if len(hex) == 3 {
hex = string([]byte{hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]})
}
if len(hex) != 6 {
return pterm.RGB{}, pterm.ErrHexCodeIsInvalid
}

i64, err := strconv.ParseInt(hex, 16, 32)
if err != nil {
return pterm.RGB{}, err
}
c := int(i64)

return pterm.RGB{
R: uint8(c >> 16),
G: uint8((c & 0x00FF00) >> 8),
B: uint8(c & 0x0000FF),
}, nil
}
Expand Up @@ -2,8 +2,8 @@ package putils

import "github.com/pterm/pterm"

// NewTreeFromLeveledList converts a TreeItems list to a TreeNode and returns it.
func NewTreeFromLeveledList(leveledListItems pterm.LeveledList) pterm.TreeNode {
// TreeFromLeveledList converts a TreeItems list to a TreeNode and returns it.
func TreeFromLeveledList(leveledListItems pterm.LeveledList) pterm.TreeNode {
if len(leveledListItems) == 0 {
return pterm.TreeNode{}
}
Expand Down

0 comments on commit e493906

Please sign in to comment.