summaryrefslogtreecommitdiff
path: root/src/unistring.h
diff options
context:
space:
mode:
authorKan-Ru Chen (陳侃如) <kanru@kanru.info>2014-02-13 18:40:06 +0800
committerKan-Ru Chen (陳侃如) <kanru@kanru.info>2014-02-13 18:40:06 +0800
commitd5b5ea02138107918642532a266550d1ab4122d3 (patch)
tree58ec47b437965f551416bfd3d125a542cd382d8e /src/unistring.h
parentc59aadaace0b142eacb89d40921ac331887bb671 (diff)
downloadfork-ledger-d5b5ea02138107918642532a266550d1ab4122d3.tar.gz
fork-ledger-d5b5ea02138107918642532a266550d1ab4122d3.tar.bz2
fork-ledger-d5b5ea02138107918642532a266550d1ab4122d3.zip
Correctly justify Unicode characters in terminal
Many Unicode characters take more spaces than one ASCII character. For example, Chinese characters are two characters wide when using monospace font in terminal. This patch use wcwidth of Markus Kuhn to count the correct width for justification.
Diffstat (limited to 'src/unistring.h')
-rw-r--r--src/unistring.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/unistring.h b/src/unistring.h
index 8e963f37..7d78d134 100644
--- a/src/unistring.h
+++ b/src/unistring.h
@@ -44,6 +44,8 @@
namespace ledger {
+int mk_wcwidth_cjk(boost::uint32_t ucs);
+
/**
* @class unistring
*
@@ -81,6 +83,14 @@ public:
return utf32chars.size();
}
+ std::size_t width() const {
+ std::size_t width = 0;
+ foreach (const boost::uint32_t& ch, utf32chars) {
+ width += mk_wcwidth_cjk(ch);
+ }
+ return width;
+ }
+
std::string extract(const std::string::size_type begin = 0,
const std::string::size_type len = 0) const
{
@@ -133,7 +143,7 @@ inline void justify(std::ostream& out,
unistring temp(str);
- int spacing = width - int(temp.length());
+ int spacing = width - int(temp.width());
while (spacing-- > 0)
out << ' ';