Skip to content

Commit

Permalink
Check LANG env in kime-check (#320)
Browse files Browse the repository at this point in the history
* Check LANG env in kime-check

* Make check message more pretty
  • Loading branch information
Riey authored Feb 25, 2021
1 parent 440b516 commit 38d64f6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/tools/check/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
38 changes: 29 additions & 9 deletions src/tools/check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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!(),
Expand All @@ -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 {
Expand Down Expand Up @@ -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",
),
}
}
}
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 38d64f6

Please sign in to comment.