-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
249 lines (216 loc) · 7.29 KB
/
main.ts
File metadata and controls
249 lines (216 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { Plugin, PluginSettingTab, Setting, TFolder, TAbstractFile, Platform, App } from "obsidian";
import { execFile } from "child_process";
import * as path from "path";
const SECTION_ID = "openterm";
interface OpenTermSettings {
showDefaultTerminal: boolean;
showPowerShell: boolean;
showCmd: boolean;
windowsDefaultExe: string;
powershellExe: string;
cmdExe: string;
macTerminalApp: string;
linuxTerminalExe: string;
}
const DEFAULT_SETTINGS: OpenTermSettings = {
showDefaultTerminal: true,
showPowerShell: true,
showCmd: true,
windowsDefaultExe: "cmd.exe",
powershellExe: "pwsh",
cmdExe: "cmd.exe",
macTerminalApp: "Terminal",
linuxTerminalExe: "x-terminal-emulator",
};
interface VaultAdapterWithBasePath {
getBasePath(): string;
}
function hasBasePath(adapter: unknown): adapter is VaultAdapterWithBasePath {
return typeof (adapter as VaultAdapterWithBasePath).getBasePath === "function";
}
function getDirectory(vaultBasePath: string, file: TAbstractFile): string {
if (file instanceof TFolder) {
return path.join(vaultBasePath, file.path);
}
return path.join(vaultBasePath, file.parent?.path ?? "");
}
function openInDefaultTerminal(directory: string, settings: OpenTermSettings): void {
if (Platform.isWin) {
const exe = settings.windowsDefaultExe || "cmd.exe";
execFile("cmd.exe", ["/c", "start", "/D", directory, exe]);
} else if (Platform.isMacOS) {
const app = settings.macTerminalApp || "Terminal";
execFile("open", ["-a", app, directory]);
} else {
const exe = settings.linuxTerminalExe || "x-terminal-emulator";
execFile(exe, [`--working-directory=${directory}`], (err) => {
if (err) {
execFile("xdg-open", [directory]);
}
});
}
}
function openInPowerShell(directory: string, settings: OpenTermSettings): void {
const exe = settings.powershellExe || "pwsh";
const psArgs = ["-NoExit", "-Command",
`Set-Location -LiteralPath '${directory.replace(/'/g, "''")}'`];
if (Platform.isWin) {
execFile("cmd.exe", ["/c", "start", exe, ...psArgs], (err) => {
const isNotFound = err && (err as NodeJS.ErrnoException).code === "ENOENT";
if (isNotFound && exe !== "powershell") {
execFile("cmd.exe", ["/c", "start", "powershell", ...psArgs]);
}
});
} else {
execFile(exe, psArgs);
}
}
function openInCmd(directory: string, settings: OpenTermSettings): void {
const exe = settings.cmdExe || "cmd.exe";
execFile("cmd.exe", ["/c", "start", "/D", directory, exe]);
}
export default class OpenTermPlugin extends Plugin {
settings: OpenTermSettings;
async onload(): Promise<void> {
await this.loadSettings();
this.addSettingTab(new OpenTermSettingTab(this.app, this));
const adapter = this.app.vault.adapter;
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
const basePath = hasBasePath(adapter) ? adapter.getBasePath() : "";
const directory = getDirectory(basePath, file);
if (this.settings.showDefaultTerminal) {
menu.addItem((item) => {
item.setTitle("Open in terminal")
.setIcon("terminal")
.setSection(SECTION_ID)
.onClick(() => openInDefaultTerminal(directory, this.settings));
});
}
if (this.settings.showPowerShell) {
menu.addItem((item) => {
item.setTitle("Open in PowerShell")
.setIcon("terminal")
.setSection(SECTION_ID)
.onClick(() => openInPowerShell(directory, this.settings));
});
}
if (Platform.isWin && this.settings.showCmd) {
menu.addItem((item) => {
item.setTitle("Open in Command Prompt")
.setIcon("terminal")
.setSection(SECTION_ID)
.onClick(() => openInCmd(directory, this.settings));
});
}
})
);
}
onunload(): void {}
async loadSettings(): Promise<void> {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}
}
class OpenTermSettingTab extends PluginSettingTab {
plugin: OpenTermPlugin;
constructor(app: App, plugin: OpenTermPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl).setName("Context menu items").setHeading();
new Setting(containerEl)
.setName("Show default terminal")
.setDesc("Show the 'Open in terminal' option in the context menu.")
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.showDefaultTerminal).onChange(async (value) => {
this.plugin.settings.showDefaultTerminal = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Show PowerShell")
.setDesc("Show the 'Open in PowerShell' option in the context menu.")
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.showPowerShell).onChange(async (value) => {
this.plugin.settings.showPowerShell = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Show Command Prompt (Windows only)")
.setDesc("Show the 'Open in Command Prompt' option in the context menu.")
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.showCmd).onChange(async (value) => {
this.plugin.settings.showCmd = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl).setName("Executables").setHeading();
new Setting(containerEl)
.setName("PowerShell executable")
.setDesc("Executable for PowerShell. Uses 'pwsh' (PowerShell 7+) by default, falling back to 'powershell' on Windows if not found.")
.addText((text) =>
text
.setPlaceholder("pwsh")
.setValue(this.plugin.settings.powershellExe)
.onChange(async (value) => {
this.plugin.settings.powershellExe = value.trim();
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Command Prompt executable (Windows)")
.setDesc("Executable for Command Prompt on Windows.")
.addText((text) =>
text
.setPlaceholder("cmd.exe")
.setValue(this.plugin.settings.cmdExe)
.onChange(async (value) => {
this.plugin.settings.cmdExe = value.trim();
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Default terminal executable (Windows)")
.setDesc("Executable used for the default terminal option on Windows.")
.addText((text) =>
text
.setPlaceholder("cmd.exe")
.setValue(this.plugin.settings.windowsDefaultExe)
.onChange(async (value) => {
this.plugin.settings.windowsDefaultExe = value.trim();
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Terminal app (macOS)")
.setDesc("Application used for the default terminal option on macOS.")
.addText((text) =>
text
.setPlaceholder("Terminal")
.setValue(this.plugin.settings.macTerminalApp)
.onChange(async (value) => {
this.plugin.settings.macTerminalApp = value.trim();
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Terminal executable (Linux)")
.setDesc("Executable used for the default terminal option on Linux.")
.addText((text) =>
text
.setPlaceholder("x-terminal-emulator")
.setValue(this.plugin.settings.linuxTerminalExe)
.onChange(async (value) => {
this.plugin.settings.linuxTerminalExe = value.trim();
await this.plugin.saveSettings();
})
);
}
}