Skip to content

Commit 50045d4

Browse files
committed
refactor: optimize loop constructs and improve code readability
1 parent 48f1dc4 commit 50045d4

File tree

8 files changed

+20
-25
lines changed

8 files changed

+20
-25
lines changed

OPC/ratatui-image

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 8d0fff0ee3ee579ed442ac4fa2ba5c3b4f8e6d62

detect_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package termimg
22

33
import (
44
"os"
5+
"slices"
56
"testing"
67
"time"
78

@@ -187,13 +188,7 @@ func TestDetermineProtocols(t *testing.T) {
187188
assert.NotEmpty(t, protocols, "Should return at least one protocol")
188189

189190
// Should always include halfblocks as fallback
190-
found := false
191-
for _, p := range protocols {
192-
if p == Halfblocks {
193-
found = true
194-
break
195-
}
196-
}
191+
found := slices.Contains(protocols, Halfblocks)
197192
assert.True(t, found, "Should include halfblocks as fallback")
198193
}
199194

@@ -316,7 +311,7 @@ func TestConcurrentDetection(t *testing.T) {
316311
// Test concurrent access to detection functions
317312
done := make(chan bool, 10)
318313

319-
for i := 0; i < 10; i++ {
314+
for range 10 {
320315
go func() {
321316
_ = DetectProtocol()
322317
_ = QueryTerminalFeatures()
@@ -325,7 +320,7 @@ func TestConcurrentDetection(t *testing.T) {
325320
}
326321

327322
// Wait for all goroutines
328-
for i := 0; i < 10; i++ {
323+
for range 10 {
329324
select {
330325
case <-done:
331326
// Success

kitty_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func TestKittyZlibCompression(t *testing.T) {
2626
t.Log(output)
2727

2828
// Handle tmux wrapping if present
29-
if strings.HasPrefix(output, "\x1bPtmux;\x1b") {
30-
unwrapped := strings.TrimPrefix(output, "\x1bPtmux;\x1b")
29+
if after, ok := strings.CutPrefix(output, "\x1bPtmux;\x1b"); ok {
30+
unwrapped := after
3131
unwrapped = strings.TrimSuffix(unwrapped, "\x1b\\")
3232
output = strings.ReplaceAll(unwrapped, "\x1b\x1b", "\x1b")
3333
}

renderers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
func createRendererTestImage(width, height int) image.Image {
1414
img := image.NewRGBA(image.Rect(0, 0, width, height))
1515
// Create a simple gradient pattern
16-
for y := 0; y < height; y++ {
17-
for x := 0; x < width; x++ {
16+
for y := range height {
17+
for x := range width {
1818
img.Set(x, y, color.RGBA{
1919
R: uint8((x * 255) / width),
2020
G: uint8((y * 255) / height),

resize_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
func createTestImage(width, height int) image.Image {
1515
img := image.NewRGBA(image.Rect(0, 0, width, height))
1616
// Fill with a simple pattern for visual verification
17-
for y := 0; y < height; y++ {
18-
for x := 0; x < width; x++ {
17+
for y := range height {
18+
for x := range width {
1919
img.Set(x, y, color.RGBA{
2020
R: uint8((x * 255) / width),
2121
G: uint8((y * 255) / height),
@@ -245,11 +245,11 @@ func TestResizeConcurrency(t *testing.T) {
245245
results := make(chan image.Image, numGoroutines*numOperations)
246246

247247
// Launch multiple goroutines doing resize operations
248-
for i := 0; i < numGoroutines; i++ {
248+
for i := range numGoroutines {
249249
wg.Add(1)
250250
go func(id int) {
251251
defer wg.Done()
252-
for j := 0; j < numOperations; j++ {
252+
for j := range numOperations {
253253
size := uint(20 + (id+j)%30) // Vary sizes
254254
result := ResizeImage(img, size, size, fmt.Sprintf("concurrent_%d_%d", id, j))
255255
results <- result
@@ -331,8 +331,8 @@ func TestImageProcessingQuality(t *testing.T) {
331331
img := image.NewRGBA(image.Rect(0, 0, 4, 4))
332332

333333
// Create a checkerboard pattern
334-
for y := 0; y < 4; y++ {
335-
for x := 0; x < 4; x++ {
334+
for y := range 4 {
335+
for x := range 4 {
336336
if (x+y)%2 == 0 {
337337
img.Set(x, y, color.RGBA{255, 255, 255, 255}) // White
338338
} else {
@@ -423,7 +423,7 @@ func TestMemoryUsage(t *testing.T) {
423423
runtime.ReadMemStats(&m1)
424424

425425
// Create and resize many images
426-
for i := 0; i < 100; i++ {
426+
for i := range 100 {
427427
img := createTestImage(100, 100)
428428
_ = ResizeImage(img, 50, 50, fmt.Sprintf("memory_test_%d", i))
429429
if i%10 == 0 {

termimg_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717

1818
func createAPITestImage(width, height int) image.Image {
1919
img := image.NewRGBA(image.Rect(0, 0, width, height))
20-
for y := 0; y < height; y++ {
21-
for x := 0; x < width; x++ {
20+
for y := range height {
21+
for x := range width {
2222
img.Set(x, y, color.RGBA{
2323
R: uint8((x * 255) / width),
2424
G: uint8((y * 255) / height),

tools_cellbuf.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build tools
2-
// +build tools
32

43
package termimg
54

tui.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (g *ImageGallery) Render() (string, error) {
322322
// Calculate grid layout
323323
rows := (len(g.images) + g.columns - 1) / g.columns
324324

325-
for row := 0; row < rows; row++ {
325+
for row := range rows {
326326
// Render each image in the row
327327
var imageOutputs []string
328328
maxLines := 0
@@ -385,7 +385,7 @@ func combineImagesHorizontally(images []string, spacing int, maxLines int) strin
385385
spacingStr := strings.Repeat(" ", spacing)
386386

387387
// Combine line by line
388-
for line := 0; line < maxLines; line++ {
388+
for line := range maxLines {
389389
for i, imageLines := range imageLinesSet {
390390
if i > 0 {
391391
result.WriteString(spacingStr)

0 commit comments

Comments
 (0)