Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from pterm/366-move-ptermnewrgbfromhex-to-putils
- Loading branch information
Showing
12 changed files
with
159 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.