cwcwidth/README.md

56 lines
1.4 KiB
Markdown
Raw Normal View History

2021-01-22 23:51:11 +01:00
# Python bindings for wc(s)width
`cwcwidth` provides Python bindings for `wcwidth` and `wcswidth` functions defined in POSIX.1-2001
2021-01-23 16:12:48 +01:00
and POSIX.1-2008 based on [Cython](https://cython.org/). These functions compute the printable
length of a unicode character/string on a terminal. The module provides the same functions as
[wcwidth](https://pypi.org/project/wcwidth/) and its behavior is compatible.
2021-01-23 15:31:18 +01:00
On systems not conforming to POSIX.1-2001 and POSIX.1-2008, Markus Kuhn's
[implementation](https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c) is used to provide the
functionality.
2021-01-22 23:51:11 +01:00
## Dependencies
* `Cython >= 0.28` (optional, only for building). If Cython is not available, the C files are not
regenerated from their source.
2021-01-22 23:51:11 +01:00
2021-01-23 15:31:18 +01:00
## Quick installation guide
2021-01-22 23:51:11 +01:00
`cwcwidth` can be installed via `pip`:
```sh
pip install cwcwidth
```
or by running:
```sh
python3 setup.py install
```
## Usage
```python3
2021-02-06 22:08:37 +01:00
>>> import cwcwidth
>>> cwcwidth.wcwidth("a")
1
>>> cwcwidth.wcswidth("コ")
2
>>> cwcwidth.wcswidth("コンニチハ, セカイ!")
19
>>> cwcwidth.wcswidth("コンニチハ, セカイ!", 5)
10
2021-01-22 23:51:11 +01:00
```
## Comparison with `wcwidth`
```python3
>>> import wcwidth, cwcwidth, timeit
>>> timeit.timeit(lambda: wcwidth.wcswidth("コンニチハ, セカイ!"))
19.14463168097427
>>> timeit.timeit(lambda: cwcwidth.wcswidth("コンニチハ, セカイ!"))
0.16294104099506512
```
## License
2021-02-05 21:42:57 +01:00
The code is licensed under the MIT license.
2021-02-06 21:49:58 +01:00