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

feat: Update new border info api (show information on bottom border) #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions borders.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,24 @@ func (s Style) applyBorder(str string) string {

// Render bottom
if hasBottom {
bottom := renderHorizontalEdge(border.BottomLeft, border.Bottom, border.BottomRight, width)
bottom = s.styleBorder(bottom, bottomFG, bottomBG)
bottom := ""
info := s.GetBorderInfoTitle()

// Render border info if user set
if len(strings.TrimSpace(info)) > 0 {
infoStyle := s.GetBorderInfoStyle().Copy().MaxWidth(width)
if infoStyle.GetHorizontalPadding() == 0 {
infoStyle = infoStyle.Padding(0, 1)
}

beforeInfo, afterInfo := renderBorderContent(info, infoStyle, border, width)
bottom = s.styleBorder(beforeInfo, bottomFG, bottomBG) +
infoStyle.Render(info) +
s.styleBorder(afterInfo, bottomFG, bottomBG)
} else {
bottom = renderHorizontalEdge(border.BottomLeft, border.Bottom, border.BottomRight, width)
bottom = s.styleBorder(bottom, bottomFG, bottomBG)
}
out.WriteRune('\n')
out.WriteString(bottom)
}
Expand Down Expand Up @@ -401,6 +417,29 @@ func renderHorizontalEdge(left, middle, right string, width int) string {
return out.String()
}

// Render border info or border ttitle.
func renderBorderContent(content string, contentStyle Style, border Border, width int) (beforeContent, afterContent string) {
const sideCount = 2

contentLen := contentStyle.GetHorizontalFrameSize() + ansi.StringWidth(content)
beforeContent = border.BottomLeft
afterContent = border.BottomRight

switch contentStyle.GetAlignHorizontal() {
case Right:
beforeContent = border.BottomLeft + strings.Repeat(border.Top, max(0, width-1-contentLen))
case Center:
noContentLen := width - 1 - contentLen
noContentLen2 := noContentLen / sideCount
beforeContent = border.BottomLeft + strings.Repeat(border.Top, max(0, noContentLen2))
afterContent = strings.Repeat(border.Top, max(0, noContentLen-noContentLen2)) + border.BottomRight
case Left:
afterContent = strings.Repeat(border.Top, max(0, width-1-contentLen)) + border.BottomRight
}

return beforeContent, afterContent
}

// Apply foreground and background styling to a border.
func (s Style) styleBorder(border string, fg, bg TerminalColor) string {
if fg == noColor && bg == noColor {
Expand Down
26 changes: 26 additions & 0 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ func (s Style) GetTransform() func(string) string {
return s.getAsTransform(transformKey)
}

func (s Style) GetBorderInfoStyle() Style {
return s.getAsStyle(borderInfoStyleKey)
}

func (s Style) GetBorderInfoTitle() string {
return s.getAsString(borderInfoKey)
}

// Returns whether or not the given property is set.
func (s Style) isSet(k propKey) bool {
_, exists := s.rules[k]
Expand Down Expand Up @@ -486,6 +494,24 @@ func (s Style) getAsTransform(k propKey) func(string) string {
return nil
}

func (s Style) getAsString(k propKey) string {
if v, ok := s.rules[k]; ok {
if s, ok := v.(string); ok {
return s
}
}
return ""
}

func (s Style) getAsStyle(k propKey) Style {
if v, ok := s.rules[k]; ok {
if s, ok := v.(Style); ok {
return s
}
}
return NewStyle()
}

// Split a string into lines, additionally returning the size of the widest
// line.
func getLines(s string) (lines []string, widest int) {
Expand Down
12 changes: 12 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,18 @@ func (s Style) BorderLeftBackground(c TerminalColor) Style {
return s
}

// BorderInfoStyle set border info style
func (s Style) BorderInfoStyle(style Style) Style {
s.set(borderInfoStyleKey, style)
return s
}

// BorderInfo set border info
func (s Style) BorderInfo(info string) Style {
s.set(borderInfoKey, info)
return s
}

// Inline makes rendering output one line and disables the rendering of
// margins, padding and borders. This is useful when you need a style to apply
// only to font rendering and don't want it to change any physical dimensions.
Expand Down
3 changes: 3 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const (
borderBottomBackgroundKey
borderLeftBackgroundKey

borderInfoStyleKey
borderInfoKey

inlineKey
maxWidthKey
maxHeightKey
Expand Down