mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-26 02:21:25 -08:00
feat: env var segment
This commit is contained in:
parent
3193487234
commit
c49a8ee443
52
docs/docs/segment-environment.md
Normal file
52
docs/docs/segment-environment.md
Normal file
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ module.exports = {
|
|||
items: [
|
||||
"battery",
|
||||
"command",
|
||||
"environment",
|
||||
"exit",
|
||||
"git",
|
||||
"node",
|
||||
|
|
|
@ -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 = @{
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
27
segment_envar.go
Normal file
27
segment_envar.go
Normal file
|
@ -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
|
||||
}
|
42
segment_envar_test.go
Normal file
42
segment_envar_test.go
Normal file
|
@ -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())
|
||||
}
|
Loading…
Reference in a new issue