Use lo.Map instead of manual append loops

Not only is this nicer code (and more idiomatic at least in this code
base), but it also avoids linter warnings about missing preallocations
(lo.Map does preallocate the result array).
This commit is contained in:
Stefan Haller 2026-08-16 16:29:08 +02:00
parent 1db9f9cdb8
commit f0ccb937d3
2 changed files with 9 additions and 14 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/gdamore/tcell/v3"
"github.com/gdamore/tcell/v3/color"
"github.com/rivo/uniseg"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
)
@ -106,10 +107,8 @@ func TestWriteString(t *testing.T) {
for _, s := range test.stringsToWrite {
v.writeString(s)
}
var resultingLines [][]string
for _, l := range v.buf.lines {
resultingLines = append(resultingLines, cellsToStrings(l.cells))
}
resultingLines := lo.Map(v.buf.lines,
func(l lineType, _ int) []string { return cellsToStrings(l.cells) })
assert.Equal(t, test.expectedLines, resultingLines)
}
}
@ -465,11 +464,7 @@ func cellsToString(cells []cell) string {
}
func cellsToStrings(cells []cell) []string {
s := []string{}
for _, c := range cells {
s = append(s, c.chr)
}
return s
return lo.Map(cells, func(c cell, _ int) string { return c.chr })
}
func TestLineWrap(t *testing.T) {

View file

@ -3,6 +3,8 @@ package components
import (
"fmt"
"os"
"github.com/samber/lo"
)
const (
@ -43,11 +45,9 @@ var hostEnvironmentAllowlist = [...]string{
// Returns a copy of the environment filtered by
// hostEnvironmentAllowlist
func allowedHostEnvironment() []string {
env := []string{}
for _, envVar := range hostEnvironmentAllowlist {
env = append(env, fmt.Sprintf("%s=%s", envVar, os.Getenv(envVar)))
}
return env
return lo.Map(hostEnvironmentAllowlist[:], func(envVar string, _ int) string {
return fmt.Sprintf("%s=%s", envVar, os.Getenv(envVar))
})
}
func NewTestEnvironment(rootDir string) []string {