Skip to content

Commit

Permalink
fix(search): cancel previous search processes when typing
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlindquist committed Sep 28, 2024
1 parent 4188b56 commit f72e40f
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 184 deletions.
99 changes: 57 additions & 42 deletions src/lib/file.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
import type { ExecaChildProcess } from "@johnlindquist/globals/types/execa"
import { Channel } from "../core/enum.js"

// TODO: Optimize, etc
// "kMDItemContentType = 'com.apple.application-bundle'"
let activeFileSearchProcess: ExecaChildProcess<string>
global.fileSearch = async (
name,
{ onlyin = "~", kind = "", kMDItemContentType = "" } = {}
name,
{ onlyin = "~", kind = "", kMDItemContentType = "" } = {}
) => {
let command = `/usr/bin/mdfind${
name ? ` -name ${name}` : ""
}${onlyin ? ` -onlyin ${onlyin}` : ``}${kind ? ` "kind:${kind}"` : ``}${
kMDItemContentType ? ` "kMDItemContentType:${kMDItemContentType}"` : ``
}`

let results = []

try {
results = (
await global.exec(command, {
windowsHide: true,
shell: true
})
).stdout
.split("\n")
.filter(Boolean)
} catch (e) {
warn(e)
results = []
}

return results
let command = `/usr/bin/mdfind${
name ? ` -name ${name}` : ""
}${onlyin ? ` -onlyin ${onlyin}` : ``}${
kind ? ` "kind:${kind}"` : ``
}${
kMDItemContentType
? ` "kMDItemContentType:${kMDItemContentType}"`
: ``
}`

let results = []

try {
if (activeFileSearchProcess) {
activeFileSearchProcess.kill()
}

activeFileSearchProcess = global.exec(command, {
windowsHide: true,
shell: true,
})

let { stdout } = await activeFileSearchProcess

results = stdout.split("\n").filter(Boolean)
activeFileSearchProcess = null
} catch (e) {
warn(e)
results = []
}

return results
}

global.getSelectedFile = async () => {
return await applescript(
String.raw`
return await applescript(
String.raw`
tell application "Finder"
set finderSelList to selection as alias list
end tell
Expand All @@ -46,11 +57,11 @@ global.getSelectedFile = async () => {
set AppleScript's text item delimiters to linefeed
finderSelList as text
end if`
)
)
}

global.getSelectedDir = async () => {
return await applescript(`
return await applescript(`
tell application "Finder"
if exists Finder window 1 then
set currentDir to target of Finder window 1 as alias
Expand All @@ -63,33 +74,37 @@ global.getSelectedDir = async () => {
}

global.setSelectedFile = async (filePath: string) => {
await applescript(`
await applescript(`
set aFile to (POSIX file "${filePath}") as alias
tell application "Finder" to select aFile
`)
}

global.copyPathAsImage = async (path) =>
await applescript(
String.raw`set the clipboard to (read (POSIX file "${path}") as JPEG picture)`
)
global.copyPathAsImage = async path =>
await applescript(
String.raw`set the clipboard to (read (POSIX file "${path}") as JPEG picture)`
)

global.copyPathAsPicture = copyPathAsImage

global.selectFolder = async (message: string = "Pick a folder:") => {
return await sendWaitLong(Channel.SELECT_FOLDER, message)
global.selectFolder = async (
message: string = "Pick a folder:"
) => {
return await sendWaitLong(Channel.SELECT_FOLDER, message)
}

global.selectFile = async (message: string = "Pick a file:") => {
return await sendWaitLong(Channel.SELECT_FILE, message)
global.selectFile = async (
message: string = "Pick a file:"
) => {
return await sendWaitLong(Channel.SELECT_FILE, message)
}

global.revealFile = async (filePath) => {
return sendWaitLong(Channel.REVEAL_FILE, filePath.trim())
global.revealFile = async filePath => {
return sendWaitLong(Channel.REVEAL_FILE, filePath.trim())
}

global.revealInFinder = async (filePath) => {
return sendWaitLong(Channel.REVEAL_FILE, filePath.trim())
global.revealInFinder = async filePath => {
return sendWaitLong(Channel.REVEAL_FILE, filePath.trim())
}

export {}
127 changes: 68 additions & 59 deletions src/main/file-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,86 @@
// Trigger: .
// Pass: true

import { keywordInputTransformer, isMac } from "../core/utils.js"
import {
keywordInputTransformer,
isMac,
} from "../core/utils.js"
import { actionFlags } from "./common.js"

let flags = {}
for (let flag of actionFlags) {
flags[flag.name] = flag
flags[flag.name] = flag
}

let pleaseType = [
{
name: "Type at least 3 characters",
info: true
}
{
name: "Type at least 3 characters",
info: true,
},
]

let transformer = keywordInputTransformer(arg?.keyword)
let selectedFile = await arg(
{
preventCollapse: true,
input: arg?.pass ? arg.pass : arg?.keyword ? `${arg.keyword} ` : "",
...(!arg?.pass && { initialChoices: pleaseType }),
onMenuToggle: async (input, state) => {
if (state.flag) {
setPlaceholder("Select Action")
setEnter("Submit")
} else {
setPlaceholder("Search Files")
setEnter("Actions")
}
},
onSubmit: async (input, state) => {
if (!state?.flag) {
await setFlagValue(state?.focused)
return preventSubmit
}
},
placeholder: "Search Files",
enter: "Actions",
shortcuts: [
{
key: "right"
}
],
resize: true,
flags
},
async (input) => {
input = transformer(input)
{
preventCollapse: true,
input: arg?.pass
? arg.pass
: arg?.keyword
? `${arg.keyword} `
: "",
...(!arg?.pass && { initialChoices: pleaseType }),
onMenuToggle: async (input, state) => {
if (state.flag) {
setPlaceholder("Select Action")
setEnter("Submit")
} else {
setPlaceholder("Search Files")
setEnter("Actions")
}
},
onSubmit: async (input, state) => {
if (!state?.flag) {
await setFlagValue(state?.focused)
return preventSubmit
}
},
placeholder: "Search Files",
enter: "Actions",
shortcuts: [
{
key: "right",
},
],
resize: true,
flags,
},
async input => {
input = transformer(input)

if (!input || input?.length < 3) {
return pleaseType
}
let files = await fileSearch(input)
if (!input || input?.length < 3) {
return pleaseType
}
let files = await fileSearch(input)

if (files.length === 0) {
return [
{
name: `No results found for ${input}`,
info: true
}
]
}
return files.map((p) => {
return {
name: path.basename(p),
description: p,
drag: p,
value: p
}
})
}
if (files.length === 0) {
return [
{
name: `No results found for ${input}`,
info: true,
},
]
}
return files.map(p => {
return {
name: path.basename(p),
description: p,
drag: p,
value: p,
}
})
}
)

await actionFlags.find((f) => flag?.[f.name])?.action?.(selectedFile)
await actionFlags
.find(f => flag?.[f.name])
?.action?.(selectedFile)
Loading

0 comments on commit f72e40f

Please sign in to comment.