mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 11:59:40 -08:00
feat: deprecate envvar segment
favors text in a more generic way
This commit is contained in:
parent
ce04362bb4
commit
3c40c04d36
|
@ -1,61 +0,0 @@
|
|||
---
|
||||
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 -Force
|
||||
```
|
||||
|
||||
The segment will show when the value of the environment variable isn't empty.
|
||||
|
||||
## Sample *Configuration*
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "envvar",
|
||||
"style": "powerline",
|
||||
"powerline_symbol": "\uE0B0",
|
||||
"foreground": "#ffffff",
|
||||
"background": "#0077c2",
|
||||
"properties": {
|
||||
"var_name": "POSH"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- var_name: `string` - the name of the environment variable
|
||||
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
|
||||
properties below - defaults to the value of the environment variable.
|
||||
|
||||
## Template Properties
|
||||
|
||||
- `.Value`: `string` - the value of the environment variable
|
||||
|
||||
[go-text-template]: https://golang.org/pkg/text/template/
|
||||
[sprig]: https://masterminds.github.io/sprig/
|
|
@ -22,11 +22,23 @@ Display text.
|
|||
}
|
||||
```
|
||||
|
||||
:::tip
|
||||
If you're using PowerShell, you can override a function to populate environment variables before the
|
||||
prompt is rendered.
|
||||
|
||||
```powershell
|
||||
function Set-EnvVar {
|
||||
$env:POSH=$(Get-Date)
|
||||
}
|
||||
New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
## Properties
|
||||
|
||||
- text: `string` - text/icon to display. Accepts [coloring foreground][coloring] just like `prefix` and `postfix`.
|
||||
Powered by go [text/template][go-text-template] templates extended with [sprig][sprig] utilizing the
|
||||
properties below.
|
||||
- template: `string` - text/icon to display. Powered by go [text/template][go-text-template] templates extended
|
||||
with [sprig][sprig] utilizing the properties below.
|
||||
|
||||
## Template Properties
|
||||
|
||||
|
|
|
@ -46,7 +46,6 @@ module.exports = {
|
|||
"crystal",
|
||||
"dart",
|
||||
"dotnet",
|
||||
"environment",
|
||||
"executiontime",
|
||||
"exit",
|
||||
"git",
|
||||
|
|
|
@ -25,6 +25,14 @@ func (p properties) getOneOfBool(property, legacyProperty Property, defaultValue
|
|||
return p.getBool(property, defaultValue)
|
||||
}
|
||||
|
||||
func (p properties) getOneOfString(property, legacyProperty Property, defaultValue string) string {
|
||||
_, found := p[legacyProperty]
|
||||
if found {
|
||||
return p.getString(legacyProperty, defaultValue)
|
||||
}
|
||||
return p.getString(property, defaultValue)
|
||||
}
|
||||
|
||||
func (p properties) hasOneOf(properties ...Property) bool {
|
||||
for _, property := range properties {
|
||||
if _, found := p[property]; found {
|
||||
|
@ -405,3 +413,44 @@ const (
|
|||
// DisplayPackageManager shows if NPM or Yarn is used
|
||||
DisplayPackageManager Property = "display_package_manager"
|
||||
)
|
||||
|
||||
// Environment Variable
|
||||
|
||||
type envvar struct {
|
||||
props properties
|
||||
env environmentInfo
|
||||
Value string
|
||||
}
|
||||
|
||||
const (
|
||||
// VarName name of the variable
|
||||
VarName Property = "var_name"
|
||||
)
|
||||
|
||||
func (e *envvar) enabled() bool {
|
||||
name := e.props.getString(VarName, "")
|
||||
e.Value = e.env.getenv(name)
|
||||
return e.Value != ""
|
||||
}
|
||||
|
||||
func (e *envvar) string() string {
|
||||
segmentTemplate := e.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return e.Value
|
||||
}
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: e,
|
||||
Env: e.env,
|
||||
}
|
||||
text, err := template.render()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (e *envvar) init(props properties, env environmentInfo) {
|
||||
e.props = props
|
||||
e.env = env
|
||||
}
|
||||
|
|
|
@ -762,3 +762,34 @@ func TestPythonVirtualEnv(t *testing.T) {
|
|||
assert.Equal(t, tc.Expected, python.string(), tc.Case)
|
||||
}
|
||||
}
|
||||
|
||||
// Environment Variable
|
||||
|
||||
func TestEnvvarAvailable(t *testing.T) {
|
||||
name := "HERP"
|
||||
expected := "derp"
|
||||
env := new(MockedEnvironment)
|
||||
env.On("getenv", name).Return(expected)
|
||||
e := &envvar{
|
||||
env: env,
|
||||
props: map[Property]interface{}{
|
||||
VarName: name,
|
||||
},
|
||||
}
|
||||
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)
|
||||
e := &envvar{
|
||||
env: env,
|
||||
props: map[Property]interface{}{
|
||||
VarName: name,
|
||||
},
|
||||
}
|
||||
assert.False(t, e.enabled())
|
||||
}
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
package main
|
||||
|
||||
type envvar struct {
|
||||
props properties
|
||||
env environmentInfo
|
||||
Value string
|
||||
}
|
||||
|
||||
const (
|
||||
// VarName name of the variable
|
||||
VarName Property = "var_name"
|
||||
)
|
||||
|
||||
func (e *envvar) enabled() bool {
|
||||
name := e.props.getString(VarName, "")
|
||||
e.Value = e.env.getenv(name)
|
||||
return e.Value != ""
|
||||
}
|
||||
|
||||
func (e *envvar) string() string {
|
||||
segmentTemplate := e.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return e.Value
|
||||
}
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: e,
|
||||
Env: e.env,
|
||||
}
|
||||
text, err := template.render()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (e *envvar) init(props properties, env environmentInfo) {
|
||||
e.props = props
|
||||
e.env = env
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
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)
|
||||
e := &envvar{
|
||||
env: env,
|
||||
props: map[Property]interface{}{
|
||||
VarName: name,
|
||||
},
|
||||
}
|
||||
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)
|
||||
e := &envvar{
|
||||
env: env,
|
||||
props: map[Property]interface{}{
|
||||
VarName: name,
|
||||
},
|
||||
}
|
||||
assert.False(t, e.enabled())
|
||||
}
|
|
@ -12,7 +12,7 @@ const (
|
|||
)
|
||||
|
||||
func (t *text) enabled() bool {
|
||||
textProperty := t.props.getString(TextProperty, "!!text property not defined!!")
|
||||
textProperty := t.props.getOneOfString(TextProperty, SegmentTemplate, "!!text property not defined!!")
|
||||
template := &textTemplate{
|
||||
Template: textProperty,
|
||||
Context: t,
|
||||
|
|
|
@ -71,15 +71,14 @@
|
|||
"type": "rprompt",
|
||||
"segments": [
|
||||
{
|
||||
"type": "envvar",
|
||||
"type": "text",
|
||||
"style": "diamond",
|
||||
"leading_diamond": "\uE0B2",
|
||||
"trailing_diamond": "\uE0B4",
|
||||
"foreground": "#ffffff",
|
||||
"background": "#1f4b8e",
|
||||
"properties": {
|
||||
"var_name": "DAYS",
|
||||
"template": "\uf1bb {{ .Value }} days till Christmas"
|
||||
"template": "\uf1bb {{ .Env.DAYS }} days till Christmas"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -160,7 +160,6 @@
|
|||
"shell",
|
||||
"node",
|
||||
"os",
|
||||
"envvar",
|
||||
"az",
|
||||
"kubectl",
|
||||
"dotnet",
|
||||
|
@ -422,32 +421,6 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
"type": { "const": "envvar" }
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"title": "Environment Variable Segment",
|
||||
"description": "https://ohmyposh.dev/docs/environment",
|
||||
"properties": {
|
||||
"properties": {
|
||||
"properties": {
|
||||
"template": {
|
||||
"$ref": "#/definitions/template"
|
||||
},
|
||||
"var_name": {
|
||||
"type": "string",
|
||||
"title": "Variable Name",
|
||||
"description": "The name of the environment variable",
|
||||
"default": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
|
|
Loading…
Reference in a new issue