mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 03:49:40 -08:00
feat(pwsh): posh-git segment
This commit is contained in:
parent
83246dffc4
commit
320ec1d7d3
27
docs/docs/segment-posh-git.mdx
Normal file
27
docs/docs/segment-posh-git.mdx
Normal 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
|
|
@ -25,6 +25,7 @@ module.exports = {
|
|||
"node",
|
||||
"os",
|
||||
"path",
|
||||
"poshgit",
|
||||
"python",
|
||||
"root",
|
||||
"ruby",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
28
src/segment_posh_git.go
Normal 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
|
||||
}
|
32
src/segment_posh_git_test.go
Normal file
32
src/segment_posh_git_test.go
Normal 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())
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue