Skip to content

Commit

Permalink
feat: env var segment
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 9, 2020
1 parent 3193487 commit c49a8ee
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 3 deletions.
52 changes: 52 additions & 0 deletions 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
3 changes: 1 addition & 2 deletions docs/docs/segment-os.md
Expand Up @@ -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

1 change: 1 addition & 0 deletions docs/sidebars.js
Expand Up @@ -16,6 +16,7 @@ module.exports = {
items: [
"battery",
"command",
"environment",
"exit",
"git",
"node",
Expand Down
2 changes: 1 addition & 1 deletion packages/powershell/oh-my-posh/oh-my-posh.psd1
Expand Up @@ -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 = @{
Expand Down
3 changes: 3 additions & 0 deletions packages/powershell/oh-my-posh/oh-my-posh.psm1
Expand Up @@ -31,6 +31,8 @@ if ($IsWindows) {
& $poshCommand | Out-Null
}

function Set-PoshContext {}

function Set-PoshPrompt {
param(
[Parameter(Mandatory = $false)]
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions segment.go
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
27 changes: 27 additions & 0 deletions 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
}
42 changes: 42 additions & 0 deletions 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())
}

0 comments on commit c49a8ee

Please sign in to comment.