Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(box): fixed wrong title length calculation #527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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