Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color for --help when output a tty #377

Closed
wants to merge 9 commits into from

Conversation

dborzov
Copy link
Contributor

@dborzov dborzov commented Nov 23, 2014

IPFS's --help output now gets Chrismas lights when the output is on the terminal!

See #340

Color rendering is only when stdoutput is on the terminal.

This uses

  • github.com/mattn/go-isatty for checking if the output is on a terminal
  • https://github.com/mitchellh/colorstring for color coding strings

I was not able to add these packages to Godep. It probably should be make vendor or godep save -r ./... but these don't do the trick. What is up with that?

Here is what it looks like for the standard Ubuntu color scheme:
ipfs-colors

@dborzov
Copy link
Contributor Author

dborzov commented Nov 23, 2014

Travis CI testing fails because the added packages are not vendored. Could you guys help me out with this? All the actual tests pass fine.

@jbenet
Copy link
Member

jbenet commented Nov 23, 2014

@dborzov I'll try vendoring it later tonight/tomorrow. but if you get at it before me, you should be able to:

  1. go get the package you want.
  2. make sure you have godep installed (go get github.com/tools/godep)
  3. add your imports as usual (you already have these)
  4. in the project root, run make vendor.

It sounds like you did all this already, but ran into problems. can you post any errors you got here? Thanks

@chriscool
Copy link
Contributor

@dborzov, #293 might also help you.

@dborzov
Copy link
Contributor Author

dborzov commented Nov 23, 2014

@chriscool tnx!
@jbenet , make vendor gives me this:

go get github.com/tools/godep
godep save -r ./...
godep: cannot find package "github.com/braintree/manners" in any of:
    /usr/local/go/src/pkg/github.com/braintree/manners (from $GOROOT)
    /home/borzov/src/github.com/braintree/manners (from $GOPATH)
godep: cannot find package "github.com/bren2010/proquint" in any of:
    /usr/local/go/src/pkg/github.com/bren2010/proquint (from $GOROOT)
    /home/borzov/src/github.com/bren2010/proquint (from $GOPATH)
godep: cannot find package "github.com/camlistore/lock" in any of:
    /usr/local/go/src/pkg/github.com/camlistore/lock (from $GOROOT)
    /home/borzov/src/github.com/camlistore/lock (from $GOPATH)
godep: cannot find package "github.com/coreos/go-semver/semver" in any of:
    /usr/local/go/src/pkg/github.com/coreos/go-semver/semver (from $GOROOT)
    /home/borzov/src/github.com/coreos/go-semver/semver (from $GOPATH)
godep: cannot find package "github.com/facebookgo/stack" in any of:
    /usr/local/go/src/pkg/github.com/facebookgo/stack (from $GOROOT)
    /home/borzov/src/github.com/facebookgo/stack (from $GOPATH)
godep: cannot find package "github.com/facebookgo/stackerr" in any of:
    /usr/local/go/src/pkg/github.com/facebookgo/stackerr (from $GOROOT)
    /home/borzov/src/github.com/facebookgo/stackerr (from $GOPATH)
godep: cannot find package "github.com/gorilla/context" in any of:
    /usr/local/go/src/pkg/github.com/gorilla/context (from $GOROOT)
    /home/borzov/src/github.com/gorilla/context (from $GOPATH)
godep: cannot find package "github.com/gorilla/mux" in any of:
    /usr/local/go/src/pkg/github.com/gorilla/mux (from $GOROOT)
    /home/borzov/src/github.com/gorilla/mux (from $GOPATH)
godep: cannot find package "github.com/h2so5/utp" in any of:
    /usr/local/go/src/pkg/github.com/h2so5/utp (from $GOROOT)
    /home/borzov/src/github.com/h2so5/utp (from $GOPATH)
godep: cannot find package "github.com/inconshreveable/go-update" in any of:
    /usr/local/go/src/pkg/github.com/inconshreveable/go-update (from $GOROOT)
    /home/borzov/src/github.com/inconshreveable/go-update (from $GOPATH)
godep: cannot find package "github.com/inconshreveable/go-update/check" in any of:
    /usr/local/go/src/pkg/github.com/inconshreveable/go-update/check (from $GOROOT)
    /home/borzov/src/github.com/inconshreveable/go-update/check (from $GOPATH)
godep: cannot find package "github.com/inconshreveable/go-update/download" in any of:
    /usr/local/go/src/pkg/github.com/inconshreveable/go-update/download (from $GOROOT)
    /home/borzov/src/github.com/inconshreveable/go-update/download (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-datastore" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-datastore (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-datastore (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-datastore/fs" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-datastore/fs (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-datastore/fs (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-datastore/keytransform" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-datastore/keytransform (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-datastore/keytransform (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-datastore/leveldb" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-datastore/leveldb (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-datastore/leveldb (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-datastore/namespace" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-datastore/namespace (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-datastore/namespace (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-datastore/sync" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-datastore/sync (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-datastore/sync (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-is-domain" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-is-domain (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-is-domain (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-logging" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-logging (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-logging (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-msgio" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-msgio (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-msgio (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-multiaddr" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-multiaddr (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-multiaddr (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-multiaddr-net" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-multiaddr-net (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-multiaddr-net (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-multihash" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-multihash (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-multihash (from $GOPATH)
godep: cannot find package "github.com/jbenet/go-random" in any of:
    /usr/local/go/src/pkg/github.com/jbenet/go-random (from $GOROOT)
    /home/borzov/src/github.com/jbenet/go-random (from $GOPATH)
godep: cannot find package "github.com/kr/binarydist" in any of:
    /usr/local/go/src/pkg/github.com/kr/binarydist (from $GOROOT)
    /home/borzov/src/github.com/kr/binarydist (from $GOPATH)
godep: cannot find package "github.com/maybebtc/logrus" in any of:
    /usr/local/go/src/pkg/github.com/maybebtc/logrus (from $GOROOT)
    /home/borzov/src/github.com/maybebtc/logrus (from $GOPATH)
godep: cannot find package "github.com/mitchellh/go-homedir" in any of:
    /usr/local/go/src/pkg/github.com/mitchellh/go-homedir (from $GOROOT)
    /home/borzov/src/github.com/mitchellh/go-homedir (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/cache" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/cache (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/cache (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/comparer" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/comparer (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/comparer (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/filter" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/filter (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/filter (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/iterator" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/iterator (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/iterator (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/journal" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/journal (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/journal (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/memdb" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/memdb (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/memdb (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/opt" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/opt (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/opt (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/storage" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/storage (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/storage (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/table" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/table (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/table (from $GOPATH)
godep: cannot find package "github.com/syndtr/goleveldb/leveldb/util" in any of:
    /usr/local/go/src/pkg/github.com/syndtr/goleveldb/leveldb/util (from $GOROOT)
    /home/borzov/src/github.com/syndtr/goleveldb/leveldb/util (from $GOPATH)
godep: cannot find package "github.com/tuxychandru/pubsub" in any of:
    /usr/local/go/src/pkg/github.com/tuxychandru/pubsub (from $GOROOT)
    /home/borzov/src/github.com/tuxychandru/pubsub (from $GOPATH)
godep: cannot find package "gopkg.in/natefinch/lumberjack.v2" in any of:
    /usr/local/go/src/pkg/gopkg.in/natefinch/lumberjack.v2 (from $GOROOT)
    /home/borzov/src/gopkg.in/natefinch/lumberjack.v2 (from $GOPATH)
godep: error loading dependencies
make: *** [vendor] Error 1

so apparently it attempts to look up the original vendored packages locally for some reason and freaks out when it does not find them. I guess you guys have them installed already so you don't see this.

@dborzov
Copy link
Contributor Author

dborzov commented Nov 23, 2014

OK, so the fix i found was running godep restore from repo's root to put all the vendored packages back into $GOPATH and then running godep save -r ./...

@chriscool
Copy link
Contributor

so apparently it attempts to look up the original vendored packages locally for some reason and
freaks out when it does not find them. I guess you guys have them installed already so you don't
see this.

Yeah, you may want to go get each of the packages that it cannot find first.

@dborzov
Copy link
Contributor Author

dborzov commented Nov 23, 2014

@chriscool yeah, godep restore checkouts the vendored versions back to $GOPATH for you.

@chriscool
Copy link
Contributor

@dborzov good to know, thanks!

btc pushed a commit that referenced this pull request Nov 24, 2014
to help out with concerns like this one: #377 (comment)
@btc btc added the codereview label Nov 24, 2014
@jbenet
Copy link
Member

jbenet commented Nov 24, 2014

Maybe we should put godep restore into our makefile, or at least make the error more understandable to users. (The bad part of putting it in is it may blow out versions people had set there purposefully, which is sketchy).

Thoughts @maybebtc ?

"USAGE": c.DefaultColors["light_yellow"],
"ARGUSAGE": c.DefaultColors["light_cyan"],
"ARGUSAGETEXT": c.DefaultColors["light_red"],
"DESCRIPTION": c.DefaultColors["white"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not everyone uses white on black. this should be c.DefaultColors["default"] i think.

And these colors should be configurable in the config probably. (can get to that later)

@jbenet
Copy link
Member

jbenet commented Nov 24, 2014

Also, while some people like color (i do, sometimes), others don't. This should definitely be configurable. And possibly defaulted to off.

TerminalColors flag is to turn on and off coloring for terminal output.

TerminalColors does not quite fit into the existing config subgroups, so I added Config.Preferences struct.

The idea is to keep all the local appearance and formatting prefences there, whether it is for terminal output or WebUI.
@dborzov
Copy link
Contributor Author

dborzov commented Feb 5, 2015

Hey hey!

So I made the suggested changes. I added the config flag for Terminal Colors: I ended up creating a new config subgroup for stuff like this: it is called Preferences. The idea is that all the local formatting and appearance-related settings can go there: stuff like the interface language, WebUI preferences and so on.

I moved invocation struct type definition with its methods out of main.go to a separate dedicatated file. Is that cool with you guys?

I am having a hard time reading off that flag's value when calling the help function, can anyone point me at what to call? invocation.req (commands.Request interface) is not even initialized by default so I can't just call req.Context(). I remember hearing that that there is a way to call the config loader so that these settings are read lazily, was that implemented?

func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer) error {
func ShortHelp(rootName string, root *cmds.Command, req cmds.Request, path []string, out io.Writer) error {
terminalColors := false
if req != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to read off the config's flag from here? I thought it has to be:

  ctx := invoc.req.Context()
  cfg, _ := ctx.GetConfig()
  // we get cfg.Preferences.TerminalColors

but invoc.req is not even initialized by default.

@dborzov
Copy link
Contributor Author

dborzov commented Feb 13, 2015

@jbenet, plz CR :)

@jbenet
Copy link
Member

jbenet commented Feb 16, 2015

@dborzov sorry for the delay-- i havent had much time to CR code right now-- in particular tricky places like this part (main is really tricky :/ ). I'll try to get to this in the next few days.

@whyrusleeping whyrusleeping added topic/commands Topic commands and removed codereview labels Mar 28, 2015
@whyrusleeping
Copy link
Member

closing due to inactivity, please reopen as necessary

note: all pull requests older than three weeks may be closed in an effort to keep our open pull requests more focused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic/commands Topic commands
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants