feat: also highlight hashes in log browser

Christian Rocha created

Change summary

ui/pages/repo/logitem.go | 44 +++++++++--------------
ui/styles/styles.go      | 77 ++++++++++++++++++++++++-----------------
2 files changed, 62 insertions(+), 59 deletions(-)

Detailed changes

ui/pages/repo/logitem.go 🔗

@@ -76,7 +76,6 @@ func (d LogItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
 
 // Render renders the item. Implements list.ItemDelegate.
 func (d LogItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
-	styles := d.common.Styles.Log
 	i, ok := listItem.(LogItem)
 	if !ok {
 		return
@@ -85,58 +84,49 @@ func (d LogItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l
 		return
 	}
 
-	var titleStyler,
-		descStyler,
-		keywordStyler func(string) string
-	style := styles.ItemInactive
-
+	styles := d.common.Styles.LogItem.Normal
 	if index == m.Index() {
-		titleStyler = styles.ItemTitleActive.Render
-		descStyler = styles.ItemDescActive.Render
-		keywordStyler = styles.ItemKeywordActive.Render
-		style = styles.ItemActive
-	} else {
-		titleStyler = styles.ItemTitleInactive.Render
-		descStyler = styles.ItemDescInactive.Render
-		keywordStyler = styles.ItemKeywordInactive.Render
+		styles = d.common.Styles.LogItem.Active
 	}
 
+	horizontalFrameSize := styles.Base.GetHorizontalFrameSize()
+
 	hash := i.Commit.ID.String()[:7]
 	if !i.copied.IsZero() && i.copied.Add(time.Second).After(time.Now()) {
 		hash = "copied"
 	}
-	title := titleStyler(
+	title := styles.Title.Render(
 		common.TruncateString(i.Title(),
 			m.Width()-
-				style.GetHorizontalFrameSize()-
+				horizontalFrameSize-
 				// 9 is the length of the hash (7) + the left padding (1) + the
 				// title truncation symbol (1)
 				9),
 	)
-	hashStyle := styles.ItemHash.Copy().
+	hashStyle := styles.Hash.Copy().
 		Align(lipgloss.Right).
 		PaddingLeft(1).
 		Width(m.Width() -
-			style.GetHorizontalFrameSize() -
+			horizontalFrameSize -
 			lipgloss.Width(title) - 1) // 1 is for the left padding
 	if index == m.Index() {
 		hashStyle = hashStyle.Bold(true)
 	}
 	hash = hashStyle.Render(hash)
-	if m.Width()-style.GetHorizontalFrameSize()-hashStyle.GetHorizontalFrameSize()-hashStyle.GetWidth() <= 0 {
+	if m.Width()-horizontalFrameSize-hashStyle.GetHorizontalFrameSize()-hashStyle.GetWidth() <= 0 {
 		hash = ""
-		title = titleStyler(
+		title = styles.Title.Render(
 			common.TruncateString(i.Title(),
-				m.Width()-style.GetHorizontalFrameSize()),
+				m.Width()-horizontalFrameSize),
 		)
 	}
 	author := i.Author.Name
 	committer := i.Committer.Name
 	who := ""
 	if author != "" && committer != "" {
-		who = keywordStyler(committer) + descStyler(" committed")
+		who = styles.Keyword.Render(committer) + styles.Desc.Render(" committed")
 		if author != committer {
-			who = keywordStyler(author) + descStyler(" authored and ") + who
+			who = styles.Keyword.Render(author) + styles.Desc.Render(" authored and ") + who
 		}
 		who += " "
 	}
@@ -144,15 +134,15 @@ func (d LogItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l
 	if i.Committer.When.Year() != time.Now().Year() {
 		date += fmt.Sprintf(" %d", i.Committer.When.Year())
 	}
-	who += descStyler("on ") + keywordStyler(date)
-	who = common.TruncateString(who, m.Width()-style.GetHorizontalFrameSize())
+	who += styles.Desc.Render("on ") + styles.Keyword.Render(date)
+	who = common.TruncateString(who, m.Width()-horizontalFrameSize)
 	fmt.Fprint(w,
-		style.Render(
+		styles.Base.Render(
 			lipgloss.JoinVertical(lipgloss.Top,
 				truncate.String(fmt.Sprintf("%s%s",
 					title,
 					hash,
-				), uint(m.Width()-style.GetHorizontalFrameSize())),
+				), uint(m.Width()-horizontalFrameSize)),
 				who,
 			),
 		),

ui/styles/styles.go 🔗

@@ -46,26 +46,32 @@ type Styles struct {
 
 	AboutNoReadme lipgloss.Style
 
+	LogItem struct {
+		Normal struct {
+			Base    lipgloss.Style
+			Hash    lipgloss.Style
+			Title   lipgloss.Style
+			Desc    lipgloss.Style
+			Keyword lipgloss.Style
+		}
+		Active struct {
+			Base    lipgloss.Style
+			Hash    lipgloss.Style
+			Title   lipgloss.Style
+			Desc    lipgloss.Style
+			Keyword lipgloss.Style
+		}
+	}
+
 	Log struct {
-		Item                lipgloss.Style
-		ItemSelector        lipgloss.Style
-		ItemActive          lipgloss.Style
-		ItemInactive        lipgloss.Style
-		ItemHash            lipgloss.Style
-		ItemTitleInactive   lipgloss.Style
-		ItemTitleActive     lipgloss.Style
-		ItemDescInactive    lipgloss.Style
-		ItemDescActive      lipgloss.Style
-		ItemKeywordActive   lipgloss.Style
-		ItemKeywordInactive lipgloss.Style
-		Commit              lipgloss.Style
-		CommitHash          lipgloss.Style
-		CommitAuthor        lipgloss.Style
-		CommitDate          lipgloss.Style
-		CommitBody          lipgloss.Style
-		CommitStatsAdd      lipgloss.Style
-		CommitStatsDel      lipgloss.Style
-		Paginator           lipgloss.Style
+		Commit         lipgloss.Style
+		CommitHash     lipgloss.Style
+		CommitAuthor   lipgloss.Style
+		CommitDate     lipgloss.Style
+		CommitBody     lipgloss.Style
+		CommitStatsAdd lipgloss.Style
+		CommitStatsDel lipgloss.Style
+		Paginator      lipgloss.Style
 	}
 
 	Ref struct {
@@ -115,6 +121,7 @@ func DefaultStyles() *Styles {
 	highlightColor := lipgloss.Color("210")
 	highlightColorDim := lipgloss.Color("174")
 	selectorColor := lipgloss.Color("167")
+	hashColor := lipgloss.Color("184")
 
 	s := new(Styles)
 
@@ -217,46 +224,52 @@ func DefaultStyles() *Styles {
 		MarginLeft(2).
 		Foreground(lipgloss.Color("242"))
 
-	s.Log.ItemInactive = lipgloss.NewStyle().
+	s.LogItem.Normal.Base = lipgloss.NewStyle().
 		Border(lipgloss.Border{
 			Left: " ",
 		}, false, false, false, true).
 		PaddingLeft(1)
 
-	s.Log.ItemActive = s.Log.ItemInactive.Copy().
+	s.LogItem.Active.Base = s.LogItem.Normal.Base.Copy().
 		Border(lipgloss.Border{
 			Left: "┃",
 		}, false, false, false, true).
 		BorderForeground(selectorColor)
 
-	s.Log.ItemSelector = s.Log.ItemInactive.Copy().
-		Width(1).
-		Foreground(lipgloss.Color("62"))
+	s.LogItem.Active.Hash = s.LogItem.Normal.Hash.Copy().
+		Foreground(hashColor)
 
-	s.Log.ItemHash = s.Log.ItemInactive.Copy().
-		Foreground(lipgloss.Color("184"))
+	s.LogItem.Active.Hash = lipgloss.NewStyle().
+		Bold(true).
+		Foreground(highlightColor)
 
-	s.Log.ItemTitleInactive = lipgloss.NewStyle().
+	s.LogItem.Normal.Title = lipgloss.NewStyle().
 		Foreground(lipgloss.Color("105"))
 
-	s.Log.ItemTitleActive = lipgloss.NewStyle().
+	s.LogItem.Active.Title = lipgloss.NewStyle().
 		Foreground(highlightColor).
 		Bold(true)
 
-	s.Log.ItemDescInactive = lipgloss.NewStyle().
+	s.LogItem.Normal.Desc = lipgloss.NewStyle().
 		Foreground(lipgloss.Color("246"))
 
-	s.Log.ItemDescActive = lipgloss.NewStyle().
+	s.LogItem.Active.Desc = lipgloss.NewStyle().
 		Foreground(lipgloss.Color("95"))
 
-	s.Log.ItemKeywordActive = s.Log.ItemDescActive.Copy().
+	s.LogItem.Active.Keyword = s.LogItem.Active.Desc.Copy().
 		Foreground(highlightColorDim)
 
+	s.LogItem.Normal.Hash = lipgloss.NewStyle().
+		Foreground(hashColor)
+
+	s.LogItem.Active.Hash = lipgloss.NewStyle().
+		Foreground(highlightColor)
+
 	s.Log.Commit = lipgloss.NewStyle().
 		Margin(0, 2)
 
 	s.Log.CommitHash = lipgloss.NewStyle().
-		Foreground(lipgloss.Color("184")).
+		Foreground(hashColor).
 		Bold(true)
 
 	s.Log.CommitBody = lipgloss.NewStyle().