mirror of
https://github.com/TheZoraiz/ascii-image-converter.git
synced 2026-05-17 00:45:58 +03:00
Bin piping restricted to a special character
This commit is contained in:
@@ -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 -
|
||||
> ```
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// +build unix, !windows
|
||||
//go:build (unix && ignore) || !windows
|
||||
// +build unix,ignore !windows
|
||||
|
||||
package winsize
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// +build windows, !unix
|
||||
//go:build (windows && ignore) || !unix
|
||||
// +build windows,ignore !unix
|
||||
|
||||
package winsize
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
19
cmd/util.go
19
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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user