aboutsummaryrefslogtreecommitdiffstats
path: root/ils.go
diff options
context:
space:
mode:
Diffstat (limited to 'ils.go')
-rw-r--r--ils.go59
1 files changed, 39 insertions, 20 deletions
diff --git a/ils.go b/ils.go
index c18e07d..fe6bc1a 100644
--- a/ils.go
+++ b/ils.go
@@ -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
}