Skip to content

Commit

Permalink
fix(settings): fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Mar 7, 2023
1 parent d9ce5f7 commit 8f21d83
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
12 changes: 3 additions & 9 deletions addons/launcher/src/renderer/LauncherList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,16 @@ let isEditing: boolean = $ref(false)
let keyword = $ref('')
const keywords = $computed(() => {
return keyword.trim().toLowerCase().split(/\s+/)
.map(item => item.trim())
.filter(Boolean)
return commas.helper.getWords(keyword)
})
const filteredLaunchers = $computed(() => {
return launchers.filter(launcher => {
if (isCollapsed) {
if (!getTerminalTabByLauncher(launcher)) return false
}
if (keywords.length) {
const matched = keywords.every(
item => Object.values(launcher).join(' ').toLowerCase().includes(item),
)
if (!matched) return false
}
const matched = commas.helper.matches(Object.values(launcher), keywords)
if (!matched) return false
return true
})
})
Expand Down
2 changes: 1 addition & 1 deletion addons/settings/src/renderer/SettingsPane.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const groups = $computed(() => {
key,
name,
from,
rows: keyword ? rows.filter(row => row.key.includes(keyword)) : rows,
rows: rows.filter(row => commas.helper.matches([row.key, commas.remote.translate(row.label)], keyword)),
}
}).filter(group => group.rows.length)
})
Expand Down
3 changes: 3 additions & 0 deletions api/modules/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mix, toRGBA, toCSSHEX } from '../../src/shared/color'
import { deepRef, surface, useAsyncComputed, watchBaseEffect } from '../../src/shared/compositions'
import { createIDGenerator, iterate, reuse } from '../../src/shared/helper'
import { getWords, matches } from '../../src/shared/match'
import { omitHome, resolveHome } from '../../src/shared/terminal'

export {
Expand All @@ -16,4 +17,6 @@ export {
toRGBA,
toCSSHEX,
mix,
getWords,
matches,
}
12 changes: 12 additions & 0 deletions src/shared/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function getWords(text: string) {
return text.trim().toLowerCase().split(/\s+/)
.map(item => item.trim())
.filter(Boolean)
}

export function matches(source: string | string[], keywords: string | string[]) {
const words = Array.isArray(keywords) ? keywords : getWords(keywords)
if (!words.length) return true
const text = (Array.isArray(source) ? source.join(' ') : source).toLowerCase()
return words.every(item => text.includes(item))
}

0 comments on commit 8f21d83

Please sign in to comment.