1- import type { MatchData } from "fast-fuzzy" ;
2- import { Searcher } from "fast-fuzzy" ;
31import { parseArgs } from "util" ;
4- import { servantsCache } from "~/cache" ;
52import { describeServant } from "~/utils/describeServant" ;
6- import { col , log , logger , createTimer } from "~/utils/logger" ;
3+ import { log , logger , createTimer } from "~/utils/logger" ;
4+ import {
5+ createServantSearcher ,
6+ prettyPrintMatch
7+ } from "~/utils/ServantSearcher" ;
78
89const timer = createTimer ( ) ;
910const args = parseArgs ( {
@@ -31,17 +32,6 @@ const textHelp = `Search Servant\n\nUsage:
3132 --alt Search alternative Names (enabled by default)
3233 --no-alt Do not search alternative Names` ;
3334
34- function prettyPrintMatch ( match : MatchData < unknown > ) {
35- const text = match . original ;
36- const { index, length } = match . match ;
37- const end = index + length ;
38- const before = text . slice ( 0 , index ) ;
39- const matched = text . slice ( index , end ) ;
40- const after = text . substring ( end ) ;
41-
42- return `${ before } ${ col . bold ( col . redBright ( matched ) ) } ${ after } ` ;
43- }
44-
4535async function main ( ) {
4636 // DEBUG
4737 if ( args . values . verbose ) logger . setLogLevel ( "Debug" ) ;
@@ -66,15 +56,7 @@ async function main() {
6656 }
6757
6858 // create searcher
69- const servantsList = await servantsCache . read ( ) ;
70- const searcher = new Searcher ( servantsList , {
71- keySelector : candidate =>
72- args . values . alt
73- ? [ candidate . name ] . concat ( candidate . names )
74- : [ candidate . name ] ,
75- returnMatchData : true ,
76- threshold
77- } ) ;
59+ const searcher = await createServantSearcher ( args . values . alt , threshold ) ;
7860
7961 // handle queries
8062 for ( const query of args . positionals ) {
0 commit comments