Skip to content

Commit

Permalink
feat(segment): add bun
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxtacy authored and JanDeDobbeleer committed Jun 10, 2024
1 parent 150605a commit 27b193b
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/engine/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const (
BREWFATHER SegmentType = "brewfather"
// Buf segment writes the active buf version
BUF SegmentType = "buf"
// BUN writes the active bun version
BUN SegmentType = "bun"
// CARBONINTENSITY writes the actual and forecast carbon intensity in gCO2/kWh
CARBONINTENSITY SegmentType = "carbonintensity"
// cds (SAP CAP) version
Expand Down Expand Up @@ -281,6 +283,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
BAZEL: func() SegmentWriter { return &segments.Bazel{} },
BREWFATHER: func() SegmentWriter { return &segments.Brewfather{} },
BUF: func() SegmentWriter { return &segments.Buf{} },
BUN: func() SegmentWriter { return &segments.Bun{} },
CARBONINTENSITY: func() SegmentWriter { return &segments.CarbonIntensity{} },
CDS: func() SegmentWriter { return &segments.Cds{} },
CF: func() SegmentWriter { return &segments.Cf{} },
Expand Down
34 changes: 34 additions & 0 deletions src/segments/bun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package segments

import (
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)

type Bun struct {
language
}

func (b *Bun) Template() string {
return languageTemplate
}

func (b *Bun) Init(props properties.Properties, env platform.Environment) {
b.language = language{
env: env,
props: props,
extensions: []string{"bun.lockb"},
commands: []*cmd{
{
executable: "bun",
args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
versionURLTemplate: "https://github.com/oven-sh/bun/releases/tag/bun-v{{.Full}}",
}
}

func (b *Bun) Enabled() bool {
return b.language.Enabled()
}
31 changes: 31 additions & 0 deletions src/segments/bun_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package segments

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBun(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Version string
}{
{Case: "Bun 1.1.8", ExpectedString: "1.1.8", Version: "1.1.8"},
}
for _, tc := range cases {
params := &mockedLanguageParams{
cmd: "bun",
versionParam: "--version",
versionOutput: tc.Version,
extension: "bun.lockb",
}
env, props := getMockedLanguageEnv(params)
b := &Bun{}
b.Init(props, env)
assert.True(t, b.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, renderTemplate(env, b.Template(), b), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}
52 changes: 52 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
"bazel",
"brewfather",
"buf",
"bun",
"carbonintensity",
"command",
"connection",
Expand Down Expand Up @@ -824,6 +825,57 @@
}
}
},
{
"if": {
"properties": {
"type": {
"const": "bun"
}
}
},
"then": {
"title": "Bun CLI Segment",
"description": "https://ohmyposh.dev/docs/segments/bun",
"properties": {
"properties": {
"properties": {
"home_enabled": {
"$ref": "#/definitions/home_enabled"
},
"fetch_version": {
"$ref": "#/definitions/fetch_version"
},
"missing_command_text": {
"$ref": "#/definitions/missing_command_text"
},
"display_mode": {
"$ref": "#/definitions/display_mode"
},
"version_url_template": {
"$ref": "#/definitions/version_url_template"
},
"cache_version": {
"$ref": "#/definitions/cache_version"
},
"extensions": {
"type": "array",
"title": "Extensions",
"description": "The extensions to look for when determining if a folder is a Bun workspace",
"default": [
"bun.lockb"
],
"items": {
"type": "string"
}
},
"folders": {
"$ref": "#/definitions/folders"
}
}
}
}
}
},
{
"if": {
"properties": {
Expand Down
60 changes: 60 additions & 0 deletions website/docs/segments/bun.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
id: bun
title: Bun
sidebar_label: Bun
---

## What

Display the currently active [Bun CLI][bun-docs] version.

## Sample Configuration

import Config from "@site/src/components/Config.js";

<Config
data={{
type: "bun",
style: "plain",
foreground: "#3C82F6",
template: " 🥟 {{ .Full }} ",
}}
/>

## Properties

| Name | Type | Default | Description |
| ---------------------- | :--------: | :---------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
| `fetch_version` | `boolean` | `true` | fetch the active version or not; useful if all you need is an icon indicating `bun` |
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `extensions` | `[]string` | `bun.lockb` | allows to override the default list of file extensions to validate |
| `folders` | `[]string` | | allows to override the list of folder names to validate |
| `cache_version` | `boolean` | `false` | cache the executable's version or not |

## Template ([info][templates])

:::note default template

```template
{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}
```

:::

### Properties

| Name | Type | Description |
| -------- | -------- | -------------------------------------------------- |
| `.Full` | `string` | the full version |
| `.Major` | `string` | major number |
| `.Minor` | `string` | minor number |
| `.Patch` | `string` | patch number |
| `.URL` | `string` | URL of the version info / release notes |
| `.Error` | `string` | error encountered when fetching the version string |

[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates
[bun-docs]: https://bun.sh/
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module.exports = {
"segments/bazel",
"segments/brewfather",
"segments/buf",
"segments/bun",
"segments/carbonintensity",
"segments/cds",
"segments/cf",
Expand Down

0 comments on commit 27b193b

Please sign in to comment.