JanDeDobbeleer / oh-my-posh

The most customisable and low-latency cross platform/shell prompt renderer
https://ohmyposh.dev
MIT License
17.02k stars 2.37k forks source link

Support for regexp in text templates #2823

Closed tillig closed 2 years ago

tillig commented 2 years ago

Code of Conduct

What would you like to see changed/added?

I'm using the kubectl segment and, for various reasons, I have kubectl context names that are sometimes very long due to a sort of programmatic/conventional format I use. For example, I might have a context like:

azure-subscription-guid.resource-group.cluster-name like c8286312-a409-47d0-a1c0-3be15ecefcb7.my-rg.my-cluster

I was going to try to "detect" contexts in that format so I could truncate them down to.. c8..b7.my-rg.my-cluster

which would be easy with regexp support but that's not something out of the box. A package like this one has it, but I'm not sure if that'd wreck performance or if there's a way to just opt in to certain more common functions.

In any case, it'd be cool to be able to do regexp work on the contents of templates for some extra functionality.

JanDeDobbeleer commented 2 years ago

@tillig this isn't that hard to achieve. Let me see what I can do.

tillig commented 2 years ago

While you're poking at it, it seems like there is potentially some weirdness around slice or maybe I don't understand how to use it. I tried something like this...

{{ if gt (len .Context) 5 }}{{ slice .Context 5 }}{{ else }}{{ .Context }}{{ end }}

...and that yields

unable to create text based on template

I could be doing it wrong. I was just trying to test it out, see if I could do a bunch of slices to mimic the behavior I want.

JanDeDobbeleer commented 2 years ago

@tillig you should be able to see the actual error using oh-my-posh debug. These are built-in commands.

tillig commented 2 years ago

This works great! I'm using this regex to shorten GUID-format strings to XX..YY format:

        {
          "background": "#ebcc34",
          "foreground": "#000000",
          "powerline_symbol": "",
          "style": "powerline",
          "template": " ⎈ {{ replaceP \"([a-f0-9]{2})[a-f0-9]{6}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{10}([a-f0-9]{2})\" .Context \"$1..$2\" }}{{ if .Namespace }} :: {{ .Namespace }}{{ end }} ",
          "type": "kubectl"
        }

Thanks!

github-actions[bot] commented 9 months ago

This issue has been automatically locked since there has not been any recent activity (i.e. last half year) after it was closed. It helps our maintainers focus on the active issues. If you have found a problem that seems similar, please open a discussion first, complete the body with all the details necessary to reproduce, and mention this issue as reference.