Enable color by default only if os.Stdout is a TTY

This commit is contained in:
Peter Rabbitson 2021-07-07 18:12:17 +02:00
parent 49896afe9f
commit c7bb326c78
9 changed files with 39 additions and 12 deletions

View File

@ -45,6 +45,7 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/market" "github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/lib/tablewriter" "github.com/filecoin-project/lotus/lib/tablewriter"
) )
@ -1233,7 +1234,7 @@ var clientListRetrievalsCmd = &cli.Command{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Usage: "use color in display output", Usage: "use color in display output",
Value: true, Value: cliutil.DefaultColorUse,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "show-failed", Name: "show-failed",
@ -1806,7 +1807,7 @@ var clientListDeals = &cli.Command{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Usage: "use color in display output", Usage: "use color in display output",
Value: true, Value: cliutil.DefaultColorUse,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "show-failed", Name: "show-failed",
@ -2336,7 +2337,7 @@ var clientListTransfers = &cli.Command{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Usage: "use color in display output", Usage: "use color in display output",
Value: true, Value: cliutil.DefaultColorUse,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "completed", Name: "completed",

9
cli/util/color.go Normal file
View File

@ -0,0 +1,9 @@
package cliutil
import (
"os"
"github.com/mattn/go-isatty"
)
var DefaultColorUse = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())

View File

@ -20,6 +20,7 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/lib/tablewriter" "github.com/filecoin-project/lotus/lib/tablewriter"
) )
@ -266,7 +267,7 @@ var actorControlList = &cli.Command{
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Value: true, Value: cliutil.DefaultColorUse,
}, },
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {

View File

@ -26,6 +26,7 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/lib/tablewriter" "github.com/filecoin-project/lotus/lib/tablewriter"
) )
@ -389,7 +390,7 @@ var actorControlList = &cli.Command{
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Value: true, Value: cliutil.DefaultColorUse,
}, },
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {

View File

@ -13,6 +13,7 @@ import (
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/lib/lotuslog" "github.com/filecoin-project/lotus/lib/lotuslog"
"github.com/filecoin-project/lotus/lib/tracing" "github.com/filecoin-project/lotus/lib/tracing"
"github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo"
@ -82,6 +83,7 @@ func main() {
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Value: cliutil.DefaultColorUse,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo", Name: "repo",

View File

@ -30,6 +30,7 @@ import (
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
) )
var CidBaseFlag = cli.StringFlag{ var CidBaseFlag = cli.StringFlag{
@ -754,7 +755,7 @@ var transfersListCmd = &cli.Command{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Usage: "use color in display output", Usage: "use color in display output",
Value: true, Value: cliutil.DefaultColorUse,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "completed", Name: "completed",

View File

@ -19,6 +19,7 @@ import (
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
) )
var sealingCmd = &cli.Command{ var sealingCmd = &cli.Command{
@ -36,7 +37,10 @@ var sealingWorkersCmd = &cli.Command{
Name: "workers", Name: "workers",
Usage: "list workers", Usage: "list workers",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{Name: "color"}, &cli.BoolFlag{
Name: "color",
Value: cliutil.DefaultColorUse,
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
color.NoColor = !cctx.Bool("color") color.NoColor = !cctx.Bool("color")
@ -127,7 +131,10 @@ var sealingJobsCmd = &cli.Command{
Name: "jobs", Name: "jobs",
Usage: "list running jobs", Usage: "list running jobs",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{Name: "color"}, &cli.BoolFlag{
Name: "color",
Value: cliutil.DefaultColorUse,
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "show-ret-done", Name: "show-ret-done",
Usage: "show returned but not consumed calls", Usage: "show returned but not consumed calls",

View File

@ -26,6 +26,7 @@ import (
"github.com/filecoin-project/lotus/lib/tablewriter" "github.com/filecoin-project/lotus/lib/tablewriter"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing" sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
) )
@ -163,7 +164,7 @@ var sectorsListCmd = &cli.Command{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Aliases: []string{"c"}, Aliases: []string{"c"},
Value: true, Value: cliutil.DefaultColorUse,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "fast", Name: "fast",

View File

@ -27,6 +27,7 @@ import (
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/extern/sector-storage/fsutil" "github.com/filecoin-project/lotus/extern/sector-storage/fsutil"
"github.com/filecoin-project/lotus/extern/sector-storage/stores" "github.com/filecoin-project/lotus/extern/sector-storage/stores"
"github.com/filecoin-project/lotus/extern/sector-storage/storiface" "github.com/filecoin-project/lotus/extern/sector-storage/storiface"
@ -166,7 +167,10 @@ var storageListCmd = &cli.Command{
Name: "list", Name: "list",
Usage: "list local storage paths", Usage: "list local storage paths",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{Name: "color"}, &cli.BoolFlag{
Name: "color",
Value: cliutil.DefaultColorUse,
},
}, },
Subcommands: []*cli.Command{ Subcommands: []*cli.Command{
storageListSectorsCmd, storageListSectorsCmd,
@ -479,7 +483,7 @@ var storageListSectorsCmd = &cli.Command{
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
Value: true, Value: cliutil.DefaultColorUse,
}, },
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {