add cli params for recursive modes

This commit is contained in:
Tomáš Mládek 2021-09-20 22:56:02 +02:00
parent bfe739a487
commit e391b9b6bf
2 changed files with 38 additions and 17 deletions

View file

@ -4,12 +4,12 @@ import {
collageModeType, collageModeType,
DisplayCollageModeType, DisplayCollageModeType,
displayCollageModeType, displayCollageModeType,
isDisplayCollageModeType,
} from "../src/common/collages.ts"; } from "../src/common/collages.ts";
import { denoCollageModes, ProxyImage } from "./collages.ts"; import { denoCollageModes, ProxyImage } from "./collages.ts";
import { CollageConfig } from "../src/common/types.ts"; import { CollageConfig } from "../src/common/types.ts";
import { choice, shuffle } from "../src/common/utils.ts"; import { choice, shuffle } from "../src/common/utils.ts";
import { walkSync } from "https://deno.land/std@0.107.0/fs/mod.ts"; import { walkSync } from "https://deno.land/std@0.107.0/fs/mod.ts";
import { parseCollageModes, parseDisplayCollageModes } from "./util.ts";
const args = parse(Deno.args, { const args = parse(Deno.args, {
alias: { alias: {
@ -18,11 +18,14 @@ const args = parse(Deno.args, {
o: "output", o: "output",
m: "mode", m: "mode",
}, },
boolean: ["rr"],
default: { default: {
w: 640, w: 640,
h: 640, h: 640,
include: "*.png, *.jpg", include: "*.png, *.jpg",
output: "collage.png", output: "collage.png",
rl: 3,
rr: true
}, },
}); });
@ -61,18 +64,7 @@ const shuffledFiles = shuffle(Array.from(files));
const images: ProxyImage[] = shuffledFiles.map((file) => new ProxyImage(file)); const images: ProxyImage[] = shuffledFiles.map((file) => new ProxyImage(file));
const allModeKeys: DisplayCollageModeType[] = []; const allModeKeys = args["mode"] ? parseDisplayCollageModes(args["mode"]) : displayCollageModeType;
if (args["mode"]) {
(args["mode"] as string).split(",").map((m) => m.trim()).forEach((m) => {
if (isDisplayCollageModeType(m)) {
allModeKeys.push(m);
} else {
throw Error(`"${m}" is not a valid collage mode.`);
}
});
} else {
allModeKeys.push(...displayCollageModeType);
}
const canvas = createCanvas(args["width"], args["height"]); const canvas = createCanvas(args["width"], args["height"]);
const context = canvas.getContext("2d"); const context = canvas.getContext("2d");
@ -87,9 +79,9 @@ if (modeKey === "recursive") {
`Creating a recursive collage, choosing from ${shuffledFiles.length} files...`, `Creating a recursive collage, choosing from ${shuffledFiles.length} files...`,
); );
await denoCollageModes.recursiveDraw(context, images, { await denoCollageModes.recursiveDraw(context, images, {
modes: collageModeType, modes: args["rm"] ? parseCollageModes(args["rm"]) : collageModeType,
repeat: true, repeat: args["rr"],
level: 3, level: args["rl"],
}); });
} else { } else {
const mode = denoCollageModes.modes[modeKey]; const mode = denoCollageModes.modes[modeKey];

29
cli/util.ts Normal file
View file

@ -0,0 +1,29 @@
import { CollageModeType, DisplayCollageModeType, isCollageModeType, isDisplayCollageModeType } from "../src/common/collages.ts";
export function parseDisplayCollageModes(input: string): DisplayCollageModeType[] {
const result: DisplayCollageModeType[] = [];
input.split(",").map((m) => m.trim()).forEach((m) => {
if (isDisplayCollageModeType(m)) {
result.push(m);
} else {
throw Error(`"${m}" is not a valid collage mode.`);
}
});
return result;
}
export function parseCollageModes(input: string): CollageModeType[] {
const result: CollageModeType[] = [];
input.split(",").map((m) => m.trim()).forEach((m) => {
if (isCollageModeType(m)) {
result.push(m);
} else {
throw Error(`"${m}" is not a valid collage mode.`);
}
});
return result;
}