diff --git a/docs/docs/segment-environment.md b/docs/docs/segment-environment.md new file mode 100644 index 00000000..1e3e4e38 --- /dev/null +++ b/docs/docs/segment-environment.md @@ -0,0 +1,52 @@ +--- +id: environment +title: Environment Variable +sidebar_label: Environment Variable +--- + +## What + +Show the content of an environment variable. +Can be used to visualize a local settings/context unavailable to Go my Posh otherwise. + +For example, in PowerShell, adding the below configuration to a block and extending the prompt +function to set an environment variable before the prompt, you can work a bit of magic. + +```powershell +[ScriptBlock]$Prompt = { + $realLASTEXITCODE = $global:LASTEXITCODE + $env:POSH = "hello from Powershell" + & "C:\tools\oh-my-posh.exe" -config "~/downloadedtheme.json" -error $realLASTEXITCODE -pwd $PWD + $global:LASTEXITCODE = $realLASTEXITCODE + Remove-Variable realLASTEXITCODE -Confirm:$false +} +``` + +If you're using the PowerShell module, you can override a function to achieve the same effect. +make sure to do this after importing `go-my-posh` and you're good to go. + +```powershell +function Set-EnvVar { + $env:POSH=$(Get-Date) +} +New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global +``` + +The segment will show when the value of the environment variable isn't empty. + +## Sample Configuration + +```json +{ + "type": "envvar", + "style": "powerline", + "powerline_symbol": "", + "foreground": "#ffffff", + "background": "#0077c2", + "properties": { + "var_name": "POSH" + } +} +``` + +- var_name: `string` - the name of the environment variable diff --git a/docs/docs/segment-os.md b/docs/docs/segment-os.md index 37696663..7ab711b9 100644 --- a/docs/docs/segment-os.md +++ b/docs/docs/segment-os.md @@ -25,7 +25,6 @@ Display OS specific info. Defaults to Icon. ## Properties -- macos: `string` - the string to use for macOS - defaults to macOS icon +- macos: `string` - the string to use for macOS - defaults to macOS icon - linux: `string` - the icon to use for Linux - defaults to Linux icon - windows: `string` - the icon to use for Windows - defaults to Windows icon - diff --git a/docs/sidebars.js b/docs/sidebars.js index 056b1124..0f195c7d 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -16,6 +16,7 @@ module.exports = { items: [ "battery", "command", + "environment", "exit", "git", "node", diff --git a/packages/powershell/oh-my-posh/oh-my-posh.psd1 b/packages/powershell/oh-my-posh/oh-my-posh.psd1 index b217b00d..a2403c95 100644 --- a/packages/powershell/oh-my-posh/oh-my-posh.psd1 +++ b/packages/powershell/oh-my-posh/oh-my-posh.psd1 @@ -31,7 +31,7 @@ # Aliases to export from this module AliasesToExport = '*' # Functions to export from this module - FunctionsToExport = @('Get-PoshThemes', 'Set-PoshPrompt', 'Write-PoshTheme') + FunctionsToExport = @('Get-PoshThemes', 'Set-PoshPrompt', 'Write-PoshTheme', 'Set-PoshContext') # Private data to pass to the module specified in RootModule. This may also contain a PSData hashtable with additional module metadata used by PowerShell. PrivateData = @{ PSData = @{ diff --git a/packages/powershell/oh-my-posh/oh-my-posh.psm1 b/packages/powershell/oh-my-posh/oh-my-posh.psm1 index 740d140e..28b78e8f 100644 --- a/packages/powershell/oh-my-posh/oh-my-posh.psm1 +++ b/packages/powershell/oh-my-posh/oh-my-posh.psm1 @@ -31,6 +31,8 @@ if ($IsWindows) { & $poshCommand | Out-Null } +function Set-PoshContext {} + function Set-PoshPrompt { param( [Parameter(Mandatory = $false)] @@ -52,6 +54,7 @@ function Set-PoshPrompt { $realLASTEXITCODE = $global:LASTEXITCODE $poshCommand = Get-PoshCommand $config = $global:PoshSettings.Theme + Set-PoshContext & $poshCommand -config $config -error $realLASTEXITCODE -pwd $PWD $global:LASTEXITCODE = $realLASTEXITCODE Remove-Variable realLASTEXITCODE -Confirm:$false diff --git a/segment.go b/segment.go index c837d53c..1ea916fe 100644 --- a/segment.go +++ b/segment.go @@ -58,6 +58,8 @@ const ( Node SegmentType = "node" //Os write os specific icon Os SegmentType = "os" + //EnvVar writes the content of an environment variable + EnvVar SegmentType = "envvar" //Powerline writes it Powerline style Powerline SegmentStyle = "powerline" //Plain writes it without ornaments @@ -110,6 +112,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) (*properties, ShellInfo: &shell{}, Node: &node{}, Os: &osInfo{}, + EnvVar: &envvar{}, } if writer, ok := functions[segment.Type]; ok { props := &properties{ diff --git a/segment_envar.go b/segment_envar.go new file mode 100644 index 00000000..03fe3248 --- /dev/null +++ b/segment_envar.go @@ -0,0 +1,27 @@ +package main + +type envvar struct { + props *properties + env environmentInfo + content string +} + +const ( + //VarName name of the variable + VarName Property = "var_name" +) + +func (e *envvar) enabled() bool { + name := e.props.getString(VarName, "") + e.content = e.env.getenv(name) + return e.content != "" +} + +func (e *envvar) string() string { + return e.content +} + +func (e *envvar) init(props *properties, env environmentInfo) { + e.props = props + e.env = env +} diff --git a/segment_envar_test.go b/segment_envar_test.go new file mode 100644 index 00000000..99c2bf64 --- /dev/null +++ b/segment_envar_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEnvvarAvailable(t *testing.T) { + name := "HERP" + expected := "derp" + env := new(MockedEnvironment) + env.On("getenv", name).Return(expected) + props := &properties{ + values: map[Property]interface{}{ + VarName: name, + }, + } + e := &envvar{ + env: env, + props: props, + } + assert.True(t, e.enabled()) + assert.Equal(t, expected, e.string()) +} + +func TestEnvvarNotAvailable(t *testing.T) { + name := "HERP" + expected := "" + env := new(MockedEnvironment) + env.On("getenv", name).Return(expected) + props := &properties{ + values: map[Property]interface{}{ + VarName: name, + }, + } + e := &envvar{ + env: env, + props: props, + } + assert.False(t, e.enabled()) +}