New v4.0.0 #36
Pinned
webdiscus
announced in
Announcements
Replies: 3 comments 4 replies
-
Thank you so much for describing all that, @webdiscus ! |
Beta Was this translation helpful? Give feedback.
1 reply
-
I tested beta 19 — no issues, smooth migration. |
Beta Was this translation helpful? Give feedback.
1 reply
-
when do you plan to release the final v4.0.0, @webdiscus ? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
✨ Ansis v4 - Smaller package and cleaner API
Ansis v4 drops unused duplicate aliases and legacy baggage.
This release brings a stable and more compact ANSI library.
v4 is ~15.7% smaller than v3.17
You can try it now with:
[email protected]
Follow the migration guide to upgrade.
1) Dropped support for Deno 1.x (EOL - October 9, 2024)
This version now supports Deno 2.0 and above.
2) Removed non-standard
strike
alias forstrikethrough
styleThe legacy
strike
alias has been removed to clean up the API and stay consistent with ANSI style conventions.strike
style was rarely (if ever) used and added unnecessary redundancy.ansis.strike()
was found in public GitHub repositories.strikethrough
name exclusively.3) Removed redundant aliases:
grey
,bgGrey
,blackBright
andbgBlackBright
- use the standardgray
andbgGray
instead.Holywar:
gray
vsgrey
vsblackBright
All these color names referred the same ANSI code.
However, keeping many separate names for the same color is too much for a small library.
Why
gray
only, without aliases?ANSI codes for the gray color:
90
is officially "bright black" foreground (i.e.,gray
) in terminal specs.100
is officially "bright black" background (i.e.,bgGray
) in terminal specs.Ansis prefers the more intuitive and commonly used names:
gray
andbgGray
.gray
,bgGray
- Standard spelling, common used, and intuitivegrey
,bgGrey
- British spelling, uncommon, rarely used, and a redundant alias forgray
andbgGray
blackBright
,bgBlackBright
- Spec-style names for "bright black", less intuitive, never used, awkward for practical useNote
Supporting both
gray
andgrey
(or even worse, verbose aliases likebgBlackBright
) introduces unnecessary duplication.Ansis v4 is focused on a clean, minimal API by intentionally avoiding redundant aliases.
4) Using 256-color functions
The following legacy method aliases have been removed:
ansi256(code)
fg(code)
bgAnsi256(code)
bg(code)
These aliases were originally added for compatibility with Chalk.
Starting with this release, Ansis focuses on a cleaner and compact API, free from duplicated methods and legacy layers.
Why
fg()
andbg()
are better thanansi256()
andbgAnsi256()
Ansis has grown beyond being a Chalk-compatible alternative - it's now a modern and compact ANSI library with its own identity.
Clean API
ansis.fg(code)
andansis.bg(code)
are shorter more elegant thanansis.ansi256(code)
andansis.bgAnsi256(code)
fg
andbg
clearly describe their purpose: setting foreground and background colorsfg()
andbg()
are already being used in GitHub projects5) Removed the unused
AnsiColorsExtend
type.This type was intended to support extended theme colors, but it was never used in other projects.
If you relied on it in your own code (e.g. for typing custom styles), you can easily define it yourself.
6) Improved
extend()
methodThe
extend()
method has been redesigned for better TypeScript support and flexibility.Old behavior:
void
.New behavior:
Returns an extended instance with full type support.
✅ Works with both
ansis
andnew Ansis()
:Why this change?
TypeScript cannot widen the type of an existing variable when using
asserts
.This means the old approach only worked for top-level constants like
ansis
, not new instances.By returning the extended instance, the new approach enables full type inference in all scenarios.
Summary:
asserts
version removedextend()
now returns an instance with extended types✨ Features
1) Support escape sequences in tagged template literals
Ansis now treats tagged template literals the same way as normal strings, returning the same result as the standard function call.
Example with
\n
(newline, unescaped):Output:
Example with escaped backslash:
Output:
2) Manually set the color level
Ansis automatically detects color support, but you can manually set the color level.
You can create a new instance of
Ansis
with the desired color level.Disable colors:
Use only basic 16 colors:
3) Simplified CI color detection
Ansis provides basic support for standard
CI
environments by checking the commonly usedCI
environment variable. In these environments, Ansis assumes support for at least 16 colors. If your code uses 256-color or truecolor, Ansis automatically fallback to 16 colors or to black and white if no color support is detected.Ansis focuses on the most common scenarios, as specific
CI
environments are rarely used in practice. This approach keeps the package lightweight without including unnecessary detection logic.GitHub Actions
is still detected as supportingtruecolor
, as most Ansis users rely onGitHub CI
.In general, color output in
CI
environments is not critical and can gracefully fallback when needed.The
xterm-direct
detection logic (introduced inv3.5.0
) has been removed, as it's unnecessary for identifying truecolor-capable terminals.Note
No terminal emulator sets
TERM=xterm-direct
by default.Modern terminals, including KDE Konsole, typically use
TERM=xterm-256color
along withCOLORTERM=truecolor
to indicate truecolor support.
🛠️ Bug Fixes
Defaults to 16 colors for unknown color support
Ansis now defaults uses 16 colors if it cannot detect support for 256 colors or truecolor.
Note
This is not a breaking change. Ansis gracefully interpolates higher color depths (truecolor and 256 colors)
down to 16 colors when using, e.g.,
fg()
,hex()
orrgb()
.To explicitly enable truecolor, set the environment variable
COLORTERM=24bit
orFORCE_COLOR=3
.🔄 Migrating to Ansis v4
Note
There is extremely low likelihood that you'll need to migrate, as these changes are related to very very rare use cases.
But to be sure, please check your code for these changes.
1) Upgrade to Deno v2 (if used)
This version supports Deno 2.0 and newer.
2) Replace
strike
withstrikethrough
3) Replace
grey
andblackBright
withgray
4) Replace
bgGrey
andbgBlackBright
withbgGray
5) Replace
ansi256()
withfg()
6) Replace
bgAnsi256()
withbg()
7) Update the
extend()
methodThe new
extend()
method now returns an extended instance instead of modifying the original instance in-place.To migrate, assign the result of
extend()
to a new variable (avoid reassigning the original instance):Alternatively:
8) Define
AnsiColorsExtend
type manuallyIf you previously imported the
AnsiColorsExtend
type, you’ll now need to define it manually as it has been removed from Ansis.Below is how you can define and use it in your TypeScript code:
This change ensures compatibility with the latest version of Ansis, as the
AnsiColorsExtend
type is no longer included by default.Beta Was this translation helpful? Give feedback.
All reactions