add new flags, add the ability to print to stdout

This commit is contained in:
2025-08-04 14:12:42 -04:00
parent 57fe00e981
commit f1bdaae6e8
3 changed files with 59 additions and 28 deletions

View File

@@ -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()
}
}