summaryrefslogtreecommitdiff
path: root/io.go
blob: aab57654cb200772a480e1373cfd0991d1ce7d80 (plain)
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
/*
Copyright (C) 2019 Leo Tenenbaum

This file is part of AutoArt.

AutoArt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

AutoArt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with AutoArt.  If not, see <https://www.gnu.org/licenses/>.
*/

package main

// Some functions for dealing with user input

import (
	"bufio"
	"fmt"
	"github.com/pommicket/autoart/autoart"
	"strconv"
	"strings"
)

// Reads an int64 from a buffered reader, after giving the user the given prompt
// If the number that the user entered does not satisfy valid, the user will
// be prompted again. If the user enters nothing, the default will be used.
func readInt64(reader *bufio.Reader, prompt string,
	valid func(int64) bool, def int64) (int64, error) {
	fmt.Print(prompt)
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			return 0, err
		}
		line = strings.TrimSpace(line)
		if line == "" {
			return def, nil
		}
		num, err := strconv.ParseInt(line, 0, 64)
		if err != nil {
			fmt.Println("Please enter a number.")
			fmt.Print(prompt)
			continue
		}
		if !valid(num) {
			fmt.Println("Please enter a valid option.")
			fmt.Print(prompt)
			continue
		}
		return num, nil
	}
}

// Reads a bool from the reader, prompting the user until they enter y/n.
func readBool(reader *bufio.Reader, prompt string, def bool) (bool, error) {
	for {
		fmt.Print(prompt)
		line, err := reader.ReadString('\n')
		if err != nil {
			return false, err
		}
		line = strings.ToLower(strings.TrimSpace(line))
		if line == "" {
			return def, nil
		}
		switch line[0] {
		case 'y':
			return true, nil
		case 'n':
			return false, nil
		}
		fmt.Println("Please enter yes or no.")
	}
}

// Reads an autoart.Config
func readConf(reader *bufio.Reader, conf *autoart.Config) error {
	positive := func(i int64) bool { return i > 0 }
	functionLength, err := readInt64(reader, "Function length (default: 40)? ", positive, 40)
	if err != nil {
		return err
	}
	colorSpace, err := readInt64(reader, `Which color space should be used?
1. RGB
2. Grayscale
3. CMYK
4. HSV
5. YCbCr
Please enter a number between 1 and 5 (default: 1): `, func(i int64) bool {
		return i >= 1 && i <= 5
	}, 1)

	if err != nil {
		return err
	}
	alpha, err := readBool(reader, "Should an alpha channel be included (yes/no, default: no)? ", false)
	if err != nil {
		return err
	}

	rectifier, err := readInt64(reader, `How should out of range values be dealt with?
1. Modulo
2. Clamp
3. Sigmoid
Please enter 1, 2, or 3 (default: 1): `, func(i int64) bool {
		return i >= 1 && i <= 3
	}, 1)
	if err != nil {
		return err
	}

	coords, err := readInt64(reader, `Which coordinate system should be used?
1. x, y
2. r, theta
Please enter 1 or 2 (default: 1): `, func(i int64) bool {
		return i >= 1 && i <= 2
	}, 1)
	if err != nil {
		return err
	}

	conf.FunctionLength = int(functionLength)
	conf.ColorSpace = int(colorSpace - 1)
	conf.Alpha = alpha
	conf.Rectifier = int(rectifier - 1)
	conf.CoordinateSys = int(coords - 1)
	return nil
}

func readPaletteConf(reader *bufio.Reader, conf *autoart.PaletteConfig) error {
	positive := func(i int64) bool { return i > 0 }
	ncolors, err := readInt64(reader, "How many colors do you want (default: 10)? ", positive, 10)
	if err != nil {
		return err
	}
	alpha, err := readBool(reader, "Should an alpha channel be included (yes/no, default: no)? ", false)
	if err != nil {
		return err
	}
	functionLength, err := readInt64(reader, "Function length (default: 40)? ", positive, 40)
	if err != nil {
		return err
	}
	coords, err := readInt64(reader, `Which coordinate system should be used?
1. x, y
2. r, theta
Please enter 1 or 2 (default: 1): `, func(i int64) bool {
		return i >= 1 && i <= 2
	}, 1)
	if err != nil {
		return err
	}
	conf.NColors = int(ncolors)
	conf.Alpha = alpha
	conf.FunctionLength = int(functionLength)
	conf.CoordinateSys = int(coords - 1)
	return nil
}