Skip to content

Commit

Permalink
Update help (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths authored Feb 29, 2024
1 parent 451b44f commit 59cbda6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 8 additions & 5 deletions internal/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ date Get UTC date
date.Local Get local date
date.Year Get year from date
date.Format layout Format date based on layout like time.Format()
true Returns true
false Returns false
deleteFile Deletes the current file, or a list of file names relative to the root
`

func annotations() map[string]string {
Expand All @@ -40,31 +43,31 @@ func annotations() map[string]string {
}
}

func AppendHelpFunc(original func(*cobra.Command, []string)) func(*cobra.Command, []string) {
func AppendHelpFunc(width int, original func(*cobra.Command, []string)) func(*cobra.Command, []string) {
return func(c *cobra.Command, s []string) {
original(c, s)

annotations := c.Annotations
if annotations != nil {
if variables, ok := annotations["help:variables"]; ok {
printAnnotation(c.OutOrStdout(), "Variables:", variables)
printAnnotation(c.OutOrStdout(), width, "Variables:", variables)
}
if functions, ok := annotations["help:functions"]; ok {
printAnnotation(c.OutOrStdout(), "Functions:", functions)
printAnnotation(c.OutOrStdout(), width, "Functions:", functions)
fmt.Fprintln(c.OutOrStdout())
fmt.Fprintln(c.OutOrStdout(), "For more information about functions, see https://github.com/heaths/go-template")
}
}
}
}

func printAnnotation(w io.Writer, name, values string) {
func printAnnotation(w io.Writer, width int, name, values string) {
// Print section name.
fmt.Fprintln(w)
fmt.Fprintln(w, name)

// Print value.
table := tableprinter.New(w, true, 80)
table := tableprinter.New(w, true, width)
for _, value := range strings.Split(values, "\n") {
value = strings.TrimSpace(value)
if value == "" {
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ func main() {
SilenceUsage: true,
}

width, _, err := con.Size()
if err != nil {
width = 80
}
rootCmd.SetOut(con.Stdout())
rootCmd.SetHelpFunc(cmd.AppendHelpFunc(rootCmd.HelpFunc()))
rootCmd.SetHelpFunc(cmd.AppendHelpFunc(width, rootCmd.HelpFunc()))

rootCmd.PersistentFlags().StringVarP(&repo, "repo", "R", "", "Select another repository to use using the [HOST/]OWNER/REPO format")
rootCmd.PersistentFlags().BoolVarP(&opts.Verbose, "verbose", "v", false, "Log verbose output")
Expand Down

0 comments on commit 59cbda6

Please sign in to comment.