@@ -14,13 +14,11 @@ import (
14
14
"runtime"
15
15
"strings"
16
16
"syscall"
17
- "text/template"
18
17
"time"
19
18
20
19
"golang.org/x/xerrors"
21
20
22
21
"github.com/charmbracelet/lipgloss"
23
- "github.com/creack/pty"
24
22
"github.com/kirsle/configdir"
25
23
"github.com/mattn/go-isatty"
26
24
"github.com/spf13/cobra"
@@ -443,132 +441,6 @@ func isTTYWriter(cmd *cobra.Command, writer func() io.Writer) bool {
443
441
return isatty .IsTerminal (file .Fd ())
444
442
}
445
443
446
- var templateFunctions = template.FuncMap {
447
- "usageHeader" : usageHeader ,
448
- "isWorkspaceCommand" : isWorkspaceCommand ,
449
- "ttyWidth" : ttyWidth ,
450
- }
451
-
452
- func usageHeader (s string ) string {
453
- // Customizes the color of headings to make subcommands more visually
454
- // appealing.
455
- return cliui .Styles .Placeholder .Render (s )
456
- }
457
-
458
- func isWorkspaceCommand (cmd * cobra.Command ) bool {
459
- if _ , ok := cmd .Annotations ["workspaces" ]; ok {
460
- return true
461
- }
462
- var ws bool
463
- cmd .VisitParents (func (cmd * cobra.Command ) {
464
- if _ , ok := cmd .Annotations ["workspaces" ]; ok {
465
- ws = true
466
- }
467
- })
468
- return ws
469
- }
470
-
471
- func ttyWidth () int {
472
- _ , cols , err := pty .Getsize (os .Stderr )
473
- if err != nil {
474
- // Default width
475
- return 100
476
- }
477
- return cols
478
- }
479
-
480
- func usageTemplate () string {
481
- // usageHeader is defined in init().
482
- return `{{usageHeader "Usage:"}}
483
- {{- if .Runnable}}
484
- {{.UseLine}}
485
- {{end}}
486
- {{- if .HasAvailableSubCommands}}
487
- {{.CommandPath}} [command]
488
- {{end}}
489
-
490
- {{- if gt (len .Aliases) 0}}
491
- {{usageHeader "Aliases:"}}
492
- {{.NameAndAliases}}
493
- {{end}}
494
-
495
- {{- if .HasExample}}
496
- {{usageHeader "Get Started:"}}
497
- {{.Example}}
498
- {{end}}
499
-
500
- {{- $isRootHelp := (not .HasParent)}}
501
- {{- if .HasAvailableSubCommands}}
502
- {{usageHeader "Commands:"}}
503
- {{- range .Commands}}
504
- {{- $isRootWorkspaceCommand := (and $isRootHelp (isWorkspaceCommand .))}}
505
- {{- if (or (and .IsAvailableCommand (not $isRootWorkspaceCommand)) (eq .Name "help"))}}
506
- {{rpad .Name .NamePadding }} {{.Short}}
507
- {{- end}}
508
- {{- end}}
509
- {{end}}
510
-
511
- {{- if (and $isRootHelp .HasAvailableSubCommands)}}
512
- {{usageHeader "Workspace Commands:"}}
513
- {{- range .Commands}}
514
- {{- if (and .IsAvailableCommand (isWorkspaceCommand .))}}
515
- {{rpad .Name .NamePadding }} {{.Short}}
516
- {{- end}}
517
- {{- end}}
518
- {{end}}
519
-
520
- {{- if .HasAvailableLocalFlags}}
521
- {{usageHeader "Flags:"}}
522
- {{.LocalFlags.FlagUsagesWrapped ttyWidth | trimTrailingWhitespaces}}
523
- {{end}}
524
-
525
- {{- if .HasAvailableInheritedFlags}}
526
- {{usageHeader "Global Flags:"}}
527
- {{.InheritedFlags.FlagUsagesWrapped ttyWidth | trimTrailingWhitespaces}}
528
- {{end}}
529
-
530
- {{- if .HasHelpSubCommands}}
531
- {{usageHeader "Additional help topics:"}}
532
- {{- range .Commands}}
533
- {{- if .IsAdditionalHelpTopicCommand}}
534
- {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
535
- {{- end}}
536
- {{- end}}
537
- {{end}}
538
-
539
- {{- if .HasAvailableSubCommands}}
540
- Use "{{.CommandPath}} [command] --help" for more information about a command.
541
- {{end}}`
542
- }
543
-
544
- // example represents a standard example for command usage, to be used
545
- // with formatExamples.
546
- type example struct {
547
- Description string
548
- Command string
549
- }
550
-
551
- // formatExamples formats the examples as width wrapped bulletpoint
552
- // descriptions with the command underneath.
553
- func formatExamples (examples ... example ) string {
554
- wrap := cliui .Styles .Wrap .Copy ()
555
- wrap .PaddingLeft (4 )
556
- var sb strings.Builder
557
- for i , e := range examples {
558
- if len (e .Description ) > 0 {
559
- _ , _ = sb .WriteString (" - " + wrap .Render (e .Description + ":" )[4 :] + "\n \n " )
560
- }
561
- // We add 1 space here because `cliui.Styles.Code` adds an extra
562
- // space. This makes the code block align at an even 2 or 6
563
- // spaces for symmetry.
564
- _ , _ = sb .WriteString (" " + cliui .Styles .Code .Render (fmt .Sprintf ("$ %s" , e .Command )))
565
- if i < len (examples )- 1 {
566
- _ , _ = sb .WriteString ("\n \n " )
567
- }
568
- }
569
- return sb .String ()
570
- }
571
-
572
444
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
573
445
func FormatCobraError (err error , cmd * cobra.Command ) string {
574
446
helpErrMsg := fmt .Sprintf ("Run '%s --help' for usage." , cmd .CommandPath ())
0 commit comments