Skip to content

Commit 1ba666f

Browse files
refactor: align ruby with language
1 parent c43cbac commit 1ba666f

File tree

2 files changed

+64
-55
lines changed

2 files changed

+64
-55
lines changed

src/segment_ruby.go

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,53 @@
11
package main
22

33
type ruby struct {
4-
props *properties
5-
env environmentInfo
6-
version string
4+
language *language
75
}
86

97
func (r *ruby) string() string {
10-
if r.props.getBool(DisplayVersion, true) {
11-
return r.version
8+
version := r.language.string()
9+
// asdf default non-set version
10+
if version == "______" {
11+
return ""
1212
}
13-
return ""
13+
return version
1414
}
1515

1616
func (r *ruby) init(props *properties, env environmentInfo) {
17-
r.props = props
18-
r.env = env
19-
}
20-
21-
func (r *ruby) enabled() bool {
22-
if !r.env.hasFiles("*.rb") && !r.env.hasFiles("Rakefile") && !r.env.hasFiles("Gemfile") {
23-
return false
24-
}
25-
if !r.props.getBool(DisplayVersion, true) {
26-
return true
17+
r.language = &language{
18+
env: env,
19+
props: props,
20+
extensions: []string{"*.rb", "Rakefile", "Gemfile"},
21+
commands: []*cmd{
22+
{
23+
executable: "rbenv",
24+
args: []string{"version-name"},
25+
regex: `(?P<version>.+)`,
26+
},
27+
{
28+
executable: "rvm-prompt",
29+
args: []string{"i", "v", "g"},
30+
regex: `(?P<version>.+)`,
31+
},
32+
{
33+
executable: "chruby",
34+
args: []string(nil),
35+
regex: `\* (?P<version>.+)\n`,
36+
},
37+
{
38+
executable: "asdf",
39+
args: []string{"current", "ruby"},
40+
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
41+
},
42+
{
43+
executable: "ruby",
44+
args: []string{"--version"},
45+
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
46+
},
47+
},
2748
}
28-
r.version = r.getVersion()
29-
return r.version != ""
3049
}
3150

32-
func (r *ruby) getVersion() string {
33-
options := []struct {
34-
Command string
35-
Args []string
36-
Regex string
37-
}{
38-
{Command: "rbenv", Args: []string{"version-name"}, Regex: `(?P<version>.+)`},
39-
{Command: "rvm-prompt", Args: []string{"i", "v", "g"}, Regex: `(?P<version>.+)`},
40-
{Command: "chruby", Args: []string(nil), Regex: `\* (?P<version>.+)\n`},
41-
{Command: "asdf", Args: []string{"current", "ruby"}, Regex: `ruby\s+(?P<version>[^\s_]+)\s+`},
42-
{Command: "ruby", Args: []string{"--version"}, Regex: `ruby\s+(?P<version>[^\s_]+)\s+`},
43-
}
44-
for _, option := range options {
45-
if !r.env.hasCommand(option.Command) {
46-
continue
47-
}
48-
version, _ := r.env.runCommand(option.Command, option.Args...)
49-
match := findNamedRegexMatch(option.Regex, version)
50-
if match["version"] == "" {
51-
continue
52-
}
53-
return match["version"]
54-
}
55-
return ""
51+
func (r *ruby) enabled() bool {
52+
return r.language.enabled()
5653
}

src/segment_ruby_test.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
func TestRuby(t *testing.T) {
1111
cases := []struct {
12+
Case string
1213
ExpectedString string
1314
ExpectedEnabled bool
1415
HasRbenv bool
@@ -22,13 +23,14 @@ func TestRuby(t *testing.T) {
2223
HasGemFile bool
2324
DisplayVersion bool
2425
}{
25-
{ExpectedString: "", ExpectedEnabled: false},
26-
{ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRubyFiles: true},
27-
{ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRakeFile: true},
28-
{ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasGemFile: true},
29-
{ExpectedString: "", ExpectedEnabled: false, DisplayVersion: true, HasGemFile: true},
30-
{ExpectedString: "", ExpectedEnabled: false, DisplayVersion: true},
26+
{Case: "No files", ExpectedString: "", ExpectedEnabled: false},
27+
{Case: "Ruby files", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRubyFiles: true},
28+
{Case: "Rakefile", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRakeFile: true},
29+
{Case: "Gemfile", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasGemFile: true},
30+
{Case: "Gemfile with version", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: true, HasGemFile: true},
31+
{Case: "No files with version", ExpectedString: "", ExpectedEnabled: false, DisplayVersion: true},
3132
{
33+
Case: "Version with chruby",
3234
ExpectedString: "ruby-2.6.3",
3335
ExpectedEnabled: true,
3436
DisplayVersion: true,
@@ -40,6 +42,7 @@ func TestRuby(t *testing.T) {
4042
rubinius-2.0.0-rc1`,
4143
},
4244
{
45+
Case: "Version with chruby line 2",
4346
ExpectedString: "ruby-1.9.3-p392",
4447
ExpectedEnabled: true,
4548
DisplayVersion: true,
@@ -51,6 +54,7 @@ func TestRuby(t *testing.T) {
5154
rubinius-2.0.0-rc1`,
5255
},
5356
{
57+
Case: "Version with asdf",
5458
ExpectedString: "2.6.3",
5559
ExpectedEnabled: true,
5660
DisplayVersion: true,
@@ -59,13 +63,23 @@ func TestRuby(t *testing.T) {
5963
Version: "ruby 2.6.3 /Users/jan/Projects/oh-my-posh3/.tool-versions",
6064
},
6165
{
66+
Case: "Version with asdf not set",
6267
ExpectedString: "",
63-
ExpectedEnabled: false,
68+
ExpectedEnabled: true,
6469
DisplayVersion: true,
6570
HasRubyFiles: true,
6671
HasAsdf: true,
6772
Version: "ruby ______ No version set. Run \"asdf <global|shell|local> ruby <version>\"",
6873
},
74+
{
75+
Case: "Version with ruby",
76+
ExpectedString: "2.6.3",
77+
ExpectedEnabled: true,
78+
DisplayVersion: true,
79+
HasRubyFiles: true,
80+
HasRuby: true,
81+
Version: "ruby 2.6.3 (2019-04-16 revision 67580) [universal.x86_64-darwin20]",
82+
},
6983
}
7084
for _, tc := range cases {
7185
env := new(MockedEnvironment)
@@ -87,11 +101,9 @@ func TestRuby(t *testing.T) {
87101
DisplayVersion: tc.DisplayVersion,
88102
},
89103
}
90-
ruby := &ruby{
91-
env: env,
92-
props: props,
93-
}
94-
assert.Equal(t, tc.ExpectedEnabled, ruby.enabled(), fmt.Sprintf("Failed in case: %+v", tc))
95-
assert.Equal(t, tc.ExpectedString, ruby.string(), fmt.Sprintf("Failed in case: %+v", tc))
104+
ruby := &ruby{}
105+
ruby.init(props, env)
106+
assert.Equal(t, tc.ExpectedEnabled, ruby.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
107+
assert.Equal(t, tc.ExpectedString, ruby.string(), fmt.Sprintf("Failed in case: %s", tc.Case))
96108
}
97109
}

0 commit comments

Comments
 (0)