Skip to content

Commit

Permalink
Bin piping restricted to a special character
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoraiz committed Nov 23, 2022
1 parent fbf87d3 commit d05a757
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ ascii-image-converter myImage.jpeg

> **Note:** Piped binary input is also supported
> ```
> cat myImage.png | ascii-image-converter
> cat myImage.png | ascii-image-converter -
> ```
Expand Down
4 changes: 2 additions & 2 deletions aic_package/convert_gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes, pipedInp
err error
)

if isInputFromPipe() {
if gifPath == "-" {
originalGif, err = gif.DecodeAll(bytes.NewReader(pipedInputBytes))
} else if pathIsURl {
originalGif, err = gif.DecodeAll(bytes.NewReader(urlImgBytes))
} else {
originalGif, err = gif.DecodeAll(localGif)
}
if err != nil {
if isInputFromPipe() {
if gifPath == "-" {
return fmt.Errorf("can't decode piped input: %v", err)
} else {
return fmt.Errorf("can't decode %v: %v", gifPath, err)
Expand Down
4 changes: 2 additions & 2 deletions aic_package/convert_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes, pipe
err error
)

if isInputFromPipe() {
if imagePath == "-" {
imData, _, err = image.Decode(bytes.NewReader(pipedInputBytes))
} else if pathIsURl {
imData, _, err = image.Decode(bytes.NewReader(urlImgBytes))
} else {
imData, _, err = image.Decode(localImg)
}
if err != nil {
if isInputFromPipe() {
if imagePath == "-" {
return "", fmt.Errorf("can't decode piped input: %v", err)
} else {
return "", fmt.Errorf("can't decode %v: %v", imagePath, err)
Expand Down
6 changes: 5 additions & 1 deletion aic_package/convert_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func Convert(filePath string, flags Flags) (string, error) {

// Different modes of reading data depending upon whether or not filePath is a url

if !isInputFromPipe() {
if filePath != "-" {
if pathIsURl {
fmt.Printf("Fetching file from url...\r")

Expand Down Expand Up @@ -152,6 +152,10 @@ func Convert(filePath string, flags Flags) (string, error) {
} else {
// Check file/data type of piped input

if !isInputFromPipe() {
return "", fmt.Errorf("there is no input being piped to stdin")
}

pipedInputBytes, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("unable to read piped input: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion aic_package/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func createSaveFileName(imagePath, urlImgName, label string) (string, error) {
return newName + label, nil
}

if isInputFromPipe() {
if imagePath == "-" {
if inputIsGif {
return "piped-gif" + label, nil
}
Expand Down
3 changes: 2 additions & 1 deletion aic_package/winsize/winsize_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build unix, !windows
//go:build (unix && ignore) || !windows
// +build unix,ignore !windows

package winsize

Expand Down
3 changes: 2 additions & 1 deletion aic_package/winsize/winsize_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build windows, !unix
//go:build (windows && ignore) || !unix
// +build windows,ignore !unix

package winsize

Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
rootCmd = &cobra.Command{
Use: "ascii-image-converter [image paths/urls or piped stdin]",
Short: "Converts images and gifs into ascii art",
Version: "1.13.0",
Version: "1.13.1",
Long: "This tool converts images into ascii art and prints them on the terminal.\nFurther configuration can be managed with flags.",

// Not RunE since help text is getting larger and seeing it for every error impacts user experience
Expand Down Expand Up @@ -93,8 +93,8 @@ var (
OnlySave: onlySave,
}

if isInputFromPipe() {
printAscii("", flags)
if args[0] == "-" {
printAscii(args[0], flags)
return
}

Expand Down
19 changes: 12 additions & 7 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"fmt"
"os"
"path"
)

Expand All @@ -28,6 +27,8 @@ func checkInputAndFlags(args []string) bool {
gifCount := 0
gifPresent := false
nonGifPresent := false
pipeCharPresent := false

for _, arg := range args {
extension := path.Ext(arg)

Expand All @@ -37,6 +38,10 @@ func checkInputAndFlags(args []string) bool {
} else {
nonGifPresent = true
}

if arg == "-" {
pipeCharPresent = true
}
}

if gifPresent && nonGifPresent && !onlySave {
Expand All @@ -60,11 +65,16 @@ func checkInputAndFlags(args []string) bool {
return true
}

if !isInputFromPipe() && len(args) < 1 {
if len(args) < 1 {
fmt.Printf("Error: Need at least 1 input path/url or piped input\nUse the -h flag for more info\n\n")
return true
}

if len(args) > 1 && pipeCharPresent {
fmt.Printf("Error: You cannot pass in piped input alongside other inputs\n\n")
return true
}

if customMap != "" && len(customMap) < 2 {
fmt.Printf("Need at least 2 characters for --map flag\n\n")
return true
Expand Down Expand Up @@ -169,8 +179,3 @@ func checkInputAndFlags(args []string) bool {

return false
}

func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeCharDevice == 0
}
2 changes: 1 addition & 1 deletion snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ascii-image-converter
base: core18
version: "1.13.0"
version: "1.13.1"
summary: Convert images and gifs into ascii art
description: |
ascii-image-converter is a command-line tool that converts images into ascii art and prints
Expand Down

0 comments on commit d05a757

Please sign in to comment.