Skip to content

Commit 1451861

Browse files
anujsrcJanDeDobbeleer
authored andcommitted
feat(clojure): add segment
1 parent d959a3d commit 1451861

6 files changed

Lines changed: 195 additions & 1 deletion

File tree

src/config/segment_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func init() {
4242
gob.Register(&segments.Copilot{})
4343
gob.Register(&segments.Cf{})
4444
gob.Register(&segments.CfTarget{})
45+
gob.Register(&segments.Clojure{})
4546
gob.Register(&segments.Cmake{})
4647
gob.Register(&segments.Connection{})
4748
gob.Register(&segments.Crystal{})
@@ -185,6 +186,8 @@ const (
185186
CF SegmentType = "cf"
186187
// Cloud Foundry logged in target
187188
CFTARGET SegmentType = "cftarget"
189+
// CLOJURE writes the active clojure version
190+
CLOJURE SegmentType = "clojure"
188191
// CMAKE writes the active cmake version
189192
CMAKE SegmentType = "cmake"
190193
// CONNECTION writes a connection's information
@@ -380,6 +383,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
380383
CDS: func() SegmentWriter { return &segments.Cds{} },
381384
CF: func() SegmentWriter { return &segments.Cf{} },
382385
CFTARGET: func() SegmentWriter { return &segments.CfTarget{} },
386+
CLOJURE: func() SegmentWriter { return &segments.Clojure{} },
383387
CMAKE: func() SegmentWriter { return &segments.Cmake{} },
384388
CONNECTION: func() SegmentWriter { return &segments.Connection{} },
385389
COPILOT: func() SegmentWriter { return &segments.Copilot{} },

src/segments/clojure.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package segments
2+
3+
type Clojure struct {
4+
Language
5+
}
6+
7+
func (c *Clojure) Template() string {
8+
return languageTemplate
9+
}
10+
11+
func (c *Clojure) Enabled() bool {
12+
c.init()
13+
return c.Language.Enabled()
14+
}
15+
16+
func (c *Clojure) init() {
17+
options := c.options.StringArray(Tooling, []string{})
18+
if len(options) == 0 {
19+
c.defaultTooling = []string{"clojure", "lein"}
20+
}
21+
22+
c.extensions = []string{
23+
"project.clj",
24+
"deps.edn",
25+
"build.boot",
26+
"bb.edn",
27+
"*.clj",
28+
"*.cljc",
29+
"*.cljs",
30+
}
31+
32+
c.tooling = map[string]*cmd{
33+
"clojure": {
34+
executable: "clojure",
35+
args: []string{"--version"},
36+
regex: `Clojure CLI version (?P<version>(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)(?:\.(?P<build>[0-9]+))?)`,
37+
},
38+
"lein": {
39+
executable: "lein",
40+
args: []string{"--version"},
41+
regex: `Leiningen (?P<version>(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+))`,
42+
},
43+
}
44+
}

src/segments/clojure_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package segments
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestClojure(t *testing.T) {
11+
cases := []struct {
12+
Case string
13+
ExpectedString string
14+
Version string
15+
Cmd string
16+
}{
17+
{
18+
Case: "Clojure CLI 1.11.1.1113",
19+
ExpectedString: "1.11.1.1113",
20+
Version: "Clojure CLI version 1.11.1.1113",
21+
Cmd: "clojure",
22+
},
23+
{
24+
Case: "Leiningen 2.9.8",
25+
ExpectedString: "2.9.8",
26+
Version: "Leiningen 2.9.8 on Java 11.0.11 OpenJDK 64-Bit Server VM",
27+
Cmd: "lein",
28+
},
29+
}
30+
for _, tc := range cases {
31+
params := &mockedLanguageParams{
32+
cmd: tc.Cmd,
33+
versionParam: "--version",
34+
versionOutput: tc.Version,
35+
extension: "*.clj",
36+
}
37+
env, props := getMockedLanguageEnv(params)
38+
props[LanguageExtensions] = []string{params.extension}
39+
if tc.Cmd != "clojure" {
40+
env.On("HasCommand", "clojure").Return(false)
41+
}
42+
c := &Clojure{}
43+
c.Init(props, env)
44+
assert.True(t, c.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
45+
assert.Equal(t, tc.ExpectedString, renderTemplate(env, c.Template(), c), fmt.Sprintf("Failed in case: %s", tc.Case))
46+
}
47+
}

themes/schema.json

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@
359359
"cds",
360360
"cf",
361361
"cftarget",
362+
"clojure",
362363
"cmake",
363364
"copilot",
364365
"connection",
@@ -1120,6 +1121,41 @@
11201121
}
11211122
}
11221123
},
1124+
{
1125+
"if": {
1126+
"properties": {
1127+
"type": {
1128+
"const": "clojure"
1129+
}
1130+
}
1131+
},
1132+
"then": {
1133+
"title": "Clojure Segment",
1134+
"description": "https://ohmyposh.dev/docs/segments/languages/clojure",
1135+
"properties": {
1136+
"options": {
1137+
"allOf": [
1138+
{
1139+
"$ref": "#/definitions/language_options"
1140+
}
1141+
],
1142+
"properties": {
1143+
"extensions": {
1144+
"default": [
1145+
"project.clj",
1146+
"deps.edn",
1147+
"build.boot",
1148+
"bb.edn",
1149+
"*.clj",
1150+
"*.cljc",
1151+
"*.cljs"
1152+
]
1153+
}
1154+
}
1155+
}
1156+
}
1157+
}
1158+
},
11231159
{
11241160
"if": {
11251161
"properties": {
@@ -4634,4 +4670,4 @@
46344670
"default": ""
46354671
}
46364672
}
4637-
}
4673+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
id: clojure
3+
title: Clojure
4+
sidebar_label: Clojure
5+
---
6+
7+
## What
8+
9+
Display the currently active [Clojure](https://clojure.org/) version.
10+
11+
## Sample Configuration
12+
13+
import Config from "@site/src/components/Config.js";
14+
15+
<Config
16+
data={{
17+
type: "clojure",
18+
style: "powerline",
19+
powerline_symbol: "\uE0B0",
20+
foreground: "#ffffff",
21+
background: "#9068B0",
22+
template: " \uE768 {{ .Full }}",
23+
}}
24+
/>
25+
26+
## Options
27+
28+
| Name | Type | Default | Description |
29+
| ---------------------- | :--------: | :-------------------------------------------------------------------------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
30+
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
31+
| `fetch_version` | `boolean` | `true` | fetch the clojure version |
32+
| `cache_duration` | `string` | `none` | the duration for which the version will be cached. The duration is a string in the format `1h2m3s` and is parsed using the [time.ParseDuration] function from the Go standard library. To disable the cache, use `none` |
33+
| `missing_command_text` | `string` | | text to display when the command is missing |
34+
| `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> |
35+
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
36+
| `extensions` | `[]string` | `project.clj, deps.edn, build.boot, bb.edn, *.clj, *.cljc, *.cljs` | allows to override the default list of file extensions to validate |
37+
| `folders` | `[]string` | | allows to override the list of folder names to validate |
38+
| `tooling` | `[]string` | `clojure`, `lein` | the tooling to use for fetching the version |
39+
40+
## Template ([info][templates])
41+
42+
:::note default template
43+
44+
```template
45+
{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}
46+
```
47+
48+
:::
49+
50+
### Properties
51+
52+
| Name | Type | Description |
53+
| -------- | -------- | -------------------------------------------------- |
54+
| `.Full` | `string` | the full version |
55+
| `.Major` | `string` | major number |
56+
| `.Minor` | `string` | minor number |
57+
| `.Patch` | `string` | patch number |
58+
| `.Error` | `string` | error encountered when fetching the version string |
59+
60+
[go-text-template]: https://golang.org/pkg/text/template/
61+
[templates]: configuration/templates.mdx
62+
[time.ParseDuration]: https://golang.org/pkg/time/#ParseDuration

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ module.exports = {
126126
label: "✍️ Languages",
127127
collapsed: true,
128128
items: [
129+
"segments/languages/clojure",
129130
"segments/languages/crystal",
130131
"segments/languages/dart",
131132
"segments/languages/dotnet",

0 commit comments

Comments
 (0)