From 2ef107aee0b4e88b3335b04418ec0e27ad8ebbfc Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 31 Aug 2023 09:50:17 -0700 Subject: Use one stdout buffer for the whole program. --- icat.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'icat.go') diff --git a/icat.go b/icat.go index f2c3f08..1a8dd8a 100644 --- a/icat.go +++ b/icat.go @@ -11,6 +11,8 @@ import ( "os" ) +var stdout = bufio.NewWriter(os.Stdout) + func load(filename string) (image.Image, error) { file := os.Stdin if filename != "-" { @@ -46,14 +48,12 @@ func printImg(filename string, cols, lines int) error { dst := image.NewRGBA(image.Rect(0, 0, cols, lines)) draw.ApproxBiLinear.Scale(dst, dst.Bounds(), img, img.Bounds(), draw.Over, nil) - buf := bufio.NewWriter(os.Stdout) - defer buf.Flush() for y := 0; y < lines; y++ { for x := 0; x < cols; x++ { r, g, b, _ := dst.At(x, y).RGBA() - fmt.Fprintf(buf, "\033[48;2;%d;%d;%dm ", r>>8, g>>8, b>>8) + fmt.Fprintf(stdout, "\033[48;2;%d;%d;%dm ", r>>8, g>>8, b>>8) } - fmt.Fprintln(buf, "\033[49m") + fmt.Fprintln(stdout, "\033[49m") } return nil } @@ -68,6 +68,8 @@ func icat(args []string) error { if len(args) == 0 { args = []string{"-"} } + + defer stdout.Flush() for _, f := range args { if err := printImg(f, cols, lines); err != nil { return err -- cgit v1.3.1