-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSICUCommandLine.lua
220 lines (172 loc) · 6.21 KB
/
RSICUCommandLine.lua
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
--[[--
@script RSICUCommandLine
@author RiskoZoSlovenska
@date Jan 2021
@license MIT
(idk)
A command-line REPL interface for the RSICU library.
]]
local rsicu = require("rsicu")
local vips = require("vips")
local Image = vips.Image
-- idk how to do this lol
local table = table
local string = string
local ipairs = ipairs
local pcall = pcall
local write, read = io.write, io.read
local writef = function(...) write(string.format(...)) end --- Shortcut for io.write(string.format())
local WELCOME
do
local TITLE = "Welcome to RZS's Simple Image Cleaner Command Line v4.1!\n"
WELCOME = TITLE .. string.rep("-", #TITLE - 1) .. "\n" -- Make the line as long as the title
end
local IMAGE_PROMPT = "\nInput the directory of the image which you want to process: "
local IMAGE_FAILED_ERROR = "Image %q cannot be found or opened (%s), please try again: "
local OPERATIONS_INFO = rsicu.getAvailableOperations()
local OPERATIONS_PROMPT
do -- Concat all the available operations into one string
local operationsListString = ""
for _, operation in ipairs(OPERATIONS_INFO) do
operationsListString = operationsListString .. string.format(
"\t%q (%s)\n",
operation.key,
operation.name
)
end
OPERATIONS_PROMPT = string.format("\nInput the desired operations, space-separated, in order. Options are:\n%s", operationsListString)
end
local INVALID_OPERATION_ERROR = "Invalid operation %q, please try again: "
local NUM_PROMPT_TEMPLATE = "\nInput %s: %s (%s to %s inclusive, %s): "
local INVALID_NUM_ERROR = "Invalid number %q, please try again: "
local SAVING_NOTIFICATION = "\nSaving image as: %q"
local SAVE_SUCCESS_NOTIFICATION = "\nImage saved successfully.\n"
local BAD_FILENAME_ERROR = "Bad filename %q"
local SAVE_ERROR_TEMPLATE = "!!! Error writing image to file: %s"
local CONTINUE_PROMPT = "\nPress Enter to repeat the operation"
--[[--
Repeatedly prompts the user to input a list of operation keys until a valid one is provided.
@return string[] a list of keys of operations to be performed upon an image
]]
local function loadOperationKeys()
write(OPERATIONS_PROMPT)
while true do
local input = read("*l")
local operationKeys = {}
for key in string.gmatch(input, "%w+") do
table.insert(operationKeys, key)
end
local isValid, invalidKey = rsicu.checkOperationKeys(operationKeys)
if isValid then
return operationKeys
else
writef(INVALID_OPERATION_ERROR, invalidKey)
end
end
end
--[[--
Repeatedly prompts the user to input a number, until a valid one (on that is within provided specifications) is provided.
@param string operationName the name of the operation which this arg is for
@param table paramInfo a table with desc, range and isInt fields which constrain the arg
@return number the inputted number, quaranteed to be within the supplied param infos
]]
local function loadNumber(operationName, paramInfo)
writef(
NUM_PROMPT_TEMPLATE,
operationName, paramInfo.desc, paramInfo.range[1], paramInfo.range[2], paramInfo.isInt and "integer" or "number"
)
while true do
local input = read("*l")
local value = tonumber(input)
if rsicu.checkArg(value, paramInfo) then
return value
else
writef(INVALID_NUM_ERROR, tostring(input))
end
end
end
--[[--
Performs several @{loadNumber} prompts to load all the required operation arguments, in order.
@param string[] operationKeys the list of keys of all the operations to be performed on the image
@return number[][] a 2D array of arguments, with indexes corresponding to the operation's position
in the input array
]]
local function loadOperationArgs(operationKeys)
local allArgs = {}
for _, key in ipairs(operationKeys) do
local operationInfo = OPERATIONS_INFO[key]
local operationArgs = {}
for _, paramInfo in ipairs(operationInfo.params) do
table.insert(operationArgs, loadNumber(operationInfo.name, paramInfo))
end
table.insert(allArgs, operationArgs)
end
return allArgs
end
--[[--
Repeatedly prompts the user for an image filename, until a valid one is provided.
@return Image an sRGB interpretation of the loaded image
@return string the name of the loaded image
]]
local function loadImage()
write(IMAGE_PROMPT)
while true do
local filename = read("*l")
local success, res = pcall(Image.new_from_file, filename)
if success then
return res:colourspace("srgb"), filename
else
local loadErrMsg = res:match(".+:%s*(.-)%s*$") or res
writef(IMAGE_FAILED_ERROR, tostring(filename), loadErrMsg)
end
end
end
--[[--
Saves the image to the same directory under a new file name in the format
"root tag.ext", where "root.ext" is the original file name and tag is a
string returned by rsicu.getOperationsTag.
@param Image image the image to save
@param string filename the file's original file name
@param string[] operationKeys the list of keys of all the operations performed on the image
@param number[][] operationsArgs the list of arguments used to perform the operations on the image
]]
local function saveImage(image, filename, operationKeys, operationsArgs)
local root, ext = filename:match("(.+)(%..-)$")
if not root then
writef(BAD_FILENAME_ERROR, tostring(filename))
return
end
local newFilename = root .. " " .. rsicu.getOperationsTag(operationKeys, operationsArgs) .. ext
writef(SAVING_NOTIFICATION, newFilename)
local success, err = pcall(image.write_to_file, image, newFilename)
if not success then
writef(SAVE_ERROR_TEMPLATE, err)
else
write(SAVE_SUCCESS_NOTIFICATION)
end
end
--[[--
Main program.
]]
local function main()
write(WELCOME)
local rawImage, filename = loadImage()
local operationKeys = loadOperationKeys()
local numOfParams = rsicu.countParams(operationKeys)
while true do
local operationArgs = loadOperationArgs(operationKeys)
local processedImage = rsicu.processImage(rawImage, operationKeys, operationArgs)
saveImage(processedImage, filename, operationKeys, operationArgs)
-- Discard this image and gc it so it can be opened by other programs
--processedImage:remove() -- This could possibly do it, but calling it ends the program (idk why)
processedImage = nil
collectgarbage()
collectgarbage()
-- If no args were needed, pause so we don't perform the operation over and over
if numOfParams < 1 then
write(CONTINUE_PROMPT)
read() -- Wait for enter
end
end
end
main()