Skip to main content

wedi_core/utils/
mod.rs

1mod ansi_slice;
2mod line_wrapper;
3
4pub use ansi_slice::slice_ansi_text;
5#[allow(unused_imports)]
6pub use line_wrapper::LineWrapper;
7
8use std::sync::atomic::{AtomicBool, Ordering};
9use unicode_width::UnicodeWidthChar;
10
11/// 全局調試模式標誌,支持運行時通過 --debug 參數啟用
12static DEBUG_MODE: AtomicBool = AtomicBool::new(false);
13
14/// 設置調試模式
15#[allow(dead_code)]
16pub fn set_debug_mode(enabled: bool) {
17    DEBUG_MODE.store(enabled, Ordering::Relaxed);
18}
19
20/// 檢查是否啟用調試模式
21pub fn is_debug_mode() -> bool {
22    DEBUG_MODE.load(Ordering::Relaxed)
23}
24
25/// 調試日誌宏,支持編譯時和運行時調試模式
26/// - 編譯時:cfg!(debug_assertions) 自動啟用
27/// - 運行時:可通過 --debug 參數啟用
28///
29///   支持格式化參數,使用方式與 println! 相同
30#[macro_export]
31macro_rules! debug_log {
32    ($($arg:tt)*) => {
33        if cfg!(debug_assertions) || $crate::utils::is_debug_mode() {
34            eprintln!("[DEBUG] {}", format_args!($($arg)*));
35        }
36    };
37}
38
39/// 計算字符串的視覺寬度(考慮寬字元)
40/// 中文字元等寬字元會正確計算為 2,ASCII 字元計算為 1
41pub fn visual_width(s: &str) -> usize {
42    s.chars()
43        .map(|ch| UnicodeWidthChar::width(ch).unwrap_or(1))
44        .sum()
45}
46
47/// 計算單個字符的視覺寬度
48#[allow(dead_code)]
49pub fn char_width(ch: char) -> usize {
50    UnicodeWidthChar::width(ch).unwrap_or(1)
51}