79 lines
2.6 KiB
Lua
79 lines
2.6 KiB
Lua
|
|
local lang = jarvis.context.language
|
|||
|
|
local phrase = (jarvis.context.phrase or ""):lower()
|
|||
|
|
|
|||
|
|
local triggers = {
|
|||
|
|
"найди документ", "найди файл", "поиск файла", "ищи файл", "где файл",
|
|||
|
|
"find file", "search file", "where is file", "locate file",
|
|||
|
|
"знайди файл", "пошук файлу", "де файл",
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
local query = phrase
|
|||
|
|
for _, t in ipairs(triggers) do
|
|||
|
|
local start, finish = string.find(query, t, 1, true)
|
|||
|
|
if start == 1 then
|
|||
|
|
query = query:sub(finish + 1)
|
|||
|
|
break
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
query = query:gsub("^%s+", ""):gsub("%s+$", "")
|
|||
|
|
|
|||
|
|
if query == "" then
|
|||
|
|
jarvis.log("warn", "file_search: empty query (phrase=" .. phrase .. ")")
|
|||
|
|
jarvis.system.notify(
|
|||
|
|
lang == "ru" and "Поиск файлов" or "File search",
|
|||
|
|
lang == "ru" and "Что искать?" or "What to search for?"
|
|||
|
|
)
|
|||
|
|
jarvis.audio.play_error()
|
|||
|
|
return { chain = false }
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local userprofile = jarvis.system.env("USERPROFILE") or "C:\\Users\\Public"
|
|||
|
|
local roots = {
|
|||
|
|
userprofile .. "\\Desktop",
|
|||
|
|
userprofile .. "\\Documents",
|
|||
|
|
userprofile .. "\\Downloads",
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
local escaped = query:gsub("'", "''")
|
|||
|
|
local roots_arg = "'" .. table.concat(roots, "','") .. "'"
|
|||
|
|
local ps = string.format(
|
|||
|
|
[[$ErrorActionPreference='SilentlyContinue'; $r=@(%s); $q='*%s*'; $hits=Get-ChildItem -Path $r -Filter $q -Recurse -Depth 3 -File | Select-Object -First 5; foreach($h in $hits){ Write-Output $h.FullName }]],
|
|||
|
|
roots_arg, escaped
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
local cmd = string.format('powershell -NoProfile -ExecutionPolicy Bypass -Command "%s"', ps:gsub('"', '\\"'))
|
|||
|
|
jarvis.log("info", "file_search query='" .. query .. "'")
|
|||
|
|
|
|||
|
|
local res = jarvis.system.exec(cmd)
|
|||
|
|
|
|||
|
|
if not res.success then
|
|||
|
|
jarvis.log("error", "file_search exec failed: " .. tostring(res.stderr))
|
|||
|
|
jarvis.audio.play_error()
|
|||
|
|
return { chain = false }
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local stdout = res.stdout or ""
|
|||
|
|
local lines = {}
|
|||
|
|
for line in stdout:gmatch("[^\r\n]+") do
|
|||
|
|
if line ~= "" then table.insert(lines, line) end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
if #lines == 0 then
|
|||
|
|
jarvis.system.notify(
|
|||
|
|
lang == "ru" and "Поиск файлов" or "File search",
|
|||
|
|
(lang == "ru" and "Не найдено: " or "Not found: ") .. query
|
|||
|
|
)
|
|||
|
|
jarvis.audio.play_not_found()
|
|||
|
|
else
|
|||
|
|
local first = lines[1]
|
|||
|
|
local count = #lines
|
|||
|
|
local title = lang == "ru" and "Найдено файлов: " or "Files found: "
|
|||
|
|
jarvis.system.notify(title .. count, first)
|
|||
|
|
|
|||
|
|
jarvis.log("info", "file_search opening: " .. first)
|
|||
|
|
jarvis.system.exec(string.format('explorer.exe /select,"%s"', first))
|
|||
|
|
jarvis.audio.play_ok()
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
return { chain = false }
|