Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors
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
{ 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 ✅ |
width_of
doesn't correctly count combining characters.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 ) + '#' # --> #𪜀�…#
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.
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
).
These packages have also been considered:
FAQs
The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors
We found that to-width demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.