From c7bb326c783d5860310058c0e4db327cbc37c5ea Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Wed, 7 Jul 2021 18:12:17 +0200 Subject: [PATCH 1/3] Enable color by default only if os.Stdout is a TTY --- cli/client.go | 7 ++++--- cli/util/color.go | 9 +++++++++ cmd/lotus-shed/actor.go | 3 ++- cmd/lotus-storage-miner/actor.go | 3 ++- cmd/lotus-storage-miner/main.go | 4 +++- cmd/lotus-storage-miner/market.go | 3 ++- cmd/lotus-storage-miner/sealing.go | 11 +++++++++-- cmd/lotus-storage-miner/sectors.go | 3 ++- cmd/lotus-storage-miner/storage.go | 8 ++++++-- 9 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 cli/util/color.go diff --git a/cli/client.go b/cli/client.go index dc925b72faa..2e521792d9c 100644 --- a/cli/client.go +++ b/cli/client.go @@ -45,6 +45,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/actors/builtin/market" "github.com/filecoin-project/lotus/chain/types" + cliutil "github.com/filecoin-project/lotus/cli/util" "github.com/filecoin-project/lotus/lib/tablewriter" ) @@ -1233,7 +1234,7 @@ var clientListRetrievalsCmd = &cli.Command{ &cli.BoolFlag{ Name: "color", Usage: "use color in display output", - Value: true, + Value: cliutil.DefaultColorUse, }, &cli.BoolFlag{ Name: "show-failed", @@ -1806,7 +1807,7 @@ var clientListDeals = &cli.Command{ &cli.BoolFlag{ Name: "color", Usage: "use color in display output", - Value: true, + Value: cliutil.DefaultColorUse, }, &cli.BoolFlag{ Name: "show-failed", @@ -2336,7 +2337,7 @@ var clientListTransfers = &cli.Command{ &cli.BoolFlag{ Name: "color", Usage: "use color in display output", - Value: true, + Value: cliutil.DefaultColorUse, }, &cli.BoolFlag{ Name: "completed", diff --git a/cli/util/color.go b/cli/util/color.go new file mode 100644 index 00000000000..8e2383da46e --- /dev/null +++ b/cli/util/color.go @@ -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()) diff --git a/cmd/lotus-shed/actor.go b/cmd/lotus-shed/actor.go index 9d242e2df2c..5237bb848c5 100644 --- a/cmd/lotus-shed/actor.go +++ b/cmd/lotus-shed/actor.go @@ -20,6 +20,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" + cliutil "github.com/filecoin-project/lotus/cli/util" "github.com/filecoin-project/lotus/lib/tablewriter" ) @@ -266,7 +267,7 @@ var actorControlList = &cli.Command{ }, &cli.BoolFlag{ Name: "color", - Value: true, + Value: cliutil.DefaultColorUse, }, }, Action: func(cctx *cli.Context) error { diff --git a/cmd/lotus-storage-miner/actor.go b/cmd/lotus-storage-miner/actor.go index 7e428d0e4af..37bf36e838e 100644 --- a/cmd/lotus-storage-miner/actor.go +++ b/cmd/lotus-storage-miner/actor.go @@ -26,6 +26,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" + cliutil "github.com/filecoin-project/lotus/cli/util" "github.com/filecoin-project/lotus/lib/tablewriter" ) @@ -389,7 +390,7 @@ var actorControlList = &cli.Command{ }, &cli.BoolFlag{ Name: "color", - Value: true, + Value: cliutil.DefaultColorUse, }, }, Action: func(cctx *cli.Context) error { diff --git a/cmd/lotus-storage-miner/main.go b/cmd/lotus-storage-miner/main.go index f5ff2517772..72693fdfde4 100644 --- a/cmd/lotus-storage-miner/main.go +++ b/cmd/lotus-storage-miner/main.go @@ -13,6 +13,7 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/build" 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/tracing" "github.com/filecoin-project/lotus/node/repo" @@ -81,7 +82,8 @@ func main() { Aliases: []string{"a"}, }, &cli.BoolFlag{ - Name: "color", + Name: "color", + Value: cliutil.DefaultColorUse, }, &cli.StringFlag{ Name: "repo", diff --git a/cmd/lotus-storage-miner/market.go b/cmd/lotus-storage-miner/market.go index f46ad32bfa8..5afb2db21e7 100644 --- a/cmd/lotus-storage-miner/market.go +++ b/cmd/lotus-storage-miner/market.go @@ -30,6 +30,7 @@ import ( "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" + cliutil "github.com/filecoin-project/lotus/cli/util" ) var CidBaseFlag = cli.StringFlag{ @@ -754,7 +755,7 @@ var transfersListCmd = &cli.Command{ &cli.BoolFlag{ Name: "color", Usage: "use color in display output", - Value: true, + Value: cliutil.DefaultColorUse, }, &cli.BoolFlag{ Name: "completed", diff --git a/cmd/lotus-storage-miner/sealing.go b/cmd/lotus-storage-miner/sealing.go index ad890129d0b..fd0066bba60 100644 --- a/cmd/lotus-storage-miner/sealing.go +++ b/cmd/lotus-storage-miner/sealing.go @@ -19,6 +19,7 @@ import ( "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" + cliutil "github.com/filecoin-project/lotus/cli/util" ) var sealingCmd = &cli.Command{ @@ -36,7 +37,10 @@ var sealingWorkersCmd = &cli.Command{ Name: "workers", Usage: "list workers", Flags: []cli.Flag{ - &cli.BoolFlag{Name: "color"}, + &cli.BoolFlag{ + Name: "color", + Value: cliutil.DefaultColorUse, + }, }, Action: func(cctx *cli.Context) error { color.NoColor = !cctx.Bool("color") @@ -127,7 +131,10 @@ var sealingJobsCmd = &cli.Command{ Name: "jobs", Usage: "list running jobs", Flags: []cli.Flag{ - &cli.BoolFlag{Name: "color"}, + &cli.BoolFlag{ + Name: "color", + Value: cliutil.DefaultColorUse, + }, &cli.BoolFlag{ Name: "show-ret-done", Usage: "show returned but not consumed calls", diff --git a/cmd/lotus-storage-miner/sectors.go b/cmd/lotus-storage-miner/sectors.go index 2476c16e813..b1499b011c4 100644 --- a/cmd/lotus-storage-miner/sectors.go +++ b/cmd/lotus-storage-miner/sectors.go @@ -26,6 +26,7 @@ import ( "github.com/filecoin-project/lotus/lib/tablewriter" lcli "github.com/filecoin-project/lotus/cli" + cliutil "github.com/filecoin-project/lotus/cli/util" sealing "github.com/filecoin-project/lotus/extern/storage-sealing" ) @@ -163,7 +164,7 @@ var sectorsListCmd = &cli.Command{ &cli.BoolFlag{ Name: "color", Aliases: []string{"c"}, - Value: true, + Value: cliutil.DefaultColorUse, }, &cli.BoolFlag{ Name: "fast", diff --git a/cmd/lotus-storage-miner/storage.go b/cmd/lotus-storage-miner/storage.go index f2068ea86cd..19c1b1a48cb 100644 --- a/cmd/lotus-storage-miner/storage.go +++ b/cmd/lotus-storage-miner/storage.go @@ -27,6 +27,7 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain/types" 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/stores" "github.com/filecoin-project/lotus/extern/sector-storage/storiface" @@ -166,7 +167,10 @@ var storageListCmd = &cli.Command{ Name: "list", Usage: "list local storage paths", Flags: []cli.Flag{ - &cli.BoolFlag{Name: "color"}, + &cli.BoolFlag{ + Name: "color", + Value: cliutil.DefaultColorUse, + }, }, Subcommands: []*cli.Command{ storageListSectorsCmd, @@ -479,7 +483,7 @@ var storageListSectorsCmd = &cli.Command{ Flags: []cli.Flag{ &cli.BoolFlag{ Name: "color", - Value: true, + Value: cliutil.DefaultColorUse, }, }, Action: func(cctx *cli.Context) error { From 140a2e02005888e624058a3fd67fba319ee34191 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Thu, 8 Jul 2021 10:44:13 +0200 Subject: [PATCH 2/3] Stabilize default text and `make gen docsgen-cli` --- cli/client.go | 21 ++++++++++++--------- cmd/lotus-shed/actor.go | 5 +++-- cmd/lotus-storage-miner/actor.go | 5 +++-- cmd/lotus-storage-miner/main.go | 5 +++-- cmd/lotus-storage-miner/market.go | 7 ++++--- cmd/lotus-storage-miner/sealing.go | 10 ++++++---- cmd/lotus-storage-miner/sectors.go | 7 ++++--- cmd/lotus-storage-miner/storage.go | 10 ++++++---- documentation/en/cli-lotus-miner.md | 16 ++++++++-------- documentation/en/cli-lotus.md | 6 +++--- 10 files changed, 52 insertions(+), 40 deletions(-) diff --git a/cli/client.go b/cli/client.go index 2e521792d9c..0e7603cb17a 100644 --- a/cli/client.go +++ b/cli/client.go @@ -1232,9 +1232,10 @@ var clientListRetrievalsCmd = &cli.Command{ Usage: "print verbose deal details", }, &cli.BoolFlag{ - Name: "color", - Usage: "use color in display output", - Value: cliutil.DefaultColorUse, + Name: "color", + Usage: "use color in display output", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, &cli.BoolFlag{ Name: "show-failed", @@ -1805,9 +1806,10 @@ var clientListDeals = &cli.Command{ Usage: "print verbose deal details", }, &cli.BoolFlag{ - Name: "color", - Usage: "use color in display output", - Value: cliutil.DefaultColorUse, + Name: "color", + Usage: "use color in display output", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, &cli.BoolFlag{ Name: "show-failed", @@ -2335,9 +2337,10 @@ var clientListTransfers = &cli.Command{ Usage: "print verbose transfer details", }, &cli.BoolFlag{ - Name: "color", - Usage: "use color in display output", - Value: cliutil.DefaultColorUse, + Name: "color", + Usage: "use color in display output", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, &cli.BoolFlag{ Name: "completed", diff --git a/cmd/lotus-shed/actor.go b/cmd/lotus-shed/actor.go index 5237bb848c5..2f8fe81e832 100644 --- a/cmd/lotus-shed/actor.go +++ b/cmd/lotus-shed/actor.go @@ -266,8 +266,9 @@ var actorControlList = &cli.Command{ Name: "verbose", }, &cli.BoolFlag{ - Name: "color", - Value: cliutil.DefaultColorUse, + Name: "color", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, }, Action: func(cctx *cli.Context) error { diff --git a/cmd/lotus-storage-miner/actor.go b/cmd/lotus-storage-miner/actor.go index 37bf36e838e..9aa683bd785 100644 --- a/cmd/lotus-storage-miner/actor.go +++ b/cmd/lotus-storage-miner/actor.go @@ -389,8 +389,9 @@ var actorControlList = &cli.Command{ Name: "verbose", }, &cli.BoolFlag{ - Name: "color", - Value: cliutil.DefaultColorUse, + Name: "color", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, }, Action: func(cctx *cli.Context) error { diff --git a/cmd/lotus-storage-miner/main.go b/cmd/lotus-storage-miner/main.go index 72693fdfde4..764f7da2f77 100644 --- a/cmd/lotus-storage-miner/main.go +++ b/cmd/lotus-storage-miner/main.go @@ -82,8 +82,9 @@ func main() { Aliases: []string{"a"}, }, &cli.BoolFlag{ - Name: "color", - Value: cliutil.DefaultColorUse, + Name: "color", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, &cli.StringFlag{ Name: "repo", diff --git a/cmd/lotus-storage-miner/market.go b/cmd/lotus-storage-miner/market.go index 5afb2db21e7..5fd7b5ee6b9 100644 --- a/cmd/lotus-storage-miner/market.go +++ b/cmd/lotus-storage-miner/market.go @@ -753,9 +753,10 @@ var transfersListCmd = &cli.Command{ Usage: "print verbose transfer details", }, &cli.BoolFlag{ - Name: "color", - Usage: "use color in display output", - Value: cliutil.DefaultColorUse, + Name: "color", + Usage: "use color in display output", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, &cli.BoolFlag{ Name: "completed", diff --git a/cmd/lotus-storage-miner/sealing.go b/cmd/lotus-storage-miner/sealing.go index fd0066bba60..d7836781604 100644 --- a/cmd/lotus-storage-miner/sealing.go +++ b/cmd/lotus-storage-miner/sealing.go @@ -38,8 +38,9 @@ var sealingWorkersCmd = &cli.Command{ Usage: "list workers", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "color", - Value: cliutil.DefaultColorUse, + Name: "color", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, }, Action: func(cctx *cli.Context) error { @@ -132,8 +133,9 @@ var sealingJobsCmd = &cli.Command{ Usage: "list running jobs", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "color", - Value: cliutil.DefaultColorUse, + Name: "color", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, &cli.BoolFlag{ Name: "show-ret-done", diff --git a/cmd/lotus-storage-miner/sectors.go b/cmd/lotus-storage-miner/sectors.go index b1499b011c4..5118ed82371 100644 --- a/cmd/lotus-storage-miner/sectors.go +++ b/cmd/lotus-storage-miner/sectors.go @@ -162,9 +162,10 @@ var sectorsListCmd = &cli.Command{ Usage: "show removed sectors", }, &cli.BoolFlag{ - Name: "color", - Aliases: []string{"c"}, - Value: cliutil.DefaultColorUse, + Name: "color", + Aliases: []string{"c"}, + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, &cli.BoolFlag{ Name: "fast", diff --git a/cmd/lotus-storage-miner/storage.go b/cmd/lotus-storage-miner/storage.go index 19c1b1a48cb..60bad76185f 100644 --- a/cmd/lotus-storage-miner/storage.go +++ b/cmd/lotus-storage-miner/storage.go @@ -168,8 +168,9 @@ var storageListCmd = &cli.Command{ Usage: "list local storage paths", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "color", - Value: cliutil.DefaultColorUse, + Name: "color", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, }, Subcommands: []*cli.Command{ @@ -482,8 +483,9 @@ var storageListSectorsCmd = &cli.Command{ Usage: "get list of all sector files", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "color", - Value: cliutil.DefaultColorUse, + Name: "color", + Value: cliutil.DefaultColorUse, + DefaultText: "depends on output being a TTY", }, }, Action: func(cctx *cli.Context) error { diff --git a/documentation/en/cli-lotus-miner.md b/documentation/en/cli-lotus-miner.md index aab71bede60..d27bf7c6d1d 100644 --- a/documentation/en/cli-lotus-miner.md +++ b/documentation/en/cli-lotus-miner.md @@ -41,7 +41,7 @@ COMMANDS: GLOBAL OPTIONS: --actor value, -a value specify other actor to check state for (read only) - --color (default: false) + --color (default: depends on output being a TTY) --miner-repo value, --storagerepo value Specify miner repo path. flag(storagerepo) and env(LOTUS_STORAGE_PATH) are DEPRECATION, will REMOVE SOON (default: "~/.lotusminer") [$LOTUS_MINER_PATH, $LOTUS_STORAGE_PATH] --help, -h show help (default: false) --version, -v print the version (default: false) @@ -295,7 +295,7 @@ USAGE: OPTIONS: --verbose (default: false) - --color (default: true) + --color (default: depends on output being a TTY) --help, -h show help (default: false) ``` @@ -888,7 +888,7 @@ USAGE: OPTIONS: --verbose, -v print verbose transfer details (default: false) - --color use color in display output (default: true) + --color use color in display output (default: depends on output being a TTY) --completed show completed data transfers (default: false) --watch watch deal updates in real-time, rather than a one time list (default: false) --show-failed show failed/cancelled transfers (default: false) @@ -1344,7 +1344,7 @@ USAGE: OPTIONS: --show-removed show removed sectors (default: false) - --color, -c (default: true) + --color, -c (default: depends on output being a TTY) --fast don't show on-chain info for better performance (default: false) --events display number of events the sector has received (default: false) --seal-time display how long it took for the sector to be sealed (default: false) @@ -1739,7 +1739,7 @@ COMMANDS: help, h Shows a list of commands or help for one command OPTIONS: - --color (default: false) + --color (default: depends on output being a TTY) --help, -h show help (default: false) --version, -v print the version (default: false) @@ -1754,7 +1754,7 @@ USAGE: lotus-miner storage list sectors [command options] [arguments...] OPTIONS: - --color (default: true) + --color (default: depends on output being a TTY) --help, -h show help (default: false) ``` @@ -1816,7 +1816,7 @@ USAGE: lotus-miner sealing jobs [command options] [arguments...] OPTIONS: - --color (default: false) + --color (default: depends on output being a TTY) --show-ret-done show returned but not consumed calls (default: false) --help, -h show help (default: false) @@ -1831,7 +1831,7 @@ USAGE: lotus-miner sealing workers [command options] [arguments...] OPTIONS: - --color (default: false) + --color (default: depends on output being a TTY) --help, -h show help (default: false) ``` diff --git a/documentation/en/cli-lotus.md b/documentation/en/cli-lotus.md index 2b2c104034d..6971ed6e77a 100644 --- a/documentation/en/cli-lotus.md +++ b/documentation/en/cli-lotus.md @@ -535,7 +535,7 @@ CATEGORY: OPTIONS: --verbose, -v print verbose deal details (default: false) - --color use color in display output (default: true) + --color use color in display output (default: depends on output being a TTY) --show-failed show failed/failing deals (default: true) --completed show completed retrievals (default: false) --watch watch deal updates in real-time, rather than a one time list (default: false) @@ -609,7 +609,7 @@ CATEGORY: OPTIONS: --verbose, -v print verbose deal details (default: false) - --color use color in display output (default: true) + --color use color in display output (default: depends on output being a TTY) --show-failed show failed/failing deals (default: false) --watch watch deal updates in real-time, rather than a one time list (default: false) --help, -h show help (default: false) @@ -747,7 +747,7 @@ CATEGORY: OPTIONS: --verbose, -v print verbose transfer details (default: false) - --color use color in display output (default: true) + --color use color in display output (default: depends on output being a TTY) --completed show completed data transfers (default: false) --watch watch deal updates in real-time, rather than a one time list (default: false) --show-failed show failed/cancelled transfers (default: false) From 4dd59b566ced9712a3af3e8accb6aaa612ab058b Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Thu, 8 Jul 2021 10:44:37 +0200 Subject: [PATCH 3/3] Respect GOLOG_LOG_FMT=color --- cli/util/color.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cli/util/color.go b/cli/util/color.go index 8e2383da46e..b90b218e9cf 100644 --- a/cli/util/color.go +++ b/cli/util/color.go @@ -6,4 +6,9 @@ import ( "github.com/mattn/go-isatty" ) -var DefaultColorUse = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) +// DefaultColorUse is the globally referenced variable for all Lotus CLI tools +// It sets to `true` if STDOUT is a TTY or if the variable GOLOG_LOG_FMT is set +// to color (as recognizd by github.com/ipfs/go-log/v2) +var DefaultColorUse = os.Getenv("GOLOG_LOG_FMT") == "color" || + isatty.IsTerminal(os.Stdout.Fd()) || + isatty.IsCygwinTerminal(os.Stdout.Fd())