Skip to content

Commit d30f701

Browse files
anthonykim1Tyriar
andauthored
Add Python Shell Type and .python_history (microsoft#204680)
* Add Python Shell type and history * remove one comment * remove unncessary * Why can't I manually override setShellType * Detect python shell type on Windows * fire shellType in _sendProcessTitle * Detect python shell type on Windows * delete comment * remove more comments * remove comment * remove dup * clean up * more comprehensive regex for windows python * follow previous style for map key name * remove unused variable * remove comment * last cleanup * More cleanup * more clean up * re-arrange * remove unused --------- Co-authored-by: Daniel Imms <[email protected]>
1 parent e4e853f commit d30f701

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

src/vs/platform/terminal/common/terminal.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@ export const enum PosixShellType {
140140
Csh = 'csh',
141141
Ksh = 'ksh',
142142
Zsh = 'zsh',
143+
Python = 'python'
143144
}
144145
export const enum WindowsShellType {
145146
CommandPrompt = 'cmd',
146147
PowerShell = 'pwsh',
147148
Wsl = 'wsl',
148-
GitBash = 'gitbash'
149+
GitBash = 'gitbash',
150+
Python = 'python'
149151
}
150152
export type TerminalShellType = PosixShellType | WindowsShellType;
151153

src/vs/platform/terminal/node/terminalProcess.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const posixShellTypeMap = new Map<string, PosixShellType>([
7373
['ksh', PosixShellType.Ksh],
7474
['sh', PosixShellType.Sh],
7575
['pwsh', PosixShellType.PowerShell],
76+
['python', PosixShellType.Python],
7677
['zsh', PosixShellType.Zsh]
7778
]);
7879

@@ -404,7 +405,12 @@ export class TerminalProcess extends Disposable implements ITerminalChildProcess
404405
this._onDidChangeProperty.fire({ type: ProcessPropertyType.Title, value: this._currentTitle });
405406
// If fig is installed it may change the title of the process
406407
const sanitizedTitle = this.currentTitle.replace(/ \(figterm\)$/g, '');
407-
this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: posixShellTypeMap.get(sanitizedTitle) });
408+
409+
if (sanitizedTitle.toLowerCase().startsWith('python')) {
410+
this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: PosixShellType.Python });
411+
} else {
412+
this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: posixShellTypeMap.get(sanitizedTitle) });
413+
}
408414
}
409415

410416
shutdown(immediate: boolean): void {

src/vs/platform/terminal/node/windowsShellHelper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ export class WindowsShellHelper extends Disposable implements IWindowsShellHelpe
152152
case 'sles-12.exe':
153153
return WindowsShellType.Wsl;
154154
default:
155+
if (executable.match(/python(\d(\.\d{0,2})?)?\.exe/)) {
156+
return WindowsShellType.Python;
157+
}
155158
return undefined;
156159
}
157160
}

src/vs/workbench/contrib/terminal/browser/terminalInstance.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const shellIntegrationSupportedShellTypes = [
118118
PosixShellType.Bash,
119119
PosixShellType.Zsh,
120120
PosixShellType.PowerShell,
121+
PosixShellType.Python,
121122
WindowsShellType.PowerShell
122123
];
123124

src/vs/workbench/contrib/terminal/common/history.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ export async function getShellFileHistory(accessor: ServicesAccessor, shellType:
9292
case PosixShellType.Fish:
9393
result = await fetchFishHistory(accessor);
9494
break;
95+
case PosixShellType.Python:
96+
result = await fetchPythonHistory(accessor);
97+
break;
9598
default: return [];
9699
}
97100
if (result === undefined) {
@@ -295,6 +298,30 @@ export async function fetchZshHistory(accessor: ServicesAccessor) {
295298
return result.values();
296299
}
297300

301+
302+
export async function fetchPythonHistory(accessor: ServicesAccessor): Promise<IterableIterator<string> | undefined> {
303+
const fileService = accessor.get(IFileService);
304+
const remoteAgentService = accessor.get(IRemoteAgentService);
305+
306+
const content = await fetchFileContents(env['HOME'], '.python_history', false, fileService, remoteAgentService);
307+
308+
if (content === undefined) {
309+
return undefined;
310+
}
311+
312+
// Python history file is a simple text file with one command per line
313+
const fileLines = content.split('\n');
314+
const result: Set<string> = new Set();
315+
316+
fileLines.forEach(line => {
317+
if (line.trim().length > 0) {
318+
result.add(line.trim());
319+
}
320+
});
321+
322+
return result.values();
323+
}
324+
298325
export async function fetchPwshHistory(accessor: ServicesAccessor) {
299326
const fileService: Pick<IFileService, 'readFile'> = accessor.get(IFileService);
300327
const remoteAgentService: Pick<IRemoteAgentService, 'getConnection' | 'getEnvironment'> = accessor.get(IRemoteAgentService);

0 commit comments

Comments
 (0)