feat(pty): parse env so it can be used with fork

Adds method to convert env from a Dictionary to a PackedStringArray so
it can be used with fork(). Consider making env a PackedStringArray to
begin with.
This commit is contained in:
Leroy Hopson 2024-02-25 22:09:04 +13:00
parent 8cd11fdae6
commit ee920a45fd
No known key found for this signature in database
GPG key ID: D2747312A6DB51AA
2 changed files with 14 additions and 1 deletions

View file

@ -111,7 +111,7 @@ Error PTY::fork(const String &file, const PackedStringArray &args, const String
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
String helper_path = ProjectSettings::get_singleton()->globalize_path("res://addons/godot_xterm/native/bin/spawn-helper"); String helper_path = ProjectSettings::get_singleton()->globalize_path("res://addons/godot_xterm/native/bin/spawn-helper");
result = PTYUnix::fork(fork_file, args, PackedStringArray(), cwd, cols, rows, -1, -1, true, helper_path, Callable(this, "_on_exit")); result = PTYUnix::fork(fork_file, args, _parse_env(fork_env), cwd, cols, rows, -1, -1, true, helper_path, Callable(this, "_on_exit"));
#endif #endif
Error err = static_cast<Error>((int)result["error"]); Error err = static_cast<Error>((int)result["error"]);
@ -278,6 +278,18 @@ Dictionary PTY::_get_fork_env() const {
return os_env; return os_env;
} }
PackedStringArray PTY::_parse_env(const Dictionary &env) const {
PackedStringArray parsed_env;
PackedStringArray keys = PackedStringArray(env.keys());
for (int i = 0; i < keys.size(); i++) {
String key = keys[i];
parsed_env.push_back(key + "=" + String(env[key]));
}
return parsed_env;
}
void PTY::_on_exit(int exit_code, int exit_signal) { void PTY::_on_exit(int exit_code, int exit_signal) {
emit_signal(StringName("exited"), exit_code, exit_signal); emit_signal(StringName("exited"), exit_code, exit_signal);
} }

View file

@ -82,6 +82,7 @@ namespace godot
String _get_fork_file(const String &file) const; String _get_fork_file(const String &file) const;
Dictionary _get_fork_env() const; Dictionary _get_fork_env() const;
PackedStringArray _parse_env(const Dictionary &env) const;
void _on_exit(int exit_code, int exit_signal); void _on_exit(int exit_code, int exit_signal);
void _close(); void _close();