center
reads input from stdin
, calculates the width of each line
(considering multibyte characters), and center-aligns the text within the
terminal window. Fully supports UTF-8.
Originally by Lizzy Fleckenstein 🤍
- Terminal size detection: Adjusts text to fit the terminal width.
- Multibyte support: Properly handles UTF-8 Unicode characters.
- Text centering: Prints each line centered within the terminal.
- Error handling: Graceful handling of system call failures.
If you are building center
, you'll need:
- A C compiler that supports C99 or later.
If you have Homebrew installed, just run:
brew install Neved4/homebrew-tap/center
center
operates as follows:
- Opens
/dev/tty
to get the terminal width. - Reads input line-by-line.
- Calculates character width (supporting multibyte characters).
- Prints each line centered in the terminal.
Below is an example of how the center
command works in practice:
$ echo 'Hello World!' | center
Hello World!
Runs on Linux, macOS and *BSD systems on x86_64
and
arm64
, and compiles with zig
, clang
, gcc
, tcc
and any other
compiler that supports C99 or later.
center
is compatible with POSIX.1-20241 as well as the C232 standard.
For further reference and related tools, check out the following:
center
is licensed under the terms of the GPLv3 License.
See the LICENSE file for details.
Footnotes
-
IEEE Std 1003.1-2024: Standard for Information Technology — Portable Operating System Interface (POSIX®),
ISO/IEC DIS 9945. URL: https://pubs.opengroup.org/onlinepubs/9799919799/ ↩ -
ISO/IEC 9899: Standard for Information Technology — Programming languages — C, ISO/IEC 9899:2023.
URL: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf ↩