UTF-8 encoded strings are not interpreted
Hologos opened this issue · comments
Hi,
I finally migrated my dotfiles to ansi
library. I am using ✔
as mark that my commits are being signed. Sadly, I am currently not able to print this mark using UTF-8 encoded syntax.
ansi --bold --color=64 "\xE2\x9C\x94"
prints \xE2\x9C\x94
instead of ✔
.
You don't just have a UTF-8 encoded string. Instead, you have a Unicode character encoded as UTF-8, then escaped as hex characters. You need to decode those hex characters. As proof, here's the same string passed to echo
.
What you need to do is to convert the 12 characters of encoded hex into the 3 bytes of UTF-8. Once you have that part correct, the ansi
command works just fine.
So, the input to ansi
needs to be the 3 UTF-8 encoded characters or the Unicode checkmark.
You could also store this information as variables in your shell scripts. It would be faster than invoking ansi
often. I learned this lesson with the Bash maze game when profiling how long it took to draw the maze screens. If you only show the checkbox occasionally, the time drain is minimal. When doing color code generation hundreds of times you will want to optimize it by storing the color codes in a variable when possible.
I hope this clears up the problem you were experiencing.