From d140fc72432cd93843cb614d8d1618ab38d37128 Mon Sep 17 00:00:00 2001 From: DGCK81LNN <54282183+DGCK81LNN@users.noreply.github.com> Date: Sun, 14 Apr 2024 22:51:01 +0800 Subject: [PATCH] feat(xdi8-grep): search legacy entries --- src/locales/zh.yml | 3 +++ src/plugins/xdi8-grep.ts | 32 ++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/locales/zh.yml b/src/locales/zh.yml index cf78565..8c5b570 100644 --- a/src/locales/zh.yml +++ b/src/locales/zh.yml @@ -21,10 +21,13 @@ commands: \v — 韵母 例如:“\c\g\v.H”可匹配形如 CGVRH 的希顶词。 将转义字母大写以选择补集,如 \C 匹配非声母。 + options: + legacy: 只搜索旧拼写 messages: no-result: 未找到符合条件的希顶词。 invalid-pattern: 无效的正则表达式。 result-footer: 共 {0} 条结果。 + result-footer-with-legacy: 共 {0} 条结果({1} 条旧拼写)。 xegoe: description: 希顶字母图片 usage: |- diff --git a/src/plugins/xdi8-grep.ts b/src/plugins/xdi8-grep.ts index c803ef8..433ffbc 100644 --- a/src/plugins/xdi8-grep.ts +++ b/src/plugins/xdi8-grep.ts @@ -44,7 +44,8 @@ export function apply(ctx: Context, config: Config) { checkUnknown: true, showWarning: true, }) - .action(({ session }, pattern) => { + .option("legacy", "-l", { fallback: false }) + .action(({ session, options }, pattern) => { let inCharClass = false function charClass(chars: string) { return inCharClass ? chars : `[${chars}]` @@ -73,17 +74,24 @@ export function apply(ctx: Context, config: Config) { entry.x.match(re) && !(Object.hasOwn(ahoFixes, entry.x) && !ahoFixes[entry.x].includes(entry.h)) ) - const resultCount = entries.length - if (!resultCount) return session.text(".no-result") - const more = resultCount > config.maxResults + if (!entries.length) return session.text(".no-result") - const regularEntries = [] - const legacyEntries = [] + let regularEntries = [] + let legacyEntries = [] for (const entry of entries) { if (entry.hh === "-" && entry.xh === "-") legacyEntries.push(entry) - else regularEntries.push(entry) + else if (!options.legacy) regularEntries.push(entry) + } + + if (options.legacy) { + if (!legacyEntries.length) return session.text(".no-result") + regularEntries = legacyEntries + legacyEntries = [] } + const resultCount = regularEntries.length + legacyEntries.length + const more = resultCount > config.maxResults + entries = [ ...samples(regularEntries, config.maxResults), ...samples(legacyEntries, config.maxResults - regularEntries.length), @@ -94,7 +102,15 @@ export function apply(ctx: Context, config: Config) { if (entry.n) line += `(${entry.n})` return line }) - lines.push((more ? "…" : "") + session.text(".result-footer", [resultCount])) + lines.push( + (more ? "…" : "") + + (legacyEntries.length + ? session.text(".result-footer-with-legacy", [ + resultCount, + legacyEntries.length, + ]) + : session.text(".result-footer", [resultCount])) + ) return lines.join("\n") }) }