Skip to content

Commit

Permalink
ANSI escape sequenceの出力の判断を完全にする (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip authored Sep 17, 2023
1 parent bf70839 commit 814d270
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
52 changes: 52 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/voicevox_core_c_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ name = "e2e"
directml = ["voicevox_core/directml"]

[dependencies]
anstream = { version = "0.5.0", default-features = false, features = ["auto"] }
anstyle-query = "1.0.0"
colorchoice = "1.0.0"
cstr = "0.2.11"
derive-getters.workspace = true
itertools.workspace = true
Expand Down
35 changes: 22 additions & 13 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ use self::drop_check::C_STRING_DROP_CHECKER;
use self::helpers::*;
use self::result_code::VoicevoxResultCode;
use self::slice_owner::U8_SLICE_OWNER;
use anstream::{AutoStream, RawStream};
use chrono::SecondsFormat;
use colorchoice::ColorChoice;
use derive_getters::Getters;
use once_cell::sync::Lazy;
use std::env;
use std::ffi::{CStr, CString};
use std::fmt;
use std::io::{self, IsTerminal, Write};
use std::io;
use std::os::raw::c_char;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex, MutexGuard};
Expand All @@ -37,14 +39,31 @@ static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
let _ = init_logger();

fn init_logger() -> std::result::Result<(), impl Sized> {
let ansi = {
// anstyle系のクレートを利用して次の2つを行う。
//
// * ANSI escape codeを出してよいかの判定(環境変数のチェックとisatty)
// * 必要であれば`ENABLE_VIRTUAL_TERMINAL_PROCESSING`の有効化

assert_eq!(
ColorChoice::Auto,
ColorChoice::global(),
"`ColorChoice::write_global` should not have been called",
);

AutoStream::choice(&out()) != ColorChoice::Never
&& anstyle_query::term_supports_ansi_color()
&& anstyle_query::windows::enable_ansi_colors().unwrap_or(true)
};

tracing_subscriber::fmt()
.with_env_filter(if env::var_os(EnvFilter::DEFAULT_ENV).is_some() {
EnvFilter::from_default_env()
} else {
"error,voicevox_core=info,voicevox_core_c_api=info,onnxruntime=info".into()
})
.with_timer(local_time as fn(&mut Writer<'_>) -> _)
.with_ansi(out().is_terminal() && env_allows_ansi())
.with_ansi(ansi)
.with_writer(out)
.try_init()
}
Expand All @@ -55,20 +74,10 @@ static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
wtr.write_str(&chrono::Local::now().to_rfc3339_opts(SecondsFormat::Micros, false))
}

fn out() -> impl IsTerminal + Write {
fn out() -> impl RawStream {
io::stderr()
}

fn env_allows_ansi() -> bool {
// https://docs.rs/termcolor/1.2.0/src/termcolor/lib.rs.html#245-291
// ただしWindowsではPowerShellっぽかったらそのまま許可する。
// ちゃんとやるなら`ENABLE_VIRTUAL_TERMINAL_PROCESSING`をチェックするなり、そもそも
// fwdansiとかでWin32の色に変換するべきだが、面倒。
env::var_os("TERM").map_or(
cfg!(windows) && env::var_os("PSModulePath").is_some(),
|term| term != "dumb",
) && env::var_os("NO_COLOR").is_none()
}
Runtime::new().unwrap()
});

Expand Down

0 comments on commit 814d270

Please sign in to comment.