Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added advanced mdfind filtering options #1235

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ describe(ApplicationSearch, () => {
getExtensionSettingKey("ApplicationSearch", "windowsFileExtensions"),
getExtensionSettingKey("ApplicationSearch", "includeWindowsStoreApps"),
getExtensionSettingKey("ApplicationSearch", "macOsFolders"),
getExtensionSettingKey("ApplicationSearch", "mdfindFilterOption"),
]);
});
});
3 changes: 3 additions & 0 deletions src/main/Extensions/ApplicationSearch/ApplicationSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ApplicationSearch implements Extension {
getExtensionSettingKey("ApplicationSearch", "windowsFileExtensions"),
getExtensionSettingKey("ApplicationSearch", "includeWindowsStoreApps"),
getExtensionSettingKey("ApplicationSearch", "macOsFolders"),
getExtensionSettingKey("ApplicationSearch", "mdfindFilterOption"),
];
}

Expand All @@ -69,13 +70,15 @@ export class ApplicationSearch implements Extension {
openApplication: "Open application",
openApplicationAsAdministrator: "Open application as administrator",
copyFilePathToClipboard: "Copy file path to clipboard",
advanced: "Advanced",
},
"de-CH": {
extensionName: "Anwendungssuche",
searchResultItemDescription: "Anwendung",
openApplication: "Anwendung starten",
openApplicationAsAdministrator: "Anwendung als Administrator starten",
copyFilePathToClipboard: "Dateipfad in Zwischenablage kopieren",
advanced: "Erweitert",
},
};
}
Expand Down
1 change: 1 addition & 0 deletions src/main/Extensions/ApplicationSearch/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Settings {
"/Applications",
join(this.app.getPath("home"), "Applications"),
],
mdfindFilterOption: "kMDItemKind=='Application'",
includeWindowsStoreApps: true,
windowsFileExtensions: ["lnk"],
windowsFolders: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class MacOsApplicationRepository implements ApplicationRepository {
}

private async getAllFilePaths(): Promise<string[]> {
return (await this.commandlineUtility.executeCommand(`mdfind "kMDItemKind == 'Application'"`))
return (await this.commandlineUtility.executeCommand(`mdfind "${this.getMdFindFilterOption()}"`))
.split("\n")
.map((filePath) => normalize(filePath).trim())
.filter((filePath) => filePath.endsWith(".app"))
Expand All @@ -43,6 +43,20 @@ export class MacOsApplicationRepository implements ApplicationRepository {
.filter((filePath) => ![".", ".."].includes(filePath));
}

private getMdFindFilterOption() {
const filterOptions: Record<string, string> = {
"kind:application": "kind:application",
"kMDItemKind=='Application'": "kMDItemKind == 'Application'",
"kMDItemContentType=='com.apple.application-bundle'":
"kMDItemContentType == 'com.apple.application-bundle'",
};

return (
filterOptions[this.settings.getValue<string>("mdfindFilterOption")] ??
filterOptions[<string>this.settings.getDefaultValue("mdfindFilterOption")]
);
}

private filterFilePathByConfiguredFolders(filePath: string): boolean {
return this.settings.getValue<string[]>("macOsFolders").some((folderPath) => filePath.startsWith(folderPath));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useContextBridge, useExtensionSetting } from "@Core/Hooks";
import { Setting } from "@Core/Settings/Setting";
import { SettingGroup } from "@Core/Settings/SettingGroup";
import { SettingGroupList } from "@Core/Settings/SettingGroupList";
import { Button, Input, Tooltip } from "@fluentui/react-components";
import { Button, Dropdown, Input, Option, Tooltip } from "@fluentui/react-components";
import { AddRegular, DismissRegular, FolderRegular } from "@fluentui/react-icons";
import { useState } from "react";
import { useTranslation } from "react-i18next";

export const MacOsSettings = () => {
const { contextBridge } = useContextBridge();
Expand All @@ -12,11 +14,18 @@ export const MacOsSettings = () => {

const [newFolder, setNewFolder] = useState<string>("");

const { t } = useTranslation(`extension[${extensionId}]`);

const { value: folders, updateValue: setFolders } = useExtensionSetting<string[]>({
extensionId,
key: "macOsFolders",
});

const { value: mdfindFilterOption, updateValue: updateMdfindFilterOption } = useExtensionSetting<string>({
extensionId,
key: "mdfindFilterOption",
});

const removeFolder = async (indexToRemove: number) => {
await setFolders(folders.filter((_, index) => index !== indexToRemove));
};
Expand Down Expand Up @@ -82,6 +91,29 @@ export const MacOsSettings = () => {
/>
</div>
</SettingGroup>
<SettingGroup title={t("advanced")}>
<Setting
label="mdfind filter"
control={
<Dropdown
style={{ width: "100%" }}
selectedOptions={[mdfindFilterOption]}
value={mdfindFilterOption}
onOptionSelect={(_, { optionValue }) => {
if (optionValue) {
updateMdfindFilterOption(optionValue);
}
}}
>
<Option value="kind:application">kind:application</Option>
<Option value="kMDItemKind=='Application'">kMDItemKind=='Application'</Option>
<Option value="kMDItemContentType=='com.apple.application-bundle'">
kMDItemContentType=='com.apple.application-bundle'
</Option>
</Dropdown>
}
/>
</SettingGroup>
</SettingGroupList>
);
};