From c20559442198807bdf02a5838012badbdf25f6d3 Mon Sep 17 00:00:00 2001 From: petzku Date: Sat, 22 Apr 2023 00:05:21 +0300 Subject: [PATCH] KaraTemplater: Fold generated output --- src/0x.KaraTemplater.moon | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/0x.KaraTemplater.moon b/src/0x.KaraTemplater.moon index c3ccc36..a8b11a1 100644 --- a/src/0x.KaraTemplater.moon +++ b/src/0x.KaraTemplater.moon @@ -976,6 +976,37 @@ apply_templates = (subs, lines, components, tenv) -> tenv.orgline = nil aegisub.progress.set 100 * i / #lines +fold_output = (subs) -> + parse_line_fold = (line) -> + return if not line.extra + + info = line.extra["_aegi_folddata"] + return if not info + + side, collapsed, id = info\match "^(%d+);(%d+);(%d+)$" + return {:side, :collapsed, :id} + + -- find a free fold ID to use + maxid = 0 + for i, line in ipairs subs + fold = parse_line_fold line + maxid = math.max(maxid, fold.id or 0) if fold + + -- find first and last lines of output + local firstfx, lastfx, fr, to + for i, line in ipairs subs + continue unless line.effect and line.effect == "fx" + fr, firstfx = i, line if not firstfx + to, lastfx = i, line + + -- generate fold between the two + firstfx.extra or= {} + lastfx.extra or= {} + firstfx.extra["_aegi_folddata"] = "0;1;#{maxid+1}" + lastfx.extra["_aegi_folddata"] = "1;1;#{maxid+1}" + subs[fr] = firstfx + subs[to] = lastfx + -- Entry point main = (subs, sel, active) -> math.randomseed os.time! @@ -1006,6 +1037,9 @@ main = (subs, sel, active) -> task 'Applying templates...' apply_templates subs, lines, components, tenv + task 'Folding output...' + fold_output subs + aegisub.set_undo_point 'apply karaoke template' remove_fx_main = (subs, _sel, _active) ->