Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3193487
commit c49a8ee
Showing
8 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ module.exports = { | |
items: [ | ||
"battery", | ||
"command", | ||
"environment", | ||
"exit", | ||
"git", | ||
"node", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |