summaryrefslogtreecommitdiff
path: root/git_crecord/encoding.py
blob: d023ff17c4dfb272bc8969f019d719717b277665 (plain)
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
# Unicode string width calculator
#
#  Copyright 2009, 2010 Matt Mackall <mpm@selenic.com>
#  Copyright 2010, 2011 FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
#  Copyright 2011 Augie Fackler <durin42@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later

import unicodedata


# How to treat ambiguous-width characters. Set to 'WFA' to treat as wide.
wide = "WF"


def ucolwidth(d: str) -> int:
    """Find the column width of a Unicode string for display"""
    eaw = getattr(unicodedata, 'east_asian_width', None)
    if eaw is not None:
        return sum([eaw(c) in wide and 2 or 1 for c in d])
    return len(d)