Skip to content

Commit

Permalink
Cleanup and reorganize styles, theming prep
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Aug 20, 2021
1 parent 144efbb commit afb6cf8
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 179 deletions.
39 changes: 21 additions & 18 deletions tui/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"smoothie/tui/bubbles/commits"
"smoothie/tui/bubbles/repo"
"smoothie/tui/bubbles/selection"
"smoothie/tui/style"
"strings"

tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -48,6 +49,7 @@ type SessionConfig struct {

type Bubble struct {
config *Config
styles *style.Styles
state sessionState
error string
width int
Expand All @@ -66,6 +68,7 @@ type Bubble struct {
func NewBubble(cfg *Config, sCfg *SessionConfig) *Bubble {
b := &Bubble{
config: cfg,
styles: style.DefaultStyles(),
width: sCfg.Width,
height: sCfg.Height,
windowChanges: sCfg.WindowChanges,
Expand Down Expand Up @@ -143,10 +146,9 @@ func (b *Bubble) viewForBox(i int) string {
switch box := b.boxes[i].(type) {
case *selection.Bubble:
var s lipgloss.Style
s = b.styles.Menu
if isActive {
s = menuActiveStyle
} else {
s = menuStyle
s.Copy().BorderForeground(b.styles.ActiveBorderColor)
}
return s.Render(box.View())
case *repo.Bubble:
Expand All @@ -158,8 +160,8 @@ func (b *Bubble) viewForBox(i int) string {
}

func (b Bubble) headerView() string {
w := b.width - appBoxStyle.GetHorizontalFrameSize()
return headerStyle.Copy().Width(w).Render(b.config.Name)
w := b.width - b.styles.App.GetHorizontalFrameSize()
return b.styles.Header.Copy().Width(w).Render(b.config.Name)
}

func (b Bubble) footerView() string {
Expand All @@ -179,27 +181,28 @@ func (b Bubble) footerView() string {
}
}
for i, v := range h {
fmt.Fprint(w, v)
fmt.Fprint(w, v.Render(b.styles))
if i != len(h)-1 {
fmt.Fprint(w, helpDivider)
fmt.Fprint(w, b.styles.HelpDivider)
}
}
return footerStyle.Copy().Width(b.width).Render(w.String())
return b.styles.Footer.Copy().Width(b.width).Render(w.String())
}

func (b Bubble) errorView() string {
s := lipgloss.JoinHorizontal(
s := b.styles
str := lipgloss.JoinHorizontal(
lipgloss.Top,
errorHeaderStyle.Render("Bummer"),
errorBodyStyle.Render(b.error),
s.ErrorTitle.Render("Bummer"),
s.ErrorBody.Render(b.error),
)
h := b.height -
appBoxStyle.GetVerticalFrameSize() -
s.App.GetVerticalFrameSize() -
lipgloss.Height(b.headerView()) -
lipgloss.Height(b.footerView()) -
contentBoxStyle.GetVerticalFrameSize() +
3 // TODO: figure out why we need this
return errorStyle.Copy().Height(h).Render(s)
s.RepoBody.GetVerticalFrameSize() +
3 // TODO: this is repo header height -- get it dynamically
return s.Error.Copy().Height(h).Render(str)
}

func (b Bubble) View() string {
Expand All @@ -216,14 +219,14 @@ func (b Bubble) View() string {
}
s.WriteRune('\n')
s.WriteString(b.footerView())
return appBoxStyle.Render(s.String())
return b.styles.App.Render(s.String())
}

type helpEntry struct {
key string
val string
}

func (h helpEntry) String() string {
return fmt.Sprintf("%s %s", helpKeyStyle.Render(h.key), helpValueStyle.Render(h.val))
func (h helpEntry) Render(s *style.Styles) string {
return fmt.Sprintf("%s %s", s.HelpKey.Render(h.key), s.HelpValue.Render(h.val))
}
46 changes: 23 additions & 23 deletions tui/bubbles/repo/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"smoothie/git"
"smoothie/tui/style"
"strconv"
"text/template"

Expand All @@ -22,21 +23,18 @@ type ErrMsg struct {
}

type Bubble struct {
templateObject interface{}
repoSource *git.RepoSource
name string
repo *git.Repo
readmeViewport *ViewportBubble
readme string
height int
heightMargin int
width int
widthMargin int
Active bool
TitleStyle lipgloss.Style
NoteStyle lipgloss.Style
BodyStyle lipgloss.Style
ActiveBorderColor lipgloss.Color
templateObject interface{}
repoSource *git.RepoSource
name string
repo *git.Repo
styles *style.Styles
readmeViewport *ViewportBubble
readme string
height int
heightMargin int
width int
widthMargin int
Active bool

// XXX: ideally, we get these from the parent as a pointer. Currently, we
// can't add a *tui.Config because it's an illegal import cycle. One
Expand All @@ -46,11 +44,12 @@ type Bubble struct {
Port int64
}

func NewBubble(rs *git.RepoSource, name string, width, wm, height, hm int, tmp interface{}) *Bubble {
func NewBubble(rs *git.RepoSource, name string, styles *style.Styles, width, wm, height, hm int, tmp interface{}) *Bubble {
b := &Bubble{
templateObject: tmp,
repoSource: rs,
name: name,
styles: styles,
heightMargin: hm,
widthMargin: wm,
readmeViewport: &ViewportBubble{
Expand Down Expand Up @@ -97,11 +96,11 @@ func (b *Bubble) GotoTop() {
}

func (b Bubble) headerView() string {
ts := b.TitleStyle
ns := b.NoteStyle
ts := b.styles.RepoTitle
ns := b.styles.RepoNote
if b.Active {
ts = ts.Copy().BorderForeground(b.ActiveBorderColor)
ns = ns.Copy().BorderForeground(b.ActiveBorderColor)
ts = ts.Copy().BorderForeground(b.styles.ActiveBorderColor)
ns = ns.Copy().BorderForeground(b.styles.ActiveBorderColor)
}
n := b.name
if n == "config" {
Expand All @@ -114,13 +113,14 @@ func (b Bubble) headerView() string {
}

func (b *Bubble) View() string {
s := b.styles
header := b.headerView()
bs := b.BodyStyle.Copy()
bs := s.RepoBody.Copy()
if b.Active {
bs = bs.BorderForeground(b.ActiveBorderColor)
bs = bs.BorderForeground(s.ActiveBorderColor)
}
body := bs.
Width(b.width - b.widthMargin - b.BodyStyle.GetVerticalFrameSize()).
Width(b.width - b.widthMargin - s.RepoBody.GetVerticalFrameSize()).
Height(b.height - b.heightMargin - lipgloss.Height(header)).
Render(b.readmeViewport.View())
return header + body
Expand Down
25 changes: 11 additions & 14 deletions tui/bubbles/selection/bubble.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package selection

import (
"smoothie/tui/style"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type SelectedMsg struct {
Expand All @@ -16,19 +17,15 @@ type ActiveMsg struct {
}

type Bubble struct {
NormalStyle lipgloss.Style
SelectedStyle lipgloss.Style
Cursor string
Items []string
SelectedItem int
Items []string
SelectedItem int
styles *style.Styles
}

func NewBubble(items []string, normalStyle, selectedStyle lipgloss.Style, cursor string) *Bubble {
func NewBubble(items []string, styles *style.Styles) *Bubble {
return &Bubble{
NormalStyle: normalStyle,
SelectedStyle: selectedStyle,
Cursor: cursor,
Items: items,
Items: items,
styles: styles,
}
}

Expand All @@ -40,10 +37,10 @@ func (b Bubble) View() string {
s := ""
for i, item := range b.Items {
if i == b.SelectedItem {
s += b.Cursor
s += b.SelectedStyle.Render(item)
s += b.styles.MenuCursor.String()
s += b.styles.SelectedMenuItem.Render(item)
} else {
s += b.NormalStyle.Render(item)
s += b.styles.MenuItem.Render(item)
}
if i < len(b.Items)-1 {
s += "\n"
Expand Down
14 changes: 5 additions & 9 deletions tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ func (b *Bubble) setupCmd() tea.Msg {
tmplConfig = b.config
}
width := b.width
boxLeftWidth := menuStyle.GetWidth() + menuStyle.GetHorizontalFrameSize()
boxLeftWidth := b.styles.Menu.GetWidth() + b.styles.Menu.GetHorizontalFrameSize()
// TODO: also send this along with a tea.WindowSizeMsg
var heightMargin = lipgloss.Height(b.headerView()) +
lipgloss.Height(b.footerView()) +
contentBoxStyle.GetVerticalFrameSize() +
appBoxStyle.GetVerticalMargins() +
b.styles.RepoBody.GetVerticalFrameSize() +
b.styles.App.GetVerticalMargins() +
3 // TODO: make this dynamic (this is the height of the repo info)
rb := repo.NewBubble(b.repoSource, me.Repo, width, boxLeftWidth, b.height, heightMargin, tmplConfig)
rb.TitleStyle = contentBoxTitleStyle
rb.NoteStyle = contentBoxNoteStyle
rb.BodyStyle = contentBoxStyle
rb.ActiveBorderColor = activeBorderColor
rb := repo.NewBubble(b.repoSource, me.Repo, b.styles, width, boxLeftWidth, b.height, heightMargin, tmplConfig)
rb.Host = b.config.Host
rb.Port = b.config.Port
initCmd := rb.Init()
Expand All @@ -73,7 +69,7 @@ func (b *Bubble) setupCmd() tea.Msg {
b.repoMenu = append(b.repoMenu, me)
rs = append(rs, me.Name)
}
b.repoSelect = selection.NewBubble(rs, menuItemStyle, selectedMenuItemStyle, menuCursor.String())
b.repoSelect = selection.NewBubble(rs, b.styles)
b.boxes[0] = b.repoSelect
/*
b.commitsLog = commits.NewBubble(
Expand Down
115 changes: 0 additions & 115 deletions tui/style.go

This file was deleted.

Loading

0 comments on commit afb6cf8

Please sign in to comment.