From d05a757c5e02ab23e97b6f6fca4e1fbeb10ab559 Mon Sep 17 00:00:00 2001 From: Zoraiz Date: Wed, 23 Nov 2022 13:06:36 +0500 Subject: [PATCH] Bin piping restricted to a special character --- README.md | 2 +- aic_package/convert_gif.go | 4 ++-- aic_package/convert_image.go | 4 ++-- aic_package/convert_root.go | 6 +++++- aic_package/util.go | 2 +- aic_package/winsize/winsize_unix.go | 3 ++- aic_package/winsize/winsize_windows.go | 3 ++- cmd/root.go | 6 +++--- cmd/util.go | 19 ++++++++++++------- snapcraft.yaml | 2 +- 10 files changed, 31 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b5d9260..5057b34 100644 --- a/README.md +++ b/README.md @@ -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 - > ``` diff --git a/aic_package/convert_gif.go b/aic_package/convert_gif.go index 8dd2b30..0b351e3 100644 --- a/aic_package/convert_gif.go +++ b/aic_package/convert_gif.go @@ -52,7 +52,7 @@ 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)) @@ -60,7 +60,7 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes, pipedInp 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) diff --git a/aic_package/convert_image.go b/aic_package/convert_image.go index 53bc67c..6c09fd1 100644 --- a/aic_package/convert_image.go +++ b/aic_package/convert_image.go @@ -34,7 +34,7 @@ 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)) @@ -42,7 +42,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes, pipe 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) diff --git a/aic_package/convert_root.go b/aic_package/convert_root.go index 4e2f329..c4a8782 100644 --- a/aic_package/convert_root.go +++ b/aic_package/convert_root.go @@ -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") @@ -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) diff --git a/aic_package/util.go b/aic_package/util.go index 22b84e1..e67e036 100644 --- a/aic_package/util.go +++ b/aic_package/util.go @@ -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 } diff --git a/aic_package/winsize/winsize_unix.go b/aic_package/winsize/winsize_unix.go index 3b44389..bd88215 100644 --- a/aic_package/winsize/winsize_unix.go +++ b/aic_package/winsize/winsize_unix.go @@ -1,4 +1,5 @@ -// +build unix, !windows +//go:build (unix && ignore) || !windows +// +build unix,ignore !windows package winsize diff --git a/aic_package/winsize/winsize_windows.go b/aic_package/winsize/winsize_windows.go index ff68c6e..874eecc 100644 --- a/aic_package/winsize/winsize_windows.go +++ b/aic_package/winsize/winsize_windows.go @@ -1,4 +1,5 @@ -// +build windows, !unix +//go:build (windows && ignore) || !unix +// +build windows,ignore !unix package winsize diff --git a/cmd/root.go b/cmd/root.go index 43b14e9..55277e1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -93,8 +93,8 @@ var ( OnlySave: onlySave, } - if isInputFromPipe() { - printAscii("", flags) + if args[0] == "-" { + printAscii(args[0], flags) return } diff --git a/cmd/util.go b/cmd/util.go index 5d057e0..7bc174d 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -18,7 +18,6 @@ package cmd import ( "fmt" - "os" "path" ) @@ -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) @@ -37,6 +38,10 @@ func checkInputAndFlags(args []string) bool { } else { nonGifPresent = true } + + if arg == "-" { + pipeCharPresent = true + } } if gifPresent && nonGifPresent && !onlySave { @@ -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 @@ -169,8 +179,3 @@ func checkInputAndFlags(args []string) bool { return false } - -func isInputFromPipe() bool { - fileInfo, _ := os.Stdin.Stat() - return fileInfo.Mode()&os.ModeCharDevice == 0 -} diff --git a/snapcraft.yaml b/snapcraft.yaml index 24adf10..044c453 100644 --- a/snapcraft.yaml +++ b/snapcraft.yaml @@ -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