From 94caa7667da2baba3c762e661a84a806d2212731 Mon Sep 17 00:00:00 2001 From: Theodore Dubois Date: Wed, 13 Dec 2017 09:52:36 -0800 Subject: [PATCH] Remove the launcher and set the locale/current directory in alacritty (#879) --- Cargo.lock | 1 + Cargo.toml | 3 ++ assets/osx/Alacritty.app/Contents/Info.plist | 6 +-- .../osx/Alacritty.app/Contents/MacOS/launcher | 15 ------ src/lib.rs | 5 ++ src/locale.rs | 47 +++++++++++++++++++ src/main.rs | 8 ++++ 7 files changed, 67 insertions(+), 18 deletions(-) delete mode 100755 assets/osx/Alacritty.app/Contents/MacOS/launcher create mode 100644 src/locale.rs diff --git a/Cargo.lock b/Cargo.lock index 609c555a..8cc277e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,7 @@ dependencies = [ "mio 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", "mio-more 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "notify 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "objc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index cca84f27..12d16bc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,9 @@ env_logger = "0.4" [target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os="dragonfly", target_os="openbsd"))'.dependencies] x11-dl = "2" +[target.'cfg(target_os = "macos")'.dependencies] +objc = "0.2.2" + [features] default = [] # Enabling this feature makes shaders automatically reload when changed diff --git a/assets/osx/Alacritty.app/Contents/Info.plist b/assets/osx/Alacritty.app/Contents/Info.plist index 77053db1..9016ca4d 100644 --- a/assets/osx/Alacritty.app/Contents/Info.plist +++ b/assets/osx/Alacritty.app/Contents/Info.plist @@ -5,9 +5,9 @@ CFBundleDisplayName Alacritty CFBundleExecutable - launcher - - + alacritty + CFBundleIdentifier + io.alacritty CFBundleName Alacritty CFBundleIconFile diff --git a/assets/osx/Alacritty.app/Contents/MacOS/launcher b/assets/osx/Alacritty.app/Contents/MacOS/launcher deleted file mode 100755 index d3c10724..00000000 --- a/assets/osx/Alacritty.app/Contents/MacOS/launcher +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -# Dynamically discover canonical path to alacritty binary -BIN_DIR=$(cd "$(dirname "$0")"; pwd) - -# Query OS for locale and setup alacritty shell to conform -ALACRITTY_LOCALE="$(osascript -e "return user locale of (get system info)")" -export LANG="${ALACRITTY_LOCALE}.UTF-8" -export LC_CTYPE="${ALACRITTY_LOCALE}.UTF-8" - -# Start alacritty in user's home directory -cd "$HOME" - -# Engage -exec "$BIN_DIR/alacritty" diff --git a/src/lib.rs b/src/lib.rs index 081259cd..dc05fbdd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,10 @@ #[cfg(any(target_os = "linux", target_os = "freebsd", target_os="dragonfly", target_os="openbsd"))] extern crate x11_dl; +#[cfg(target_os = "macos")] +#[macro_use] +extern crate objc; + extern crate arraydeque; extern crate cgmath; extern crate copypasta; @@ -61,6 +65,7 @@ pub mod event_loop; pub mod grid; pub mod index; pub mod input; +pub mod locale; pub mod logging; pub mod meter; pub mod renderer; 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() {} diff --git a/src/main.rs b/src/main.rs index c18764d2..e9ff12d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,12 +24,15 @@ extern crate log; use std::error::Error; use std::sync::Arc; +use std::env; use alacritty::cli; use alacritty::config::{self, Config}; use alacritty::display::Display; use alacritty::event; use alacritty::event_loop::{self, EventLoop, Msg}; +#[cfg(target_os = "macos")] +use alacritty::locale; use alacritty::logging; use alacritty::sync::FairMutex; use alacritty::term::{Term}; @@ -41,6 +44,11 @@ fn main() { let options = cli::Options::load(); let config = load_config(&options); + // Switch to home directory + env::set_current_dir(env::home_dir().unwrap()).unwrap(); + #[cfg(target_os = "macos")] + locale::set_locale_environment(); + // Run alacritty if let Err(err) = run(config, &options) { die!("Alacritty encountered an unrecoverable error:\n\n\t{}\n", Red(err));