forked from textiles-lab/knitout-backend-kniterate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaste-section.js
425 lines (403 loc) · 14 KB
/
waste-section.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/bin/sh
':'; //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
'use strict';
//------------------------------------
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let inFile, outFile, file, header;
let lines = [];
let defaultRollerAdvance = 400, defaultStitchNumber = 6, defaultSpeedNumber = 300;
let defaultWasteCarrier = '1', defaultDrawCarrier = '2', defaultCastonCarrier = '1';
let defaultCastonStyle = 1;
let rollerAdvance, stitchNumber, speedNumber;
let wasteCarrier, drawCarrier, castonCarrier;
let castonStyle;
let minN, maxN, wasteMin, wasteMax;
let width, wastePasses = 70;
let carriers = [], inCarriers = [];
// Create a promise based version of rl.question so we can use it in async functions
const question = (str) => new Promise(resolve => rl.question(str, resolve));
// all the steps involved, executed asynchronously
const steps = {
start: async () => {
inFile = await question(`Path to input file (press Enter to create waste section without file): `);
if (!inFile) {
minN = await question(`Min needle number: `);
minN = Number(minN);
maxN = await question(`Max needle number: `);
maxN = Number(maxN);
}
console.log(`\nROLLER ADVANCE: 400\nSTITCH NUMBER: 6\nSPEED NUMBER: 300\nWASTE YARN CARRIER: 1\nDRAW THREAD CARRIER: 2\nCAST-ON STYLE: closed tube\nCAST-ON CARRIER: 1`);
console.log(`\n^ **press Enter to skip any of the following prompts & use the respective default extension value (listed above) for the waste section**`);
console.log(`\nSETTINGS FOR WASTE SECTION:`);
return steps.rollerAdvance();
},
rollerAdvance: async () => {
rollerAdvance = await question(`Roller advance: `);
if (!rollerAdvance) rollerAdvance = defaultRollerAdvance;
return steps.stitchNumber();
},
stitchNumber: async () => {
stitchNumber = await question(`Stitch number: `);
if (!stitchNumber) stitchNumber = defaultStitchNumber;
return steps.speedNumber();
},
speedNumber: async () => {
speedNumber = await question(`Speed number: `);
if (!speedNumber) speedNumber = defaultSpeedNumber;
return steps.wasteCarrier();
},
wasteCarrier: async () => {
wasteCarrier = await question(`Carrier to use for waste yarn: `);
if (!wasteCarrier) wasteCarrier = defaultWasteCarrier;
carriers.push(wasteCarrier);
return steps.drawCarrier();
},
drawCarrier: async () => {
drawCarrier = await question(`Carrier to use for draw thread: `);
if (!drawCarrier) drawCarrier = defaultDrawCarrier;
if (!carriers.includes(drawCarrier)) carriers.push(drawCarrier);
return steps.castonStyle();
},
castonStyle: async () => {
castonStyle = await question(`\n*Cast-on style* --- OPTIONS:\n[0] Cast-on section already included\n[1] Closed tube\n[2] Open tube\nInput the number associated with your chosen style from the above list (or press Enter for default value): `);
if (castonStyle) {
castonStyle = Number(castonStyle);
} else castonStyle = defaultCastonStyle;
if (castonStyle === 0) return steps.readFile();
else return steps.castonCarrier();
},
castonCarrier: async () => {
castonCarrier = await question(`Carrier to use for cast-on: `);
if (!castonCarrier) castonCarrier = defaultCastonCarrier;
if (!carriers.includes(castonCarrier)) carriers.push(castonCarrier);
if (inFile) return steps.readFile();
else {
let additionalCarriers = await question(`If you will be using any other carriers besides those designated for waste yarn: ${wasteCarrier}, draw thread: ${drawCarrier}, and cast-on: ${castonCarrier}, please list them, separated by spaces (e.g. 1 2 3). Otherwise, just press Enter. `);
if (additionalCarriers) {
additionalCarriers = additionalCarriers.trim().split(' ');
for (let c in additionalCarriers) if (!carriers.includes(additionalCarriers[c])) carriers.push(additionalCarriers[c]);
}
return steps.parse();
}
},
readFile: async () => {
file = fs.readFileSync(inFile, { encoding: 'utf8'});
return steps.parse();
},
parse: async () => {
let wasteSection = [`x-roller-advance ${rollerAdvance}`, `x-stitch-number ${stitchNumber}`, `x-speed-number ${speedNumber}`];
let negCarriers = [], outCarriers = [];
if (file) {
lines = file.split('\n');
header = lines.splice(0, lines.findIndex(ln => ln.split(' ')[0] === 'in'));
let inCarrier = lines[0].split(' ')[1].charAt(0); //note: charAt is fine here since kniterate only has single-digit carriers //TODO: change if make waste-section.js for SWG too
lines.shift();
inCarriers = [inCarrier];
lines.map((ln, idx) => {
let info = ln.trim().split(' ');
if (info[0] === 'in' && info.length > 1) {
let c = info[1].charAt(0);
if (!inCarriers.includes(c)) {
lines.splice(idx, 1);
inCarriers.push(c);
if (!carriers.includes(c)) carriers.push(c);
}
}
});
} else {
// let carrierList = (machine.toLowerCase() === 'kniterate' ? '1 2 3 4 5 6' : '1 2 3 4 5 6 7 8 9 10'); //for now, assume machine is kniterate since SWGs don't *need* a waste section
header = [';!knitout-2', `;;Machine: Kniterate`, `;;Carriers: 1 2 3 4 5 6`];
}
let otherCarriers = carriers.filter(c => c !== wasteCarrier && c !== drawCarrier && c !== castonCarrier);
let wasteDir = '-', drawDir = '-', castonDir = '-';
if (inCarriers.includes(castonCarrier)) { //empty if !file, so will skip
for (let i = 0; i < lines.length; ++i) {
let info = lines[i].trim().split(' ');
if (info.length > 1 && !info[0].includes(';')) {
if (info[info.length - 1] === castonCarrier) {
if (info[1] === '+') {
castonDir = '-';
break;
} else if (info[1] === '-') {
castonDir = '+';
break;
}
}
}
}
}
if (drawCarrier === castonCarrier) {
castonDir === '+' ? drawDir = '-' : drawDir = '+';
} else {
if (inCarriers.includes(drawCarrier)) {
for (let i = 0; i < lines.length; ++i) {
let info = lines[i].trim().split(' ');
if (info.length > 1 && !info[0].includes(';')) {
if (info[info.length - 1] === drawCarrier) {
if (info[1] === '+') {
drawDir = '-';
break;
} else if (info[1] === '-') {
drawDir = '+';
break;
}
}
}
}
}
}
if (wasteCarrier === drawCarrier) {
drawDir === '+' ? wasteDir = '-' : wasteDir = '+';
} else if (wasteCarrier === castonCarrier) {
castonDir === '+' ? wasteDir = '-' : wasteDir = '+';
} else {
if (inCarriers.includes(wasteCarrier)) {
for (let i = 0; i < lines.length; ++i) {
let info = lines[i].trim().split(' ');
if (info.length > 1 && !info[0].includes(';')) {
if (info[info.length - 1] === wasteCarrier) {
if (info[1] === '+') {
wasteDir = '-';
break;
} else if (info[1] === '-') {
wasteDir = '+';
break;
}
}
}
}
}
}
if (drawDir === '-' && drawCarrier !== wasteCarrier) negCarriers.push(drawCarrier);
if (castonDir === '-' && castonCarrier !== wasteCarrier && castonCarrier !== drawCarrier) negCarriers.push(castonCarrier);
if (lines && otherCarriers.length) {
for (let c = 0; c < otherCarriers.length; ++c) {
for (let i = 0; i < lines.length; ++i) {
let info = lines[i].trim().split(' ');
if (info.length > 1 && !info[0].includes(';')) {
if (info[info.length - 1] === otherCarriers[c]) {
if (info[1] === '-') {
negCarriers.push(otherCarriers[c]);
break;
} else if (info[1] === '-') break;
}
}
}
}
}
let removeComment = (str) => {
if (str.includes(';')) return str.slice(0, str.indexOf(';'));
else return str;
};
if (file) {
let dir, carrier, rack;
findMinMax: for (let i = 0; i < lines.length; ++i) {
let info = lines[i].trim().split(' ');
if (info[0].charAt(0) !== ';' && info.length > 1) {
if (info[0] === 'rack') {
if (!rack || !dir) rack = removeComment(info[1]);
else if (rack !== info[1].charAt(0)) break findMinMax;
} else if (info[1] === '+' || info[1] === '-') {
if (!dir) {
dir = info[1];
minN = maxN = Number(removeComment(info[2].slice(1)));
carrier = info[info.length - 1];
} else {
if (info[1] === dir && removeComment(info[info.length - 1]) === carrier) {
let needle = Number(removeComment(info[2].slice(1)));
if (needle < minN) {
if (dir === '-') minN = needle;
else break findMinMax;
} else if (needle > maxN) {
if (dir === '+') maxN = needle;
else break findMinMax;
}
} else {
if (castonStyle !== 0 || removeComment(info[info.length - 1]) !== carrier) break findMinMax;
else {
for (let m = i; m < lines.length; ++m) {
info = lines[m].trim().split(' ');
if (info.length > 2) {
let needle = Number(removeComment(info[2].slice(1)));
if (info[1] === '+' || info[1] === '-') {
if (info[1] === dir || removeComment(info[info.length - 1]) !== carrier) break findMinMax;
else {
if (needle < minN) minN = needle;
if (needle > maxN) maxN = needle;
}
}
}
}
}
}
}
}
}
}
}
width = maxN - minN + 1;
if (width < 20) {
minN >= (20 - width) ? ((wasteMin = minN - (20 - width)), (wasteMax = max)) : ((wasteMin = minN), (wasteMax = maxN + (20-width)));
} else {
wasteMin = minN;
wasteMax = maxN;
}
let toDrop = [];
// initialize yarns
wasteSection.push(`;initialize yarns`);
for (let i = 0; i < carriers.length; ++i) {
wasteSection.push(`in ${carriers[i]}`);
let bed = 'f';
for (let n = wasteMin; n <= wasteMax; ++n) {
if (Math.abs(n) % carriers.length === i) {
wasteSection.push(`tuck + ${bed}${n} ${carriers[i]}`);
bed === 'f' ? bed = 'b' : bed = 'f';
} else {
if (n === wasteMax) wasteSection.push(`miss + ${bed}${n} ${carriers[i]}`);
}
if (i === 0) if (n < minN || n > maxN) toDrop.push(n);
}
bed = 'b';
for (let n = wasteMax; n >= wasteMin; --n) {
if (Math.abs(n) % carriers.length === i) {
wasteSection.push(`tuck - ${bed}${n} ${carriers[i]}`);
bed === 'f' ? bed = 'b' : bed = 'f';
} else {
if (n === wasteMin) wasteSection.push(`miss - ${bed}${n} ${carriers[i]}`);
}
}
if (negCarriers.includes(carriers[i])) {
let bed = 'f';
for (let n = wasteMin; n <= wasteMax; ++n) {
if (Math.abs(n) % carriers.length === i) {
wasteSection.push(`tuck + ${bed}${n} ${carriers[i]}`);
bed === 'f' ? bed = 'b' : bed = 'f';
} else {
if (n === wasteMax) wasteSection.push(`miss + ${bed}${n} ${carriers[i]}`);
}
if (i === 0) if (n < minN || n > maxN) toDrop.push(n);
}
}
}
// waste section
wasteSection.push(`;waste yarn section`);
for (let p = 0; p < wastePasses; ++p) {
if (p % 2 === 0) {
for (let n = wasteMin; n <= wasteMax; ++n) {
if (n % 2 === 0) {
wasteSection.push(`knit + f${n} ${wasteCarrier}`);
} else {
wasteSection.push(`knit + b${n} ${wasteCarrier}`);
}
}
} else {
for (let n = wasteMax; n >= wasteMin; --n) {
if (n % 2 === 0) {
wasteSection.push(`knit - b${n} ${wasteCarrier}`);
} else {
wasteSection.push(`knit - f${n} ${wasteCarrier}`);
}
}
}
}
for (let p = 0; p < 12; ++p) {
if (p % 2 === 0) {
for (let n = wasteMin; n <= wasteMax; ++n) {
wasteSection.push(`knit + f${n} ${wasteCarrier}`);
}
} else {
for (let n = wasteMax; n >= wasteMin; --n) {
wasteSection.push(`knit - b${n} ${wasteCarrier}`);
}
}
}
if (wasteDir === '+') {
for (let n = wasteMin; n <= wasteMax; ++n) {
wasteSection.push(`knit + f${n} ${wasteCarrier}`);
}
}
if (!inCarriers.includes(wasteCarrier) && wasteCarrier !== drawCarrier && wasteCarrier !== castonCarrier) {
if (wasteDir === '-') wasteSection.push(`out ${wasteCarrier}`); //*
else {
wasteSection.push(`miss + f${maxN + 4} ${wasteCarrier}`);
outCarriers.push(wasteCarrier);
}
}
// drop any extra needles if width < 20
if (toDrop.length) {
for (let n = 0; n < toDrop.length; ++n) {
wasteSection.push(`drop f${toDrop[n]}`);
}
}
// drop all needles on back bed
for (let n = wasteMin; n <= wasteMax; ++n) {
wasteSection.push(`drop b${n}`);
}
// draw thread
wasteSection.push(`;draw thread`);
if (drawDir === '+') {
for (let n = minN; n <= maxN; ++n) {
wasteSection.push(`knit + f${n} ${drawCarrier}`);
}
} else {
for (let n = maxN; n >= minN; --n) {
wasteSection.push(`knit - f${n} ${drawCarrier}`);
}
}
if (!inCarriers.includes(drawCarrier) && drawCarrier !== castonCarrier) {
if (drawDir === '-') wasteSection.push(`out ${drawCarrier}`); //*
else {
wasteSection.push(`miss + f${maxN + 4} ${drawCarrier}`);
outCarriers.push(drawCarrier);
}
}
if (castonStyle !== 0) {
wasteSection.push(`;cast-on`);
if (castonStyle === 1) {
wasteSection.push('rack 0.25'); // or 0.5 ? (visualizer)
if (castonDir === '+') {
for (let n = minN; n <= maxN; ++n) {
wasteSection.push(`knit + f${n} ${castonCarrier}`, `knit + b${n} ${castonCarrier}`);
}
} else {
for (let n = maxN; n >= minN; --n) {
wasteSection.push(`knit - f${n} ${castonCarrier}`, `knit - b${n} ${castonCarrier}`);
}
}
wasteSection.push(`rack 0`);
} else {
console.warn('//TODO: add support for other cast-on styles.');
}
if (!inCarriers.includes(castonCarrier)) {
if (castonDir === '-') wasteSection.push(`out ${castonCarrier}`); //*
else {
wasteSection.push(`miss + f${maxN + 4} ${castonCarrier}`);
outCarriers.push(castonCarrier);
}
}
}
if (outCarriers.length) {
for (let i = 0; i < outCarriers.length; ++i) {
wasteSection.push(`out ${outCarriers[i]}`);
}
}
lines = [...header, ...wasteSection, ...lines];
lines = lines.join('\n');
return steps.writeFile();
},
writeFile: async () => {
outFile = await question(`Filename for output knitout: `);
fs.writeFileSync(outFile, lines);
return steps.end();
},
end: async () => {
rl.close();
},
};
// Start the program
steps.start();