Skip to content

Commit

Permalink
Improve versions checking by looking at workdir files (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion authored May 22, 2023
1 parent 6e0f4e2 commit e24861c
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 15 deletions.
6 changes: 3 additions & 3 deletions cmd/gokart.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func main() {
}

func ps1() {
wdFiles := internal.ListWdFiles()

fmt.Print("\n")
fmt.Print(ansi.Bold())

fmt.Print(internal.Path())
fmt.Print(git.Git())
fmt.Print(versions.All())

fmt.Print(versions.All(wdFiles))
fmt.Print(ansi.Reset())
}

Expand Down
9 changes: 9 additions & 0 deletions cmd/gokart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "testing"

func BenchmarkPs1(b *testing.B) {
for i := 0; i < b.N; i++ {
ps1()
}
}
26 changes: 26 additions & 0 deletions internal/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package internal

import (
"os"
)

func ListWdFiles() []string {
wd, err := os.Getwd()
if err != nil {
return nil
}

entries, err := os.ReadDir(wd)
if err != nil {
return nil
}

var files []string
for _, entry := range entries {
if !entry.IsDir() {
files = append(files, entry.Name())
}
}

return files
}
18 changes: 12 additions & 6 deletions internal/versions/all.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package versions

import (
"strings"
"sync"
)

Expand All @@ -17,21 +16,28 @@ var sections = []section{
Docker,
}

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

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

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

wg.Wait()
return strings.Join(result, " ")

result := ""
for _, part := range parts {
if part != "" {
result += " " + part
}
}

return result
}
3 changes: 3 additions & 0 deletions internal/versions/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var Go = section{
upsearchFiles: []string{
"go.mod",
},
wdFiles: []string{
"*.go",
},

command: []string{"go", "version"},
versionFunc: func(output string) string {
Expand Down
10 changes: 8 additions & 2 deletions internal/versions/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ var Java = section{
color: ansi.Cyan,

upsearchFiles: []string{
"pom.xml",
"build.gradle",
"settings.gradle",
"build.xml",
"pom.xml",
"settings.gradle",
},
wdFiles: []string{
"*.class",
"*.jar",
"*.java",
"*.war",
},

command: []string{"java", "--version"},
Expand Down
6 changes: 5 additions & 1 deletion internal/versions/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ var Node = section{
color: ansi.Green,

upsearchFiles: []string{
"package.json",
"node_modules",
"package.json",
},
wdFiles: []string{
"*.js",
"*.ts",
},

command: []string{"node", "-v"},
Expand Down
3 changes: 3 additions & 0 deletions internal/versions/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var Php = section{
upsearchFiles: []string{
"composer.json",
},
wdFiles: []string{
"*.php",
},

command: []string{"php", "-v"},
versionFunc: func(output string) string {
Expand Down
3 changes: 3 additions & 0 deletions internal/versions/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var Ruby = section{
"Gemfile",
"Rakefile",
},
wdFiles: []string{
"*.rb",
},

command: []string{"ruby", "-v"},
versionFunc: func(output string) string {
Expand Down
3 changes: 3 additions & 0 deletions internal/versions/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var Rust = section{
upsearchFiles: []string{
"Cargo.toml",
},
wdFiles: []string{
"*.rs",
},

command: []string{"rustc", "--version"},
versionFunc: func(output string) string {
Expand Down
24 changes: 21 additions & 3 deletions internal/versions/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@ package versions
import (
"gokart-prompt/internal"
"gokart-prompt/internal/ansi"
"path"
)

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

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

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

return ""
}

func (s section) display(wdFiles []string) bool {
for _, file := range wdFiles {
for _, pattern := range s.wdFiles {
if ok, _ := path.Match(pattern, file); ok {
return true
}
}
}

if _, ok := internal.UpsearchWd(s.upsearchFiles); ok {
return true
}

return false
}

0 comments on commit e24861c

Please sign in to comment.