Repository navigation
Expand file tree
/
Copy pathpty-env.js
More file actions
41 lines (36 loc) · 1.55 KB
/
Copy pathpty-env.js
File metadata and controls
41 lines (36 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Environment for PTY children.
//
// Two jobs:
//
// 1. Strip Electron internals that cause nested Electron apps (or node-pty
// inside them) to malfunction.
//
// 2. Guarantee a UTF-8 character encoding. An app launched from Finder or the
// Dock on macOS inherits no shell environment, so LANG/LC_* are simply
// absent — launchd does not set them. Locale-sensitive tools then fall back
// to the "standard C encoding", which on macOS means Mac OS Roman. `pbcopy`
// is the one users notice: it reads UTF-8 bytes as Mac OS Roman, so an
// agent response copied out of a session arrives on the clipboard as
// "‚Ķ‚Äî‚Üí" instead of "…—→" (issue #89).
//
// LC_CTYPE alone is deliberate. It fixes character handling and nothing
// else — collation, dates, and number formatting stay on the system
// default rather than being forced to some guessed region.
const LOCALE_VARS = ['LC_ALL', 'LC_CTYPE', 'LANG'];
function buildPtyEnv(sourceEnv) {
const env = Object.fromEntries(
Object.entries(sourceEnv).filter(([k]) =>
!k.startsWith('ELECTRON_') &&
!k.startsWith('GOOGLE_API_KEY') &&
k !== 'NODE_OPTIONS' &&
k !== 'ORIGINAL_XDG_CURRENT_DESKTOP' &&
k !== 'WT_SESSION'
)
);
// Only when the parent handed us nothing usable — an inherited locale is the
// user's own setting and must win. An empty value counts as absent, which is
// what a GUI launch actually produces.
if (!LOCALE_VARS.some((k) => env[k])) env.LC_CTYPE = 'UTF-8';
return env;
}
module.exports = { buildPtyEnv };