Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 814ee9f8fa7014dd4fafab12974e2edd4b8f7d73
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 23:07:49 2023 -0400

    Run gofmt

commit 3db029502c74461b57e776a0ebc9d27c944acc5d
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 23:06:31 2023 -0400

    Pull out statsLine and diffLine, add a commit body line

commit eb1da4296e0181bae842622e0dc98ca8ca845049
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 23:05:36 2023 -0400

    Add flag for printing the patch only

commit e76702e92b085f02427aae1c7052f235454242fd
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 22:50:55 2023 -0400

    Add commit, author, date, and stats

commit 0359b1dc49417d11f8f8b8529f8b942417d79128
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 21:43:35 2023 -0400

    Add commit, author, date, and stats

    Missing color on stats
  • Loading branch information
luandy64 committed Jul 1, 2023
1 parent d0ac953 commit 65d308e
Showing 1 changed file with 95 additions and 19 deletions.
114 changes: 95 additions & 19 deletions server/cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package cmd
import (
"fmt"
"strings"
"time"

gansi "github.com/charmbracelet/glamour/ansi"
"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/server/ui/common"
"github.com/charmbracelet/soft-serve/server/ui/styles"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)

// commitCommand returns a command that prints the contents of a commit.
func commitCommand() *cobra.Command {
var color bool
var patchOnly bool

cmd := &cobra.Command{
Use: "commit SHA",
Expand Down Expand Up @@ -47,35 +50,58 @@ func commitCommand() *cobra.Command {
return err
}

c := string(patch)

if color {
var s strings.Builder
var pr strings.Builder

diffChroma := &gansi.CodeBlockElement{
Code: patch,
Language: "diff",
}

err = diffChroma.Render(&pr, renderCtx())
diff, err := r.Diff(commit)
if err != nil {
return err
}

if err != nil {
s.WriteString(fmt.Sprintf("\n%s", err.Error()))
} else {
s.WriteString(fmt.Sprintf("\n%s", pr.String()))
}
commonStyle := styles.DefaultStyles()
style := commonStyle.Log

s := strings.Builder{}
commitLine := "commit " + commitSHA
authorLine := "Author: " + commit.Author.Name
dateLine := "Date: " + commit.Committer.When.Format(time.UnixDate)
msgLine := strings.ReplaceAll(commit.Message, "\r\n", "\n")
statsLine := renderStats(diff, commonStyle, color)
diffLine := renderDiff(patch, color)

if patchOnly {
cmd.Println(
diffLine,
)
return nil
}

c = s.String()
if color {
s.WriteString(fmt.Sprintf("%s\n%s\n%s\n%s\n",
style.CommitHash.Render(commitLine),
style.CommitAuthor.Render(authorLine),
style.CommitDate.Render(dateLine),
style.CommitBody.Render(msgLine),
))
} else {
s.WriteString(fmt.Sprintf("%s\n%s\n%s\n%s\n",
commitLine,
authorLine,
dateLine,
msgLine,
))
}

cmd.Println(c)
s.WriteString(statsLine)
s.WriteString(diffLine)

cmd.Println(
s.String(),
)

return nil
},
}

cmd.Flags().BoolVarP(&color, "color", "c", false, "Colorize output")
cmd.Flags().BoolVarP(&patchOnly, "patch", "p", false, "Output patch only")

return cmd
}
Expand All @@ -86,3 +112,53 @@ func renderCtx() gansi.RenderContext {
Styles: common.StyleConfig(),
})
}

func renderDiff(patch string, color bool) string {

c := string(patch)

if color {
var s strings.Builder
var pr strings.Builder

diffChroma := &gansi.CodeBlockElement{
Code: patch,
Language: "diff",
}

err := diffChroma.Render(&pr, renderCtx())

if err != nil {
s.WriteString(fmt.Sprintf("\n%s", err.Error()))
} else {
s.WriteString(fmt.Sprintf("\n%s", pr.String()))
}

c = s.String()
}

return c
}

func renderStats(diff *git.Diff, commonStyle *styles.Styles, color bool) string {
style := commonStyle.Log
c := diff.Stats().String()

if color {
s := strings.Split(c, "\n")

for i, line := range s {
ch := strings.Split(line, "|")
if len(ch) > 1 {
adddel := ch[len(ch)-1]
adddel = strings.ReplaceAll(adddel, "+", style.CommitStatsAdd.Render("+"))
adddel = strings.ReplaceAll(adddel, "-", style.CommitStatsDel.Render("-"))
s[i] = strings.Join(ch[:len(ch)-1], "|") + "|" + adddel
}
}

return strings.Join(s, "\n")
}

return c
}

0 comments on commit 65d308e

Please sign in to comment.