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

Request: Partial coloring of matches #118

Open
AndydeCleyre opened this issue Dec 9, 2019 · 3 comments
Open

Request: Partial coloring of matches #118

AndydeCleyre opened this issue Dec 9, 2019 · 3 comments
Labels

Comments

@AndydeCleyre
Copy link

Example log output:

[2019-12-09T12:06:34-0500] [ALPM] running '30-systemd-update.hook'...
[2019-12-09T12:06:34-0500] [ALPM] running 'detect-old-perl-modules.hook'...
[2019-12-09T12:06:34-0500] [ALPM] running 'gtk-update-icon-cache.hook'...
[2019-12-09T12:06:35-0500] [ALPM] running 'update-desktop-database.hook'...

Example goal: colorize the <month>-<day> part of each line, if present.

Current best possibility, as far as I know:

rainbow --blue '\d\d-\d\dT'

This also colorizes the T, which is useful for matching, but it would be nice to optionally specify a subset of each match to apply the color to.

One possible approach: add up to one --groups GROUPS switch after each style-pattern switch, to limit the styling to specific parenthesized groups in the match:

rainbow --blue '(\d\d-\d\d)T' --groups 1
rainbow --blue '(\d\d)-(\d\d)T' --groups 1,2
rainbow --blue '(\d\d)-(\d\d)T' --groups 1-2  # unnecessary, just for bonus points

If it turns out to be better or simpler to work on exclusion instead, that's just as effective:

rainbow --blue '\d\d-\d\d(T)' --exclude 1
rainbow --blue '\d\d(-)\d\d(T)' --exclude 1,2
rainbow --blue '\d\d(-)\d\d(T)' --exclude 1-2  # unnecessary, just for bonus points
  1. Am I missing a way to accomplish this sort of thing already?
  2. If not, might this be a good feature for the project?
@nicoulaj
Copy link
Owner

nicoulaj commented Dec 9, 2019

I think what you are looking for is "positive lookbehind". I did not test it but it should look something like:

(?<=\d{4}-)\d{2}-\d{2}

@AndydeCleyre
Copy link
Author

AndydeCleyre commented Dec 9, 2019

I didn't realize that was supported here, thanks, I'll play with this. I also found this particular example goal can be achieved with

rainbow --blue-after '^\[\d{4}-' --reset-all-before 'T\d\d:\d\d:\d\d'

EDIT: Yes, thank you, your suggestion indeed works. I still like the --groups syntax idea, but it does seem unnecessary now. Please close this if you like. Is the specific regex syntax supported mentioned anywhere?

@AndydeCleyre
Copy link
Author

AndydeCleyre commented Dec 9, 2019

Adding here, maybe just for documentation, that positive lookahead can also be used, as here in the --bold switch:

when () {  # <pkgname-filter>
    ((( $+commands[rainbow] )) &&
        grep -aEi "ed [^ ]*$1" /var/log/pacman.log \
        | rainbow \
            --blue '(?<=^\[\d{4}-)\d{2}-\d{2}' \
            --blue reinstalled \
            --green installed \
            --yellow upgraded \
            --red removed \
            --bold '[^ \(]+(?=\)$)' \
            --cyan $1
    ) ||
        grep -aEi "ed [^ ]*$1" /var/log/pacman.log
}

image

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

No branches or pull requests

2 participants