Skip to content

Commit

Permalink
Feat [Terminal] [Colorize] Italic Formatting
Browse files Browse the repository at this point in the history
- [+] feat(colorize.go): add support for italic formatting in text surrounded by single asterisks
- [+] fix(colorize.go): adjust delimiter check to include a trailing space for list items
  • Loading branch information
H0llyW00dzZ committed Feb 10, 2024
1 parent 0fcece8 commit ae4e8e8
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions terminal/colorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,29 @@ func SingleCharColorize(text string, delimiter string, color string) string {
lines := strings.Split(text, StringNewLine)
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
if strings.HasPrefix(trimmedLine, delimiter) {
// Check for list items, which start with the delimiter followed by a space.
if strings.HasPrefix(trimmedLine, delimiter+" ") {
// Colorize the delimiter and the following space if it's a list item
result.WriteString(color)
result.WriteString(trimmedLine[:1])
result.WriteString(colors.ColorReset)
result.WriteString(trimmedLine[1:])
} else {
// No coloring needed
result.WriteString(trimmedLine)
// Apply italic formatting to the line.
processedLine := ApplyItalic(trimmedLine)
result.WriteString(processedLine)
}
result.WriteRune(nl.NewLineChars)
}
return strings.TrimRight(result.String(), StringNewLine)
}

// ApplyItalic applies italic formatting to text surrounded by single asterisks.
// It uses the global italicPattern variable to identify and format italic text.
func ApplyItalic(text string) string {
return italicAnsiRegex.ReplaceAllStringFunc(text, func(match string) string {
// Remove the asterisks and apply italic formatting to the inner text.
innerContent := match[1 : len(match)-1] // Strip the asterisks
return colors.ColorHex95b806 + ItalicText + innerContent + ResetItalicText + colors.ColorReset
})
}

0 comments on commit ae4e8e8

Please sign in to comment.