aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2023-08-30 23:16:50 -0700
committerRose Hogenson <rosehogenson@posteo.net>2023-08-30 23:16:50 -0700
commit3d0c7978699c1280fd830324326c675e6570ed55 (patch)
treea42de64923c28ed70c39bc52f5e52883f6f51c80
parentc20c9685aae51deeeef560d16bcf6e6039438b6c (diff)
downloadimgutil-3d0c7978699c1280fd830324326c675e6570ed55.tar.zst
Load terminal size once at the beginning.
-rw-r--r--icat.go16
1 files changed, 8 insertions, 8 deletions
diff --git a/icat.go b/icat.go
index 2b2a847..c26d0d2 100644
--- a/icat.go
+++ b/icat.go
@@ -28,18 +28,12 @@ func load(filename string) (image.Image, error) {
return img, nil
}
-func printImg(filename string) error {
+func printImg(filename string, cols, lines int) error {
img, err := load(filename)
if err != nil {
return err
}
- cols, lines, err := term.GetSize(1)
- if err != nil {
- return fmt.Errorf("terminal size: %s", err)
- }
- lines-- // Leave a line for the status bar.
-
// Try not to stretch the image.
if img.Bounds().Dy()*cols <= img.Bounds().Dx()*lines*5/2 {
lines = img.Bounds().Dy() * cols / (img.Bounds().Dx() * 5 / 2)
@@ -61,11 +55,17 @@ func printImg(filename string) error {
}
func icat(args []string) error {
+ cols, lines, err := term.GetSize(1)
+ if err != nil {
+ return fmt.Errorf("terminal size: %s", err)
+ }
+ lines-- // Leave a line for the status bar.
+
if len(args) == 0 {
args = []string{"-"}
}
for _, f := range args {
- if err := printImg(f); err != nil {
+ if err := printImg(f, cols, lines); err != nil {
return err
}
}