forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(posh-git): parse environment variable
BREAKING CHANGE: this removes the posh-git segment. To mitigate, rename the posh-git segment to git. In case you had a custom template, make sure to migrate to the git segment's template. You can now also leverage the same logic and properties as the git segment in both the text template and/or color templates.
- Loading branch information
1 parent
17f75fa
commit 3ded4c0
Showing
10 changed files
with
366 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,88 @@ | ||
package segments | ||
|
||
import ( | ||
"oh-my-posh/environment" | ||
"oh-my-posh/properties" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type PoshGit struct { | ||
props properties.Properties | ||
env environment.Environment | ||
|
||
Status string | ||
} | ||
|
||
const ( | ||
poshGitEnv = "POSH_GIT_STATUS" | ||
) | ||
|
||
func (p *PoshGit) Template() string { | ||
return " {{ .Status }} " | ||
type poshGit struct { | ||
StashCount int `json:"StashCount"` | ||
AheadBy int `json:"AheadBy"` | ||
Index *poshGitStatus `json:"Index"` | ||
RepoName string `json:"RepoName"` | ||
HasWorking bool `json:"HasWorking"` | ||
Branch string `json:"Branch"` | ||
HasIndex bool `json:"HasIndex"` | ||
GitDir string `json:"GitDir"` | ||
BehindBy int `json:"BehindBy"` | ||
HasUntracked bool `json:"HasUntracked"` | ||
Working *poshGitStatus `json:"Working"` | ||
Upstream string `json:"Upstream"` | ||
} | ||
|
||
type poshGitStatus struct { | ||
Added []string `json:"Added"` | ||
Modified []string `json:"Modified"` | ||
Deleted []string `json:"Deleted"` | ||
Unmerged []string `json:"Unmerged"` | ||
} | ||
|
||
func (s *GitStatus) parsePoshGitStatus(p *poshGitStatus) { | ||
if p == nil { | ||
return | ||
} | ||
s.Added = len(p.Added) | ||
s.Deleted = len(p.Deleted) | ||
s.Modified = len(p.Modified) | ||
s.Unmerged = len(p.Unmerged) | ||
} | ||
|
||
func (p *PoshGit) Enabled() bool { | ||
status := p.env.Getenv(poshGitEnv) | ||
p.Status = strings.TrimSpace(status) | ||
return p.Status != "" | ||
func (g *Git) hasPoshGitStatus() bool { | ||
envStatus := g.env.Getenv(poshGitEnv) | ||
if len(envStatus) == 0 { | ||
return false | ||
} | ||
var posh poshGit | ||
err := json.Unmarshal([]byte(envStatus), &posh) | ||
if err != nil { | ||
return false | ||
} | ||
g.setDir(posh.GitDir) | ||
g.Working = &GitStatus{} | ||
g.Working.parsePoshGitStatus(posh.Working) | ||
g.Staging = &GitStatus{} | ||
g.Staging.parsePoshGitStatus(posh.Index) | ||
g.HEAD = g.parsePoshGitHEAD(posh.Branch) | ||
g.StashCount = posh.StashCount | ||
g.Ahead = posh.AheadBy | ||
g.Behind = posh.BehindBy | ||
g.UpstreamGone = len(posh.Upstream) == 0 | ||
g.Upstream = posh.Upstream | ||
g.setBranchStatus() | ||
if len(g.Upstream) != 0 && g.props.GetBool(FetchUpstreamIcon, false) { | ||
g.UpstreamIcon = g.getUpstreamIcon() | ||
} | ||
return true | ||
} | ||
|
||
func (p *PoshGit) Init(props properties.Properties, env environment.Environment) { | ||
p.props = props | ||
p.env = env | ||
func (g *Git) parsePoshGitHEAD(head string) string { | ||
// commit | ||
if strings.HasSuffix(head, "...)") { | ||
head = strings.TrimLeft(head, "(") | ||
head = strings.TrimRight(head, ".)") | ||
return fmt.Sprintf("%s%s", g.props.GetString(CommitIcon, "\uF417"), head) | ||
} | ||
// tag | ||
if strings.HasPrefix(head, "(") { | ||
head = strings.TrimLeft(head, "(") | ||
head = strings.TrimRight(head, ")") | ||
return fmt.Sprintf("%s%s", g.props.GetString(TagIcon, "\uF412"), head) | ||
} | ||
// regular branch | ||
return fmt.Sprintf("%s%s", g.props.GetString(BranchIcon, "\uE0A0"), g.formatHEAD(head)) | ||
} |
Oops, something went wrong.