From 38d64f67ba29330a4c67dd7cec12c733c17bf5b1 Mon Sep 17 00:00:00 2001 From: Riey Date: Fri, 26 Feb 2021 00:43:37 +0900 Subject: [PATCH] Check LANG env in kime-check (#320) * Check LANG env in kime-check * Make check message more pretty --- Cargo.lock | 16 ++++++++++++++++ docs/CHANGELOG.md | 1 + src/tools/check/Cargo.toml | 1 + src/tools/check/src/main.rs | 38 ++++++++++++++++++++++++++++--------- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de828b2a..08099e17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -615,6 +615,7 @@ dependencies = [ "ansi_term", "kime-engine-cffi", "kime-engine-core", + "pad", "serde_yaml", "strum 0.20.0", "xdg", @@ -907,6 +908,15 @@ version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" +[[package]] +name = "pad" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ad9b889f1b12e0b9ee24db044b5129150d5eada288edc800f789928dc8c0e3" +dependencies = [ + "unicode-width", +] + [[package]] name = "pango-sys" version = "0.10.0" @@ -1374,6 +1384,12 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.2.1" diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e99969dc..f16b6420 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -11,6 +11,7 @@ * Make character typing order strict `ㅏ + ㄱ = ㅏㄱ` * Add `TreatJongseongAsChoseong` addon * Add `FlexibleComposeOrder` addon [#318](https://github.com/Riey/kime/issues/318) +* Check LANG env in kime-check [#317](https://github.com/Riey/kime/issues/317) ## 1.1.3 diff --git a/src/tools/check/Cargo.toml b/src/tools/check/Cargo.toml index c2169ec5..d259b5eb 100644 --- a/src/tools/check/Cargo.toml +++ b/src/tools/check/Cargo.toml @@ -9,6 +9,7 @@ license = "GPL-3.0" ansi_term = "0.12.1" kime-engine-cffi = { path = "../../engine/cffi" } kime-engine-core = { path = "../../engine/core" } +pad = "0.1.6" serde_yaml = "0.8.17" strum = { version = "0.20.0", features = ["derive"] } xdg = "2.2.0" diff --git a/src/tools/check/src/main.rs b/src/tools/check/src/main.rs index b0cd1e9f..9da94c44 100644 --- a/src/tools/check/src/main.rs +++ b/src/tools/check/src/main.rs @@ -3,6 +3,7 @@ use kime_engine_cffi::{ Config, InputEngine, InputResult_CONSUMED, InputResult_HAS_PREEDIT, InputResult_NEED_FLUSH, InputResult_NEED_RESET, }; +use pad::PadStr; use std::env; use strum::{EnumIter, EnumMessage, IntoEnumIterator, IntoStaticStr}; @@ -28,7 +29,11 @@ impl CondResult { pub fn print(&self, message: &str) { let c = self.color(); - print!("{:<10} {:<30}", c.paint(<&str>::from(self)), message); + print!( + "{} {}", + c.paint(<&str>::from(self).pad_to_width(8)), + Color::White.bold().paint(message.pad_to_width(30)) + ); match self { CondResult::Ok => println!(), @@ -47,12 +52,14 @@ enum Check { Config, #[strum(message = "Engine works")] EngineWorks, - #[strum(message = "XMODIFIERS env")] + #[strum(message = "XMODIFIERS has @im=kime")] XModifier, - #[strum(message = "GTK_IM_MODULE env")] + #[strum(message = "GTK_IM_MODULE has kime")] GtkImModule, - #[strum(message = "QT_IM_MODULE env")] + #[strum(message = "QT_IM_MODULE has kime")] QtImModule, + #[strum(message = "LANG has UTF-8")] + Lang, } impl Check { @@ -121,11 +128,24 @@ impl Check { CondResult::Ok } Check::XModifier => match env::var("XDG_SESSION_TYPE").unwrap().as_str() { - "x11" => check_var("XMODIFIERS", "@im=kime", "set XMODIFIERS=@im=kime"), + "x11" => check_var( + "XMODIFIERS", + |v| v.contains("@im=kime"), + "set XMODIFIERS=@im=kime", + ), other => CondResult::Ignore(format!("Session type is {} not x11", other)), }, - Check::GtkImModule => check_var("GTK_IM_MODULE", "kime", "set GTK_IM_MODULE=kime"), - Check::QtImModule => check_var("QT_IM_MODULE", "kime", "set QT_IM_MODULE=kime"), + Check::GtkImModule => { + check_var("GTK_IM_MODULE", |v| v == "kime", "set GTK_IM_MODULE=kime") + } + Check::QtImModule => { + check_var("QT_IM_MODULE", |v| v == "kime", "set QT_IM_MODULE=kime") + } + Check::Lang => check_var( + "LANG", + |v| v.to_ascii_lowercase().ends_with("utf-8"), + "set LANG encoding UTF-8", + ), } } } @@ -177,8 +197,8 @@ fn check_input( CondResult::Ok } -fn check_var(name: &str, value: &str, reason: &str) -> CondResult { - if env::var(name).map_or(false, |v| v.contains(value)) { +fn check_var(name: &str, pred: impl Fn(&str) -> bool, reason: &str) -> CondResult { + if env::var(name).map_or(false, |v| pred(&v)) { CondResult::Ok } else { CondResult::Fail(reason.into())