mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
feat: add nix-shell segment
This commit is contained in:
parent
a6461d2d79
commit
dca6a52300
|
@ -27,7 +27,6 @@ const (
|
||||||
Accordion SegmentStyle = "accordion"
|
Accordion SegmentStyle = "accordion"
|
||||||
// Diamond writes the prompt shaped with a leading and trailing symbol
|
// Diamond writes the prompt shaped with a leading and trailing symbol
|
||||||
Diamond SegmentStyle = "diamond"
|
Diamond SegmentStyle = "diamond"
|
||||||
|
|
||||||
// ANGULAR writes which angular cli version us currently active
|
// ANGULAR writes which angular cli version us currently active
|
||||||
ANGULAR SegmentType = "angular"
|
ANGULAR SegmentType = "angular"
|
||||||
// ARGOCD writes the current argocd context
|
// ARGOCD writes the current argocd context
|
||||||
|
@ -128,6 +127,8 @@ const (
|
||||||
NPM SegmentType = "npm"
|
NPM SegmentType = "npm"
|
||||||
// NX writes which Nx version us currently active
|
// NX writes which Nx version us currently active
|
||||||
NX SegmentType = "nx"
|
NX SegmentType = "nx"
|
||||||
|
// NIXSHELL writes the active nix shell details
|
||||||
|
NIXSHELL SegmentType = "nix-shell"
|
||||||
// OCAML writes the active Ocaml version
|
// OCAML writes the active Ocaml version
|
||||||
OCAML SegmentType = "ocaml"
|
OCAML SegmentType = "ocaml"
|
||||||
// OS write os specific icon
|
// OS write os specific icon
|
||||||
|
@ -266,6 +267,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
|
||||||
NIGHTSCOUT: func() SegmentWriter { return &segments.Nightscout{} },
|
NIGHTSCOUT: func() SegmentWriter { return &segments.Nightscout{} },
|
||||||
NODE: func() SegmentWriter { return &segments.Node{} },
|
NODE: func() SegmentWriter { return &segments.Node{} },
|
||||||
NPM: func() SegmentWriter { return &segments.Npm{} },
|
NPM: func() SegmentWriter { return &segments.Npm{} },
|
||||||
|
NIXSHELL: func() SegmentWriter { return &segments.NixShell{} },
|
||||||
NX: func() SegmentWriter { return &segments.Nx{} },
|
NX: func() SegmentWriter { return &segments.Nx{} },
|
||||||
OCAML: func() SegmentWriter { return &segments.OCaml{} },
|
OCAML: func() SegmentWriter { return &segments.OCaml{} },
|
||||||
OS: func() SegmentWriter { return &segments.Os{} },
|
OS: func() SegmentWriter { return &segments.Os{} },
|
||||||
|
|
|
@ -54,7 +54,7 @@ const (
|
||||||
NBAScoreURL string = "https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json"
|
NBAScoreURL string = "https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json"
|
||||||
NBAScheduleURL string = "https://stats.nba.com/stats/internationalbroadcasterschedule?LeagueID=00&Season=%s&Date=%s&RegionID=1&EST=Y"
|
NBAScheduleURL string = "https://stats.nba.com/stats/internationalbroadcasterschedule?LeagueID=00&Season=%s&Date=%s&RegionID=1&EST=Y"
|
||||||
|
|
||||||
Unknown = "Unknown"
|
UNKNOWN = "unknown"
|
||||||
|
|
||||||
currentNBASeason = "2023"
|
currentNBASeason = "2023"
|
||||||
NBADateFormat = "02/01/2006"
|
NBADateFormat = "02/01/2006"
|
||||||
|
@ -92,7 +92,7 @@ func (gs GameStatus) String() string {
|
||||||
case NotFound:
|
case NotFound:
|
||||||
return "Not Found"
|
return "Not Found"
|
||||||
default:
|
default:
|
||||||
return Unknown
|
return UNKNOWN
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
65
src/segments/nixshell.go
Normal file
65
src/segments/nixshell.go
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package segments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NONE = "none"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NixShell struct {
|
||||||
|
props properties.Properties
|
||||||
|
env runtime.Environment
|
||||||
|
|
||||||
|
Type string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NixShell) Template() string {
|
||||||
|
return "via {{ .Type }}-shell"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NixShell) DetectType() string {
|
||||||
|
shellType := n.env.Getenv("IN_NIX_SHELL")
|
||||||
|
|
||||||
|
switch shellType {
|
||||||
|
case "pure", "impure":
|
||||||
|
return shellType
|
||||||
|
default:
|
||||||
|
if n.InNewNixShell() {
|
||||||
|
return UNKNOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
return NONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hack to detect if we're in a `nix shell` (in contrast to a `nix-shell`).
|
||||||
|
// A better way to do this will be enabled by https://github.com/NixOS/nix/issues/6677
|
||||||
|
// so we check if the PATH contains a nix store.
|
||||||
|
func (n *NixShell) InNewNixShell() bool {
|
||||||
|
paths := filepath.SplitList(n.env.Getenv("PATH"))
|
||||||
|
|
||||||
|
for _, p := range paths {
|
||||||
|
if strings.Contains(p, "/nix/store") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NixShell) Init(props properties.Properties, env runtime.Environment) {
|
||||||
|
n.props = props
|
||||||
|
n.env = env
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NixShell) Enabled() bool {
|
||||||
|
n.Type = n.DetectType()
|
||||||
|
|
||||||
|
return n.Type != NONE
|
||||||
|
}
|
73
src/segments/nixshell_test.go
Normal file
73
src/segments/nixshell_test.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package segments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alecthomas/assert"
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
nixPath = "/nix/store/zznw8fnzss1vaqfg5hmv3y79s3hkqczi-devshell-dir/bin"
|
||||||
|
defaultPath = "/users/xyz/testing"
|
||||||
|
fullNixPath = defaultPath + ":" + nixPath
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNixShellSegment(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
expectedString string
|
||||||
|
shellType string
|
||||||
|
enabled bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Pure Nix Shell",
|
||||||
|
expectedString: "via pure-shell",
|
||||||
|
shellType: "pure",
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Impure Nix Shell",
|
||||||
|
expectedString: "via impure-shell",
|
||||||
|
shellType: "impure",
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unknown Nix Shell",
|
||||||
|
expectedString: "via unknown-shell",
|
||||||
|
shellType: "unknown",
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No Nix Shell",
|
||||||
|
expectedString: "",
|
||||||
|
shellType: "",
|
||||||
|
enabled: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
env := new(mock.Environment)
|
||||||
|
env.On("Getenv", "IN_NIX_SHELL").Return(tc.shellType)
|
||||||
|
|
||||||
|
path := defaultPath
|
||||||
|
if tc.shellType != "" {
|
||||||
|
path = fullNixPath
|
||||||
|
}
|
||||||
|
|
||||||
|
env.On("Getenv", "PATH").Return(path)
|
||||||
|
|
||||||
|
n := NixShell{}
|
||||||
|
n.Init(properties.Map{}, env)
|
||||||
|
|
||||||
|
assert.Equal(t, tc.enabled, n.Enabled(), fmt.Sprintf("Failed in case: %s", tc.name))
|
||||||
|
|
||||||
|
if tc.enabled {
|
||||||
|
assert.Equal(t, tc.expectedString, renderTemplate(env, n.Template(), n), tc.name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -156,7 +156,7 @@ func (u *Umbraco) TryFindLegacyUmbraco(configPath string) bool {
|
||||||
u.Modern = false
|
u.Modern = false
|
||||||
|
|
||||||
if len(appSetting.Value) == 0 {
|
if len(appSetting.Value) == 0 {
|
||||||
u.Version = Unknown
|
u.Version = UNKNOWN
|
||||||
} else {
|
} else {
|
||||||
u.Version = appSetting.Value
|
u.Version = appSetting.Value
|
||||||
}
|
}
|
||||||
|
|
|
@ -351,6 +351,7 @@
|
||||||
"mvn",
|
"mvn",
|
||||||
"nbgv",
|
"nbgv",
|
||||||
"nightscout",
|
"nightscout",
|
||||||
|
"nix-shell",
|
||||||
"node",
|
"node",
|
||||||
"npm",
|
"npm",
|
||||||
"nx",
|
"nx",
|
||||||
|
@ -4610,20 +4611,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"then": {
|
"then": {
|
||||||
"title": "Display something umbraco",
|
"title": "Umbraco Segment",
|
||||||
"description": "https://ohmyposh.dev/docs/segments/umbraco",
|
"description": "https://ohmyposh.dev/docs/segments/umbraco"
|
||||||
"properties": {
|
|
||||||
"properties": {
|
|
||||||
"properties": {
|
|
||||||
"newprop": {
|
|
||||||
"type": "string",
|
|
||||||
"title": "New Property",
|
|
||||||
"description": "the default text to display",
|
|
||||||
"default": "Hello"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -4723,6 +4712,19 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"if": {
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"const": "nix-shell"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"then": {
|
||||||
|
"title": "Nix Shell",
|
||||||
|
"description": "https://ohmyposh.dev/docs/segments/cli/nix-shell"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
42
website/docs/segments/cli/nix-shell.mdx
Normal file
42
website/docs/segments/cli/nix-shell.mdx
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
---
|
||||||
|
id: nix-shell
|
||||||
|
title: Nix Shell
|
||||||
|
sidebar_label: Nix Shell
|
||||||
|
---
|
||||||
|
|
||||||
|
## What
|
||||||
|
|
||||||
|
Displays the [nix shell] status if inside a nix-shell environment.
|
||||||
|
|
||||||
|
## Sample Configuration
|
||||||
|
|
||||||
|
import Config from "@site/src/components/Config.js";
|
||||||
|
|
||||||
|
<Config
|
||||||
|
data={{
|
||||||
|
type: "nix-shell",
|
||||||
|
style: "powerline",
|
||||||
|
foreground: "blue",
|
||||||
|
background: "transparent",
|
||||||
|
template: "(nix-{{ .Type }})",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
## Template ([info][templates])
|
||||||
|
|
||||||
|
:::note default template
|
||||||
|
|
||||||
|
```template
|
||||||
|
via {{ .Type }}-shell"
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
|
||||||
|
### Properties
|
||||||
|
|
||||||
|
| Name | Type | Description |
|
||||||
|
| ------- | -------- | ----------------------------------------------------------- |
|
||||||
|
| `.Type` | `string` | the type of nix shell, can be `pure`, `impure` or `unknown` |
|
||||||
|
|
||||||
|
[nix shell]: https://nixos.org/guides/nix-pills/developing-with-nix-shell.html
|
||||||
|
[templates]: /docs/configuration/templates
|
|
@ -71,6 +71,7 @@ module.exports = {
|
||||||
"segments/cli/kubectl",
|
"segments/cli/kubectl",
|
||||||
"segments/cli/mvn",
|
"segments/cli/mvn",
|
||||||
"segments/cli/nbgv",
|
"segments/cli/nbgv",
|
||||||
|
"segments/cli/nix-shell",
|
||||||
"segments/cli/npm",
|
"segments/cli/npm",
|
||||||
"segments/cli/nx",
|
"segments/cli/nx",
|
||||||
"segments/cli/pnpm",
|
"segments/cli/pnpm",
|
||||||
|
|
Loading…
Reference in a new issue