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

Fix escape sequences in prompts #1862

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 9 additions & 2 deletions src/extension/executors/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import {
RunProgramOptions,
} from '../../runner'
import { IRunnerEnvironment } from '../../runner/environment'
import { getAnnotations, getCellRunmeId, getTerminalByCell } from '../../utils'
import {
getAnnotations,
getCellRunmeId,
getTerminalByCell,
unescapeShellLiteral,
} from '../../utils'
import { postClientMessage } from '../../../utils/messaging'
import {
getCloseTerminalOnSuccess,
Expand Down Expand Up @@ -609,9 +614,11 @@ export async function promptVariablesAsync(
case UNRESOLVED_WITH_PLACEHOLDER:
case UNRESOLVED_WITH_SECRET: {
const key = variable.name
const placeHolder = variable.resolvedValue || variable.originalValue || 'Enter a value please'
const hasStringValue = variable.status === UNRESOLVED_WITH_PLACEHOLDER
const isPassword = variable.status === UNRESOLVED_WITH_SECRET
const placeHolder = unescapeShellLiteral(
variable.resolvedValue || variable.originalValue || 'Enter a value please',
)

const userInput = await promptUserForVariable(key, placeHolder, hasStringValue, isPassword)

Expand Down
10 changes: 10 additions & 0 deletions src/extension/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ export function normalizeLanguage(l?: string) {
}
}

export function unescapeShellLiteral(escaped: string) {
return escaped
.replace(/\\\(/g, '(')
.replace(/\\\)/g, ')')
.replace(/\\\{/g, '{')
.replace(/\\\}/g, '}')
.replace(/\\\[/g, '[')
.replace(/\\\]/g, ']')
}

export async function verifyCheckedInFile(filePath: string) {
const fileDir = path.dirname(filePath)
const workspaceFolder = vscode.workspace.workspaceFolders?.find((ws) =>
Expand Down
14 changes: 14 additions & 0 deletions tests/extension/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
asWorkspaceRelativePath,
editJsonc,
getGitContext,
unescapeShellLiteral,
} from '../../src/extension/utils'
import { ENV_STORE, DEFAULT_ENV } from '../../src/extension/constants'
import { CellAnnotations } from '../../src/types'
Expand Down Expand Up @@ -251,6 +252,19 @@ test('getKeyInfo', () => {
})
})

test('unescapeShellLiteral', () => {
expect(unescapeShellLiteral('echo "Hello World!"')).toBe('echo "Hello World!"')
expect(unescapeShellLiteral('echo "Hello ${name}!"')).toBe('echo "Hello ${name}!"')
expect(unescapeShellLiteral('[Guest type \\(hyperv,proxmox,openstack\\)]')).toBe(
'[Guest type (hyperv,proxmox,openstack)]',
)
expect(unescapeShellLiteral('[IP of waiting server \\{foo\\}]')).toBe(
'[IP of waiting server {foo}]',
)
expect(unescapeShellLiteral('[Guest\\ Type]')).toBe('[Guest\\ Type]')
expect(unescapeShellLiteral('\\[Guest Type\\]')).toBe('[Guest Type]')
})

suite('normalizeLanguage', () => {
test('with zsh', () => {
const lang = normalizeLanguage('zsh')
Expand Down
Loading