Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add word-wrap to summarization #37

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions field_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// FieldDescriptionSet accepts field descriptions
type FieldDescriptionSet interface {
Add(ptr any, description string)
//Deprecated(ptr any, alternative string)

Check failure on line 18 in field_describer.go

View workflow job for this annotation

GitHub Actions / Static analysis

commentFormatting: put a space between `//` and comment text (gocritic)
}

// FieldDescriptionSetProvider implements both DescriptionProvider and FieldDescriptionSet
Expand Down Expand Up @@ -54,6 +55,17 @@
}
}

//func (d *directDescriber) Deprecated(ptr any, alternative string) {

Check failure on line 58 in field_describer.go

View workflow job for this annotation

GitHub Actions / Static analysis

commentFormatting: put a space between `//` and comment text (gocritic)
// v := reflect.ValueOf(ptr)
// if !isPtr(v.Type()) {
// panic(fmt.Sprintf("Add() requires a pointer, but got: %#v", ptr))
// }
// p := v.Pointer()
// d.flagRefs[p] = &pflag.Flag{
// Usage: description,
// }
//}

func (d *directDescriber) GetDescription(v reflect.Value, _ reflect.StructField) string {
if v.CanAddr() {
v = v.Addr()
Expand Down
87 changes: 81 additions & 6 deletions summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"reflect"
"strings"
"unicode"
"unicode/utf8"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -273,31 +275,35 @@ func stringifySection(cfg Config, out *bytes.Buffer, s *section, indent string)
if s.name != "" {
nextIndent += " "

desc := &bytes.Buffer{}

if s.description != "" {
// support multi-line descriptions
lines := strings.Split(strings.TrimSpace(s.description), "\n")
for idx, line := range lines {
out.WriteString(indent + "# " + line)
desc.WriteString(indent + "# " + line)
if idx < len(lines)-1 {
out.WriteString("\n")
desc.WriteString("\n")
}
}
}
if s.env != "" {
value := fmt.Sprintf("(env: %s)", s.env)
if s.description == "" {
// since there is no description, we need to start the comment
out.WriteString(indent + "# ")
desc.WriteString(indent + "# ")
} else {
// buffer between description and env hint
out.WriteString(" ")
desc.WriteString(" ")
}
out.WriteString(value)
desc.WriteString(value)
}
if s.description != "" || s.env != "" {
out.WriteString("\n")
desc.WriteString("\n")
}

out.WriteString(wrapCommentLines(desc.String(), indent, 95))

out.WriteString(indent)

out.WriteString(s.name)
Expand All @@ -322,3 +328,72 @@ func stringifySection(cfg Config, out *bytes.Buffer, s *section, indent string)
}
}
}

func wrapCommentLines(commentLine, indent string, limit int) string {
commentLine = strings.TrimSpace(removeComment(commentLine))

ogLines := strings.Split(commentLine, "(env: ")
if len(ogLines) == 0 {
return ""
}

if len(ogLines) > 1 {
for i := 1; i < len(ogLines); i++ {
ogLines[i] = "(env: " + ogLines[i]
}
}

ogLines[0] = strings.TrimSpace(ogLines[0])
if len(ogLines[0]) == 0 {
if len(ogLines) > 1 {
ogLines = ogLines[1:]
} else {
return ""
}
}

var lines []string
for _, line := range ogLines {
wrappedLine := wordWrap(line, limit)
lines = append(lines, strings.Split(wrappedLine, "\n")...)
}

return indent + "# " + strings.Join(lines, "\n"+indent+"# ") + "\n"
}

// source: https://codereview.stackexchange.com/questions/244435/word-wrap-in-go
func wordWrap(text string, lineWidth int) string {
wrap := make([]byte, 0, len(text)+2*len(text)/lineWidth)
eoLine := lineWidth
inWord := false
for i, j := 0, 0; ; {
r, size := utf8.DecodeRuneInString(text[i:])
if size == 0 && r == utf8.RuneError {
r = ' '
}
if unicode.IsSpace(r) {
if inWord {
if i >= eoLine {
wrap = append(wrap, '\n')
eoLine = len(wrap) + lineWidth
} else if len(wrap) > 0 {
wrap = append(wrap, ' ')
}
wrap = append(wrap, text[j:i]...)
}
inWord = false
} else if !inWord {
inWord = true
j = i
}
if size == 0 && r == ' ' {
break
}
i += size
}
return string(wrap)
}

func removeComment(s string) string {
return strings.TrimSpace(strings.ReplaceAll(s, "# ", ""))
}
Loading