Bin piping restricted to a special character

This commit is contained in:
Zoraiz 2022-11-23 13:06:36 +05:00
parent fbf87d33d5
commit d05a757c5e
10 changed files with 31 additions and 20 deletions

View File

@ -170,7 +170,7 @@ ascii-image-converter myImage.jpeg
> **Note:** Piped binary input is also supported > **Note:** Piped binary input is also supported
> ``` > ```
> cat myImage.png | ascii-image-converter > cat myImage.png | ascii-image-converter -
> ``` > ```

View File

@ -52,7 +52,7 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes, pipedInp
err error err error
) )
if isInputFromPipe() { if gifPath == "-" {
originalGif, err = gif.DecodeAll(bytes.NewReader(pipedInputBytes)) originalGif, err = gif.DecodeAll(bytes.NewReader(pipedInputBytes))
} else if pathIsURl { } else if pathIsURl {
originalGif, err = gif.DecodeAll(bytes.NewReader(urlImgBytes)) originalGif, err = gif.DecodeAll(bytes.NewReader(urlImgBytes))
@ -60,7 +60,7 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes, pipedInp
originalGif, err = gif.DecodeAll(localGif) originalGif, err = gif.DecodeAll(localGif)
} }
if err != nil { if err != nil {
if isInputFromPipe() { if gifPath == "-" {
return fmt.Errorf("can't decode piped input: %v", err) return fmt.Errorf("can't decode piped input: %v", err)
} else { } else {
return fmt.Errorf("can't decode %v: %v", gifPath, err) return fmt.Errorf("can't decode %v: %v", gifPath, err)

View File

@ -34,7 +34,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes, pipe
err error err error
) )
if isInputFromPipe() { if imagePath == "-" {
imData, _, err = image.Decode(bytes.NewReader(pipedInputBytes)) imData, _, err = image.Decode(bytes.NewReader(pipedInputBytes))
} else if pathIsURl { } else if pathIsURl {
imData, _, err = image.Decode(bytes.NewReader(urlImgBytes)) imData, _, err = image.Decode(bytes.NewReader(urlImgBytes))
@ -42,7 +42,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes, pipe
imData, _, err = image.Decode(localImg) imData, _, err = image.Decode(localImg)
} }
if err != nil { if err != nil {
if isInputFromPipe() { if imagePath == "-" {
return "", fmt.Errorf("can't decode piped input: %v", err) return "", fmt.Errorf("can't decode piped input: %v", err)
} else { } else {
return "", fmt.Errorf("can't decode %v: %v", imagePath, err) return "", fmt.Errorf("can't decode %v: %v", imagePath, err)

View File

@ -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 // Different modes of reading data depending upon whether or not filePath is a url
if !isInputFromPipe() { if filePath != "-" {
if pathIsURl { if pathIsURl {
fmt.Printf("Fetching file from url...\r") fmt.Printf("Fetching file from url...\r")
@ -152,6 +152,10 @@ func Convert(filePath string, flags Flags) (string, error) {
} else { } else {
// Check file/data type of piped input // 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) pipedInputBytes, err = ioutil.ReadAll(os.Stdin)
if err != nil { if err != nil {
return "", fmt.Errorf("unable to read piped input: %v", err) return "", fmt.Errorf("unable to read piped input: %v", err)

View File

@ -67,7 +67,7 @@ func createSaveFileName(imagePath, urlImgName, label string) (string, error) {
return newName + label, nil return newName + label, nil
} }
if isInputFromPipe() { if imagePath == "-" {
if inputIsGif { if inputIsGif {
return "piped-gif" + label, nil return "piped-gif" + label, nil
} }

View File

@ -1,4 +1,5 @@
// +build unix, !windows //go:build (unix && ignore) || !windows
// +build unix,ignore !windows
package winsize package winsize

View File

@ -1,4 +1,5 @@
// +build windows, !unix //go:build (windows && ignore) || !unix
// +build windows,ignore !unix
package winsize package winsize

View File

@ -58,7 +58,7 @@ var (
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "ascii-image-converter [image paths/urls or piped stdin]", Use: "ascii-image-converter [image paths/urls or piped stdin]",
Short: "Converts images and gifs into ascii art", 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.", 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 // Not RunE since help text is getting larger and seeing it for every error impacts user experience
@ -93,8 +93,8 @@ var (
OnlySave: onlySave, OnlySave: onlySave,
} }
if isInputFromPipe() { if args[0] == "-" {
printAscii("", flags) printAscii(args[0], flags)
return return
} }

View File

@ -18,7 +18,6 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"path" "path"
) )
@ -28,6 +27,8 @@ func checkInputAndFlags(args []string) bool {
gifCount := 0 gifCount := 0
gifPresent := false gifPresent := false
nonGifPresent := false nonGifPresent := false
pipeCharPresent := false
for _, arg := range args { for _, arg := range args {
extension := path.Ext(arg) extension := path.Ext(arg)
@ -37,6 +38,10 @@ func checkInputAndFlags(args []string) bool {
} else { } else {
nonGifPresent = true nonGifPresent = true
} }
if arg == "-" {
pipeCharPresent = true
}
} }
if gifPresent && nonGifPresent && !onlySave { if gifPresent && nonGifPresent && !onlySave {
@ -60,11 +65,16 @@ func checkInputAndFlags(args []string) bool {
return true 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") fmt.Printf("Error: Need at least 1 input path/url or piped input\nUse the -h flag for more info\n\n")
return true 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 { if customMap != "" && len(customMap) < 2 {
fmt.Printf("Need at least 2 characters for --map flag\n\n") fmt.Printf("Need at least 2 characters for --map flag\n\n")
return true return true
@ -169,8 +179,3 @@ func checkInputAndFlags(args []string) bool {
return false return false
} }
func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeCharDevice == 0
}

View File

@ -1,6 +1,6 @@
name: ascii-image-converter name: ascii-image-converter
base: core18 base: core18
version: "1.13.0" version: "1.13.1"
summary: Convert images and gifs into ascii art summary: Convert images and gifs into ascii art
description: | description: |
ascii-image-converter is a command-line tool that converts images into ascii art and prints ascii-image-converter is a command-line tool that converts images into ascii art and prints