Minimal Unicode‑aware & ANSI‑aware string library (header‑only, class‑less)
libstrui
is a tiny C++17 header‑only library for measuring terminal strings.
Think of it ascut
,sed
, orwc
, but escape‑safe, UTF‑8‑capable (incl. wide & combined emojis).
Ready to drop in one header.
- UTF‑8 aware (powered by
libunistring
) - ANSI / escape‑safe cleaning & width detection
- Visual width handling (CJK, wide & combined emojis)
- Line height calculation
- Substring occurrence counting
- Split & repeat helpers
- Zero‑dependency at runtime, header‑only at build time
- Plays nicely with any POSIX shell tool or C++ project
Just include the header and go:
#include <StrUI.hpp> // path relative to your project
#include <iostream>
int main() {
std::cout << strui::width("\033[31m1🛑4\033[0m") << std::endl; // → 4
}
Add the include directory to your compiler flags, e.g.:
g++ -std=c++17 -I/path/to/libstrui/include example.cpp -lunistring
Note:
libunistring
is only required at link time when you compile the optional.cpp
tests.
The header does#ifdef
guards so you can keep your project header‑only if you already linklibunistring
.
Function | Description |
---|---|
strui::width(str) |
Return visual width (columns) of str |
strui::height(str) |
Return number of lines in str |
strui::clean(str) |
Remove ANSI escape sequences |
strui::split(str, sep) |
Vector‑split str on sep |
strui::join(str, sep = "") |
Return merged std::string (optional sep ) |
strui::repeat(count, str, sep = "") |
Repeat str count times (optional sep ) |
strui::count(str, value) |
Count occurrences of value in str |
Display width
is measured incolumns
, ofcharacters
orstrings
, when output to a device that usesnon-proportional fonts
.
Note that for some rarely used characters the actualfonts
orterminal emulators
can use a differentwidth
. There is no mechanism for communicating thedisplay width
ofcharacters
across a Unix pseudo-terminal (tty
).
Also, there are scripts with complex rendering, like theIndic
scripts. For these scripts, there is no such concept asnon-proportional fonts
. Therefore the results of these functions usually work fine on most scripts and on mostcharacters
but can fail to represent the actualdisplay width
.
📚 See libunistring documentation – Display width <uniwidth.h> for details on how display width is computed.
No build is needed to use the library ✌️.
But you can build and run the library test program:
# run unit tests locally + inside an Ubuntu container (requires Docker)
sh tests/run.sh
libunistring
≥ 1.1 (link‑time only)- Any C++17 compiler (
g++
,clang++
, …)
libstrui/
├── LICENCE.txt
├── README.md
├── include/
│ └── strui.hpp
└── tests/
├── run.sh
├── Makefile
├── test.cpp
└── Dockerfile
Made with ❤️ by @guillaumeast