From 9f8f2966d9bd6e8bc015d8ff69098af2a83a618f Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Sat, 23 Oct 2021 19:14:33 +0200 Subject: [PATCH] fixed #4794 - env var expansion in profiles --- .../components/environmentEditor.component.pug | 13 ++++++++++--- .../components/environmentEditor.component.ts | 9 +++++++++ tabby-local/src/session.ts | 17 ++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/tabby-local/src/components/environmentEditor.component.pug b/tabby-local/src/components/environmentEditor.component.pug index cd804c8a..222c5462 100644 --- a/tabby-local/src/components/environmentEditor.component.pug +++ b/tabby-local/src/components/environmentEditor.component.pug @@ -8,6 +8,13 @@ button.btn.btn-secondary((click)='removeEnvironmentVar(pair.key)') i.fas.fa-fw.fa-trash -button.btn.btn-secondary((click)='addEnvironmentVar()') - i.fas.fa-plus.mr-2 - span Add +.d-flex + button.btn.btn-secondary((click)='addEnvironmentVar()') + i.fas.fa-plus.mr-2 + span Add + + .ml-auto + .text-muted Substitutions allowed. + .d-flex.ml-1(*ngIf='shouldShowExample()') + .text-muted Example: + a.ml-1((click)='addExample()', href='#') extend PATH diff --git a/tabby-local/src/components/environmentEditor.component.ts b/tabby-local/src/components/environmentEditor.component.ts index 7eeea06b..ed4fe365 100644 --- a/tabby-local/src/components/environmentEditor.component.ts +++ b/tabby-local/src/components/environmentEditor.component.ts @@ -44,4 +44,13 @@ export class EnvironmentEditorComponent { this.emitUpdate() } + shouldShowExample (): boolean { + return !this.vars.find(v => v.key.toLowerCase() === 'path') + } + + addExample (): void { + const value = process.platform === 'win32' ? 'C:\\Program Files\\Custom:%PATH%' : '/opt/custom:$PATH' + this.vars.push({ key: 'PATH', value }) + this.emitUpdate() + } } diff --git a/tabby-local/src/session.ts b/tabby-local/src/session.ts index 5b4639bd..19fb7618 100644 --- a/tabby-local/src/session.ts +++ b/tabby-local/src/session.ts @@ -95,6 +95,21 @@ function mergeEnv (...envs) { return result } +function substituteEnv (env: Record) { + env = { ...env } + const pattern = process.platform === 'win32' ? /%(\w+)%/g : /\$(\w+)\b/g + for (const [key, value] of Object.entries(env)) { + env[key] = value.replace(pattern, function (substring, p1) { + if (process.platform === 'win32') { + return Object.entries(process.env).find(x => x[0].toLowerCase() === p1.toLowerCase())?.[1] ?? '' + } else { + return process.env[p1] ?? '' + } + }) + } + return env +} + /** @hidden */ export class Session extends BaseSession { private pty: PTYProxy|null = null @@ -128,7 +143,7 @@ export class Session extends BaseSession { TERM: 'xterm-256color', TERM_PROGRAM: 'Tabby', }, - options.env, + substituteEnv(options.env ?? {}), this.config.store.terminal.environment || {}, ) -- GitLab