aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 4d4039598bd0b0b3b09f076f247d4482e09f7fba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main

import (
	"flag"
	"fmt"
	"image/color"
	"image/png"
	"log"
	"os"
	"strings"
)

const (
	format_separator = "::"
)

var (
	threshold     *int
	outfile       *string
	infile        *string
	format_string *string
	minBrightness *int
	maxBrightness *int
	imageWidth    *int
	imageHeight   *int

	advancedoptions *bool
)

func usage() {
	fmt.Println("Usage: schemer2 [FLAGS] -format [INPUTFORMAT]" + format_separator + "[OUTPUTFORMAT] -in [INPUTFILE] -out [OUTPUTFILE]")
}

func flags_usage() {
	usage()
	fmt.Printf("\n\n") // Spacing
	inputs_outputs()
	fmt.Printf("\n\n") // Spacing

	if !*advancedoptions {
		fmt.Println("Run with -help-advanced flag to show advanced options")
	} else {
		flag.PrintDefaults()
	}
}

func inputs_outputs() {
	inSupport := "Input formats:\n"
	outSupport := "Output formats:\n"
	for _, f := range formats {
		if f.output != nil {
			outSupport += strings.Join([]string{"    ", f.friendlyName, ":", f.flagName, "\n"}, " ")
		}

		if f.input != nil {
			inSupport += strings.Join([]string{"    ", f.friendlyName, ":", f.flagName, "\n"}, " ")
		}
	}
	// Special case for img output
	outSupport += "     Image output : img\n"

	fmt.Print(inSupport, "\n", outSupport)
}

func main() {
	threshold = flag.Int("threshold", 50, "Threshold for minimum color difference (image input only)")
	infile = flag.String("in", "", "Input file")
	outfile = flag.String("out", "", "File to write output to.")
	format_string = flag.String("format", "", "Format of input and output. Eg. 'image"+format_separator+"xterm'")
	minBrightness = flag.Int("minBright", 0, "Minimum brightness for colors (image input only)")
	maxBrightness = flag.Int("maxBright", 200, "Maximum brightness for colors (image input only)")
	imageHeight = flag.Int("height", 1080, "Height of output image")
	imageWidth = flag.Int("width", 1920, "Width of output image")
	advancedoptions = flag.Bool("help-advanced", false, "Show advanced command line options")

	flag.Usage = flags_usage
	flag.Parse()
	if *advancedoptions {
		flags_usage()
		os.Exit(1)
	}
	if *format_string == "" {
		fmt.Println("Input and output format must be specified using '-format' flag.")
		usage()
		os.Exit(2)
	}
	if *infile == "" {
		fmt.Println("Input file must be provided using '-in' flag.")
		usage()
		os.Exit(2)
	}
	if *minBrightness > 255 || *maxBrightness > 255 {
		fmt.Print("Minimum and maximum brightness must be an integer between 0 and 255.\n")
		os.Exit(2)
	}
	if *threshold > 255 {
		fmt.Print("Threshold should be an integer between 0 and 255.\n")
		os.Exit(2)
	}

	if *imageWidth < 100 || *imageHeight < 100 {
		log.Fatal("Minimum resolution of image output is 100x100")
	}

	// Determine format and filenames
	// And get colors from file using specified format
	if len(strings.SplitN(*format_string, format_separator, 2)) < 2 {
		fmt.Println("Invalid format string. Separate input and output formats with: '" + format_separator + "'")
		usage()
		os.Exit(2)
	}
	input_format := strings.SplitN(*format_string, format_separator, 2)[0]
	output_format := strings.SplitN(*format_string, format_separator, 2)[1]

	formatInMatch := false
	var colors []color.Color
	var err error
	for _, f := range formats {
		if input_format == f.flagName {
			if f.input == nil {
				fmt.Printf("Unrecognised input format: %v \n", input_format)
				return
			}
			colors, err = f.input(*infile)
			if err != nil {
				fmt.Print(err, "\n")
				return
			}
			formatInMatch = true
			break
		}
	}
	if !formatInMatch {
		fmt.Printf("Did not recognise format %v. \n", input_format)
		return
	}

	// Ensure there are 16 colors
	if len(colors) > 16 {
		colors = colors[:16]
	} else if len(colors) < 16 {
		// TODO: Should this just be a warning (for cases where only 8 colors are defined?)
		log.Fatal("Less than 16 colors. Aborting.")
	}

	// Output the configuration for terminal, or image
	if output_format != "img" {
		formatOutMatch := false
		for _, f := range formats {
			if output_format == f.flagName {
				if f.output == nil {
					fmt.Printf("Unrecognised output format: %v \n", output_format)
					return
				}
				result := f.output(colors)
				// If outfile is specified, write output to file
				// Otherwise, write to stdout.
				// TODO: Make it abundantly clear that the output is *only* the colors
				// and attempting to write directly to a config file will overwrite all other
				// data in the config file.
				if *outfile != "" {
					file, err := os.OpenFile(*outfile, os.O_CREATE|os.O_WRONLY, 0666)
					if err != nil {
						fmt.Println(err)
						os.Exit(1)
					}
					defer file.Close()
					err = file.Truncate(0)
					if err != nil {
						fmt.Println(err)
						os.Exit(1)
					}
					fmt.Fprint(file, result)
				} else {
					fmt.Printf(result)
				}
				formatOutMatch = true
				break
			}
		}
		if !formatOutMatch {
			fmt.Printf("Did not recognise format %v. \n", output_format)
		}
	} else {
		var file *os.File
		var err error
		if *outfile == "" {
			fmt.Println("Warning: Image output requested, yet no output file provided.")
			fmt.Println("Writing image data to /tmp/schemer_out.png")
			file, err = os.OpenFile("/tmp/schemer_out.png", os.O_CREATE|os.O_WRONLY, 0666)
		} else {
			file, err = os.OpenFile(*outfile, os.O_CREATE|os.O_WRONLY, 0666)
		}
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		img := imageFromColors(colors, *imageWidth, *imageHeight) // TODO

		png.Encode(file, img)
	}
}