Skip to content

Commit

Permalink
fix width issue for list items
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Nov 6, 2023
1 parent 6da3fd9 commit efa39cf
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions internal/tui/listitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,24 @@ func RenderItem(title string, subtitle string, accessories []string, width int,

subtitle = strings.Split(subtitle, "\n")[0]
subtitle = " " + subtitle
var blanks string

accessory := " " + strings.Join(accessories, " · ")

var blanks string
// If the width is too small, we need to truncate the subtitle, accessory, or title (in that order)
if width >= lipgloss.Width(title+subtitle+accessory) {
availableWidth := width - lipgloss.Width(title+subtitle+accessory)
blanks = strings.Repeat(" ", availableWidth)
} else if width >= lipgloss.Width(title+accessory) {
subtitle = subtitle[:width-lipgloss.Width(title+accessory)]
} else if width >= lipgloss.Width(accessory) {
subtitle = ""
title = title[:width-lipgloss.Width(accessory)]
extraWidth := width - lipgloss.Width(title+subtitle+accessory)
blanks = strings.Repeat(" ", extraWidth)
} else {
subtitle = ""
accessory = ""
title = title[:min(len(title), width)]
for width != lipgloss.Width(title+subtitle+accessory) {
extraWidth := lipgloss.Width(title+subtitle+accessory) - width
if lipgloss.Width(subtitle) > 0 {
subtitle = subtitle[:max(0, len(subtitle)-extraWidth)]
} else if lipgloss.Width(title) > 0 {
title = title[:max(0, len(title)-extraWidth)]
} else {
accessory = accessory[:max(0, len(accessory)-extraWidth)]
}
}
}

title = titleStyle.Render(title)
Expand Down

0 comments on commit efa39cf

Please sign in to comment.