clearStyle
The clearStyle function appends targeted ANSI reset escape codes to the end of a string based on the provided ClearStyleOptions. Instead of a blanket \x1b[0m reset, it uses precise codes: DEFAULT_COLOR (\x1b[39m) for foreground color, DEFAULT_BACKGROUND (\x1b[49m) for background color, and NORMAL_INTENSITY (\x1b[22m) for bold/dim styles. Falls back to a full \x1b[0m reset only when called with no options. This is useful when working with the endless option in s().
Signature
function clearStyle(str: string, options?: ClearStyleOptions): string
Parameters
str: The string to append the reset codes to.options(optional): AClearStyleOptionsobject controlling which attributes to reset:color: reset foreground color withDEFAULT_COLOR(\x1b[39m)backgroundColor: reset background color withDEFAULT_BACKGROUND(\x1b[49m)style: reset individual styles; bold/dim useNORMAL_INTENSITY(\x1b[22m)
Return Value
Returns the original string with the appropriate targeted reset escape codes appended. When no options are provided, falls back to a full \x1b[0m reset.
Usage
import { s, clearStyle, cs } from "ts-better-console";
const styled = s("Red text", { color: "red", endless: true });
const result = clearStyle(cs([styled, "normal text"], false), { color: true });
console.log(result);
Want to support this project?
If you find ts-better-console useful and want to support its development, consider starring the GitHub repository or buying me a coffee! Your support helps me dedicate more time to improving the library and adding new features.
Want to contribute to this project?
Contributions are welcome! If you're interested in improving the library, fixing bugs, or adding new features, feel free to check out the GitHub repository and submit a pull request. Whether you're a seasoned developer or new to open source, your contributions can make a difference!