Skip to content

Commit

Permalink
feat: rust segment
Browse files Browse the repository at this point in the history
  • Loading branch information
relativityhd committed Jul 4, 2021
1 parent aeacba7 commit c358d4f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 10 deletions.
33 changes: 33 additions & 0 deletions docs/docs/segment-rust.md
@@ -0,0 +1,33 @@
---
id: rust
title: Rust
sidebar_label: Rust
---

## What

Display the currently active rust version.

## Sample Configuration

```json
{
"type": "rust",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#193549",
"background": "#99908a",
"properties": {
"prefix": " \uE7a8 "
}
}
```

## Properties

- display_version: `boolean` - display the rust version (`rustc --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 `*.rs`, `Cargo.toml` or `Cargo.lock` files are present (default)
18 changes: 9 additions & 9 deletions docs/sidebars.js
Expand Up @@ -9,20 +9,15 @@ module.exports = {
{
type: "category",
label: "🚀 Installation",
items: [
"pwsh",
"windows",
"macos",
"linux",
],
items: ["pwsh", "windows", "macos", "linux"],
},
"configure",
"beta",
"themes",
"share",
"fonts",
"faq",
"contributors"
"contributors",
],
},
{
Expand Down Expand Up @@ -53,19 +48,24 @@ module.exports = {
"python",
"root",
"ruby",
"rust",
"session",
"shell",
"spotify",
"terraform",
"text",
"time",
"ytm",
]
],
},
{
type: "category",
label: "Contributing",
items: ["contributing_started", "contributing_segment", "contributing_git"],
items: [
"contributing_started",
"contributing_segment",
"contributing_git",
],
},
],
};
3 changes: 3 additions & 0 deletions src/segment.go
Expand Up @@ -119,6 +119,8 @@ const (
Dart SegmentType = "dart"
// Nbgv writes the nbgv version information
Nbgv SegmentType = "nbgv"
// Rust writes the cargo version information if cargo.toml is present
Rust SegmentType = "rust"
)

func (segment *Segment) string() string {
Expand Down Expand Up @@ -258,6 +260,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
Crystal: &crystal{},
Dart: &dart{},
Nbgv: &nbgv{},
Rust: &rust{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{
Expand Down
28 changes: 28 additions & 0 deletions src/segment_rust.go
@@ -0,0 +1,28 @@
package main

type rust struct {
language *language
}

func (r *rust) string() string {
return r.language.string()
}

func (r *rust) init(props *properties, env environmentInfo) {
r.language = &language{
env: env,
props: props,
extensions: []string{"*.rs", "Cargo.toml", "Cargo.lock"},
commands: []*cmd{
{
executable: "rustc",
args: []string{"--version"},
regex: `rustc (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
}
}

func (r *rust) enabled() bool {
return r.language.enabled()
}
31 changes: 31 additions & 0 deletions src/segment_rust_test.go
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"testing"

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

func TestRust(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Version string
}{
{Case: "Rust 1.53.0", ExpectedString: "1.53.0", Version: "rustc 1.53.0 (4369396ce 2021-04-27)"},
}
for _, tc := range cases {
params := &mockedLanguageParams{
cmd: "rustc",
versionParam: "--version",
versionOutput: tc.Version,
extension: "*.rs",
}
env, props := getMockedLanguageEnv(params)
r := &rust{}
r.init(props, env)
assert.True(t, r.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, r.string(), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}
29 changes: 28 additions & 1 deletion themes/schema.json
Expand Up @@ -154,7 +154,8 @@
"poshgit",
"azfunc",
"crystal",
"dart"
"dart",
"rust"
]
},
"style": {
Expand Down Expand Up @@ -828,6 +829,32 @@
}
}
},
{
"if": {
"properties": {
"type": { "const": "rust" }
}
},
"then": {
"title": "Rust Segment",
"description": "https://ohmyposh.dev/docs/rust",
"properties": {
"properties": {
"properties": {
"display_version": {
"$ref": "#/definitions/display_version"
},
"display_mode": {
"$ref": "#/definitions/display_mode"
},
"missing_command_text": {
"$ref": "#/definitions/missing_command_text"
}
}
}
}
}
},
{
"if": {
"properties": {
Expand Down

0 comments on commit c358d4f

Please sign in to comment.