350
}
351
352
>
export async function fetchPwshHistory(accessor: ServicesAccessor): Promise<IShellFileHistoryEntry | undefined> {
history.ts
353
>
const fileService: Pick<IFileService, 'readFile'> = accessor.get(IFileService);
354
>
const remoteAgentService: Pick<IRemoteAgentService, 'getConnection' | 'getEnvironment'> = accessor.get(IRemoteAgentService);
355
>
let folderPrefix: string | undefined;
356
>
let filePath: string;
357
>
const remoteEnvironment = await remoteAgentService.getEnvironment();
358
>
const isFileWindows = remoteEnvironment?.os === OperatingSystem.Windows || !remoteEnvironment && isWindows;
359
>
let sourceLabel: string;
360
>
if (isFileWindows) {
361
folderPrefix = env['APPDATA'];
362
filePath = 'Microsoft\\Windows\\PowerShell\\PSReadLine\\ConsoleHost_history.txt';
363
sourceLabel = `$APPDATA\\Microsoft\\Windows\\PowerShell\\PSReadLine\\ConsoleHost_history.txt`;
365
folderPrefix = remoteEnvironment?.userHome?.fsPath ?? env['HOME'];
366
filePath = '.local/share/powershell/PSReadline/ConsoleHost_history.txt';
367
sourceLabel = `~/${filePath}`;
368
}
369
>
const resolvedFile = await fetchFileContents(folderPrefix, filePath, isFileWindows, fileService, remoteAgentService);
history.ts
370
>
if (resolvedFile === undefined) {
371
return undefined;
372
}
373
>
const fileLines = resolvedFile.content.split('\n');
history.ts
374
>
const result: Set<string> = new Set();
375
>
let currentLine: string;
376
>
let currentCommand: string | undefined = undefined;
377
>
let wrapChar: string | undefined = undefined;
378
>
for (let i = 0; i < fileLines.length; i++) {
379
>
currentLine = fileLines[i];
380
>
if (currentCommand === undefined) {
381
>
currentCommand = currentLine;
382
>
} else {
383
>
currentCommand += `\n${currentLine}`;
384
>
}
385
>
if (!currentLine.endsWith('`')) {
386
>
const sanitized = currentCommand.trim();
387
>
if (sanitized.length > 0) {
388
>
result.add(sanitized);
389
>
}
390
>
currentCommand = undefined;
391
>
continue;
392
>
}
393
>
// If the line ends with `, the line may be wrapped. Need to also test the case where ` is
394
>
// the last character in the line
395
>
for (let c = 0; c < currentLine.length; c++) {
396
>
if (wrapChar) {
397
if (currentLine[c] === wrapChar) {
398
wrapChar = undefined;
399
}
401
>
if (currentLine[c].match(/`/)) {
402
>
wrapChar = currentLine[c];
403
>
}
404
>
}
405
>
}
406
>
// Having an even number of backticks means the line is terminated
407
>
// TODO: This doesn't cover more complicated cases where ` is within quotes
408
>
if (!wrapChar) {
409
const sanitized = currentCommand.trim();
410
if (sanitized.length > 0) {