Skip to content

Commit

Permalink
Merge pull request #527 from pterm/526-wrong-print-of-box-with-title-…
Browse files Browse the repository at this point in the history
…including-asian-characters
  • Loading branch information
MarvinJWendt committed Jun 18, 2023
2 parents 20d3f63 + 0a7e718 commit 156868c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
4 changes: 2 additions & 2 deletions box_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ func (p BoxPrinter) Sprint(a ...interface{}) string {
maxWidth+p.LeftPadding+p.RightPadding) + p.BoxStyle.Sprint(p.TopLeftCornerString)
} else {
p.Title = strings.ReplaceAll(p.Title, "\n", " ")
if (maxWidth + p.RightPadding + p.LeftPadding - 4) < len(RemoveColorFromString(p.Title)) {
p.RightPadding = len(RemoveColorFromString(p.Title)) - (maxWidth + p.RightPadding + p.LeftPadding - 5)
if (maxWidth + p.RightPadding + p.LeftPadding - 4) < internal.GetStringMaxWidth(p.Title) {
p.RightPadding = internal.GetStringMaxWidth(p.Title) - (maxWidth + p.RightPadding + p.LeftPadding - 5)
}
if p.TitleTopLeft {
topLine = p.BoxStyle.Sprint(p.BottomRightCornerString) + internal.AddTitleToLine(p.Title, p.BoxStyle.Sprint(p.HorizontalString), maxWidth+p.LeftPadding+p.RightPadding, true) + p.BoxStyle.Sprint(p.BottomLeftCornerString)
Expand Down
8 changes: 3 additions & 5 deletions internal/title_in_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package internal

import (
"strings"

"github.com/gookit/color"
)

// AddTitleToLine adds a title to a site of a line ex: "─ This is the title ──────"
func AddTitleToLine(title, line string, length int, left bool) string {
var ret string
if left {
ret += line + " " + title + " " + line + strings.Repeat(line, length-(4+len(color.ClearCode(title))))
ret += line + " " + title + " " + line + strings.Repeat(line, length-(4+GetStringMaxWidth(title)))
} else {
ret += strings.Repeat(line, length-(4+len(color.ClearCode(title)))) + line + " " + title + " " + line
ret += strings.Repeat(line, length-(4+GetStringMaxWidth(title))) + line + " " + title + " " + line
}

return ret
Expand All @@ -21,7 +19,7 @@ func AddTitleToLine(title, line string, length int, left bool) string {
// AddTitleToLineCenter adds a title to the center of a line ex: "─ This is the title ──────"
func AddTitleToLineCenter(title, line string, length int) string {
var ret string
repeatString := length - (4 + len(color.ClearCode(title)))
repeatString := length - (4 + GetStringMaxWidth(title))
unevenRepeatString := repeatString % 2

ret += strings.Repeat(line, repeatString/2) + line + " " + title + " " + line + strings.Repeat(line, repeatString/2+unevenRepeatString)
Expand Down

0 comments on commit 156868c

Please sign in to comment.