From 94caa7667da2baba3c762e661a84a806d2212731 Mon Sep 17 00:00:00 2001 From: Theodore Dubois Date: Wed, 13 Dec 2017 09:52:36 -0800 Subject: Remove the launcher and set the locale/current directory in alacritty (#879) --- src/locale.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/locale.rs (limited to 'src/locale.rs') diff --git a/src/locale.rs b/src/locale.rs new file mode 100644 index 00000000..03ae99d3 --- /dev/null +++ b/src/locale.rs @@ -0,0 +1,47 @@ +// Copyright 2016 Joe Wilm, The Alacritty Project Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#![cfg(target_os = "macos")] +use std::os::raw::c_char; +use std::slice; +use std::str; +use std::env; + +use objc::runtime::{Class, Object}; + +pub fn set_locale_environment() { + let locale_id = unsafe { + let locale_class = Class::get("NSLocale").unwrap(); + let locale: *const Object = msg_send![locale_class, currentLocale]; + msg_send![locale_class, release]; + let identifier: *const Object = msg_send![locale, localeIdentifier]; + msg_send![locale, release]; + let identifier_str = nsstring_as_str(identifier).to_owned(); + msg_send![identifier, release]; + identifier_str + }; + let locale_id = locale_id + ".UTF-8"; + env::set_var("LANG", &locale_id); + env::set_var("LC_CTYPE", &locale_id); +} + +const UTF8_ENCODING: usize = 4; + +unsafe fn nsstring_as_str<'a>(nsstring: *const Object) -> &'a str { + let cstr: *const c_char = msg_send![nsstring, UTF8String]; + let len: usize = msg_send![nsstring, lengthOfBytesUsingEncoding:UTF8_ENCODING]; + str::from_utf8(slice::from_raw_parts(cstr as *const u8, len)).unwrap() +} + +#[cfg(not(target_os = "macos"))] +pub fn set_locale_environment() {} -- cgit v1.2.3