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 (optional) color to make output of --help easier to read #340

Open
cleichner opened this issue Nov 15, 2014 · 14 comments
Open

Add (optional) color to make output of --help easier to read #340

cleichner opened this issue Nov 15, 2014 · 14 comments
Labels
exp/novice Someone with a little familiarity can pick up help wanted Seeking public contribution on this issue topic/commands Topic commands

Comments

@cleichner
Copy link
Contributor

No description provided.

@chriscool
Copy link
Contributor

Optional probably means a config flag (like color.ui in Git).

And generaly speaking, colors in the output should be removed when the output is not a tty, otherwise it can create problems when output is piped into a command.

As --help would probably be piped to a pager, we could avoid checking if the output is a tty and instead tell people to use a pager that can accept colors (like less -R). Or we could launch the pager ourselves like Git sometimes does...

Also the output of --help could be quite nice already if it was really a man page, or if there was a flag (like git help -w) to open an HTML version of the help text in a web browser.

@chriscool
Copy link
Contributor

https://github.com/mattn/go-isatty/ can be used to know if the output is on a terminal.

@chriscool
Copy link
Contributor

See also issue #275 about man pages and issue #303 about implementing the ipfs help command.

@dborzov
Copy link
Contributor

dborzov commented Nov 21, 2014

@chriscool go-isatty looks great, but maybe going throuth the trouble of vendoring one more package that just wraps one system call is a little too much for our purposes?

Checking if the stdout is on a terminal goes through this system call for stdoutput tty parameters on both linux and BSD (Mac OS):

func isTerminal() {
    const stdoutFD = 1
    var dimensions [4]uint16

    if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(stdoutFD), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 {
        return 'Not a terminal'
    }
    terminalWidth = int(dimensions[1])
       return "Is a terminal with " + string(terminalWidth) + " symbol width";
}

Only on Windows it is different, but Powershell does not understand the standard escape symbol coloring anyway, if I understand it correctly.

I have been using this system call in my lsp project: dborzov/lsp@345d5ff#diff-ec233a1b052bc886bbdad1626a6443b6R34

and it also yeilds other useful parameters like dimensions of the terminal (symbol width) which you can use to format nice things (like hr lines).

I am going to work on this issue, if everyone is cool with this approach of just using that system call for Unix systems.

@whyrusleeping
Copy link
Member

@dborzov I like this, vendoring is great, but an entire package for a single call is a bit much. On windows i think we can just always return false (windows doesnt have ttys!!) and not have colors in windows.

@btc
Copy link
Contributor

btc commented Nov 21, 2014

To be fair, there are only three functions in the package. (bsd, linux, windows)

func IsTerminal(fd uintptr) bool {
    var st uint32
    r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0)
    return r != 0 && e == 0
}
func IsTerminal(fd uintptr) bool {
    var termios syscall.Termios
    _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
    return err == 0
}
func IsTerminal(fd uintptr) bool {
    var termios syscall.Termios
    _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
    return err == 0
}

@whyrusleeping
Copy link
Member

haha, just that? okay then

@jbenet
Copy link
Member

jbenet commented Nov 22, 2014

maybe going throuth the trouble of vendoring one more package that just wraps one system call is a little too much for our purposes?

vendoring is great, but an entire package for a single call is a bit much.

This is one of the really stupid things about how Go packaging is done today. That current vendoring overhead encourages this kind of thinking. to those of us that have had the lovely experience of working with npm and proper packaging, packages tend to be only one function.

Whenever possible, reuse code. vendoring is not a huge deal. I know godep is not the nicest/easiest thing in the world, but using packages is TRTTD.

@dborzov
Copy link
Contributor

dborzov commented Nov 23, 2014

@jbenet , yeah, fair enough, we should vendor this out.

@dborzov
Copy link
Contributor

dborzov commented Nov 23, 2014

Plus, it turned out there is already an isTerminal() check function in the source written by @mappum ! https://github.com/jbenet/go-ipfs/blame/master/commands/cli/parse.go#L345 Pretty neat :)

@daviddias
Copy link
Member

@jbenet are you still up to CR @dborzov's PR (#377)? We will need to have it rebased by now though, @dborzov would you have availability to do it?

@daviddias daviddias removed the icebox label Jan 2, 2016
@RichardLitt RichardLitt added exp/novice Someone with a little familiarity can pick up and removed difficulty: easy labels Feb 2, 2016
@RichardLitt RichardLitt added help wanted Seeking public contribution on this issue and removed help wanted Seeking public contribution on this issue labels May 31, 2016
@brodo
Copy link

brodo commented Dec 12, 2016

I've started working on this (see screenshot).
Currently, I'm checking if the output is a TTY with the isTty function in cli/parse.go. The problem is checking the config. Currently the config is only read on demand. If we want to have the option in the config, it needs to be read it directly in ipfs/main.go. My personal preference would be not to put the option in the config file, but introduce another command line flag like --color.

If you guys still prefer the config file, I need some help in how to restructure the starting process.

colorful-help

@hsanjuan
Copy link
Contributor

I like --color, but let's see if others can weight in. Maybe there are considerations for having this in the config.

@btc
Copy link
Contributor

btc commented Dec 13, 2016

If we want to have the option in the config, it needs to be read it directly in ipfs/main.go.

Perhaps, when no config exists, the program can be treated as though the color option is not specified.

ariescodescream pushed a commit to ariescodescream/go-ipfs that referenced this issue Oct 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exp/novice Someone with a little familiarity can pick up help wanted Seeking public contribution on this issue topic/commands Topic commands
Projects
No open projects
Development

No branches or pull requests

10 participants