Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package main

import (
"bufio"
"bytes"
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"github.com/balpha/go-unicornify/unicornify"
"image"
"image/png"
"math/rand"
"os"
"strings"
"time"

"github.com/balpha/go-unicornify/unicornify"
)

func main() {
var mail, hash string
var random, free, zoomOut, nodouble, noshading, nograss, serial bool
var random, free, zoomOut, nodouble, noshading, nograss, serial, writeStdout bool
var size int
var outfile string

Expand All @@ -32,6 +34,7 @@ func main() {
flag.BoolVar(&noshading, "noshading", false, "do not add shading, this will make unicorns look flatter")
flag.BoolVar(&nograss, "nograss", false, "do not add grass to the ground")
flag.BoolVar(&serial, "serial", false, "do not parallelize the drawing")
flag.BoolVar(&writeStdout, "stdout", false, "write image to STDOUT")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change the info message (see my other comment) to go to stderr instead of stdout, then it seems like this option isn't necessary anymore, since you can specify stdout as the outfile.

At least on my machine, this works fine:

$ ./go-unicornify -r -o /dev/stdout

What do you think?


flag.Parse()
inputs := 0
Expand Down Expand Up @@ -66,7 +69,10 @@ func main() {
outfile = hash + ".png"
}

fmt.Printf("Creating size %v avatar for hash %v, writing into %v\n", size, hash, outfile)
if !writeStdout {
fmt.Printf("Creating size %v avatar for hash %v, writing into %v\n", size, hash, outfile)
}
Comment on lines +72 to +74
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just send this to stderr instead of disabling it. Not sure why I sent this to stdout and the rest to stderr -- it should all go to stderr.


actualSize := size
if !nodouble {
actualSize *= 2
Expand All @@ -88,6 +94,18 @@ func main() {
img = downscale(img)
}

if writeStdout {
buf := new(bytes.Buffer)
err := png.Encode(buf, img)
if err != nil {
os.Stderr.WriteString("Error encoding to PNG\n")
os.Exit(1)
}

os.Stdout.Write(buf.Bytes())
return
}

f, err := os.Create(outfile)
if err != nil {
os.Stderr.WriteString("Could not create output file " + outfile + "\n")
Expand Down