chore: use go fix to enhance codebase (#29)
This pull request modernizes the codebase by adopting Go 1.21+ features, including built-in functions like min and max, standard library iterators such as strings.SplitSeq and strings.FieldsSeq, utility functions like maps.Copy and slices.Contains, and string manipulation helpers like strings.Cut and strings.CutPrefix. Feedback is provided on a test file where the usage of strings.Builder can be further optimized to avoid unnecessary string allocations during concatenation. `go fix -diff ./...`
This commit is contained in:
+7
-10
@@ -1,6 +1,8 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -24,9 +26,7 @@ func GetAliasesCopy() map[string]string {
|
||||
shellAliasesMu.RLock()
|
||||
defer shellAliasesMu.RUnlock()
|
||||
cp := make(map[string]string, len(ShellAliases))
|
||||
for k, v := range ShellAliases {
|
||||
cp[k] = v
|
||||
}
|
||||
maps.Copy(cp, ShellAliases)
|
||||
return cp
|
||||
}
|
||||
|
||||
@@ -123,11 +123,8 @@ func Lookup(input string) []Suggestion {
|
||||
for _, sub := range currentSubs {
|
||||
match := sub.Name == tok
|
||||
if !match {
|
||||
for _, a := range sub.Aliases {
|
||||
if a == tok {
|
||||
match = true
|
||||
break
|
||||
}
|
||||
if slices.Contains(sub.Aliases, tok) {
|
||||
match = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,8 +206,8 @@ func Lookup(input string) []Suggestion {
|
||||
suggested = "\"" + suggested + "\""
|
||||
}
|
||||
|
||||
// if the suggestion is a full path that includes
|
||||
// words already in the command line (multi-word support), we replace
|
||||
// if the suggestion is a full path that includes
|
||||
// words already in the command line (multi-word support), we replace
|
||||
// the entire argument part by using prefix only
|
||||
finalCmd := ""
|
||||
if len(tokens) > depth+1 && strings.HasPrefix(g.Cmd, tokens[depth]) {
|
||||
|
||||
@@ -51,10 +51,7 @@ func ZoxideGenerator() core.GeneratorFunc {
|
||||
home, _ := os.UserHomeDir()
|
||||
|
||||
if fullQuery == "" {
|
||||
limit := 20
|
||||
if len(dirs) < limit {
|
||||
limit = len(dirs)
|
||||
}
|
||||
limit := min(len(dirs), 20)
|
||||
for i := 0; i < limit; i++ {
|
||||
path := dirs[i]
|
||||
display := strings.Replace(path, home, "~", 1)
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ func GitCommitGenerator(tokens []string, _ string, _ string) []core.Suggestion {
|
||||
}
|
||||
|
||||
var results []core.Suggestion
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
for line := range strings.SplitSeq(string(out), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
|
||||
@@ -22,7 +22,7 @@ func dockerContainerGenerator(tokens []string, _ string, _ string) []core.Sugges
|
||||
}
|
||||
|
||||
var results []core.Suggestion
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
for line := range strings.SplitSeq(string(out), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
@@ -45,7 +45,7 @@ func dockerRunningContainerGenerator(tokens []string, _ string, _ string) []core
|
||||
}
|
||||
|
||||
var results []core.Suggestion
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
for line := range strings.SplitSeq(string(out), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
@@ -69,7 +69,7 @@ func dockerImageGenerator(tokens []string, _ string, _ string) []core.Suggestion
|
||||
|
||||
seen := make(map[string]bool)
|
||||
var results []core.Suggestion
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
for line := range strings.SplitSeq(string(out), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || line == "<none>:<none>" || seen[line] {
|
||||
continue
|
||||
|
||||
@@ -34,7 +34,7 @@ func installedPackageGenerator(pm string) core.GeneratorFunc {
|
||||
}
|
||||
|
||||
var results []core.Suggestion
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
for line := range strings.SplitSeq(string(out), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
|
||||
@@ -25,7 +25,7 @@ func pipPackageGenerator(tokens []string, _ string, _ string) []core.Suggestion
|
||||
}
|
||||
|
||||
var results []core.Suggestion
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
for line := range strings.SplitSeq(string(out), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
|
||||
@@ -34,10 +34,10 @@ func init() {
|
||||
lastComment := ""
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
|
||||
|
||||
// Handle comments
|
||||
if strings.HasPrefix(line, "#") {
|
||||
lastComment = strings.TrimSpace(strings.TrimPrefix(line, "#"))
|
||||
if after, ok := strings.CutPrefix(line, "#"); ok {
|
||||
lastComment = strings.TrimSpace(after)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func init() {
|
||||
seen[recipe] = true
|
||||
|
||||
cmd := recipe
|
||||
|
||||
|
||||
desc := "just recipe"
|
||||
if lastComment != "" {
|
||||
desc = lastComment
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ func processGenerator(tokens []string, _ string, _ string) []core.Suggestion {
|
||||
|
||||
seen := make(map[string]bool)
|
||||
var results []core.Suggestion
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
for line := range strings.SplitSeq(string(out), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user