Code Monkey home page Code Monkey logo

to-width's Introduction

To-Width

The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors.

Table of Contents generated with DocToc

Usage

{ to_width, width_of, } = require 'to-width'

width_of is provided by sindresorhus/string-width; it provides a fairly reliable way to determine the width of strings on character devices. All people who deal with string.length, encodings and buffers in JavaScript will enjoy the following table:

string string.length Buffer.byteLength string width_of string
'abcd' 4 ✅ 4 ✅ 4 ✅
'äöüß' 4 ✅ 8 ❌ 4 ✅
'äöüß' (using combining diacritics) 7 (✅) 11 ❌ 7 ❌
'北京' 2 (✅) 6 ❌ 4 ✅
'𪜀𪜁' 4 ❌ 8 ❌ 4 ✅

Bugs

  • width_of doesn't correctly count combining characters.
  • 32bit Unicode glyphs (those from the 'Astral Planes') may be split by to_width, and combining diacritics may get lost:
'#' + ( to_width 'abcdabcd', 4 ) + '#' # --> #abc…#
'#' + ( to_width 'äöüßäöüß', 4 ) + '#' # --> #äöü…#
'#' + ( to_width 'äöüßäöüß', 4 ) + '#' # --> #äöu…#
'#' + ( to_width '北京北京', 4 ) + '#' # --> #北……#
'#' + ( to_width '𪜀𪜁𪜀𪜁', 4 ) + '#' # --> #𪜀�…#

Why?

When I needed tabular data display on the command line, I got dissatisfied with existing solutions. There are some promising modules for doing this on npm, but nothing satisfied me in the end.

I realized that the key requirement for doing tables in the terminal is the ability to format data so that each chunk of text (that you build table cells with) has exactly the correct visual width. Actually, string length fitting seems to have become quite the rage among people these days, at least judging by the recent left-pad hype.

How?

The core functionality of this module has been implemented using

These two modules do the heavy lifting (looking for wide characters, combining characters, and ANSI color codes); to-width does only a little bit of glueing (and fixing providing a workaround for a minor bug in wcstring).

Similar

These packages have also been considered:

to-width's People

Contributors

loveencounterflow avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.