Skip to content

Commit

Permalink
Merge pull request #1 from lazygyu/feature/pipe
Browse files Browse the repository at this point in the history
read from the stdin if it is a pipe
  • Loading branch information
lazygyu authored Dec 9, 2022
2 parents 7d59e27 + 6b75749 commit 009e6bb
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package main

import (
"flag"
"fmt"
"image"
"image/draw"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
_ "image/gif"
"image/draw"
"log"
"os"
"io"
"bufio"
"flag"

"github.com/nfnt/resize"
"golang.org/x/term"
Expand All @@ -28,21 +30,28 @@ func main() {

args := flag.Args()

if len(args) < 1 {
fmt.Println("Usage: gv [options] filename")
flag.PrintDefaults()
os.Exit(0)
}

filename := args[0]

// open file
f, err := os.Open(filename)
if err != nil {
log.Fatal("cannot open the file")
}

defer f.Close()
stat, _ := os.Stdin.Stat()
var f io.Reader

if (stat.Mode() & os.ModeCharDevice) == 0 {
// from pipe
f = bufio.NewReader(os.Stdin)
} else {
if len(args) < 1 {
fmt.Println("Usage: gv [options] filename")
flag.PrintDefaults()
os.Exit(0)
}

filename := args[0]

// open file
f, err := os.Open(filename)
if err != nil {
log.Fatal("cannot open the file")
}
defer f.Close()
}

// load img
img, _, err := image.Decode(f)
Expand Down

0 comments on commit 009e6bb

Please sign in to comment.