-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jupyterlite/env_vars
Support environment variables that persist beyond single commands
- Loading branch information
Showing
8 changed files
with
263 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Collection of environment variables that are known to a shell and are passed in and out of | ||
* commands. | ||
*/ | ||
export class Environment { | ||
constructor() { | ||
this._env.set("PS1", "\x1b[1;31mjs-shell:$\x1b[1;0m ") // red color | ||
} | ||
|
||
/** | ||
* Copy environment variables back from a command after it has run. | ||
*/ | ||
copyFromCommand(source: string[]) { | ||
for (const str of source) { | ||
const split = str.split("=") | ||
const key = split.shift() | ||
if (key && !this._ignore.has(key)) { | ||
this._env.set(key, split.join("=")) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Copy environment variables into a command before it is run. | ||
*/ | ||
copyIntoCommand(target: { [key: string]: string }) { | ||
for (const [key, value] of this._env.entries()) { | ||
target[key] = value | ||
} | ||
} | ||
|
||
get(key: string): string | null { | ||
return this._env.get(key) ?? null | ||
} | ||
|
||
getPrompt(): string { | ||
return this._env.get("PS1") ?? "$ " | ||
} | ||
|
||
set(key: string, value: string) { | ||
this._env.set(key, value) | ||
} | ||
|
||
private _env: Map<string, string> = new Map() | ||
|
||
// Keys to ignore when copying back from a command's env vars. | ||
private _ignore: Set<string> = new Set(["USER", "LOGNAME", "HOME", "LANG", "_"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.