diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/os/locale.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/os/locale.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/os/locale.zig | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/vendor/libghostty-vt/src/os/locale.zig b/vendor/libghostty-vt/src/os/locale.zig index faba3bd2..ceb3c883 100644 --- a/vendor/libghostty-vt/src/os/locale.zig +++ b/vendor/libghostty-vt/src/os/locale.zig @@ -9,13 +9,10 @@ const i18n = internal_os.i18n; const log = std.log.scoped(.os_locale); /// Ensure that the locale is set. -pub fn ensureLocale(alloc: std.mem.Allocator) !void { - assert(builtin.link_libc); - - // Get our LANG env var. We use this many times but we also need - // the original value later. - const lang = try internal_os.getenv(alloc, "LANG"); - defer if (lang) |v| v.deinit(alloc); +pub fn ensureLocale() !void { + // This does a lot of in-process mutation of the environment and can't be + // run in a test as a result. + assert(!builtin.is_test); // On macOS, pre-populate the LANG env var with system preferences. // When launching the .app, LANG is not set so we must query it from the @@ -23,7 +20,8 @@ pub fn ensureLocale(alloc: std.mem.Allocator) !void { // process. if (comptime builtin.target.os.tag.isDarwin()) { // Set the lang if it is not set or if its empty. - if (lang == null or lang.?.value.len == 0) { + const lang = std.posix.system.getenv("LANG"); + if (lang == null or lang.?[0] == 0) { setLangFromCocoa(); } } @@ -34,16 +32,29 @@ pub fn ensureLocale(alloc: std.mem.Allocator) !void { return; } + if (builtin.os.tag == .windows) { + // Exit early for Windows. + // + // NOTE: There currently is no official Windows version of Ghostty, + // either by way of an official Windows port or through use of + // libghostty-internal. As such this function is likely unused on those + // platforms, so this serves as a TODO stub to support more robust + // locale support on Windows. setlocale on Windows sets an + // "implementation-defined native environment" with an empty string, so + // we are currently just doing best-effort for now. + log.info("TODO: setlocale failed on Windows, implement better fallbacks", .{}); + return; + } + // setlocale failed. This is probably because the LANG env var is // invalid. Try to set it without the LANG var set to use the system // default. - if ((try internal_os.getenv(alloc, "LANG"))) |old_lang| { - defer old_lang.deinit(alloc); - if (old_lang.value.len > 0) { + if (std.posix.system.getenv("LANG")) |lang| { + if (lang[0] != 0) { // We don't need to do both of these things but we do them // both to be sure that lang is either empty or unset completely. - _ = internal_os.setenv("LANG", ""); - _ = internal_os.unsetenv("LANG"); + _ = setenv("LANG", "", 1); + _ = unsetenv("LANG"); if (setlocale(LC_ALL, "")) |v| { log.info("setlocale after unset lang result={s}", .{v}); @@ -59,7 +70,7 @@ pub fn ensureLocale(alloc: std.mem.Allocator) !void { // Failure again... fallback to en_US.UTF-8 log.warn("setlocale failed with LANG and system default. Falling back to en_US.UTF-8", .{}); if (setlocale(LC_ALL, "en_US.UTF-8")) |v| { - _ = internal_os.setenv("LANG", "en_US.UTF-8"); + _ = setenv("LANG", "en_US.UTF-8", 1); log.info("setlocale default result={s}", .{v}); return; } else log.warn("setlocale failed even with the fallback, uncertain results", .{}); @@ -106,7 +117,7 @@ fn setLangFromCocoa() void { log.info("detected system locale={s}", .{env_value}); // Set it onto our environment - if (internal_os.setenv("LANG", env_value) < 0) { + if (setenv("LANG", @ptrCast(env_value), 1) < 0) { log.warn("error setting locale env var", .{}); return; } @@ -129,7 +140,7 @@ fn setLangFromCocoa() void { "setting LANGUAGE from preferred languages value={s}", .{pref}, ); - _ = internal_os.setenv("LANGUAGE", pref); + _ = setenv("LANGUAGE", @ptrCast(pref), 1); } } @@ -156,8 +167,7 @@ fn preferredLanguageFromCocoa( buf: []u8, NSLocale: objc.Class, ) error{NoSpaceLeft}!?[:0]const u8 { - var fbs = std.io.fixedBufferStream(buf); - const writer = fbs.writer(); + var writer: std.Io.Writer = .fixed(buf); // We need to get our app's preferred languages. These may not // match the system locale (NSLocale.currentLocale). @@ -180,7 +190,7 @@ fn preferredLanguageFromCocoa( }; // Append our separator if we have any previous languages - if (fbs.pos > 0) { + if (writer.end > 0) { _ = writer.writeByte(':') catch return error.NoSpaceLeft; } @@ -188,10 +198,10 @@ fn preferredLanguageFromCocoa( // Apple languages are in BCP-47 format, and we need to // canonicalize them to the POSIX format. const canon = try i18n.canonicalizeLocale( - fbs.buffer[fbs.pos..], + writer.buffer[writer.end..], c_str, ); - fbs.seekBy(@intCast(canon.len)) catch unreachable; + writer.end += canon.len; // The canonicalized locale never contains the encoding and // all of our translations require UTF-8 so we add that. @@ -199,14 +209,14 @@ fn preferredLanguageFromCocoa( } // If we had no preferred languages then we return nothing. - if (fbs.pos == 0) return null; + if (writer.end == 0) return null; // Null terminate it _ = writer.writeByte(0) catch return error.NoSpaceLeft; // Get our slice, this won't be null terminated so we have to // reslice it with the null terminator. - const slice = fbs.getWritten(); + const slice = writer.buffered(); return slice[0 .. slice.len - 1 :0]; } @@ -217,3 +227,6 @@ const locale_t = c.locale_t; const setlocale = c.setlocale; const newlocale = c.newlocale; const freelocale = c.freelocale; + +extern "c" fn setenv(name: ?[*]const u8, value: ?[*]const u8, overwrite: c_int) c_int; +extern "c" fn unsetenv(name: ?[*]const u8) c_int; |
