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

Add Python Shell Type and .python_history #204680

Merged
merged 25 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1e65289
Add Python Shell type and history
anthonykim1 Feb 8, 2024
e3d444d
remove one comment
anthonykim1 Feb 8, 2024
7140126
remove unncessary
anthonykim1 Feb 8, 2024
cd4c648
Why can't I manually override setShellType
anthonykim1 Feb 9, 2024
24af1d4
Detect python shell type on Windows
Tyriar Feb 13, 2024
18b82f1
fire shellType in _sendProcessTitle
anthonykim1 Feb 13, 2024
c2d2434
Detect python shell type on Windows
Tyriar Feb 13, 2024
a139138
delete comment
anthonykim1 Feb 13, 2024
39b2ffb
Merge pull request #1 from microsoft/PythonShellHistory
anthonykim1 Feb 13, 2024
9ea5d05
remove more comments
anthonykim1 Feb 13, 2024
b9e3208
Merge branch 'microsoft:PythonShellHistory' into PythonShellHistory
anthonykim1 Feb 13, 2024
b7f7079
remove comment
anthonykim1 Feb 13, 2024
16f2804
Merge branch 'microsoft:PythonShellHistory' into PythonShellHistory
anthonykim1 Feb 13, 2024
e2fb0a5
remove dup
anthonykim1 Feb 13, 2024
4933817
Merge pull request #2 from microsoft/PythonShellHistory
anthonykim1 Feb 13, 2024
9a9bde5
clean up
anthonykim1 Feb 13, 2024
14fc736
more comprehensive regex for windows python
anthonykim1 Feb 14, 2024
fb845b3
follow previous style for map key name
anthonykim1 Feb 14, 2024
cd0553b
remove unused variable
anthonykim1 Feb 14, 2024
8b509bb
remove comment
anthonykim1 Feb 14, 2024
70d8847
last cleanup
anthonykim1 Feb 15, 2024
e94472c
More cleanup
anthonykim1 Feb 15, 2024
a356935
more clean up
anthonykim1 Feb 15, 2024
b928d97
re-arrange
anthonykim1 Feb 15, 2024
c936921
remove unused
anthonykim1 Feb 15, 2024
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
4 changes: 3 additions & 1 deletion src/vs/platform/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ export const enum PosixShellType {
Csh = 'csh',
Ksh = 'ksh',
Zsh = 'zsh',
Python = 'python'
}
export const enum WindowsShellType {
CommandPrompt = 'cmd',
PowerShell = 'pwsh',
Wsl = 'wsl',
GitBash = 'gitbash'
GitBash = 'gitbash',
Python = 'python'
}
export type TerminalShellType = PosixShellType | WindowsShellType;

Expand Down
8 changes: 7 additions & 1 deletion src/vs/platform/terminal/node/terminalProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const posixShellTypeMap = new Map<string, PosixShellType>([
['ksh', PosixShellType.Ksh],
['sh', PosixShellType.Sh],
['pwsh', PosixShellType.PowerShell],
['python', PosixShellType.Python],
['zsh', PosixShellType.Zsh]
]);

Expand Down Expand Up @@ -404,7 +405,12 @@ export class TerminalProcess extends Disposable implements ITerminalChildProcess
this._onDidChangeProperty.fire({ type: ProcessPropertyType.Title, value: this._currentTitle });
// If fig is installed it may change the title of the process
const sanitizedTitle = this.currentTitle.replace(/ \(figterm\)$/g, '');
this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: posixShellTypeMap.get(sanitizedTitle) });

if (sanitizedTitle.toLowerCase().startsWith('python')) {
this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: PosixShellType.Python });
} else {
this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: posixShellTypeMap.get(sanitizedTitle) });
}
}

shutdown(immediate: boolean): void {
Expand Down
3 changes: 3 additions & 0 deletions src/vs/platform/terminal/node/windowsShellHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export class WindowsShellHelper extends Disposable implements IWindowsShellHelpe
case 'sles-12.exe':
return WindowsShellType.Wsl;
default:
if (executable.match(/python(\d(\.\d{0,2})?)?\.exe/)) {
return WindowsShellType.Python;
}
return undefined;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const shellIntegrationSupportedShellTypes = [
PosixShellType.Bash,
PosixShellType.Zsh,
PosixShellType.PowerShell,
PosixShellType.Python,
WindowsShellType.PowerShell
];

Expand Down
27 changes: 27 additions & 0 deletions src/vs/workbench/contrib/terminal/common/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export async function getShellFileHistory(accessor: ServicesAccessor, shellType:
case PosixShellType.Fish:
result = await fetchFishHistory(accessor);
break;
case PosixShellType.Python:
result = await fetchPythonHistory(accessor);
break;
default: return [];
}
if (result === undefined) {
Expand Down Expand Up @@ -295,6 +298,30 @@ export async function fetchZshHistory(accessor: ServicesAccessor) {
return result.values();
}


export async function fetchPythonHistory(accessor: ServicesAccessor): Promise<IterableIterator<string> | undefined> {
const fileService = accessor.get(IFileService);
const remoteAgentService = accessor.get(IRemoteAgentService);

const content = await fetchFileContents(env['HOME'], '.python_history', false, fileService, remoteAgentService);

if (content === undefined) {
return undefined;
}

// Python history file is a simple text file with one command per line
const fileLines = content.split('\n');
const result: Set<string> = new Set();

fileLines.forEach(line => {
if (line.trim().length > 0) {
result.add(line.trim());
}
});

return result.values();
}

export async function fetchPwshHistory(accessor: ServicesAccessor) {
const fileService: Pick<IFileService, 'readFile'> = accessor.get(IFileService);
const remoteAgentService: Pick<IRemoteAgentService, 'getConnection' | 'getEnvironment'> = accessor.get(IRemoteAgentService);
Expand Down
Loading