Naive question about unicode characters in terminals

โš“ rust    ๐Ÿ“… 2025-05-26    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 4      

surdeus

Warning

This post was published 34 days ago. The information described in this article may have changed.

I have an application that prints an accounts table in a terminal. I tried to use a lock (':locked:`') to signify that an account is locked, but it made the table columns shift by one character.

I'm aware of the difference between s.len() and s.chars().count(), and I am using the latter to calculate the spaces between table cells.

To illustrate the issue, I use this:

fn main() {
  let foo = "foo "; // one space after foo
  let bar = "bar๐Ÿ”’"; // single lock emoji after bar, no spaces

  println!("{} (blen={}, clen={})", foo, foo.len(), foo.chars().count());
  println!("{} (blen={}, clen={})", bar, bar.len(), bar.chars().count());
}

In the Terminal app on macos, the two lines become misaligned, despite them containing the same number of "characters".

I assume that what's going on is that the emoji simply take up too much space, so the terminal allows it to take up two character cells instead of one. Is there a way for me to control for that? I.e. is this something embedded in unicode or is it entirely up to the terminal? If it's something specified by unicode, is there any way for me to determine how many fixed character cells a "character" will take? Specifically, is there a Rust crate for this?

Or is this just one of those "For your own sanity, don't use emojis in monospace environments" life lessons?

2 posts - 2 participants

Read full topic

๐Ÿท๏ธ rust_feed