All Projects → janlelis → Unicode Display_width

janlelis / Unicode Display_width

Licence: mit
Monospace Unicode character width in Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Unicode Display width

Ansiweather
Weather in terminal, with ANSI colors and Unicode symbols
Stars: ✭ 1,663 (+1596.94%)
Mutual labels:  terminal, unicode
Unibits
Visualize different Unicode encodings in the terminal
Stars: ✭ 125 (+27.55%)
Mutual labels:  terminal, unicode
Plotille
Plot in the terminal using braille dots.
Stars: ✭ 99 (+1.02%)
Mutual labels:  terminal, unicode
Git Praise
A nicer git blame.
Stars: ✭ 24 (-75.51%)
Mutual labels:  terminal, unicode
Figures
Unicode symbols with Windows CMD fallbacks
Stars: ✭ 438 (+346.94%)
Mutual labels:  terminal, unicode
Cowsay Files
A collection of additional/alternative cowsay files.
Stars: ✭ 216 (+120.41%)
Mutual labels:  terminal, unicode
Box Cli Maker
Make Highly Customized Boxes for your CLI
Stars: ✭ 115 (+17.35%)
Mutual labels:  terminal, unicode
Cmd2
cmd2 - quickly build feature-rich and user-friendly interactive command line applications in Python
Stars: ✭ 342 (+248.98%)
Mutual labels:  terminal, unicode
Unicodeplots.jl
Unicode-based scientific plotting for working in the terminal
Stars: ✭ 724 (+638.78%)
Mutual labels:  terminal, unicode
Lehar
Visualize data using relative ordering
Stars: ✭ 81 (-17.35%)
Mutual labels:  terminal, unicode
Dotfiles
👾 ~/
Stars: ✭ 91 (-7.14%)
Mutual labels:  terminal
String Extra
Unicode/String support for Twig
Stars: ✭ 92 (-6.12%)
Mutual labels:  unicode
Gay
Colour your text / terminal to be more gay. 🏳️‍🌈
Stars: ✭ 95 (-3.06%)
Mutual labels:  terminal
Tooling
Advancing Node.js as a framework for writing great tools
Stars: ✭ 98 (+0%)
Mutual labels:  terminal
Termtosvg
Record terminal sessions as SVG animations
Stars: ✭ 9,310 (+9400%)
Mutual labels:  terminal
Normality
A tiny library for Python text normalisation. Useful for ad-hoc text processing.
Stars: ✭ 94 (-4.08%)
Mutual labels:  unicode
Asciinema
Terminal session recorder 📹
Stars: ✭ 9,880 (+9981.63%)
Mutual labels:  terminal
Run
⚡The resource runtime
Stars: ✭ 90 (-8.16%)
Mutual labels:  terminal
Coinpare
Compare cryptocurrency trading data across multiple exchanges and blockchains in the comfort of your terminal
Stars: ✭ 89 (-9.18%)
Mutual labels:  terminal
Cancer
It's terminal.
Stars: ✭ 98 (+0%)
Mutual labels:  terminal

Unicode::DisplayWidth [version]

Determines the monospace display width of a string in Ruby. Implementation based on EastAsianWidth.txt and other data, 100% in Ruby. It does not rely on the OS vendor (like wcwidth()) to provide an up-to-date method for measuring string width.

Unicode version: 13.0.0 (March 2020)

Supported Rubies: 3.0, 2.7, 2.6, 2.5

Old Rubies which might still work: 2.4, 2.3, 2.2, 2.1, 2.0, 1.9

Version 2.0 — Breaking Changes

Some features of this library were marked deprecated for a long time and have been removed with Version 2.0:

  • Aliases of display_width (…_size, …_length) have been removed
  • Auto-loading of string core extension has been removed:

If you are relying on the String#display_width string extension to be automatically loaded (old behavior), please load it explicitly now:

require "unicode/display_width/string_ext"

You could also change your Gemfile line to achieve this:

gem "unicode-display_width", require: "unicode/display_width/string_ext"

Introduction to Character Widths

Guessing the correct space a character will consume on terminals is not easy. There is no single standard. Most implementations combine data from East Asian Width, some General Categories, and hand-picked adjustments.

How this Library Handles Widths

Further at the top means higher precedence. Please expect changes to this algorithm with every MINOR version update (the X in 1.X.0)!

Width Characters Comment
X (user defined) Overwrites any other values
-1 "\b" Backspace (total width never below 0)
0 "\0", "\x05", "\a", "\n", "\v", "\f", "\r", "\x0E", "\x0F" C0 control codes that do not change horizontal width
1 "\u{00AD}" SOFT HYPHEN
2 "\u{2E3A}" TWO-EM DASH
3 "\u{2E3B}" THREE-EM DASH
0 General Categories: Mn, Me, Cf (non-arabic) Excludes ARABIC format characters
0 "\u{1160}".."\u{11FF}" HANGUL JUNGSEONG
0 "\u{2060}".."\u{206F}", "\u{FFF0}".."\u{FFF8}", "\u{E0000}".."\u{E0FFF}" Ignorable ranges
2 East Asian Width: F, W Full-width characters
2 "\u{3400}".."\u{4DBF}", "\u{4E00}".."\u{9FFF}", "\u{F900}".."\u{FAFF}", "\u{20000}".."\u{2FFFD}", "\u{30000}".."\u{3FFFD}" Full-width ranges
1 or 2 East Asian Width: A Ambiguous characters, user defined, default: 1
1 All other codepoints -

Install

Install the gem with:

$ gem install unicode-display_width

Or add to your Gemfile:

gem 'unicode-display_width'

Usage

Classic API

require 'unicode/display_width'

Unicode::DisplayWidth.of("⚀") # => 1
Unicode::DisplayWidth.of("一") # => 2

Ambiguous Characters

The second parameter defines the value returned by characters defined as ambiguous:

Unicode::DisplayWidth.of("·", 1) # => 1
Unicode::DisplayWidth.of("·", 2) # => 2

Custom Overwrites

You can overwrite how to handle specific code points by passing a hash (or even a proc) as third parameter:

Unicode::DisplayWidth.of("a\tb", 1, "\t".ord => 10)) # => tab counted as 10, so result is 12

Emoji Support

Emoji width support is included, but in must be activated manually. It will adjust the string's size for modifier and zero-width joiner sequences. You also need to add the unicode-emoji gem to your Gemfile:

gem 'unicode-display_width'
gem 'unicode-emoji'

Enable the emoji string width adjustments by passing emoji: true as fourth parameter:

Unicode::DisplayWidth.of "🤾🏽‍♀️" # => 5
Unicode::DisplayWidth.of "🤾🏽‍♀️", 1, {}, emoji: true # => 2

Usage with String Extension

require 'unicode/display_width/string_ext'

"⚀".display_width # => 1
'一'.display_width # => 2

Modern API: Keyword-arguments Based Config Object

Version 2.0 introduces a keyword-argument based API, which allows you to save your configuration for later-reuse. This requires an extra line of code, but has the advantage that you'll need to define your string-width options only once:

require 'unicode/display_width'

display_width = Unicode::DisplayWidth.new(
  # ambiguous: 1,
  overwrite: { "A".ord => 100 },
  emoji: true,
)

display_width.of "⚀" # => 1
display_width.of "🤾🏽‍♀️" # => 2
display_width.of "A" # => 100

Usage From the CLI

Use this one-liner to print out display widths for strings from the command-line:

$ gem install unicode-display_width
$ ruby -r unicode/display_width -e 'puts Unicode::DisplayWidth.of $*[0]' -- "一"

Replace "一" with the actual string to measure

Other Implementations & Discussion

See unicode-x for more Unicode related micro libraries.

Copyright & Info

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].