dedent based on shortest indent

This commit is contained in:
Shi Han NG 2020-07-16 18:57:44 +09:00
parent 7120b95731
commit 3fb90b43ba
No known key found for this signature in database
GPG key ID: 09FC1637248554BD
2 changed files with 35 additions and 18 deletions

View file

@ -3,7 +3,6 @@ package command
import (
"bytes"
"fmt"
"regexp"
"strings"
"github.com/cli/cli/utils"
@ -118,11 +117,11 @@ func rootHelpFunc(command *cobra.Command, args []string) {
flagUsages := command.LocalFlags().FlagUsages()
if flagUsages != "" {
helpEntries = append(helpEntries, helpEntry{"FLAGS", stripIndent(flagUsages)})
helpEntries = append(helpEntries, helpEntry{"FLAGS", dedent(flagUsages)})
}
inheritedFlagUsages := command.InheritedFlags().FlagUsages()
if inheritedFlagUsages != "" {
helpEntries = append(helpEntries, helpEntry{"INHERITED FLAGS", stripIndent(inheritedFlagUsages)})
helpEntries = append(helpEntries, helpEntry{"INHERITED FLAGS", dedent(inheritedFlagUsages)})
}
if _, ok := command.Annotations["help:arguments"]; ok {
helpEntries = append(helpEntries, helpEntry{"ARGUMENTS", command.Annotations["help:arguments"]})
@ -160,20 +159,30 @@ func rpad(s string, padding int) string {
return fmt.Sprintf(template, s)
}
func stripIndent(s string) string {
dedent := regexp.MustCompile(`(?m)^ `)
dedended := dedent.ReplaceAllString(s, "")
func dedent(s string) string {
lines := strings.Split(s, "\n")
buf := new(bytes.Buffer)
n := int(^uint(0) >> 1)
for _, line := range strings.Split(dedended, "\n") {
// Abort strip if shorthand exist
if strings.HasPrefix(line, "-") {
return dedended
for _, l := range lines {
if len(l) == 0 {
continue
}
fmt.Fprintln(buf, strings.TrimPrefix(line, " "))
if c := countLeadingSpaces(l); c < n {
n = c
}
}
return buf.String()
var buf bytes.Buffer
for _, l := range lines {
fmt.Fprintln(&buf, strings.TrimPrefix(l, strings.Repeat(" ", n)))
}
return strings.TrimSuffix(buf.String(), "\n")
}
func countLeadingSpaces(s string) int {
return len(s) - len(strings.TrimLeft(s, " "))
}

View file

@ -4,7 +4,7 @@ import (
"testing"
)
func TestStripIndent(t *testing.T) {
func TestDedent(t *testing.T) {
type c struct {
input string
expected string
@ -12,16 +12,24 @@ func TestStripIndent(t *testing.T) {
cases := []c{
{
input: " --help Show help for command\n --version Show gh version",
input: " --help Show help for command\n --version Show gh version\n",
expected: "--help Show help for command\n--version Show gh version\n",
},
{
input: " --help Show help for command\n -R, --repo OWNER/REPO Select another repository using the OWNER/REPO format",
expected: " --help Show help for command\n-R, --repo OWNER/REPO Select another repository using the OWNER/REPO format",
input: " --help Show help for command\n -R, --repo OWNER/REPO Select another repository using the OWNER/REPO format\n",
expected: " --help Show help for command\n-R, --repo OWNER/REPO Select another repository using the OWNER/REPO format\n",
},
{
input: " line 1\n\n line 2\n line 3",
expected: " line 1\n\n line 2\nline 3",
},
{
input: " line 1\n line 2\n line 3\n\n",
expected: "line 1\nline 2\nline 3\n\n",
},
}
for _, kase := range cases {
eq(t, stripIndent(kase.input), kase.expected)
eq(t, dedent(kase.input), kase.expected)
}
}