From b0509920b4c28f2e933ae16ddabed1489c4359ea Mon Sep 17 00:00:00 2001
From: Tulir Asokan <tulir@maunium.net>
Date: Sun, 13 Sep 2020 17:29:08 +0300
Subject: [PATCH] Add support for ordering stickers in custom packs

---
 README.md       | 2 ++
 sticker/pack.py | 8 +++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 5dc9cc0..8c2666b 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,8 @@ Notes:
 1. Create a directory with your sticker images.
    * The file name (excluding extension) will be used as the caption.
    * The directory name will be used as the pack name/ID.
+   * If you want the stickers to appear in a specific order, prefix them with `<number>-`,
+     e.g. `01-Cat.png`. The number and dash won't be included in the caption.
 2. Run `sticker-pack <pack directory>`.
    * If you want to override the pack displayname, pass `--title <custom title>`.
 3. Copy `<pack directory>/pack.json` to `web/packs/your-pack-name.json`.
diff --git a/sticker/pack.py b/sticker/pack.py
index 9959d8e..f35665e 100644
--- a/sticker/pack.py
+++ b/sticker/pack.py
@@ -42,7 +42,7 @@ async def main(args: argparse.Namespace) -> None:
     else:
         old_stickers = {sticker["id"]: sticker for sticker in pack["stickers"]}
         pack["stickers"] = []
-    for file in os.listdir(args.path):
+    for file in sorted(os.listdir(args.path)):
         if file.startswith("."):
             continue
         path = os.path.join(args.path, file)
@@ -60,6 +60,12 @@ async def main(args: argparse.Namespace) -> None:
             continue
         print(f"Processing {file}", end="", flush=True)
         name = os.path.splitext(file)[0]
+
+        # If the name starts with "number-", remove the prefix
+        name_split = name.split("-", 1)
+        if len(name_split) == 2 and name_split[0].isdecimal():
+            name = name_split[1]
+
         sticker_id = f"sha256:{sha256(image_data).hexdigest()}"
         print(".", end="", flush=True)
         if sticker_id in old_stickers:
-- 
GitLab