fix: selection item truncate string

Ayman Bagabas created

Change summary

ui/common/utils.go         | 11 +++++++++++
ui/pages/repo/filesitem.go |  2 +-
ui/pages/repo/logitem.go   | 12 ++----------
ui/pages/repo/refsitem.go  |  2 +-
ui/pages/selection/item.go | 10 ++++++++--
5 files changed, 23 insertions(+), 14 deletions(-)

Detailed changes

ui/common/utils.go 🔗

@@ -0,0 +1,11 @@
+package common
+
+import "github.com/muesli/reflow/truncate"
+
+// TruncateString is a convenient wrapper around truncate.TruncateString.
+func TruncateString(s string, max int) string {
+	if max < 0 {
+		max = 0
+	}
+	return truncate.StringWithTail(s, uint(max), "…")
+}

ui/pages/repo/filesitem.go 🔗

@@ -122,7 +122,7 @@ func (d FileItemDelegate) Render(w io.Writer, m list.Model, index int, listItem
 		s.TreeFileMode.GetWidth() +
 		cs.GetMarginLeft()
 	rightMargin := s.TreeFileSize.GetMarginLeft() + lipgloss.Width(size)
-	name = truncateString(name, m.Width()-leftMargin-rightMargin, "…")
+	name = common.TruncateString(name, m.Width()-leftMargin-rightMargin)
 	sizeStyle := s.TreeFileSize.Copy().
 		Width(m.Width() -
 			leftMargin -

ui/pages/repo/logitem.go 🔗

@@ -12,7 +12,6 @@ import (
 	"github.com/charmbracelet/lipgloss"
 	"github.com/charmbracelet/soft-serve/git"
 	"github.com/charmbracelet/soft-serve/ui/common"
-	"github.com/muesli/reflow/truncate"
 )
 
 // LogItem is a item in the log list that displays a git commit.
@@ -101,7 +100,7 @@ func (d LogItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l
 		hash = "copied"
 	}
 	title := titleStyle.Render(
-		truncateString(i.Title(), m.Width()-style.GetHorizontalFrameSize()-width(hash)-2, "…"),
+		common.TruncateString(i.Title(), m.Width()-style.GetHorizontalFrameSize()-width(hash)-2),
 	)
 	hashStyle := styles.LogItemHash.Copy().
 		Align(lipgloss.Right).
@@ -129,7 +128,7 @@ func (d LogItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l
 		date += fmt.Sprintf(" %d", i.Committer.When.Year())
 	}
 	who += date
-	who = truncateString(who, m.Width()-style.GetHorizontalFrameSize(), "…")
+	who = common.TruncateString(who, m.Width()-style.GetHorizontalFrameSize())
 	fmt.Fprint(w,
 		style.Render(
 			lipgloss.JoinVertical(lipgloss.Top,
@@ -142,10 +141,3 @@ func (d LogItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l
 		),
 	)
 }
-
-func truncateString(s string, max int, tail string) string {
-	if max < 0 {
-		max = 0
-	}
-	return truncate.StringWithTail(s, uint(max), tail)
-}

ui/pages/repo/refsitem.go 🔗

@@ -99,7 +99,7 @@ func (d RefItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l
 		s.RefItemSelector.GetMarginLeft() -
 		s.RefItemSelector.GetWidth() -
 		s.RefItemInactive.GetMarginLeft()
-	ref = truncateString(ref, refMaxWidth, "…")
+	ref = common.TruncateString(ref, refMaxWidth)
 	if index == m.Index() {
 		fmt.Fprint(w, s.RefItemSelector.Render(">")+
 			s.RefItemActive.Render(ref))

ui/pages/selection/item.go 🔗

@@ -108,6 +108,7 @@ func (d ItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
 	}
 
 	title := i.Title()
+	title = common.TruncateString(title, m.Width()-itemStyle.GetHorizontalFrameSize())
 	if i.repo.IsPrivate() {
 		title += " 🔒"
 	}
@@ -115,6 +116,9 @@ func (d ItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
 		title += " "
 	}
 	updatedStr := fmt.Sprintf(" Updated %s", humanize.Time(i.lastUpdate))
+	if m.Width()-itemStyle.GetHorizontalFrameSize()-lipgloss.Width(updatedStr)-lipgloss.Width(title) <= 0 {
+		updatedStr = ""
+	}
 	updatedStyle := styles.MenuLastUpdate.Copy().
 		Align(lipgloss.Right).
 		Width(m.Width() - itemStyle.GetHorizontalFrameSize() - lipgloss.Width(title))
@@ -142,9 +146,11 @@ func (d ItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
 		titleStyle = titleStyle.Bold(true)
 	}
 	title = titleStyle.Render(title)
-	desc := lipgloss.NewStyle().
+	desc := i.Description()
+	desc = common.TruncateString(desc, m.Width()-itemStyle.GetHorizontalFrameSize())
+	desc = lipgloss.NewStyle().
 		Faint(true).
-		Render(i.Description())
+		Render(desc)
 
 	s.WriteString(lipgloss.JoinHorizontal(lipgloss.Bottom, title, updated))
 	s.WriteString("\n")