cjk_align/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! This library provides a wrapper struct [CJKAlign] to align CJK and emoji characters
//! correctly on terminals. Despite its name, it works for other Unicode characters too
//! as supported by the unicode-width crate.
//!
//! ```
//! use cjk_align::CJKAlign;
//!
//! assert_eq!(format!("{:6}", CJKAlign("你好")), "你好  ");
//! assert_eq!(format!("{:>6}", CJKAlign("你好")), "  你好");
//! assert_eq!(format!("{:^6}", CJKAlign("你好")), " 你好 ");
//! assert_eq!(format!("{:^7}", CJKAlign("你好")), "  你好 ");
//! ```
//!
//! To treat East Asian ambiguous width characters as double width, use
//! [CJKAlignWide] instead:
//!
//! ```
//! use cjk_align::{CJKAlign, CJKAlignWide};
//!
//! assert_eq!(format!("{:8}", CJKAlign("“……”")), "“……”    ");
//! assert_eq!(format!("{:8}", CJKAlignWide("“……”")), "“……”");
//! ```

use std::fmt::{Display, Formatter, Error, Alignment};

use unicode_width::UnicodeWidthStr;

pub struct CJKAlign<'a>(pub &'a str);
impl<'a> Display for CJKAlign<'a> {
  fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
    StringWidth::fmt(self, f)
  }
}

pub struct CJKAlignWide<'a>(pub &'a str);
impl<'a> Display for CJKAlignWide<'a> {
  fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
    StringWidth::fmt(self, f)
  }
}

trait StringWidth {
  fn width(&self) -> usize;
  fn str(&self) -> &str;

  fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
    let w = f.width().unwrap_or(0);
    if w == 0 {
      write!(f, "{}", self.str())
    } else {
      let self_width = self.width();
      let pad = w.saturating_sub(self_width);
      match f.align() {
        Some(Alignment::Left) | None => write!(f, "{}{}", self.str(), " ".repeat(pad)),
        Some(Alignment::Right) => write!(f, "{}{}", " ".repeat(pad), self.str()),
        Some(Alignment::Center) => {
          write!(f, "{}{}{}", " ".repeat(pad - pad / 2), self.str(), " ".repeat(pad / 2))
        },
      }
    }
  }
}

impl<'a> StringWidth for CJKAlign<'a> {
  fn width(&self) -> usize {
    UnicodeWidthStr::width(self.0)
  }

  fn str(&self) -> &str {
    self.0
  }
}

impl<'a> StringWidth for CJKAlignWide<'a> {
  fn width(&self) -> usize {
    UnicodeWidthStr::width_cjk(self.0)
  }

  fn str(&self) -> &str {
    self.0
  }
}