From 0bbfa21a02047298eac6d2673b1859270eb50c4c Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 23 May 2022 15:50:01 -0400 Subject: [PATCH] fix: selection item truncate string --- 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(-) create mode 100644 ui/common/utils.go diff --git a/ui/common/utils.go b/ui/common/utils.go new file mode 100644 index 0000000000000000000000000000000000000000..7c817a50ebcb6b92b04006837bde1bbd3ae732b0 --- /dev/null +++ b/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), "…") +} diff --git a/ui/pages/repo/filesitem.go b/ui/pages/repo/filesitem.go index cd93b3838f52ecffc09587f86f9807c7a55da7e9..f0e53a508b4ec7b3c5260dee3682e47d56a76652 100644 --- a/ui/pages/repo/filesitem.go +++ b/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 - diff --git a/ui/pages/repo/logitem.go b/ui/pages/repo/logitem.go index 33b285e19f2e6b709ee2acffe068b486b7b77232..8f1110f94e2760f47a431e45c038527855cd7185 100644 --- a/ui/pages/repo/logitem.go +++ b/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) -} diff --git a/ui/pages/repo/refsitem.go b/ui/pages/repo/refsitem.go index 54819fe32b4579f61058b53da7f0e5d4f58d8482..cc94f3cdc181437e780b5d3e32bca61e148e4247 100644 --- a/ui/pages/repo/refsitem.go +++ b/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)) diff --git a/ui/pages/selection/item.go b/ui/pages/selection/item.go index e7086fc2d82dddc25408f91712f78a8cd02b4cc9..0c0b910f0f6021fe858173fa34a42b566a1fdb1f 100644 --- a/ui/pages/selection/item.go +++ b/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")