node-gh / gh

(DEPRECATED) GitHub CLI made with NodeJS. Use the official https://cli.github.com/ instead.
http://nodegh.io
Other
1.71k stars 217 forks source link

Difficult consuming output #544

Closed bpatram closed 6 years ago

bpatram commented 6 years ago

Hi, I'm running version 1.12.12 and am trying to grab a PR title and author based on a given PR number (via gh pull-request --info ####). This all works great, however pulling out just the information I need is a bit of a hassle. Since the output is colored there are hidden control characters which makes using regex not so straightforward. Ideally, I would love a way for me to control the output of this function.

I have two requests:

Here is my use case: $ gh pull-request --info 1234

This may output something like this (control characters shown below):

#1234 My PR title @bpatram (7 hours ago)
The full PR description shows here

Currently what I have to do is pipe that output to strip those characters: $ gh pull-request --info 1234 | sed -E "s/[[:cntrl:]]\\[[0-9]{1,3}m//g"

Which works great, then I have to use regex to pull out the PR title and author

[[ $GH_PR_OUTPUT =~ ^\#[[:digit:]]+[[:space:]]*(.*)@([[:alnum:]]+) ]]; echo "${BASH_REMATCH[1]} ${BASH_REMATCH[2]}"

Nevertheless, doing all that is exhausting and a bit contrived for such a simple use case.


Proposed Solution:

I would love to be able to do something like the following:

$ gh pull-request --info 1234 --format=%au --no-color
$ gh pull-request --info 1234 --format=%s --no-color

%au: Author Username %s: PR Title/Subject %n: PR number

This would then output:

@bpatram
My PR title

Of course it would be lovely to be able to compose complex formats, for example:

$ gh pull-request --info 1234 --format="#%n %au %s (%cr)\n%b" --no-color

Which would, in theory, output what is currently output today:

#1234 My PR title @bpatram (7 hours ago)
The full PR description shows here
protoEvangelion commented 6 years ago

@bpatram Thanks for putting in all the time for thinking this through.

I will take a look at your PR more closely & ideally get it merged & released around Monday.

bpatram commented 6 years ago

@protoEvangelion thanks! To note, my PR is affectively only 50% of what I detailed above which just simply strips away color from the output. It does not add the --format flag as I discussed. I cannot promise I'll find the time to knock that out before Monday however, but I will try :)

protoEvangelion commented 6 years ago

@bpatram We can just merge the --no-color flag related work and merge --format related code at a separate time if you would prefer.

protoEvangelion commented 6 years ago

@bpatram If you would still like to add the format flag please open up a new issue as that is a separate feature.

Again thanks for all the work you did for setting up the --no-color flag.

I think that is going to make peoples' lives a lot easier when trying to parse output!