Skip to content

Commit

Permalink
Make versions section load everything concurrently (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion authored May 20, 2023
1 parent e9018ed commit 6e0f4e2
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 116 deletions.
13 changes: 1 addition & 12 deletions cmd/gokart.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,9 @@ func ps1() {
fmt.Print("\n")
fmt.Print(ansi.Bold())

// Path and project info
fmt.Print(internal.Path())
fmt.Print(git.Git())

// Languages
fmt.Print(versions.GoVersion())
fmt.Print(versions.JavaVersion())
fmt.Print(versions.NodeVersion())
fmt.Print(versions.PhpVersion())
fmt.Print(versions.RubyVersion())
fmt.Print(versions.RustVersion())

// Tools
fmt.Print(versions.DockerVersion())
fmt.Print(versions.All())

fmt.Print(ansi.Reset())
}
Expand Down
37 changes: 37 additions & 0 deletions internal/versions/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package versions

import (
"strings"
"sync"
)

var sections = []section{
// Languages
Go,
Java,
Node,
Php,
Ruby,
Rust,
// Tools
Docker,
}

func All() string {
var wg sync.WaitGroup
result := make([]string, len(sections))

for i, s := range sections {
wg.Add(1)

i := i
s := s
go func() {
result[i] = s.version()
wg.Done()
}()
}

wg.Wait()
return strings.Join(result, " ")
}
29 changes: 13 additions & 16 deletions internal/versions/docker.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"strings"
)

var dockerFiles = []string{
"Dockerfile",
"docker-compose.yml",
"docker-compose.yaml",
}
var Docker = section{
symbol: "🐳",
color: ansi.Blue,

func DockerVersion() string {
if _, ok := internal.UpsearchWd(dockerFiles); !ok {
return ""
}
upsearchFiles: []string{
"Dockerfile",
"docker-compose.yml",
"docker-compose.yaml",
},

if version, ok := internal.Command("docker", "-v"); ok {
command: []string{"docker", "-v"},
versionFunc: func(output string) string {
/*
Example:
Docker version 23.0.1, build a5ee5b1dfc
*/
version, _, _ = strings.Cut(version[15:], ",")
return ansi.Color(ansi.Blue, " 🐳 v"+version)
}

return ""
version, _, _ := strings.Cut(output[15:], ",")
return "v" + version
},
}
24 changes: 10 additions & 14 deletions internal/versions/golang.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"strings"
)

var goFiles = []string{
"go.mod",
}
var Go = section{
symbol: "🐹",
color: ansi.Cyan,

func GoVersion() string {
if _, ok := internal.UpsearchWd(goFiles); !ok {
return ""
}
upsearchFiles: []string{
"go.mod",
},

if version, ok := internal.Command("go", "version"); ok {
command: []string{"go", "version"},
versionFunc: func(output string) string {
/*
Example:
go version go1.20.2 linux/amd64
*/
version := strings.SplitN(version, " ", 4)[2][2:]
return ansi.Color(ansi.Cyan, " 🐹 v"+version)
}

return ""
return "v" + strings.SplitN(output, " ", 4)[2][2:]
},
}
30 changes: 13 additions & 17 deletions internal/versions/java.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"strings"
)

var javaFiles = []string{
"pom.xml",
"build.gradle",
"settings.gradle",
"build.xml",
}
var Java = section{
symbol: "☕",
color: ansi.Cyan,

func JavaVersion() string {
if _, ok := internal.UpsearchWd(javaFiles); !ok {
return ""
}
upsearchFiles: []string{
"pom.xml",
"build.gradle",
"settings.gradle",
"build.xml",
},

if version, ok := internal.Command("java", "--version"); ok {
command: []string{"java", "--version"},
versionFunc: func(output string) string {
/*
Example:
openjdk 19.0.2 2023-01-17
OpenJDK Runtime Environment (build 19.0.2+7)
OpenJDK 64-Bit Server VM (build 19.0.2+7, mixed mode)
*/
version := strings.SplitN(version, " ", 3)[1]
return ansi.Color(ansi.Cyan, " ☕ v"+version)
}

return ""
return "v" + strings.SplitN(output, " ", 3)[1]
},
}
25 changes: 11 additions & 14 deletions internal/versions/node.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
)

var nodeFiles = []string{
"package.json",
"node_modules",
}
var Node = section{
symbol: "⬢",
color: ansi.Green,

func NodeVersion() string {
if _, ok := internal.UpsearchWd(nodeFiles); !ok {
return ""
}
upsearchFiles: []string{
"package.json",
"node_modules",
},

if version, ok := internal.Command("node", "-v"); ok {
command: []string{"node", "-v"},
versionFunc: func(output string) string {
/*
Example:
v19.1.0
*/
return ansi.Color(ansi.Green, " ⬢ "+version)
}

return ""
return output
},
}
24 changes: 10 additions & 14 deletions internal/versions/php.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"strings"
)

var phpFiles = []string{
"composer.json",
}
var Php = section{
symbol: "🐘",
color: ansi.Magenta,

func PhpVersion() string {
if _, ok := internal.UpsearchWd(phpFiles); !ok {
return ""
}
upsearchFiles: []string{
"composer.json",
},

if version, ok := internal.Command("php", "-v"); ok {
command: []string{"php", "-v"},
versionFunc: func(output string) string {
/*
Example:
PHP 8.2.4 (cli) (built: Mar 15 2023 15:27:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
*/
version := strings.SplitN(version, " ", 3)[1]
return ansi.Color(ansi.Magenta, " 🐘 v"+version)
}

return ""
return "v" + strings.SplitN(output, " ", 3)[1]
},
}
26 changes: 11 additions & 15 deletions internal/versions/ruby.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"strings"
)

var rubyFiles = []string{
"Gemfile",
"Rakefile",
}
var Ruby = section{
symbol: "💎",
color: ansi.Red,

func RubyVersion() string {
if _, ok := internal.UpsearchWd(rubyFiles); !ok {
return ""
}
upsearchFiles: []string{
"Gemfile",
"Rakefile",
},

if version, ok := internal.Command("ruby", "-v"); ok {
command: []string{"ruby", "-v"},
versionFunc: func(output string) string {
/*
Example:
ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [x86_64-linux]
*/
version = strings.SplitN(version, " ", 3)[1]
return ansi.Color(ansi.Red, " 💎 v"+version)
}

return ""
return "v" + strings.SplitN(output, " ", 3)[1]
},
}
24 changes: 10 additions & 14 deletions internal/versions/rust.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"strings"
)

var rustFiles = []string{
"Cargo.toml",
}
var Rust = section{
symbol: "🦀",
color: ansi.Red,

func RustVersion() string {
if _, ok := internal.UpsearchWd(rustFiles); !ok {
return ""
}
upsearchFiles: []string{
"Cargo.toml",
},

if version, ok := internal.Command("rustc", "--version"); ok {
command: []string{"rustc", "--version"},
versionFunc: func(output string) string {
/*
Example:
rustc 1.69.0 (84c898d65 2023-04-16) (Arch Linux rust 1:1.69.0-2)
*/
version = strings.SplitN(version, " ", 3)[1]
return ansi.Color(ansi.Red, " 🦀 v"+version)
}

return ""
return "v" + strings.SplitN(output, " ", 3)[1]
},
}
26 changes: 26 additions & 0 deletions internal/versions/section.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package versions

import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
)

type section struct {
symbol string
color ansi.ColorCode
upsearchFiles []string
command []string
versionFunc func(output string) string
}

func (s section) version() string {
if _, ok := internal.UpsearchWd(s.upsearchFiles); !ok {
return ""
}

if output, ok := internal.Command(s.command[0], s.command[1:]...); ok {
return ansi.Color(s.color, " "+s.symbol+" "+s.versionFunc(output))
}

return ""
}

0 comments on commit 6e0f4e2

Please sign in to comment.