Skip to content

Commit

Permalink
Get available terminals from Terminal module
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Jun 8, 2024
1 parent 2c0ea01 commit ad1841f
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 34 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
2 changes: 2 additions & 0 deletions src/common/Core/ContextBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ExtensionInfo } from "./ExtensionInfo";
import type { OperatingSystem } from "./OperatingSystem";
import type { SearchResultItem } from "./SearchResultItem";
import type { SearchResultItemAction } from "./SearchResultItemAction";
import { Terminal } from "./Terminal";
import type { Resources, Translations } from "./Translator";

/**
Expand All @@ -23,6 +24,7 @@ export type ContextBridge = {
fileExists: (filePath: string) => boolean;
getAboutUeli: () => AboutUeli;
getAvailableExtensions: () => ExtensionInfo[];
getAvailableTerminals: () => Terminal[];
getEnabledExtensions: () => ExtensionInfo[];
getExtension: (extensionId: string) => ExtensionInfo;
getExtensionResources: <T extends Translations>() => { extensionId: string; resources: Resources<T> }[];
Expand Down
5 changes: 5 additions & 0 deletions src/common/Core/Terminal/Terminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Terminal = {
id: string;
name: string;
assetFilePath: string;
};
1 change: 1 addition & 0 deletions src/common/Core/Terminal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Terminal";
16 changes: 16 additions & 0 deletions src/main/Core/Terminal/TerminalModule.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { Dependencies } from "@Core/Dependencies";
import type { DependencyRegistry } from "@Core/DependencyRegistry";
import type { Terminal } from "@common/Core/Terminal";
import { TerminalRegistry } from "./TerminalRegistry";
import { CommandPrompt, Iterm, MacOsTerminal, Powershell, PowershellCore, Wsl } from "./Terminals";

export class TerminalModule {
public static bootstrap(dependencyRegistry: DependencyRegistry<Dependencies>) {
const ipcMain = dependencyRegistry.get("IpcMain");
const assetPathResolver = dependencyRegistry.get("AssetPathResolver");

const terminalRegistry = new TerminalRegistry(dependencyRegistry.get("OperatingSystem"), {
Linux: () => [],
macOS: () => [
Expand All @@ -20,5 +24,17 @@ export class TerminalModule {
});

dependencyRegistry.register("TerminalRegistry", terminalRegistry);

ipcMain.on(
"getAvailableTerminals",
(event) =>
(event.returnValue = terminalRegistry.getAll().map(
({ terminalId, getAssetFileName, getTerminalName }): Terminal => ({
id: terminalId,
name: getTerminalName(),
assetFilePath: assetPathResolver.getModuleAssetPath("Terminal", getAssetFileName()),
}),
)),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class TerminalLauncherExtension implements Extension {

public getImage(): Image {
return {
url: `file://${this.assetPathResolver.getExtensionAssetPath(this.id, "windows-terminal.png")}`,
url: `file://${this.assetPathResolver.getModuleAssetPath("Terminal", "windows-terminal.png")}`,
};
}

Expand Down Expand Up @@ -117,8 +117,8 @@ export class TerminalLauncherExtension implements Extension {
}

public getAssetFilePath(terminalId: string): string {
return this.assetPathResolver.getExtensionAssetPath(
this.id,
return this.assetPathResolver.getModuleAssetPath(
"Terminal",
this.terminalRegistry.getById(terminalId).getAssetFileName(),
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/Extensions/Workflow/WorkflowExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class WorkflowExtension implements Extension {
type: "Type",
"type.OpenFile": "Open file",
"type.OpenUrl": "Open URL",
"type.OpenTerminal": "Open terminal",
"type.ExecuteCommand": "Execute command",
newAction: "New Action",
actionName: "Action name",
Expand Down Expand Up @@ -121,6 +122,7 @@ export class WorkflowExtension implements Extension {
type: "Typ",
"type.OpenFile": "Datei öffnen",
"type.OpenUrl": "URL öffnen",
"type.OpenTerminal": "Terminal öffnen",
"type.ExecuteCommand": "Befehl ausführen",
newAction: "Neue Aktion",
actionName: "Name der Aktion",
Expand Down
1 change: 1 addition & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const contextBridgeImplementation: ContextBridge = {
fileExists: (filePath: string) => ipcRenderer.sendSync("fileExists", { filePath }),
getAboutUeli: () => ipcRenderer.sendSync("getAboutUeli"),
getAvailableExtensions: () => ipcRenderer.sendSync("getAvailableExtensions"),
getAvailableTerminals: () => ipcRenderer.sendSync("getAvailableTerminals"),
getEnabledExtensions: () => ipcRenderer.sendSync("getEnabledExtensions"),
getExtension: (extensionId) => ipcRenderer.sendSync("getExtension", { extensionId }),
getExtensionResources: () => ipcRenderer.sendSync("getExtensionResources"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useContextBridge, useExtensionSetting } from "@Core/Hooks";
import { Section } from "@Core/Settings/Section";
import { SectionList } from "@Core/Settings/SectionList";
import type { OperatingSystem } from "@common/Core";
import { Dropdown, Field, Input, Option } from "@fluentui/react-components";
import { useTranslation } from "react-i18next";

Expand All @@ -12,19 +11,15 @@ export const TerminalLauncherSettings = () => {
const extensionId = "TerminalLauncher";
const ns = "extension[TerminalLauncher]";

const options: Record<OperatingSystem, string[]> = {
macOS: ["Terminal", "iTerm"],
Windows: ["Command Prompt", "Powershell", "Powershell Core", "WSL"],
Linux: [],
};

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

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

const availableTerminals = contextBridge.getAvailableTerminals();

return (
<SectionList>
<Section>
Expand All @@ -41,14 +36,10 @@ export const TerminalLauncherSettings = () => {
multiselect
onOptionSelect={(_, { selectedOptions }) => setTerminalIds(selectedOptions)}
>
{options[contextBridge.getOperatingSystem()].map((option) => (
<Option key={option} value={option} text={option}>
<img
src={`file://${contextBridge.getExtensionAssetFilePath(extensionId, option)}`}
width={24}
alt={option}
/>
<span>{option}</span>
{availableTerminals.map((terminal) => (
<Option key={terminal.id} value={terminal.id} text={terminal.name}>
<img src={`file://${terminal.assetFilePath}`} width={24} alt={terminal.name} />
<span>{terminal.name}</span>
</Option>
))}
</Dropdown>
Expand Down
29 changes: 13 additions & 16 deletions src/renderer/Extensions/Workflow/NewActionOpenTerminal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { useContextBridge } from "@Core/Hooks";
import { OperatingSystem } from "@common/Core";
import type { OpenTerminalActionArgs } from "@common/Extensions/Workflow";
import { Dropdown, Field, Input, Option } from "@fluentui/react-components";
import type { NewActionTypeProps } from "./NewActionTypeProps";

export const NewActionOpenTerminal = ({ args, setArgs }: NewActionTypeProps) => {
console.log({
m: "render",
args,
});

const { contextBridge } = useContextBridge();

const { terminalId, command } = args as OpenTerminalActionArgs;
Expand All @@ -18,31 +12,34 @@ export const NewActionOpenTerminal = ({ args, setArgs }: NewActionTypeProps) =>

const setCommand = (newCommand: string) => setArgs({ terminalId, command: newCommand });

const terminalIds: Record<OperatingSystem, string[]> = {
Linux: [],
macOS: ["Terminal", "iTerm"],
Windows: ["Command Prompt", "PowerShell", "Windows Terminal"],
};
const defaultTerminalId = contextBridge.getAvailableTerminals()[0]?.id;

return (
<>
<Field label="Terminal">
<Dropdown
selectedOptions={[terminalId]}
selectedOptions={[terminalId || defaultTerminalId]}
value={terminalId}
placeholder="Select a terminal"
onOptionSelect={(_, { optionValue }) => optionValue && setTerminalId(optionValue)}
disabled={!terminalId}
size="small"
>
{terminalIds[contextBridge.getOperatingSystem()].map((terminalId) => (
<Option key={terminalId} value={terminalId}>
{terminalId}
{contextBridge.getAvailableTerminals().map(({ id, name, assetFilePath }) => (
<Option key={id} value={id} text={name}>
<img src={`file://${assetFilePath}`} alt={name} width={16} />
<span>{name}</span>
</Option>
))}
</Dropdown>
</Field>
<Field label="Command">
<Input value={command} onChange={(_, { value }) => setCommand(value)} placeholder="Enter a command" />
<Input
value={command}
onChange={(_, { value }) => setCommand(value)}
placeholder="Enter a command"
size="small"
/>
</Field>
</>
);
Expand Down

0 comments on commit ad1841f

Please sign in to comment.