Skip to content

Commit

Permalink
♻️ Simplify commit message output
Browse files Browse the repository at this point in the history
The commit message text that is output on applying a commit is generated
by combining the emoji, summary, body and footer. Each component has a
customisable style however the output for all was the same. Removing
these styles and combine with a single style for the entire message
reduced the complexity significantly.

The final view was rendered using vertical dividers. This was causing
line spacing issues when components were removed. Changing this to a
slice allows for joining after all the elements have been added. This
can then be styled afterwards.

Removal of comments was problematic and has been removed. This will be
reimplemented at a later date when editor mode is added.
  • Loading branch information
mikelorant committed Feb 18, 2023
1 parent d3604de commit 8e7e3c3
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 95 deletions.
8 changes: 2 additions & 6 deletions internal/ui/colour/colour.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ type info struct {
}

type message struct {
Summary lipgloss.TerminalColor
Body lipgloss.TerminalColor
Footer lipgloss.TerminalColor
Message lipgloss.TerminalColor
}

type shortcut struct {
Expand Down Expand Up @@ -217,9 +215,7 @@ func (c *Colour) Message() message {
clr := c.registry

return message{
Summary: clr.Fg(),
Body: clr.Fg(),
Footer: clr.Fg(),
Message: clr.Fg(),
}
}

Expand Down
12 changes: 3 additions & 9 deletions internal/ui/colour/colour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ type info struct {
}

type message struct {
Summary Colour
Body Colour
Footer Colour
Message Colour
}

type shortcut struct {
Expand Down Expand Up @@ -374,9 +372,7 @@ func TestMessage(t *testing.T) {
{
name: "Message",
message: message{
Summary: Colour{Dark: "#bbbbbb"},
Body: Colour{Dark: "#bbbbbb"},
Footer: Colour{Dark: "#bbbbbb"},
Message: Colour{Dark: "#bbbbbb"},
},
},
}
Expand All @@ -389,9 +385,7 @@ func TestMessage(t *testing.T) {

clr := colour.New(theme.New(config.ColourAdaptive)).Message()

assert.Equal(t, tt.message.Summary, toColour(clr.Summary), "Summary")
assert.Equal(t, tt.message.Body, toColour(clr.Body), "Body")
assert.Equal(t, tt.message.Footer, toColour(clr.Footer), "Footer")
assert.Equal(t, tt.message.Message, toColour(clr.Message), "Summary")
})
}
}
Expand Down
46 changes: 14 additions & 32 deletions internal/ui/message/message.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package message

import (
"bufio"
"fmt"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mikelorant/committed/internal/theme"
)

Expand Down Expand Up @@ -46,42 +44,26 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m Model) View() string {
message := m.styles.summary.Render(m.summary)
if m.emoji != "" {
s := fmt.Sprintf("%s %s", m.emoji, m.summary)
message = m.styles.summary.Render(s)
var str []string

if m.summary != "" {
switch {
case m.emoji != "":
str = append(str, fmt.Sprintf("%v %v", m.emoji, m.summary))
default:
str = append(str, m.summary)
}
}

body := removeComments(m.body)
if body != "" {
b := m.styles.body.Render(body)
message = lipgloss.JoinVertical(lipgloss.Top, message, b)
if m.body != "" {
str = append(str, m.body)
}

if m.footer != "" {
f := m.styles.footer.Render(m.footer)
message = lipgloss.JoinVertical(lipgloss.Top, message, f)
str = append(str, m.footer)
}

return m.styles.message.Render(message)
}

func removeComments(str string) string {
var sb strings.Builder

r := strings.NewReader(str)

scanner := bufio.NewScanner(r)

for scanner.Scan() {
txt := scanner.Text()

if strings.HasPrefix(txt, "#") {
continue
}

fmt.Fprintln(&sb, txt)
}
msg := strings.Join(str, "\n\n")

return strings.TrimSpace(sb.String())
return m.styles.message.Render(msg)
}
25 changes: 0 additions & 25 deletions internal/ui/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,6 @@ func TestModel(t *testing.T) {
footer: "footer",
},
},
{
name: "comments_body",
args: args{
emoji: ":art:",
summary: "summary",
body: "# body",
},
},
{
name: "comments_body_mixed",
args: args{
emoji: ":art:",
summary: "summary",
body: "line 1\n# line 2\nline 3\n",
},
},
{
name: "comments_body_footer",
args: args{
emoji: ":art:",
summary: "summary",
body: "# body",
footer: "footer",
},
},
}

for _, tt := range tests {
Expand Down
14 changes: 2 additions & 12 deletions internal/ui/message/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,8 @@ func defaultStyles(th theme.Theme) Styles {

s.message = lipgloss.NewStyle().
MarginLeft(4).
MarginBottom(2)

s.summary = lipgloss.NewStyle().
Foreground(clr.Summary)

s.body = lipgloss.NewStyle().
MarginTop(1).
Foreground(clr.Body)

s.footer = lipgloss.NewStyle().
MarginTop(1).
Foreground(clr.Footer)
MarginBottom(2).
Foreground(clr.Message)

return s
}
2 changes: 0 additions & 2 deletions internal/ui/message/testdata/comments_body.golden

This file was deleted.

4 changes: 0 additions & 4 deletions internal/ui/message/testdata/comments_body_footer.golden

This file was deleted.

5 changes: 0 additions & 5 deletions internal/ui/message/testdata/comments_body_mixed.golden

This file was deleted.

0 comments on commit 8e7e3c3

Please sign in to comment.