Oh My Posh: getting whether or not in a Git repo in other segments

156 Views Asked by At

So I was creating my own Oh-My-Posh profile when I ran into a problem.

The profile I want to make is:

When into a repo:

{{ .Git.UserName }}@{{ .Git.Branch }}:{{ .Git.Repo.Name }}{{ .FurtherPath }}$

and when not into a repo:

{{ .System.UserName }}@{{ .System.Hostname }}:{{ .System.Path }}$

so for example Let's say that there exists a git repo at the path: ~\directory0\directory1\repo\ and that there exists the directory ~\directory0\directory1\repo\source\code.

Then the prompt should look like this when in for example directory directory1:

user@host:~\directory0\directory1$

but when I go into the code directory inside of the repo it should look like this:

git_user@git_branch:git_repo_name\source\code$

I managed to fetch of UserName, HostName, and Path. That was easy, however the problem is that I can't tell wether or not I'm in a git repo. I could use the "type" : "Git", however if you are not in a repo then this would result in that that segment not being rendered.

So my question is:

  1. is there a way such that in another segment I can use something like {{ if .Git }}...{{ end }}? Then I could use something like {{ if .Git }}{{ .Git.UserName }}{{ else }}{{ .UserName }}{{ end }}.
  2. Is there a way such that I can get the path starting within the repo starting directory?
1

There are 1 best solutions below

0
On

is there a way such that in another segment I can use something like {{ if .Git }}...{{ end }}? Then I could use something like {{ if .Git }}{{ .Git.UserName }}{{ else }}{{ .UserName }}{{ end }}.

You can accomplish what you want in question 1 by taking advantage Cross Segment Template Properties.

This will allow you to reference segment data available in the Git segment. In your case, it would be the Git User information. NOTE: In order to have the Git user information available, you will have to make sure to enable the fetch_user property.

Here's a sample template (yaml format) that does what you're requesting in part 1:

$schema: https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json
version: 2
blocks:
  
  - newline: true
    alignment: left
    segments:
      - type: path
        style: plain
        template: "{{ if .Segments.Git.User.Name }}{{ .Segments.Git.User.Name }}@{{ .Segments.Git.User.Email }}:{{ .Segments.Git.RepoName }} {{else}} {{ .UserName }}@{{ .HostName }}:{{ .Path }} {{ end }}"
        properties:
          style: folder

      - type: git
        style: plain
        template: " {{ .HEAD }}"
        properties:
          fetch_user: true
    type: prompt

  - newline: true
    alignment: left
    segments:
      - style: plain
        type: text
        template: "$"
    type: prompt
final_space: true

Is there a way such that I can get the path starting within the repo starting directory?

This may be doable with matchP Helper Function and/or/combo-of-both mapped_locations property of the Path segment. However, I don't think this would be very straight forward; and I personally am not sure how to do it.

I do think that taking advantage of the Path Segment and its different styles could get you something that would be a good alternative; but that would depend on what you're wanting from changing the relative start directory, based on being in a repository or not.