From f1bdaae6e89cdbcb849871c385a4f91667f8181c Mon Sep 17 00:00:00 2001 From: Amber Date: Mon, 4 Aug 2025 14:12:42 -0400 Subject: [PATCH] add new flags, add the ability to print to stdout --- go.mod | 4 +-- go.sum | 4 +-- rgb2rgb8.go | 79 +++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 59 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 17f8d09..0f080e4 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/amberisvibin/rgb2rgb8 -go 1.23.5 +go 1.15 // calculated with mingo require github.com/wayneashleyberry/truecolor v1.0.1 -require github.com/ogier/pflag v0.0.1 +require github.com/spf13/pflag v1.0.7 diff --git a/go.sum b/go.sum index 52c09b3..b694205 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -github.com/ogier/pflag v0.0.1 h1:RW6JSWSu/RkSatfcLtogGfFgpim5p7ARQ10ECk5O750= -github.com/ogier/pflag v0.0.1/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g= +github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M= +github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/wayneashleyberry/truecolor v1.0.1 h1:REnJBycjnvg0AFErbLx2GCmLLar8brlqm62kOKnRsGs= github.com/wayneashleyberry/truecolor v1.0.1/go.mod h1:fyL3jRES70g94n+Eu+XLhXYvcseza55ph8zlkmUKW7Q= diff --git a/rgb2rgb8.go b/rgb2rgb8.go index 9329eee..fd64686 100644 --- a/rgb2rgb8.go +++ b/rgb2rgb8.go @@ -4,17 +4,21 @@ import ( "bufio" "encoding/hex" "fmt" - "github.com/ogier/pflag" + "github.com/spf13/pflag" "github.com/wayneashleyberry/truecolor/pkg/color" "math" "os" ) +var output *os.File + var ( outputFile string verbose bool version bool colors bool + print bool + help bool ) func init() { @@ -26,13 +30,12 @@ func init() { "output file", ) pflag.BoolVarP( - &verbose, - "verbose", - "v", + &print, + "print", + "p", false, - "verbose output", + "print to stdout instead of output file. -o will have no effect", ) - pflag.BoolVarP( &colors, "colors", @@ -40,16 +43,30 @@ func init() { false, "print colors to terminal (needs 24 bit color support)", ) + pflag.BoolVarP( + &verbose, + "verbose", + "v", + false, + "verbose output", + ) pflag.BoolVar( &version, "version", false, "version info", ) + pflag.BoolVarP( + &help, + "help", + "h", + false, + "displays help", + ) } -//converts different size bytes to float64 to compare -//high and low and return the one thats closer +// converts different size bytes to float64 to compare +// high and low and return the one thats closer func compareChannels(base, high, low byte) byte { absHigh := math.Abs(float64(high - base)) absLow := math.Abs(float64(low - base)) @@ -59,24 +76,33 @@ func compareChannels(base, high, low byte) byte { return low } +func printDefaults() { + fmt.Fprintf(os.Stderr, "Usage: %s [options] [-o outfile] infile\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "Options:\n") + pflag.PrintDefaults() +} + func main() { - pflag.Usage = func() { - fmt.Fprintf(os.Stderr, "%s is a utility that converts RGB24 hex palette files to 8bit 3-3-2 RGB.\n", os.Args[0]) - fmt.Fprintf(os.Stderr, "Usage:\n") - pflag.PrintDefaults() - } + pflag.Usage = printDefaults pflag.Parse() if version { - fmt.Println("rgb2rgb8 v0.1") + fmt.Fprintf(os.Stderr, "%s is a utility that converts RGB24 hex palette files to 8bit 3-3-2 RGB.\n", os.Args[0]) + fmt.Println("rgb2rgb8 v0.1.0") fmt.Println("(c) 2025 Amber Zeller") fmt.Println("Distributed under the MIT license") os.Exit(0) } + if help { + printDefaults() + os.Exit(0) + } + if len(pflag.Args()) == 0 { fmt.Println("Error: No file specified.") + printDefaults() os.Exit(1) } @@ -97,14 +123,14 @@ func main() { defer input.Close() //create output file - output, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE, 0644) - if err != nil { - fmt.Println("Error: Cannot create output file.", err) - os.Exit(3) + if !print { + output, err = os.Create(outputFile) + if err != nil { + fmt.Println("Error: Cannot create output file.", err) + os.Exit(3) + } } - defer output.Close() - //scan through input scanner := bufio.NewScanner(input) var data string @@ -177,9 +203,14 @@ func main() { fmt.Println(scanner.Err()) } - _, err = output.WriteString(data) - if err != nil { - fmt.Println("Error: Failed to write output file.", err) - os.Exit(4) + if print { + fmt.Fprintf(os.Stderr, "%s", data) + } else { + _, err = output.WriteString(data) + if err != nil { + fmt.Println("Error: Failed to write output file.", err) + os.Exit(4) + } + output.Close() } }