Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANSI escape sequenceの出力に制限をかける #386

Merged
merged 24 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
75b4120
ASCI escape sequenceの出力に制限をかける
qryxip Jan 19, 2023
8a2748f
不要なlifetime指定を削除
qryxip Jan 19, 2023
b32a4ab
`ansi`をインラインに
qryxip Jan 19, 2023
7495a27
順番の入れ替え
qryxip Jan 19, 2023
9036eb4
`ENABLE_VIRTUAL_TERMINAL_PROCESSING`を問い合わせる
qryxip Jan 19, 2023
b9393a8
`dependencies`への追加が抜けていたので追加
qryxip Jan 20, 2023
30a19aa
`windows/Win32_System_Console`を有効化
qryxip Jan 20, 2023
66e3acf
`AsRawhandle`を使う
qryxip Jan 20, 2023
978334d
`bool`じゃなくて`BOOL`なので`Into`する
qryxip Jan 20, 2023
ded168a
Remove dead code
qryxip Jan 20, 2023
27b9288
`addr_of!` → `addr_of_mut!`
qryxip Jan 20, 2023
4b3d48e
トレイトをuseする
qryxip Jan 20, 2023
b1a0ca3
`mode`も必要だった
qryxip Jan 20, 2023
66f4854
Revert "`mode`も必要だった"
qryxip Jan 26, 2023
8e7f43d
Revert "トレイトをuseする"
qryxip Jan 26, 2023
068f161
Revert "`addr_of!` → `addr_of_mut!`"
qryxip Jan 26, 2023
3147414
Revert "Remove dead code"
qryxip Jan 26, 2023
4d9da6b
Revert "`bool`じゃなくて`BOOL`なので`Into`する"
qryxip Jan 26, 2023
08d0021
Revert "`AsRawhandle`を使う"
qryxip Jan 26, 2023
bd7e219
Revert "`windows/Win32_System_Console`を有効化"
qryxip Jan 26, 2023
2117311
Revert "`dependencies`への追加が抜けていたので追加"
qryxip Jan 26, 2023
8ca51d2
Revert "`ENABLE_VIRTUAL_TERMINAL_PROCESSING`を問い合わせる"
qryxip Jan 26, 2023
d0015a9
PowerShellっぽかったら許可する
qryxip Jan 26, 2023
3a2c5ef
Merge branch 'main' into limit-ansi-escape-sequence
qryxip Jan 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 92 additions & 4 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 crates/voicevox_core_c_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ directml = ["voicevox_core/directml"]

[dependencies]
voicevox_core.workspace = true
is-terminal = "0.4.2"
libc = "0.2.134"
once_cell.workspace = true
serde_json.workspace = true
Expand Down
43 changes: 32 additions & 11 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
mod compatible_engine;
mod helpers;
use self::helpers::*;
use is_terminal::IsTerminal;
use libc::c_void;
use once_cell::sync::Lazy;
use std::env;
use std::ffi::{CStr, CString};
use std::io::{self, Write};
use std::os::raw::c_char;
use std::ptr::null;
use std::sync::{Mutex, MutexGuard};
use std::{env, io};
use tracing_subscriber::EnvFilter;
use voicevox_core::AudioQueryModel;
use voicevox_core::Result;
Expand All @@ -20,16 +22,35 @@ use rstest::*;
type Internal = VoicevoxCore;

static INTERNAL: Lazy<Mutex<Internal>> = Lazy::new(|| {
let _ = 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_writer(io::stderr)
.try_init();

Internal::new_with_mutex()
let _ = init_logger();
return Internal::new_with_mutex();

fn init_logger() -> std::result::Result<(), impl Sized> {
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_ansi(out().is_terminal() && env_allows_ansi())
.with_writer(out)
.try_init()
}

fn out() -> impl IsTerminal + Write {
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()
}
});

pub(crate) fn lock_internal() -> MutexGuard<'static, Internal> {
Expand Down