From 04ba5b7e4e7da120a713ce02cdf5429b1dbf1327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 22 Sep 2021 23:55:48 +0200 Subject: [PATCH] show used images in CLI and bot; caption cmdline in bot --- cli/collages.ts | 4 ++++ cli/main.ts | 1 + tgbot/kollagen-bot/main.py | 28 +++++++++++++++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/cli/collages.ts b/cli/collages.ts index 61b5ec8..65bb3a5 100644 --- a/cli/collages.ts +++ b/cli/collages.ts @@ -22,6 +22,10 @@ export class ProxyImage implements CollageImage { } } + public get path(): string | null { + return this.filepath; + } + public get image(): Image { if (!this._image) { const image = canvasKit.MakeImageFromEncoded( diff --git a/cli/main.ts b/cli/main.ts index bce77f3..f7e3326 100644 --- a/cli/main.ts +++ b/cli/main.ts @@ -94,6 +94,7 @@ if (modeKey === "recursive") { images, ); mode.place(context, images, segments); + console.log(`Used: ${images.slice(0, segments.length).map((img) => img.path).join(", ")}`) } const output = args["output"]; diff --git a/tgbot/kollagen-bot/main.py b/tgbot/kollagen-bot/main.py index 21eb3c0..b3ef9b2 100644 --- a/tgbot/kollagen-bot/main.py +++ b/tgbot/kollagen-bot/main.py @@ -26,7 +26,7 @@ class KollagenBot: ) -> None: self.logger = logging.getLogger("kollagen") self.kollagen_path = kollagen_path - self.base_dir = base_dir + self.base_dir = os.path.abspath(base_dir) if base_dir else None self._init_parser() @@ -174,14 +174,36 @@ class KollagenBot: "-o", ntf.name, ] + self.logger.debug(f"Running: " + str(shell_cmd_line)) - subprocess.run( + result = subprocess.run( shell_cmd_line, check=True, capture_output=True, ) ntf.seek(0) - update.message.reply_photo(ntf) + + used_line = next( + ( + line + for line in result.stdout.decode("utf-8").splitlines() + if line.startswith("Used: ") + ), + "", + ).replace(f"{self.base_dir}/" if self.base_dir else "", "") + + caption = "" + caption += ( + f"`{' '.join(['/generate', *cmd_line])}`\n" if len(cmd_line) else "" + ) + caption += used_line.replace("_", "\\_") + caption = caption[:200] + + update.message.reply_photo( + ntf, + caption=caption, + parse_mode=ParseMode.MARKDOWN, + ) return True def tg_help(self, update: Update, context: CallbackContext):