Fixed bug with output redirect for linux

This commit is contained in:
Zoraiz 2021-06-10 23:59:02 +05:00
parent d3f8df41cb
commit 03a9748a9f
6 changed files with 87 additions and 6 deletions

View File

@ -44,7 +44,7 @@ var (
rootCmd = &cobra.Command{
Use: "ascii-image-converter [image paths/urls]",
Short: "Converts images into ascii art",
Version: "1.3.3",
Version: "1.3.4",
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
@ -138,7 +138,7 @@ func init() {
defaultUsageTemplate := rootCmd.UsageTemplate()
rootCmd.SetUsageTemplate(defaultUsageTemplate + "\nCopyright © 2021 Zoraiz Hassan <hzoraiz8@gmail.com>\n" +
"Distributed under the Apache License Version 2.0 (Apache-2.0)\n" +
"For further details, visit https://github.com/TheZoraiz/ascii-image-converter\n\n")
"For further details, visit https://github.com/TheZoraiz/ascii-image-converter\n")
}
// initConfig reads in config file and ENV variables if set.

View File

@ -21,7 +21,7 @@ import (
"image"
"image/color"
"github.com/nathan-fiscaletti/consolesize-go"
"github.com/TheZoraiz/ascii-image-converter/image_manipulation/winsize"
"github.com/nfnt/resize"
)
@ -45,7 +45,11 @@ func ConvertToAsciiPixels(img image.Image, dimensions []int, flipX, flipY bool)
// Following code in this condition calculates ratio according to terminal height
terminalWidth, terminalHeight := consolesize.GetConsoleSize()
terminalWidth, terminalHeight, err := winsize.GetTerminalSize()
if err != nil {
return nil, 0, 0, err
}
asciiHeight = terminalHeight - 1
// Passing 0 in place of width keeps the original image's aspect ratio
@ -76,7 +80,10 @@ func ConvertToAsciiPixels(img image.Image, dimensions []int, flipX, flipY bool)
// If there are passed dimensions, check whether the width exceeds terminal width
if len(dimensions) > 0 {
defaultTermWidth, _ := consolesize.GetConsoleSize()
defaultTermWidth, _, err := winsize.GetTerminalSize()
if err != nil {
return nil, 0, 0, err
}
defaultTermWidth -= 1
if dimensions[0] > defaultTermWidth {

View File

@ -0,0 +1,3 @@
## Note
These files are just wrappers around [consolesize-go](https://github.com/nathan-fiscaletti/consolesize-go) package. For unix, they resort to terminal size calculation from stdin if stdout is not directed to terminal. For windows, they currently throw an error.

View File

@ -0,0 +1,50 @@
// +build unix, !windows
package winsize
import (
"os"
"syscall"
"unsafe"
"github.com/nathan-fiscaletti/consolesize-go"
)
// By default, this functions calculates terminal dimensions from stdout but in case
// stdout isn't a the terminal, it'll calculate terminal dimensions from stdin. This
// functionality isn't supported for windows yet
func GetTerminalSize() (int, int, error) {
// Check if stdout is terminal
fileInfo, err := os.Stdout.Stat()
if err != nil {
return 0, 0, err
}
var stdoutIsTerminal bool
if (fileInfo.Mode() & os.ModeCharDevice) != 0 {
stdoutIsTerminal = true
} else {
stdoutIsTerminal = false
}
if stdoutIsTerminal {
x, y := consolesize.GetConsoleSize()
return x, y, nil
} else {
// Get size from stdin if stdout is not terminal
var sz struct {
rows uint16
cols uint16
xpixels uint16
ypixels uint16
}
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
return int(sz.cols), int(sz.rows), nil
}
}

View File

@ -0,0 +1,21 @@
// +build windows, !unix
package winsize
import (
"fmt"
"github.com/nathan-fiscaletti/consolesize-go"
)
// By default, this functions calculates terminal dimensions from stdout but in case
// stdout isn't a the terminal, it'll throw an error instead of panicking.
func GetTerminalSize() (int, int, error) {
x, y := consolesize.GetConsoleSize()
if x < 1 && y < 1 {
return x, y, fmt.Errorf("altering stdout isn't currently supported on windows")
} else {
return x, y, nil
}
}

View File

@ -1,6 +1,6 @@
name: ascii-image-converter
base: core18
version: "1.3.3"
version: "1.3.4"
summary: Converts images into ascii art
description: |
This tool converts images into ascii format and prints them onto the terminal window.