diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2026-06-12 20:09:47 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2026-06-12 20:09:47 -0700 |
| commit | 084bd5469beba8fc230bd46f8b951f10a3e9d076 (patch) | |
| tree | 9307ceab243b0798e5f5ca62df9708bd98ff6ee9 | |
| parent | adef688e11cabba97fb08909399b41992a78076c (diff) | |
| download | imgutil-084bd5469beba8fc230bd46f8b951f10a3e9d076.tar.zst | |
Slight refactor to deduplicate some logic
| -rw-r--r-- | ils.go | 59 |
1 files changed, 39 insertions, 20 deletions
@@ -25,6 +25,7 @@ import ( var ( x = flag.Int("x", 15, "set image width in columns") + l = flag.Bool("l", false, "print image attributes") m = flagPrintMode(flag.CommandLine, "m", modeSixel, "one of 'block', 'block24', or 'sixel'") ) @@ -143,15 +144,31 @@ type pixelAspectRatio struct { x, y int } -func compositeImageRow(imagePaths []string, width, rowWidth, cellX int, aspectRatio pixelAspectRatio) (image.Image, error) { - var logicalWidth, maxHeight int - for i, filename := range imagePaths { +func layoutRow(imagePaths []string, width, rowWidth, cellX int) []int { + filenameWidths := make([]int, len(imagePaths)) + for i, path := range imagePaths { + filenameWidths[i] = cellX * runewidth.StringWidth(escapeString(filepath.Base(path))) + } + var logicalWidth int + for i, filenameWidth := range filenameWidths { if i > 0 { logicalWidth += 2 * cellX } - fileNameWidth := cellX * runewidth.StringWidth(escapeString(filepath.Base(filename))) - logicalWidth += max(width, fileNameWidth) + logicalWidth += max(width, filenameWidth) + } + layout := make([]int, len(imagePaths)) + var offset int + for i, filenameWidth := range filenameWidths { + imageWidth := max(width, filenameWidth) + layout[i] = divRound(offset*rowWidth, logicalWidth) + offset += imageWidth + 2*cellX + } + return layout +} +func compositeImageRow(imagePaths []string, layout []int, width, rowWidth, cellX int, aspectRatio pixelAspectRatio) (image.Image, error) { + var maxHeight int + for _, filename := range imagePaths { imageWidth, imageHeight, err := imageDimensions(filename) if err != nil { return nil, err @@ -160,20 +177,20 @@ func compositeImageRow(imagePaths []string, width, rowWidth, cellX int, aspectRa } bounds := image.Rect(0, 0, rowWidth, maxHeight) result := image.NewRGBA(bounds) - offset := 0 - for _, filename := range imagePaths { + for i, filename := range imagePaths { img, err := load(filename) if err != nil { return nil, err } + start := layout[i] + end := rowWidth + if i+1 < len(imagePaths) { + end = layout[i+1] + } height := divRound(img.Bounds().Dy()*width*aspectRatio.x, img.Bounds().Dx()*aspectRatio.y) - imageWidth := max(width, cellX*runewidth.StringWidth(escapeString(filepath.Base(filename)))) - start := divRound(offset*rowWidth, logicalWidth) - end := divRound((offset+imageWidth)*rowWidth, logicalWidth) offsetStart := start + (end-start-width)/2 dstBounds := image.Rect(offsetStart, (maxHeight-height)/2, offsetStart+width, (maxHeight-height)/2+height) draw.BiLinear.Scale(result, dstBounds, img, img.Bounds(), draw.Src, nil) - offset += imageWidth + 2*cellX } return result, nil } @@ -213,6 +230,10 @@ func thumbnailFiles(w io.Writer, imagePaths []string, width, rowWidth, cellX, ce padding *= cellX } rows := splitRows(imagePaths, width, rowWidth) + layouts := make([][]int, len(rows)) + for i, row := range rows { + layouts[i] = layoutRow(row, width*cellX, rowWidth*cellX, cellX) + } rowImages := make([]chan image.Image, len(rows)) for i := range rowImages { rowImages[i] = make(chan image.Image, 1) @@ -224,7 +245,7 @@ func thumbnailFiles(w io.Writer, imagePaths []string, width, rowWidth, cellX, ce sem <- struct{}{} go func() { defer func() { <-sem }() - img, err := compositeImageRow(row, width*cellX, rowWidth*cellX, cellX, aspectRatio) + img, err := compositeImageRow(row, layouts[i], width*cellX, rowWidth*cellX, cellX, aspectRatio) if err != nil { select { case errCh <- err: @@ -252,19 +273,17 @@ func thumbnailFiles(w io.Writer, imagePaths []string, width, rowWidth, cellX, ce } printImg(w, rowImage) fmt.Fprintln(w) - offset := 0 realOffset := 0 - for i, filename := range row { - if i > 0 { - offset += 2 + for j, filename := range row { + start := divRound(layouts[i][j], cellX) + end := rowWidth + if j+1 < len(row) { + end = divRound(layouts[i][j+1], cellX) } - start := divRound(offset*rowWidth, thisRowWidth) fmt.Fprintf(w, "%*s", start-realOffset, "") realOffset = start fileNameWidth := runewidth.StringWidth(escapeString(filepath.Base(filename))) - offset += max(width, fileNameWidth) - end := divRound(offset*rowWidth, thisRowWidth) - padding := (end - start - fileNameWidth) / 2 + padding := max((end-start-fileNameWidth)/2, 0) fmt.Fprintf(w, "%*s%s", padding, "", escapeString(filepath.Base(filename))) realOffset += padding + fileNameWidth } |
