Skip to content

Commit

Permalink
CLI: colored help (all variations)
Browse files Browse the repository at this point in the history
* commands, subcommands, and the app ('ais --help') itself
* a bunch of colored templates and wiring
* separately, `more` pagination: replace memsys with simple buffer
* with refactoring and cleanup

Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Feb 12, 2025
1 parent 1b65a37 commit e95f3ac
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 169 deletions.
78 changes: 14 additions & 64 deletions cmd/cli/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"sync"
"time"

"github.com/NVIDIA/aistore/cmd/cli/config"
"github.com/NVIDIA/aistore/cmd/cli/teb"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/debug"
"github.com/NVIDIA/aistore/memsys"
"github.com/fatih/color"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -73,64 +70,8 @@ var (
fred, fcyan, fblue, fgreen func(a ...any) string
)

// `ais help [COMMAND]`
var helpCommand = cli.Command{
Name: "help",
Usage: "Show a list of commands; show help for a given command",
ArgsUsage: "[COMMAND]",
Action: helpCmdHandler,
BashComplete: func(c *cli.Context) {
for _, cmd := range c.App.Commands {
fmt.Println(cmd.Name)
}
},
}

func paginate(_ io.Writer, templ string, data interface{}) {
gmm, err := memsys.NewMMSA("cli-out-buffer", true)
if err != nil {
exitln("memsys:", err)
}
sgl := gmm.NewSGL(0)

r, w, err := os.Pipe()
if err != nil {
exitln("os.pipe:", err)
}
var wg sync.WaitGroup
wg.Add(1)

go func() {
defer wg.Done()
if _, err := io.Copy(sgl, r); err != nil {
exitln("write sgl:", err)
}
r.Close()
}()

cli.HelpPrinterCustom(w, templ, data, nil)
w.Close()
wg.Wait()

cmd := exec.Command("more")
cmd.Stdin = sgl
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
exitln("cmd more:", err)
}
}

func cliConfVerbose() bool { return cfg.Verbose } // more warnings, errors with backtraces and details

func helpCmdHandler(c *cli.Context) error {
args := c.Args()
if args.Present() {
return cli.ShowCommandHelp(c, args.First())
}
return cli.ShowAppHelp(c)
}

// main method
func Run(version, buildtime string, args []string) error {
a := acli{app: cli.NewApp(), outWriter: os.Stdout, errWriter: os.Stderr, longRun: &longRun{}}
Expand Down Expand Up @@ -256,14 +197,23 @@ func (a *acli) init(version string, emptyCmdline bool) {
app.Writer = a.outWriter
app.ErrWriter = a.errWriter
app.Description = cliDescr

// paginate help via `more`
if !cfg.NoMore {
cli.HelpPrinter = paginate
cli.HelpPrinter = helpMorePrinter
}

// custom templates
cli.AppHelpTemplate = appHelpTemplate
cli.CommandHelpTemplate = commandHelpTemplate
cli.SubcommandHelpTemplate = subcommandHelpTemplate
// custom templates and help coloring
if !cfg.NoColor {
cli.AppHelpTemplate = appColoredHelpTemplate
cli.CommandHelpTemplate = commandColoredHelpTemplate
cli.SubcommandHelpTemplate = subcommandColoredHelpTemplate
} else {
cli.AppHelpTemplate = appHelpTemplate
cli.CommandHelpTemplate = commandHelpTemplate
cli.SubcommandHelpTemplate = subcommandHelpTemplate
funcColorMap = nil
}

a.setupCommands(emptyCmdline)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/cli/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,8 @@ var (
}
archmimeFlag = cli.StringFlag{ // for apc.QparamArchmime
Name: "archmime",
Usage: "Expected format (mime type) of an object (\"shard\") formatted as: " + archFormats + ";\n" +
indent4 + "\tespecially usable for shards with non-standard extensions\n",
Usage: "Expected format (mime type) of an object (\"shard\") formatted as " + archFormats + ";\n" +
indent4 + "\tespecially usable for shards with non-standard extensions",
}
archregxFlag = cli.StringFlag{ // for apc.QparamArchregx
Name: "archregx",
Expand Down
81 changes: 0 additions & 81 deletions cmd/cli/cli/custom_templates.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/cli/cli/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type (
//////////////

func (e *errUsage) Error() string {
msg := helpMessage(e.helpTemplate, e.helpData)
msg := helpErrMessage(e.helpTemplate, e.helpData)

// remove "alias for" (simplify)
reg := regexp.MustCompile(aliasForRegex)
Expand Down
Loading

0 comments on commit e95f3ac

Please sign in to comment.