Skip to content

Commit

Permalink
Merge pull request #22 from git-chglog/feat/add-preview-style-link
Browse files Browse the repository at this point in the history
Add URL of output example for template style
  • Loading branch information
wadackel committed May 6, 2018
2 parents e2e3797 + 87df4b4 commit 55dbf00
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 83 deletions.
7 changes: 4 additions & 3 deletions cmd/git-chglog/config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func NewConfigBuilder() ConfigBuilder {
func (*configBuilderImpl) Build(ans *Answer) (string, error) {
var msgFormat *CommitMessageFormat

for _, f := range formats {
if f.Display == ans.CommitMessageFormat {
for _, ff := range formats {
f, _ := ff.(*CommitMessageFormat)
if f.display == ans.CommitMessageFormat {
msgFormat = f
break
}
Expand Down Expand Up @@ -65,7 +66,7 @@ options:
ans.Style,
defaultTemplateFilename,
repoURL,
msgFormat.Pattern,
msgFormat.pattern,
msgFormat.PatternMapString(),
)

Expand Down
16 changes: 8 additions & 8 deletions cmd/git-chglog/config_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ func TestConfigBulider(t *testing.T) {
out, err := builder.Build(&Answer{
RepositoryURL: "https://github.com/git-chglog/git-chglog/git-chglog/",
Style: styleNone,
CommitMessageFormat: fmtGitBasic.Display,
Template: tplStandard,
CommitMessageFormat: fmtGitBasic.display,
Template: tplStandard.display,
})

assert.Nil(err)
assert.Contains(out, "style: none")
assert.Contains(out, "template: CHANGELOG.tpl.md")
assert.Contains(out, " repository_url: https://github.com/git-chglog/git-chglog/git-chglog")
assert.Contains(out, fmt.Sprintf(" pattern: \"%s\"", fmtGitBasic.Pattern))
assert.Contains(out, fmt.Sprintf(" pattern: \"%s\"", fmtGitBasic.pattern))
assert.Contains(out, fmt.Sprintf(
` pattern_maps:
- %s
- %s`,
fmtGitBasic.PatternMaps[0],
fmtGitBasic.PatternMaps[1],
fmtGitBasic.patternMaps[0],
fmtGitBasic.patternMaps[1],
))
}

Expand All @@ -39,8 +39,8 @@ func TestConfigBuliderEmptyRepoURL(t *testing.T) {
out, err := builder.Build(&Answer{
RepositoryURL: "",
Style: styleNone,
CommitMessageFormat: fmtGitBasic.Display,
Template: tplStandard,
CommitMessageFormat: fmtGitBasic.display,
Template: tplStandard.display,
})

assert.Nil(err)
Expand All @@ -55,7 +55,7 @@ func TestConfigBuliderInvalidFormat(t *testing.T) {
RepositoryURL: "",
Style: styleNone,
CommitMessageFormat: "",
Template: tplStandard,
Template: tplStandard.display,
})

assert.Contains(err.Error(), "invalid commit message format")
Expand Down
8 changes: 4 additions & 4 deletions cmd/git-chglog/custom_template_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ func (*customTemplateBuilderImpl) versionHeader(style, template string) string {

// format
switch template {
case tplStandard:
case tplStandard.display:
tpl = fmt.Sprintf("%s## %s (%s)\n\n",
tpl,
tagName,
date,
)
case tplCool:
case tplCool.display:
tpl = fmt.Sprintf("%s## %s\n\n> %s\n\n",
tpl,
tagName,
Expand All @@ -82,13 +82,13 @@ func (*customTemplateBuilderImpl) commits(template, format string) string {
)

switch format {
case fmtSubject.Display:
case fmtSubject.display:
body = `{{ range .Commits -}}
* {{ .Header }}
{{ end }}`

default:
if format == fmtTypeScopeSubject.Display {
if format == fmtTypeScopeSubject.display {
header = "{{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}"
} else {
header = "{{ .Subject }}"
Expand Down
28 changes: 14 additions & 14 deletions cmd/git-chglog/custom_template_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestCustomTemplateBuilderDefault(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleGitHub,
CommitMessageFormat: fmtTypeScopeSubject.Display,
Template: tplStandard,
CommitMessageFormat: fmtTypeScopeSubject.display,
Template: tplStandard.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down Expand Up @@ -65,8 +65,8 @@ func TestCustomTemplateBuilderNone(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtTypeScopeSubject.Display,
Template: tplStandard,
CommitMessageFormat: fmtTypeScopeSubject.display,
Template: tplStandard.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down Expand Up @@ -117,8 +117,8 @@ func TestCustomTemplateBuilderSubjectOnly(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtSubject.Display,
Template: tplStandard,
CommitMessageFormat: fmtSubject.display,
Template: tplStandard.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down Expand Up @@ -167,8 +167,8 @@ func TestCustomTemplateBuilderSubject(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtTypeSubject.Display,
Template: tplStandard,
CommitMessageFormat: fmtTypeSubject.display,
Template: tplStandard.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down Expand Up @@ -219,8 +219,8 @@ func TestCustomTemplateBuilderIgnoreReverts(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtTypeSubject.Display,
Template: tplStandard,
CommitMessageFormat: fmtTypeSubject.display,
Template: tplStandard.display,
IncludeMerges: true,
IncludeReverts: false,
})
Expand Down Expand Up @@ -263,8 +263,8 @@ func TestCustomTemplateBuilderIgnoreMerges(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtTypeSubject.Display,
Template: tplStandard,
CommitMessageFormat: fmtTypeSubject.display,
Template: tplStandard.display,
IncludeMerges: false,
IncludeReverts: true,
})
Expand Down Expand Up @@ -307,8 +307,8 @@ func TestCustomTemplateBuilderCool(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtTypeScopeSubject.Display,
Template: tplCool,
CommitMessageFormat: fmtTypeScopeSubject.display,
Template: tplCool.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/git-chglog/kac_template_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (t *kacTemplateBuilderImpl) commits(commitGroups, format string) string {
)

switch format {
case fmtSubject.Display:
case fmtSubject.display:
body = `{{ range .Commits -}}
- {{ .Header }}
{{ end }}`
Expand Down
12 changes: 6 additions & 6 deletions cmd/git-chglog/kac_template_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestKACTemplateBuilderDefault(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleGitHub,
CommitMessageFormat: fmtTypeScopeSubject.Display,
Template: tplKeepAChangelog,
CommitMessageFormat: fmtTypeScopeSubject.display,
Template: tplKeepAChangelog.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down Expand Up @@ -83,8 +83,8 @@ func TestKACTemplateBuilderNone(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtTypeScopeSubject.Display,
Template: tplKeepAChangelog,
CommitMessageFormat: fmtTypeScopeSubject.display,
Template: tplKeepAChangelog.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down Expand Up @@ -143,8 +143,8 @@ func TestKACTemplateBuilderSubject(t *testing.T) {

out, err := builder.Build(&Answer{
Style: styleNone,
CommitMessageFormat: fmtSubject.Display,
Template: tplKeepAChangelog,
CommitMessageFormat: fmtSubject.display,
Template: tplKeepAChangelog.display,
IncludeMerges: true,
IncludeReverts: true,
})
Expand Down
38 changes: 21 additions & 17 deletions cmd/git-chglog/questioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ func (q *questionerImpl) Ask() (*Answer, error) {

func (q *questionerImpl) ask() (*Answer, error) {
ans := &Answer{}
fmts := q.getFormats()
fmts := q.getPreviewableList(formats)
tpls := q.getPreviewableList(templates)

var previewableTransform = func(ans interface{}) (newAns interface{}) {
if s, ok := ans.(string); ok {
newAns = q.parsePreviewableList(s)
}
return
}

questions := []*survey.Question{
{
Expand All @@ -102,20 +110,16 @@ func (q *questionerImpl) ask() (*Answer, error) {
Options: fmts,
Default: fmts[0],
},
Transform: func(ans interface{}) (newAns interface{}) {
if s, ok := ans.(string); ok {
newAns = q.parseFormat(s)
}
return
},
Transform: previewableTransform,
},
{
Name: "template",
Prompt: &survey.Select{
Message: "What is your favorite template style?",
Options: templates,
Default: templates[0],
Options: tpls,
Default: tpls[0],
},
Transform: previewableTransform,
},
{
Name: "include_merges",
Expand Down Expand Up @@ -148,29 +152,29 @@ func (q *questionerImpl) ask() (*Answer, error) {
return ans, nil
}

func (*questionerImpl) getFormats() []string {
arr := make([]string, len(formats))
func (*questionerImpl) getPreviewableList(list []Previewable) []string {
arr := make([]string, len(list))
max := 0

for _, f := range formats {
l := len(f.Display)
for _, p := range list {
l := len(p.Display())
if max < l {
max = l
}
}

for i, f := range formats {
for i, p := range list {
arr[i] = fmt.Sprintf(
"%s -- %s",
f.Display+strings.Repeat(" ", max-len(f.Display)),
f.Preview,
p.Display()+strings.Repeat(" ", max-len(p.Display())),
p.Preview(),
)
}

return arr
}

func (*questionerImpl) parseFormat(input string) string {
func (*questionerImpl) parsePreviewableList(input string) string {
return strings.TrimSpace(strings.Split(input, "--")[0])
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/git-chglog/template_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type TemplateBuilderFactory = func(string) TemplateBuilder

func templateBuilderFactory(template string) TemplateBuilder {
switch template {
case tplKeepAChangelog:
case tplKeepAChangelog.display:
return NewKACTemplateBuilder()
default:
return NewCustomTemplateBuilder()
Expand Down
Loading

0 comments on commit 55dbf00

Please sign in to comment.