feat(pwsh): posh-git segment

This commit is contained in:
Jan De Dobbeleer 2021-03-27 17:04:09 +01:00 committed by Jan De Dobbeleer
parent 83246dffc4
commit 320ec1d7d3
7 changed files with 107 additions and 3 deletions

View file

@ -0,0 +1,27 @@
---
id: poshgit
title: Posh-Git
sidebar_label: Posh-Git
---
## What
Display the [posh-git][posh-git] prompt.
:::info
This segment only works within Powershell and requires the posh-git module to be installed and imported.
:::
## Sample Configuration
```json
{
"type": "poshgit",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#ffffff",
"background": "#0077c2"
}
```
[posh-git]: https://github.com/dahlbyk/posh-git

View file

@ -25,6 +25,7 @@ module.exports = {
"node",
"os",
"path",
"poshgit",
"python",
"root",
"ruby",

View file

@ -19,7 +19,8 @@ function global:Set-PoshContext {}
function global:Set-PoshGitStatus {
if (Get-Module -Name "posh-git") {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification = 'Variable used later(not in this scope)')]
$Global:GitStatus = Get-GitStatus
$global:GitStatus = Get-GitStatus
$env:POSH_GIT_STATUS = Write-GitStatus -Status $global:GitStatus
}
}
@ -46,6 +47,7 @@ function global:Set-PoshGitStatus {
$executionTime = ($history.EndExecutionTime - $history.StartExecutionTime).TotalMilliseconds
$global:omp_lastHistoryId = $history.Id
}
Set-PoshGitStatus
$omp = "::OMP::"
$config = $global:PoshSettings.Theme
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
@ -53,7 +55,6 @@ function global:Set-PoshGitStatus {
$standardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time="$executionTime" --config="$config" 2>&1)
# the output can be multiline, joining these ensures proper rendering by adding line breaks with `n
$standardOut -join "`n"
Set-PoshGitStatus
$global:LASTEXITCODE = $realLASTEXITCODE
#remove temp variables
Remove-Variable realLASTEXITCODE -Confirm:$false

View file

@ -96,6 +96,8 @@ const (
Aws SegmentType = "aws"
// Java writes the active java version
Java SegmentType = "java"
// PoshGit writes the posh git prompt
PoshGit SegmentType = "poshgit"
)
func (segment *Segment) string() string {
@ -219,6 +221,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
Ruby: &ruby{},
Aws: &aws{},
Java: &java{},
PoshGit: &poshgit{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{

28
src/segment_posh_git.go Normal file
View file

@ -0,0 +1,28 @@
package main
import "strings"
type poshgit struct {
props *properties
env environmentInfo
gitStatus string
}
const (
poshGitEnv = "POSH_GIT_STATUS"
)
func (p *poshgit) enabled() bool {
status := p.env.getenv(poshGitEnv)
p.gitStatus = strings.TrimSpace(status)
return p.gitStatus != ""
}
func (p *poshgit) string() string {
return p.gitStatus
}
func (p *poshgit) init(props *properties, env environmentInfo) {
p.props = props
p.env = env
}

View file

@ -0,0 +1,32 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPoshGitSegment(t *testing.T) {
cases := []struct {
Case string
PoshGitPrompt string
Expected string
Enabled bool
}{
{Case: "regular prompt", PoshGitPrompt: "my prompt", Expected: "my prompt", Enabled: true},
{Case: "prompt with spaces", PoshGitPrompt: " my prompt", Expected: "my prompt", Enabled: true},
{Case: "no prompt", PoshGitPrompt: "", Enabled: false},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getenv", poshGitEnv).Return(tc.PoshGitPrompt)
p := &poshgit{
env: env,
}
assert.Equal(t, tc.Enabled, p.enabled())
if tc.Enabled {
assert.Equal(t, tc.Expected, p.string())
}
}
}

View file

@ -155,7 +155,8 @@
"ytm",
"executiontime",
"aws",
"java"
"java",
"poshgit"
]
},
"style": {
@ -1419,6 +1420,17 @@
}
}
}
},
{
"if": {
"properties": {
"type": { "const": "poshgit" }
}
},
"then": {
"title": "Posh-Git Segment",
"description": "https://ohmyposh.dev/docs/poshgit"
}
}
]
}