Skip to content

Commit

Permalink
feat(xdi8-grep): search legacy entries
Browse files Browse the repository at this point in the history
  • Loading branch information
DGCK81LNN committed Apr 14, 2024
1 parent 66dd5ae commit d140fc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/locales/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand Down
32 changes: 24 additions & 8 deletions src/plugins/xdi8-grep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`
Expand Down Expand Up @@ -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),
Expand All @@ -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")
})
}

0 comments on commit d140fc7

Please sign in to comment.