From 320ec1d7d32467dfa924f929cd1849fc3c1e4b77 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 27 Mar 2021 17:04:09 +0100 Subject: [PATCH] feat(pwsh): posh-git segment --- docs/docs/segment-posh-git.mdx | 27 +++++++++++++++++++++++++++ docs/sidebars.js | 1 + src/init/omp.ps1 | 5 +++-- src/segment.go | 3 +++ src/segment_posh_git.go | 28 ++++++++++++++++++++++++++++ src/segment_posh_git_test.go | 32 ++++++++++++++++++++++++++++++++ themes/schema.json | 14 +++++++++++++- 7 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 docs/docs/segment-posh-git.mdx create mode 100644 src/segment_posh_git.go create mode 100644 src/segment_posh_git_test.go diff --git a/docs/docs/segment-posh-git.mdx b/docs/docs/segment-posh-git.mdx new file mode 100644 index 00000000..dc5b5327 --- /dev/null +++ b/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 diff --git a/docs/sidebars.js b/docs/sidebars.js index 7c8d32bb..f46d74e1 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -25,6 +25,7 @@ module.exports = { "node", "os", "path", + "poshgit", "python", "root", "ruby", diff --git a/src/init/omp.ps1 b/src/init/omp.ps1 index 53ba1b77..647958ff 100644 --- a/src/init/omp.ps1 +++ b/src/init/omp.ps1 @@ -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 diff --git a/src/segment.go b/src/segment.go index 3e36b768..9d725779 100644 --- a/src/segment.go +++ b/src/segment.go @@ -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{ diff --git a/src/segment_posh_git.go b/src/segment_posh_git.go new file mode 100644 index 00000000..32ee1ef5 --- /dev/null +++ b/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 +} diff --git a/src/segment_posh_git_test.go b/src/segment_posh_git_test.go new file mode 100644 index 00000000..d8e3b3b7 --- /dev/null +++ b/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()) + } + } +} diff --git a/themes/schema.json b/themes/schema.json index 050d23a8..794745e2 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -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" + } } ] }