|
| 1 | +import { |
| 2 | + FileFinder, |
| 3 | + type DirItem, |
| 4 | + type DirSearchResult, |
| 5 | + type FileItem, |
| 6 | + type GrepCursor, |
| 7 | + type GrepMatch, |
| 8 | + type GrepResult, |
| 9 | + type InitOptions, |
| 10 | + type MixedItem, |
| 11 | + type MixedSearchResult, |
| 12 | + type SearchResult, |
| 13 | +} from "@ff-labs/fff-bun" |
| 14 | + |
| 15 | +export type Result<T> = { ok: true; value: T } | { ok: false; error: string } |
| 16 | + |
| 17 | +export type Init = InitOptions |
| 18 | + |
| 19 | +export interface Search { |
| 20 | + items: FileItem[] |
| 21 | + scores: SearchResult["scores"] |
| 22 | + totalMatched: number |
| 23 | + totalFiles: number |
| 24 | +} |
| 25 | + |
| 26 | +export interface DirSearch { |
| 27 | + items: DirItem[] |
| 28 | + scores: DirSearchResult["scores"] |
| 29 | + totalMatched: number |
| 30 | + totalDirs: number |
| 31 | +} |
| 32 | + |
| 33 | +export interface MixedSearch { |
| 34 | + items: MixedItem[] |
| 35 | + scores: MixedSearchResult["scores"] |
| 36 | + totalMatched: number |
| 37 | + totalFiles: number |
| 38 | + totalDirs: number |
| 39 | +} |
| 40 | + |
| 41 | +export type File = FileItem |
| 42 | +export type Directory = DirItem |
| 43 | +export type Mixed = MixedItem |
| 44 | +export type Cursor = GrepCursor | null |
| 45 | +export type Hit = GrepMatch |
| 46 | + |
| 47 | +export interface Grep { |
| 48 | + items: GrepResult["items"] |
| 49 | + totalMatched: number |
| 50 | + totalFilesSearched: number |
| 51 | + totalFiles: number |
| 52 | + filteredFileCount: number |
| 53 | + nextCursor: Cursor |
| 54 | + regexFallbackError?: string |
| 55 | +} |
| 56 | + |
| 57 | +export interface Picker { |
| 58 | + destroy(): void |
| 59 | + isScanning(): boolean |
| 60 | + waitForScan(timeoutMs?: number): Promise<Result<boolean>> |
| 61 | + refreshGitStatus(): Result<number> |
| 62 | + fileSearch( |
| 63 | + query: string, |
| 64 | + opts?: { |
| 65 | + currentFile?: string |
| 66 | + pageIndex?: number |
| 67 | + pageSize?: number |
| 68 | + }, |
| 69 | + ): Result<Search> |
| 70 | + glob( |
| 71 | + pattern: string, |
| 72 | + opts?: { |
| 73 | + currentFile?: string |
| 74 | + pageIndex?: number |
| 75 | + pageSize?: number |
| 76 | + }, |
| 77 | + ): Result<Search> |
| 78 | + directorySearch( |
| 79 | + query: string, |
| 80 | + opts?: { |
| 81 | + currentFile?: string |
| 82 | + pageIndex?: number |
| 83 | + pageSize?: number |
| 84 | + }, |
| 85 | + ): Result<DirSearch> |
| 86 | + mixedSearch( |
| 87 | + query: string, |
| 88 | + opts?: { |
| 89 | + currentFile?: string |
| 90 | + pageIndex?: number |
| 91 | + pageSize?: number |
| 92 | + }, |
| 93 | + ): Result<MixedSearch> |
| 94 | + grep( |
| 95 | + query: string, |
| 96 | + opts?: { |
| 97 | + mode?: "plain" | "regex" | "fuzzy" |
| 98 | + maxMatchesPerFile?: number |
| 99 | + timeBudgetMs?: number |
| 100 | + beforeContext?: number |
| 101 | + afterContext?: number |
| 102 | + cursor?: Cursor |
| 103 | + pageSize?: number |
| 104 | + }, |
| 105 | + ): Result<Grep> |
| 106 | + trackQuery(query: string, file: string): Result<boolean> |
| 107 | + getHistoricalQuery(offset: number): Result<string | null> |
| 108 | +} |
| 109 | + |
| 110 | +export function available() { |
| 111 | + return FileFinder.isAvailable() |
| 112 | +} |
| 113 | + |
| 114 | +export function create(opts: Init): Result<Picker> { |
| 115 | + const made = FileFinder.create(opts) |
| 116 | + if (!made.ok) return made |
| 117 | + const pick = made.value |
| 118 | + return { |
| 119 | + ok: true, |
| 120 | + value: { |
| 121 | + destroy: () => pick.destroy(), |
| 122 | + isScanning: () => pick.isScanning(), |
| 123 | + waitForScan: (timeoutMs) => pick.waitForScan(timeoutMs), |
| 124 | + refreshGitStatus: () => pick.refreshGitStatus(), |
| 125 | + fileSearch: (query, next) => pick.fileSearch(query, next), |
| 126 | + glob: (pattern, next) => pick.glob(pattern, next), |
| 127 | + directorySearch: (query, next) => pick.directorySearch(query, next), |
| 128 | + mixedSearch: (query, next) => pick.mixedSearch(query, next), |
| 129 | + grep: (query, next) => pick.grep(query, next), |
| 130 | + trackQuery: (query, file) => pick.trackQuery(query, file), |
| 131 | + getHistoricalQuery: (offset) => pick.getHistoricalQuery(offset), |
| 132 | + }, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +export * as Fff from "./fff.bun" |
0 commit comments