feat(swift): add swift segment

This commit is contained in:
jedwillick 2022-03-03 11:38:09 +10:00 committed by Jan De Dobbeleer
parent db52fcd198
commit 9cb0775b25
6 changed files with 158 additions and 1 deletions

View file

@ -0,0 +1,57 @@
---
id: swift
title: Swift
sidebar_label: Swift
---
## What
Display the currently active [Swift][swift] version.
## Sample Configuration
```json
{
"type": "swift",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#ffffff",
"background": "#f6553c",
"properties": {
"template": " \ue755 {{ .Full }} "
}
}
```
## Properties
- home_enabled: `boolean` - display the segment in the HOME folder or not - defaults to `false`
- fetch_version: `boolean` - display the swift version - defaults to `true`
- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true`
- missing_command_text: `string` - text to display when the command is missing - defaults to empty
- display_mode: `string` - determines when the segment is displayed
- `always`: the segment is always displayed
- `files`: the segment is only displayed when `*.swift` or `*.SWIFT` files are present (default)
## Template ([info][templates])
:::note default template
``` template
{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}
```
:::
### Properties
- `.Full`: `string` - the full version
- `.Major`: `string` - major number
- `.Minor`: `string` - minor number
- `.Patch`: `string` - patch number
- `.Prerelease`: `string` - prerelease info text
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-templates
[swift]: https://www.swift.org/

View file

@ -77,6 +77,7 @@ module.exports = {
"shell",
"spotify",
"strava",
"swift",
"sysinfo",
"terraform",
"text",

View file

@ -160,6 +160,8 @@ const (
CFTARGET SegmentType = "cftarget"
// KOTLIN writes the active kotlin version
KOTLIN SegmentType = "kotlin"
// SWIFT writes the active swift version
SWIFT SegmentType = "swift"
)
func (segment *Segment) shouldIncludeFolder() bool {
@ -287,6 +289,7 @@ func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error
CF: &segments.Cf{},
CFTARGET: &segments.CfTarget{},
KOTLIN: &segments.Kotlin{},
SWIFT: &segments.Swift{},
}
if segment.Properties == nil {
segment.Properties = make(properties.Map)

34
src/segments/swift.go Normal file
View file

@ -0,0 +1,34 @@
package segments
import (
"oh-my-posh/environment"
"oh-my-posh/properties"
)
type Swift struct {
language
}
func (s *Swift) Template() string {
return languageTemplate
}
func (s *Swift) Init(props properties.Properties, env environment.Environment) {
s.language = language{
env: env,
props: props,
extensions: []string{"*.swift", "*.SWIFT"},
commands: []*cmd{
{
executable: "swift",
args: []string{"--version"},
regex: `Swift version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)((.|-)(?P<patch>[0-9]+|dev))?))`,
},
},
versionURLTemplate: "https://github.com/apple/swift/releases/tag/swift-{{ .Full }}-RELEASE",
}
}
func (s *Swift) Enabled() bool {
return s.language.Enabled()
}

View file

@ -0,0 +1,35 @@
package segments
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSwift(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Version string
}{
{Case: "Swift 5.5.3", ExpectedString: "5.5.3", Version: "Swift version 5.5.3 (swift-5.5.3-RELEASE)"},
{Case: "Swift 5.5.3 on Windows", ExpectedString: "5.5.3", Version: "compnerd.org Swift version 5.5.3 (swift-5.5.3-RELEASE)"},
{Case: "Swift 5.5.3 on Mac", ExpectedString: "5.5.3", Version: "Apple Swift version 5.5.3 (swift-5.5.3-RELEASE)"},
{Case: "Swift 5.5", ExpectedString: "5.5", Version: "Swift version 5.5 (swift-5.5-RELEASE)"},
{Case: "Swift 5.6-dev", ExpectedString: "5.6-dev", Version: "Swift version 5.6-dev (LLVM 62b900d3d0d5be9, Swift ce64fe8867792d4)"},
}
for _, tc := range cases {
params := &mockedLanguageParams{
cmd: "swift",
versionParam: "--version",
versionOutput: tc.Version,
extension: "*.swift",
}
env, props := getMockedLanguageEnv(params)
s := &Swift{}
s.Init(props, env)
assert.True(t, s.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, renderTemplate(env, s.Template(), s), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}

View file

@ -197,7 +197,8 @@
"ipify",
"haskell",
"ui5tooling",
"kotlin"
"kotlin",
"swift"
]
},
"style": {
@ -2010,6 +2011,32 @@
}
}
}
},
{
"if": {
"properties": {
"type": { "const": "swift" }
}
},
"then": {
"title": "Swift Segment",
"description": "https://ohmyposh.dev/docs/swift",
"properties": {
"properties": {
"properties": {
"fetch_version": {
"$ref": "#/definitions/fetch_version"
},
"display_mode": {
"$ref": "#/definitions/display_mode"
},
"missing_command_text": {
"$ref": "#/definitions/missing_command_text"
}
}
}
}
}
}
]
}