Skip to content

Commit

Permalink
feat(segments): add Distrobox segment
Browse files Browse the repository at this point in the history
Introduce a new segment to display the active Distrobox container name.
  • Loading branch information
BastianAsmussen committed Dec 8, 2024
1 parent 2ddb3b1 commit c8f484e
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/config/segment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{} },
Expand Down
25 changes: 25 additions & 0 deletions src/segments/distrobox.go
Original file line number Diff line number Diff line change
@@ -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 }} "
}
53 changes: 53 additions & 0 deletions src/segments/distrobox_test.go
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 14 in src/segments/distrobox_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

var-naming: struct field ContainerId should be ContainerID (revive)
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)
}
}
}
26 changes: 26 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
"crystal",
"dart",
"deno",
"distrobox",
"docker",
"dotnet",
"elixir",
Expand Down Expand Up @@ -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": {
Expand Down
47 changes: 47 additions & 0 deletions website/docs/segments/cli/distrobox.mdx
Original file line number Diff line number Diff line change
@@ -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";

<Config
data={{
type: "distrobox",
style: "powerline",
powerline_symbol: "\uE0B0",
foreground: "#ffffff",
background: "#f07b3c",
template: "{{ .Icon }}{{ .ContainerId }}",
properties: {
icon: "\uED95 "
}
}}
/>

## 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
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c8f484e

Please sign in to comment.