* Add "reference" help topic * Only print reference as a help topic * fix for color fns, slightly generalize * WIP for switching to markdown * escape gt/lt * minor * higher wrap point * detect terminal theme * futz with angle brackets once more * minor cleanup * prepend parent commands * rename help topic fns and add test * Simplify reference help generation - the `<...>` characters from command usage line are now preserved by enclosing the entire usage synopsis in a code span - hard breaks in flag usage lines are preserved by enclosing flag usage in a code block - TTY detection and Markdown rendering are now delayed until the user explicitly requests `gh help reference` - `gh help reference` output is now pager-enabled Co-authored-by: vilmibm <vilmibm@github.com> Co-authored-by: vilmibm <vilmibm@neongrid.space> Co-authored-by: Mislav Marohnić <mislav@github.com>
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package root
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/cli/cli/pkg/iostreams"
|
|
"github.com/cli/cli/pkg/markdown"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func referenceHelpFn(io *iostreams.IOStreams) func(*cobra.Command, []string) {
|
|
return func(cmd *cobra.Command, args []string) {
|
|
wrapWidth := 0
|
|
style := "notty"
|
|
if io.IsStdoutTTY() {
|
|
wrapWidth = io.TerminalWidth()
|
|
style = markdown.GetStyle(io.DetectTerminalTheme())
|
|
}
|
|
|
|
md, err := markdown.RenderWrap(cmd.Long, style, wrapWidth)
|
|
if err != nil {
|
|
fmt.Fprintln(io.ErrOut, err)
|
|
return
|
|
}
|
|
|
|
if !io.IsStdoutTTY() {
|
|
fmt.Fprint(io.Out, dedent(md))
|
|
return
|
|
}
|
|
|
|
_ = io.StartPager()
|
|
defer io.StopPager()
|
|
fmt.Fprint(io.Out, md)
|
|
}
|
|
}
|
|
|
|
func referenceLong(cmd *cobra.Command) string {
|
|
buf := bytes.NewBufferString("# gh reference\n\n")
|
|
for _, c := range cmd.Commands() {
|
|
if c.Hidden {
|
|
continue
|
|
}
|
|
cmdRef(buf, c, 2)
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func cmdRef(w io.Writer, cmd *cobra.Command, depth int) {
|
|
// Name + Description
|
|
fmt.Fprintf(w, "%s `%s`\n\n", strings.Repeat("#", depth), cmd.UseLine())
|
|
fmt.Fprintf(w, "%s\n\n", cmd.Short)
|
|
|
|
// Flags
|
|
// TODO: fold in InheritedFlags/PersistentFlags, but omit `--help` due to repetitiveness
|
|
if flagUsages := cmd.Flags().FlagUsages(); flagUsages != "" {
|
|
fmt.Fprintf(w, "```\n%s````\n\n", dedent(flagUsages))
|
|
}
|
|
|
|
// Subcommands
|
|
for _, c := range cmd.Commands() {
|
|
if c.Hidden {
|
|
continue
|
|
}
|
|
cmdRef(w, c, depth+1)
|
|
}
|
|
}
|