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:
- 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 }}
. - Is there a way such that I can get the path starting within the repo starting directory?
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:
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.