diff --git a/src/config/segment_types.go b/src/config/segment_types.go index 63df09bdc91c..7f1ede9c4ea0 100644 --- a/src/config/segment_types.go +++ b/src/config/segment_types.go @@ -73,6 +73,8 @@ const ( DART SegmentType = "dart" // DENO writes the active deno version DENO SegmentType = "deno" + // DISTROBOX writes the active distrobox container name + DISTROBOX SegmentType = "distrobox" // DOCKER writes the docker context DOCKER SegmentType = "docker" // DOTNET writes which dotnet version is currently active @@ -254,6 +256,7 @@ var Segments = map[SegmentType]func() SegmentWriter{ CRYSTAL: func() SegmentWriter { return &segments.Crystal{} }, DART: func() SegmentWriter { return &segments.Dart{} }, DENO: func() SegmentWriter { return &segments.Deno{} }, + DISTROBOX: func() SegmentWriter { return &segments.Distrobox{} }, DOCKER: func() SegmentWriter { return &segments.Docker{} }, DOTNET: func() SegmentWriter { return &segments.Dotnet{} }, ELIXIR: func() SegmentWriter { return &segments.Elixir{} }, diff --git a/src/segments/distrobox.go b/src/segments/distrobox.go new file mode 100644 index 000000000000..b911e92ccae1 --- /dev/null +++ b/src/segments/distrobox.go @@ -0,0 +1,25 @@ +package segments + +import "github.com/jandedobbeleer/oh-my-posh/src/properties" + +type Distrobox struct { + base + + ContainerId string + Icon string +} + +const ( + DistroboxIcon properties.Property = "icon" +) + +func (d *Distrobox) Enabled() bool { + d.ContainerId = d.env.Getenv("CONTAINER_ID") + d.Icon = d.props.GetString(DistroboxIcon, "\uED95 ") + + return d.ContainerId != "" +} + +func (d *Distrobox) Template() string { + return " {{ .Icon }} {{ .ContainerId }} " +} diff --git a/src/segments/distrobox_test.go b/src/segments/distrobox_test.go new file mode 100644 index 000000000000..82878af91f66 --- /dev/null +++ b/src/segments/distrobox_test.go @@ -0,0 +1,53 @@ +package segments + +import ( + "testing" + + "github.com/jandedobbeleer/oh-my-posh/src/properties" + "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" + "github.com/stretchr/testify/assert" +) + +func TestDistrobox(t *testing.T) { + cases := []struct { + Case string + ContainerId string + ExpectedEnabled bool + ExpectedString string + Template string + }{ + { + Case: "in distrobox container", + ContainerId: "fedora-toolbox", + ExpectedEnabled: true, + ExpectedString: "fedora-toolbox", + Template: "{{ .ContainerId }}", + }, + { + Case: "not in container", + ContainerId: "", + ExpectedEnabled: false, + }, + { + Case: "with icon template", + ContainerId: "ubuntu-dev", + ExpectedEnabled: true, + ExpectedString: "\uED95 ubuntu-dev", + Template: "{{ .Icon }} {{ .ContainerId }}", + }, + } + + for _, tc := range cases { + distrobox := &Distrobox{} + env := new(mock.Environment) + + env.On("Getenv", "CONTAINER_ID").Return(tc.ContainerId) + + distrobox.Init(properties.Map{}, env) + + assert.Equal(t, tc.ExpectedEnabled, distrobox.Enabled(), tc.Case) + if tc.ExpectedEnabled { + assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, distrobox), tc.Case) + } + } +} diff --git a/themes/schema.json b/themes/schema.json index 1318b394b056..97618d474406 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -331,6 +331,7 @@ "crystal", "dart", "deno", + "distrobox", "docker", "dotnet", "elixir", @@ -1071,6 +1072,31 @@ } } }, + { + "if": { + "properties": { + "type": { + "const": "distrobox" + }, + }, + }, + "then": { + "title": "Distrobox Segment", + "description": "https://ohmyposh.dev/docs/cli/distrobox", + "properties": { + "properties": { + "properties": { + "icon": { + "type": "string", + "title": "Container Icon", + "description": "The icon to display before the container name", + "default": "\uED95 " + }, + }, + }, + }, + }, + }, { "if": { "properties": { diff --git a/website/docs/segments/cli/distrobox.mdx b/website/docs/segments/cli/distrobox.mdx new file mode 100644 index 000000000000..ec211d41788d --- /dev/null +++ b/website/docs/segments/cli/distrobox.mdx @@ -0,0 +1,47 @@ +--- +id: distrobox +title: Distrobox +sidebar_label: Distrobox +--- + +## What + +Displays the [Distrobox] container name when inside a Distrobox container environment. + +## Sample Configuration + +import Config from "@site/src/components/Config.js"; + + + +## Template ([info][templates]) + +:::note default template + +```template + {{ .Icon }}{{ .ContainerId }} +``` + +::: + +### Properties + +| Name | Type | Description | +| -------------- | -------- | ---------------------------------------------------------- | +| `icon` | `string` | the icon to display - defaults to `\uED95 ` | +| `.ContainerId` | `string` | the name of the active distrobox container | + +[Distrobox]: https://github.com/89luca89/distrobox +[templates]: /docs/configuration/templates diff --git a/website/sidebars.js b/website/sidebars.js index d3326e30ea8b..8b7c3732be40 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -68,6 +68,7 @@ module.exports = { "segments/cli/bun", "segments/cli/cmake", "segments/cli/deno", + "segments/cli/distrobox", "segments/cli/docker", "segments/cli/firebase", "segments/cli/flutter",