chore: organize styles a bit

Christian Rocha created

Change summary

ui/pages/repo/filesitem.go |  34 +++++-----
ui/pages/repo/refsitem.go  |  20 +++---
ui/pages/repo/repo.go      |  20 +++---
ui/pages/selection/item.go |   2 
ui/styles/styles.go        | 114 +++++++++++++++++++++------------------
5 files changed, 98 insertions(+), 92 deletions(-)

Detailed changes

ui/pages/repo/filesitem.go 🔗

@@ -96,7 +96,7 @@ func (d FileItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
 
 // Render implements list.ItemDelegate.
 func (d FileItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
-	s := d.common.Styles
+	s := d.common.Styles.Tree
 	i, ok := listItem.(FileItem)
 	if !ok {
 		return
@@ -109,32 +109,32 @@ func (d FileItemDelegate) Render(w io.Writer, m list.Model, index int, listItem
 	if i.entry.IsTree() {
 		size = strings.Repeat(" ", sizeLen)
 		if index == m.Index() {
-			name = s.TreeFileDirActive.Render(name)
+			name = s.FileDirActive.Render(name)
 		} else {
-			name = s.TreeFileDirInactive.Render(name)
+			name = s.FileDirInactive.Render(name)
 		}
 	}
 	var nameStyle, sizeStyle, modeStyle lipgloss.Style
 	mode := i.Mode()
 	if index == m.Index() {
-		nameStyle = s.TreeItemActive
-		sizeStyle = s.TreeFileSizeActive
-		modeStyle = s.TreeFileModeActive
-		fmt.Fprint(w, s.TreeItemSelector.Render(">"))
+		nameStyle = s.ItemActive
+		sizeStyle = s.FileSizeActive
+		modeStyle = s.FileModeActive
+		fmt.Fprint(w, s.ItemSelector.Render(">"))
 	} else {
-		nameStyle = s.TreeItemInactive
-		sizeStyle = s.TreeFileSizeInactive
-		modeStyle = s.TreeFileModeInactive
-		fmt.Fprint(w, s.TreeItemSelector.Render(" "))
+		nameStyle = s.ItemInactive
+		sizeStyle = s.FileSizeInactive
+		modeStyle = s.FileModeInactive
+		fmt.Fprint(w, s.ItemSelector.Render(" "))
 	}
 	sizeStyle = sizeStyle.Copy().
 		Width(8).
 		Align(lipgloss.Right).
 		MarginLeft(1)
-	leftMargin := s.TreeItemSelector.GetMarginLeft() +
-		s.TreeItemSelector.GetWidth() +
-		s.TreeFileModeInactive.GetMarginLeft() +
-		s.TreeFileModeInactive.GetWidth() +
+	leftMargin := s.ItemSelector.GetMarginLeft() +
+		s.ItemSelector.GetWidth() +
+		s.FileModeInactive.GetMarginLeft() +
+		s.FileModeInactive.GetWidth() +
 		nameStyle.GetMarginLeft() +
 		sizeStyle.GetHorizontalFrameSize()
 	name = common.TruncateString(name, m.Width()-leftMargin)
@@ -142,8 +142,8 @@ func (d FileItemDelegate) Render(w io.Writer, m list.Model, index int, listItem
 	size = sizeStyle.Render(size)
 	modeStr := modeStyle.Render(mode.String())
 	truncate := lipgloss.NewStyle().MaxWidth(m.Width() -
-		s.TreeItemSelector.GetHorizontalFrameSize() -
-		s.TreeItemSelector.GetWidth())
+		s.ItemSelector.GetHorizontalFrameSize() -
+		s.ItemSelector.GetWidth())
 	fmt.Fprint(w,
 		truncate.Render(fmt.Sprintf("%s%s%s",
 			modeStr,

ui/pages/repo/refsitem.go 🔗

@@ -85,7 +85,7 @@ func (d RefItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
 
 // Render implements list.ItemDelegate.
 func (d RefItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
-	s := d.common.Styles
+	s := d.common.Styles.Ref
 	i, ok := listItem.(RefItem)
 	if !ok {
 		return
@@ -98,27 +98,27 @@ func (d RefItemDelegate) Render(w io.Writer, m list.Model, index int, listItem l
 	isActive := index == m.Index()
 
 	if isTag && isActive {
-		st = s.RefItemTagActive
+		st = s.ItemTagActive
 	} else if isTag {
-		st = s.RefItemTagInactive
+		st = s.ItemTagInactive
 	} else if isActive {
-		st = s.RefItemActive
+		st = s.ItemActive
 	} else {
-		st = s.RefItemInactive
+		st = s.ItemInactive
 	}
 
 	if isActive {
-		selector = s.RefItemSelector.String()
+		selector = s.ItemSelector.String()
 	} else {
 		selector = "  "
 	}
 
 	ref := i.Short()
-	ref = s.RefItemBranch.Render(ref)
+	ref = s.ItemBranch.Render(ref)
 	refMaxWidth := m.Width() -
-		s.RefItemSelector.GetMarginLeft() -
-		s.RefItemSelector.GetWidth() -
-		s.RefItemInactive.GetMarginLeft()
+		s.ItemSelector.GetMarginLeft() -
+		s.ItemSelector.GetWidth() -
+		s.ItemInactive.GetMarginLeft()
 	ref = common.TruncateString(ref, refMaxWidth)
 	ref = st.Render(ref)
 	fmt.Fprint(w, selector, ref)

ui/pages/repo/repo.go 🔗

@@ -100,9 +100,9 @@ func New(cfg *config.Config, c common.Common) *Repo {
 // SetSize implements common.Component.
 func (r *Repo) SetSize(width, height int) {
 	r.common.SetSize(width, height)
-	hm := r.common.Styles.RepoBody.GetVerticalFrameSize() +
-		r.common.Styles.RepoHeader.GetHeight() +
-		r.common.Styles.RepoHeader.GetVerticalFrameSize() +
+	hm := r.common.Styles.Repo.Body.GetVerticalFrameSize() +
+		r.common.Styles.Repo.Header.GetHeight() +
+		r.common.Styles.Repo.Header.GetVerticalFrameSize() +
 		r.common.Styles.StatusBar.GetHeight() +
 		r.common.Styles.Tabs.GetHeight() +
 		r.common.Styles.Tabs.GetVerticalFrameSize()
@@ -242,13 +242,13 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 
 // View implements tea.Model.
 func (r *Repo) View() string {
-	s := r.common.Styles.Repo.Copy().
+	s := r.common.Styles.Repo.Base.Copy().
 		Width(r.common.Width).
 		Height(r.common.Height)
-	repoBodyStyle := r.common.Styles.RepoBody.Copy()
+	repoBodyStyle := r.common.Styles.Repo.Body.Copy()
 	hm := repoBodyStyle.GetVerticalFrameSize() +
-		r.common.Styles.RepoHeader.GetHeight() +
-		r.common.Styles.RepoHeader.GetVerticalFrameSize() +
+		r.common.Styles.Repo.Header.GetHeight() +
+		r.common.Styles.Repo.Header.GetVerticalFrameSize() +
 		r.common.Styles.StatusBar.GetHeight() +
 		r.common.Styles.Tabs.GetHeight() +
 		r.common.Styles.Tabs.GetVerticalFrameSize()
@@ -270,13 +270,13 @@ func (r *Repo) headerView() string {
 	}
 	cfg := r.cfg
 	truncate := lipgloss.NewStyle().MaxWidth(r.common.Width)
-	name := r.common.Styles.RepoHeaderName.Render(r.selectedRepo.Name())
+	name := r.common.Styles.Repo.HeaderName.Render(r.selectedRepo.Name())
 	desc := r.selectedRepo.Description()
 	if desc == "" {
 		desc = name
 		name = ""
 	} else {
-		desc = r.common.Styles.RepoHeaderDesc.Render(desc)
+		desc = r.common.Styles.Repo.HeaderDesc.Render(desc)
 	}
 	// TODO move this into a style.
 	urlStyle := lipgloss.NewStyle().
@@ -287,7 +287,7 @@ func (r *Repo) headerView() string {
 	url := git.RepoURL(cfg.Host, cfg.Port, r.selectedRepo.Repo())
 	url = common.TruncateString(url, r.common.Width-lipgloss.Width(desc)-1)
 	url = urlStyle.Render(url)
-	style := r.common.Styles.RepoHeader.Copy().Width(r.common.Width)
+	style := r.common.Styles.Repo.Header.Copy().Width(r.common.Width)
 	return style.Render(
 		lipgloss.JoinVertical(lipgloss.Top,
 			truncate.Render(name),

ui/pages/selection/item.go 🔗

@@ -159,7 +159,7 @@ func (d ItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
 	s.WriteString("\n")
 	s.WriteString(desc)
 	s.WriteString("\n")
-	cmdStyle := styles.RepoCommand.Copy()
+	cmdStyle := styles.Repo.Command.Copy()
 	cmd := common.TruncateString(i.Command(), m.Width()-itemStyle.GetHorizontalFrameSize())
 	cmd = cmdStyle.Render(cmd)
 	if !i.copied.IsZero() && i.copied.Add(time.Second).After(time.Now()) {

ui/styles/styles.go 🔗

@@ -24,13 +24,15 @@ type Styles struct {
 	SelectorBox lipgloss.Style
 	ReadmeBox   lipgloss.Style
 
-	Repo           lipgloss.Style
-	RepoTitle      lipgloss.Style
-	RepoCommand    lipgloss.Style
-	RepoBody       lipgloss.Style
-	RepoHeader     lipgloss.Style
-	RepoHeaderName lipgloss.Style
-	RepoHeaderDesc lipgloss.Style
+	Repo struct {
+		Base       lipgloss.Style
+		Title      lipgloss.Style
+		Command    lipgloss.Style
+		Body       lipgloss.Style
+		Header     lipgloss.Style
+		HeaderName lipgloss.Style
+		HeaderDesc lipgloss.Style
+	}
 
 	Footer      lipgloss.Style
 	Branch      lipgloss.Style
@@ -66,26 +68,30 @@ type Styles struct {
 		Paginator           lipgloss.Style
 	}
 
-	RefItemSelector    lipgloss.Style
-	RefItemActive      lipgloss.Style
-	RefItemInactive    lipgloss.Style
-	RefItemBranch      lipgloss.Style
-	RefItemTagInactive lipgloss.Style
-	RefItemTagActive   lipgloss.Style
-	RefPaginator       lipgloss.Style
-
-	TreeItemSelector     lipgloss.Style
-	TreeItemActive       lipgloss.Style
-	TreeItemInactive     lipgloss.Style
-	TreeFileDirInactive  lipgloss.Style
-	TreeFileDirActive    lipgloss.Style
-	TreeFileModeInactive lipgloss.Style
-	TreeFileModeActive   lipgloss.Style
-	TreeFileSizeInactive lipgloss.Style
-	TreeFileSizeActive   lipgloss.Style
-	TreeFileContent      lipgloss.Style
-	TreePaginator        lipgloss.Style
-	TreeNoItems          lipgloss.Style
+	Ref struct {
+		ItemSelector    lipgloss.Style
+		ItemActive      lipgloss.Style
+		ItemInactive    lipgloss.Style
+		ItemBranch      lipgloss.Style
+		ItemTagInactive lipgloss.Style
+		ItemTagActive   lipgloss.Style
+		Paginator       lipgloss.Style
+	}
+
+	Tree struct {
+		ItemSelector     lipgloss.Style
+		ItemActive       lipgloss.Style
+		ItemInactive     lipgloss.Style
+		FileDirInactive  lipgloss.Style
+		FileDirActive    lipgloss.Style
+		FileModeInactive lipgloss.Style
+		FileModeActive   lipgloss.Style
+		FileSizeInactive lipgloss.Style
+		FileSizeActive   lipgloss.Style
+		FileContent      lipgloss.Style
+		Paginator        lipgloss.Style
+		NoItems          lipgloss.Style
+	}
 
 	Spinner lipgloss.Style
 
@@ -151,26 +157,26 @@ func DefaultStyles() *Styles {
 
 	s.ReadmeBox = lipgloss.NewStyle()
 
-	s.Repo = lipgloss.NewStyle()
+	s.Repo.Base = lipgloss.NewStyle()
 
-	s.RepoTitle = lipgloss.NewStyle().
+	s.Repo.Title = lipgloss.NewStyle().
 		Padding(0, 2)
 
-	s.RepoCommand = lipgloss.NewStyle().
+	s.Repo.Command = lipgloss.NewStyle().
 		Foreground(lipgloss.Color("168"))
 
-	s.RepoBody = lipgloss.NewStyle().
+	s.Repo.Body = lipgloss.NewStyle().
 		Margin(1, 0)
 
-	s.RepoHeader = lipgloss.NewStyle().
+	s.Repo.Header = lipgloss.NewStyle().
 		Height(2).
 		Border(lipgloss.NormalBorder(), false, false, true, false).
 		BorderForeground(lipgloss.Color("238"))
 
-	s.RepoHeaderName = lipgloss.NewStyle().
+	s.Repo.HeaderName = lipgloss.NewStyle().
 		Bold(true)
 
-	s.RepoHeaderDesc = lipgloss.NewStyle().
+	s.Repo.HeaderDesc = lipgloss.NewStyle().
 		Faint(true)
 
 	s.Footer = lipgloss.NewStyle().
@@ -269,65 +275,65 @@ func DefaultStyles() *Styles {
 		Margin(0).
 		Align(lipgloss.Center)
 
-	s.RefItemInactive = lipgloss.NewStyle()
+	s.Ref.ItemInactive = lipgloss.NewStyle()
 
-	s.RefItemSelector = lipgloss.NewStyle().
+	s.Ref.ItemSelector = lipgloss.NewStyle().
 		Foreground(selectorColor).
 		SetString("> ")
 
-	s.RefItemActive = s.RefItemActive.Copy().
+	s.Ref.ItemActive = s.Ref.ItemActive.Copy().
 		Foreground(highlightColorDim)
 
-	s.RefItemBranch = lipgloss.NewStyle()
+	s.Ref.ItemBranch = lipgloss.NewStyle()
 
-	s.RefItemTagInactive = lipgloss.NewStyle().
+	s.Ref.ItemTagInactive = lipgloss.NewStyle().
 		Foreground(lipgloss.Color("#A3A322"))
 
-	s.RefItemTagActive = lipgloss.NewStyle().
+	s.Ref.ItemTagActive = lipgloss.NewStyle().
 		Bold(true).
 		Foreground(highlightColor)
 
-	s.RefItemActive = lipgloss.NewStyle().
+	s.Ref.ItemActive = lipgloss.NewStyle().
 		Bold(true).
 		Foreground(highlightColor)
 
-	s.RefPaginator = s.Log.Paginator.Copy()
+	s.Ref.Paginator = s.Log.Paginator.Copy()
 
-	s.TreeItemSelector = s.TreeItemInactive.Copy().
+	s.Tree.ItemSelector = s.Tree.ItemInactive.Copy().
 		Width(1).
 		Foreground(selectorColor)
 
-	s.TreeItemInactive = lipgloss.NewStyle().
+	s.Tree.ItemInactive = lipgloss.NewStyle().
 		MarginLeft(1)
 
-	s.TreeItemActive = s.TreeItemInactive.Copy().
+	s.Tree.ItemActive = s.Tree.ItemInactive.Copy().
 		Bold(true).
 		Foreground(highlightColor)
 
-	s.TreeFileDirInactive = lipgloss.NewStyle().
+	s.Tree.FileDirInactive = lipgloss.NewStyle().
 		Foreground(lipgloss.Color("39"))
 
-	s.TreeFileDirActive = lipgloss.NewStyle().
+	s.Tree.FileDirActive = lipgloss.NewStyle().
 		Foreground(highlightColor)
 
-	s.TreeFileModeInactive = s.TreeItemInactive.Copy().
+	s.Tree.FileModeInactive = s.Tree.ItemInactive.Copy().
 		Width(10).
 		Foreground(lipgloss.Color("243"))
 
-	s.TreeFileModeActive = s.TreeFileModeInactive.Copy().
+	s.Tree.FileModeActive = s.Tree.FileModeInactive.Copy().
 		Foreground(highlightColorDim)
 
-	s.TreeFileSizeInactive = s.TreeItemInactive.Copy().
+	s.Tree.FileSizeInactive = s.Tree.ItemInactive.Copy().
 		Foreground(lipgloss.Color("243"))
 
-	s.TreeFileSizeActive = s.TreeItemInactive.Copy().
+	s.Tree.FileSizeActive = s.Tree.ItemInactive.Copy().
 		Foreground(highlightColorDim)
 
-	s.TreeFileContent = lipgloss.NewStyle()
+	s.Tree.FileContent = lipgloss.NewStyle()
 
-	s.TreePaginator = s.Log.Paginator.Copy()
+	s.Tree.Paginator = s.Log.Paginator.Copy()
 
-	s.TreeNoItems = s.AboutNoReadme.Copy()
+	s.Tree.NoItems = s.AboutNoReadme.Copy()
 
 	s.Spinner = lipgloss.NewStyle().
 		MarginTop(1).