Skip to content

Commit

Permalink
feat(pwsh): posh-git segment
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Mar 27, 2021
1 parent 83246df commit 320ec1d
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 3 deletions.
27 changes: 27 additions & 0 deletions docs/docs/segment-posh-git.mdx
@@ -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
1 change: 1 addition & 0 deletions docs/sidebars.js
Expand Up @@ -25,6 +25,7 @@ module.exports = {
"node",
"os",
"path",
"poshgit",
"python",
"root",
"ruby",
Expand Down
5 changes: 3 additions & 2 deletions src/init/omp.ps1
Expand Up @@ -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
}
}

Expand All @@ -46,14 +47,14 @@ 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("\")
$cleanPSWD = $PWD.ToString().TrimEnd("\")
$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
Expand Down
3 changes: 3 additions & 0 deletions src/segment.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
28 changes: 28 additions & 0 deletions src/segment_posh_git.go
@@ -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 changes: 32 additions & 0 deletions src/segment_posh_git_test.go
@@ -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())
}
}
}
14 changes: 13 additions & 1 deletion themes/schema.json
Expand Up @@ -155,7 +155,8 @@
"ytm",
"executiontime",
"aws",
"java"
"java",
"poshgit"
]
},
"style": {
Expand Down Expand Up @@ -1419,6 +1420,17 @@
}
}
}
},
{
"if": {
"properties": {
"type": { "const": "poshgit" }
}
},
"then": {
"title": "Posh-Git Segment",
"description": "https://ohmyposh.dev/docs/poshgit"
}
}
]
}
Expand Down

0 comments on commit 320ec1d

Please sign in to comment.