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

Color output of keys and values like jq #17

Open
marcellodesales opened this issue Jan 26, 2018 · 10 comments
Open

Color output of keys and values like jq #17

marcellodesales opened this issue Jan 26, 2018 · 10 comments

Comments

@marcellodesales
Copy link

marcellodesales commented Jan 26, 2018

Awesome project! I'm dealing now with a lot of yml files to be processed in kubernetes...

Is there a way to parse and print the key values in color?

Maybe just the same colors as jq or like in the vim editor?

screen shot 2018-01-26 at 12 51 27 am

@kislyuk
Copy link
Owner

kislyuk commented Jan 28, 2018

You can pip install pygments and pipe the output of yq to pygmentize -l yaml to highlight in the terminal. Unfortunately the Pygments yaml highlighter is very drab and does not highlight much. https://bitbucket.org/birkenfeld/pygments-main/issues?q=yaml

@maximbaz
Copy link

I think it's still worth doing as part of yq itself, many people use cat a.json | jq . to preview highlighted and prettified json, it is so tempting to use cat b.yaml | yq -y . to achieve the same for yaml files.

It's okay if pygments is not ideal, just redirect people to their repo in case someone wants to report an issue.

@EronWright
Copy link

Here's the alternative I landed on, use the cli-highlight tool.

$ npm install -g cli-highlight
$ kubectl get pods -o yaml | highlight -l yaml 

I also defined an alias for highlight -l yaml.

@lindhe
Copy link

lindhe commented Feb 10, 2020

There have been a corresponding issue on jq: jqlang/jq#764

And the same solution works here: cat stuff.yaml | yq '' -C | less -R

@sbourlon
Copy link

Another alternative for vim users: cat f.yaml | vim -R -c 'set ft=yaml' -. I personally created an alias alias yaml="vim -R -c 'set ft=yaml'", then cat f.yaml | yaml -

@theFlyingCat04
Copy link

theFlyingCat04 commented Mar 16, 2020

The problem I have with the missing yaml highlighting is that cat a.yml | yq '.' has highlighting but cat a.yml | yq -y '.' has not.

I know that this has the technical reason that without the 'y' switch jq does the highlighting, but it's a strange behavior anyway. So I would vote for directly implementing it into the yq wrapper.

@romain-dartigues
Copy link

Until Pygments improve it's YAML colourisation (relates to #67), an all-Python highlight seems to be out of question.

The cli-highlight mentioned above is in TypeScript/npm which probably pull the world...

A lot of binary highlighter exists; my personal favourite is bat, properly packaged, supporting as of today 124 languages and support automatic pagination as requested in stedolan/jq#764.
Without tuning your ~/.config/bat/config you probably want: your command | bat -pl yaml.

Older binary alternatives would be highlight from https://gitlab.com/saalen/highlight and source-highlight from http://www.gnu.org/software/src-highlite/ (which does not come with a YAML out of the box but it's easy to add).

@gaochundong
Copy link

nice

@akvadrako
Copy link

You don't need Pygments to highlight YAML since you already have an AST. You can customize the emitter, something like this:

class ColoredDumper(yaml.SafeDumper):
    def write_indicator(self, indicator: str, need_whitespace, **kw):
        self.stream.write('\x1b[34m')
        super().write_indicator(indicator, need_whitespace, **kw)
        self.stream.write('\x1b[0m')

    def process_scalar(self):
        if self.simple_key_context:
            self.stream.write('\x1b[31m')
        elif self.event.tag == 'tag:yaml.org,2002:int':
            self.stream.write('\x1b[32m')
        elif self.event.tag == 'tag:yaml.org,2002:str':
            ...

        super().process_scalar()
        self.stream.write('\x1b[0m')

@bgalvao
Copy link

bgalvao commented Nov 27, 2022

I for one am using bat to help me with this

yq . resume.yaml -y | bat -ppl YAML

And boy, it looks good! I should make a shell function that pipes the output like that.

Edit: here's the shell function

function yqq {
	yq -y $1 $2 | bat -ppl YAML
}

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

No branches or pull requests