From 027180c81ce258e7aa88f84ccaa79bbc485372d6 Mon Sep 17 00:00:00 2001 From: Marvin Wendt Date: Tue, 14 Jun 2022 16:32:13 +0200 Subject: [PATCH 1/2] moved `NewRGBFromHex` to `putils` --- deprecated.go | 29 +++++++++++++++++++++++++++++ putils/rgb_from_hex.go | 35 +++++++++++++++++++++++++++++++++++ rgb.go | 28 ---------------------------- 3 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 putils/rgb_from_hex.go diff --git a/deprecated.go b/deprecated.go index 0ca3d8869..21d73fea5 100644 --- a/deprecated.go +++ b/deprecated.go @@ -1,6 +1,7 @@ package pterm import ( + "strconv" "strings" "github.com/pterm/pterm/internal" @@ -117,3 +118,31 @@ func NewTreeFromLeveledList(leveledListItems LeveledList) TreeNode { return *root } + +// NewRGBFromHEX converts a HEX and returns a new RGB. +// +// Deprecated: use putils.NewRGBFromHEX 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 +} diff --git a/putils/rgb_from_hex.go b/putils/rgb_from_hex.go new file mode 100644 index 000000000..05cc7f2d3 --- /dev/null +++ b/putils/rgb_from_hex.go @@ -0,0 +1,35 @@ +package putils + +import ( + "strconv" + "strings" + + "github.com/pterm/pterm" +) + +// NewRGBFromHEX converts a HEX and returns a new RGB. +// If the hex code is not valid it returns pterm.ErrHexCodeIsInvalid. +func NewRGBFromHEX(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 +} diff --git a/rgb.go b/rgb.go index 02acce075..fee602a5b 100644 --- a/rgb.go +++ b/rgb.go @@ -2,8 +2,6 @@ package pterm import ( "fmt" - "strconv" - "strings" "github.com/gookit/color" @@ -29,32 +27,6 @@ func NewRGB(r, g, b uint8) RGB { return RGB{R: r, G: g, B: b} } -// NewRGBFromHEX converts a HEX and returns a new RGB. -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 -} - // Fade fades one RGB value (over other RGB values) to another RGB value, by giving the function a minimum, maximum and current value. func (p RGB) Fade(min, max, current float32, end ...RGB) RGB { if min < 0 { From 24fab6b57c79b7c9e6c6f317296fc94e5b75dcdc Mon Sep 17 00:00:00 2001 From: Marvin Wendt Date: Tue, 14 Jun 2022 16:39:39 +0200 Subject: [PATCH 2/2] renamed putils function to not start with `New` --- _examples/bigtext/demo/main.go | 10 ++--- _examples/bulletlist/demo/main.go | 2 +- _examples/demo/demo/main.go | 8 ++-- _examples/tree/demo/main.go | 2 +- deprecated.go | 16 +++---- putils/bullet_list.go | 31 +++++++++++++ putils/letters_from_string.go | 44 +++++++++++++++++++ putils/new_bullet_list.go | 31 ------------- putils/new_letters_from_string.go | 44 ------------------- putils/rgb_from_hex.go | 4 +- ...eled_list.go => tree_from_leveled_list.go} | 4 +- 11 files changed, 98 insertions(+), 98 deletions(-) create mode 100644 putils/bullet_list.go create mode 100644 putils/letters_from_string.go delete mode 100644 putils/new_bullet_list.go delete mode 100644 putils/new_letters_from_string.go rename putils/{new_tree_from_leveled_list.go => tree_from_leveled_list.go} (82%) diff --git a/_examples/bigtext/demo/main.go b/_examples/bigtext/demo/main.go index 2250a8f0e..4457957cb 100644 --- a/_examples/bigtext/demo/main.go +++ b/_examples/bigtext/demo/main.go @@ -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() } diff --git a/_examples/bulletlist/demo/main.go b/_examples/bulletlist/demo/main.go index c8ac041df..b1cab805d 100644 --- a/_examples/bulletlist/demo/main.go +++ b/_examples/bulletlist/demo/main.go @@ -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() diff --git a/_examples/demo/demo/main.go b/_examples/demo/demo/main.go index 7e96b2c94..a055dca39 100644 --- a/_examples/demo/demo/main.go +++ b/_examples/demo/demo/main.go @@ -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() @@ -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) diff --git a/_examples/tree/demo/main.go b/_examples/tree/demo/main.go index efc0e5175..6de095717 100644 --- a/_examples/tree/demo/main.go +++ b/_examples/tree/demo/main.go @@ -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() diff --git a/deprecated.go b/deprecated.go index 21d73fea5..912ebd7e9 100644 --- a/deprecated.go +++ b/deprecated.go @@ -10,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{} @@ -34,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{} @@ -52,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 { @@ -63,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{ @@ -74,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{} @@ -121,7 +121,7 @@ func NewTreeFromLeveledList(leveledListItems LeveledList) TreeNode { // NewRGBFromHEX converts a HEX and returns a new RGB. // -// Deprecated: use putils.NewRGBFromHEX instead. +// Deprecated: use putils.RGBFromHEX instead. func NewRGBFromHEX(hex string) (RGB, error) { hex = strings.ToLower(hex) hex = strings.ReplaceAll(hex, "#", "") diff --git a/putils/bullet_list.go b/putils/bullet_list.go new file mode 100644 index 000000000..0e61dda47 --- /dev/null +++ b/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) +} diff --git a/putils/letters_from_string.go b/putils/letters_from_string.go new file mode 100644 index 000000000..42076ff26 --- /dev/null +++ b/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 +} diff --git a/putils/new_bullet_list.go b/putils/new_bullet_list.go deleted file mode 100644 index 740006f64..000000000 --- a/putils/new_bullet_list.go +++ /dev/null @@ -1,31 +0,0 @@ -package putils - -import ( - "strings" - - "github.com/pterm/pterm" - "github.com/pterm/pterm/internal" -) - -// NewBulletListFromStrings returns a BulletListPrinter with Text using the NewTreeListItemFromString method. -func NewBulletListFromStrings(s []string, padding string) pterm.BulletListPrinter { - var lis []pterm.BulletListItem - for _, line := range s { - lis = append(lis, NewBulletListItemFromString(line, padding)) - } - return *pterm.DefaultBulletList.WithItems(lis) -} - -// NewBulletListItemFromString returns a BulletListItem with a Text. The padding is counted in the Text to define the Level of the ListItem. -func NewBulletListItemFromString(text string, padding string) pterm.BulletListItem { - s, l := internal.RemoveAndCountPrefix(text, padding) - return pterm.BulletListItem{ - Level: l, - Text: s, - } -} - -// NewBulletListFromString returns a BulletListPrinter with Text using the NewTreeListItemFromString method, splitting after return (\n). -func NewBulletListFromString(s string, padding string) pterm.BulletListPrinter { - return NewBulletListFromStrings(strings.Split(s, "\n"), padding) -} diff --git a/putils/new_letters_from_string.go b/putils/new_letters_from_string.go deleted file mode 100644 index e454636b7..000000000 --- a/putils/new_letters_from_string.go +++ /dev/null @@ -1,44 +0,0 @@ -package putils - -import ( - "strings" - - "github.com/pterm/pterm" -) - -// 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. -func NewLettersFromString(text string) pterm.Letters { - return NewLettersFromStringWithStyle(text, &pterm.ThemeDefault.LetterStyle) -} - -// NewLettersFromStringWithStyle creates a Letters object from a string and applies a Style to it. -func NewLettersFromStringWithStyle(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 -} - -// NewLettersFromStringWithRGB creates a Letters object from a string and applies an RGB color to it (overwrites style). -func NewLettersFromStringWithRGB(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 -} diff --git a/putils/rgb_from_hex.go b/putils/rgb_from_hex.go index 05cc7f2d3..ea63473ac 100644 --- a/putils/rgb_from_hex.go +++ b/putils/rgb_from_hex.go @@ -7,9 +7,9 @@ import ( "github.com/pterm/pterm" ) -// NewRGBFromHEX converts a HEX and returns a new RGB. +// RGBFromHEX converts a HEX and returns a new RGB. // If the hex code is not valid it returns pterm.ErrHexCodeIsInvalid. -func NewRGBFromHEX(hex string) (pterm.RGB, error) { +func RGBFromHEX(hex string) (pterm.RGB, error) { hex = strings.ToLower(hex) hex = strings.ReplaceAll(hex, "#", "") hex = strings.ReplaceAll(hex, "0x", "") diff --git a/putils/new_tree_from_leveled_list.go b/putils/tree_from_leveled_list.go similarity index 82% rename from putils/new_tree_from_leveled_list.go rename to putils/tree_from_leveled_list.go index 34419b6c2..59b684c12 100644 --- a/putils/new_tree_from_leveled_list.go +++ b/putils/tree_from_leveled_list.go @@ -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{} }