Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
ansi-fragments
Advanced tools
A tiny library with builders to help making logs/CLI pretty with a nice DX.
A tiny library with builders to help making logs/CLI pretty with a nice DX.
yarn add ansi-fragments
import { color, modifier, pad, container } from 'ansi-fragments';
const prettyLog = (level, message) => container(
color('green', modifier('italic', level)),
pad(1),
message
).build();
console.log(prettyLog('success', 'Yay!'));
Each fragment implements IFragment
interface:
interface IFragment {
build(): string;
}
The build
method is responsible for traversing the tree of fragments and create a string representation with ANSI escape codes.
color
color(
ansiColor: AnsiColor,
...children: Array<string | IFragment>
): IFragment
Creates fragment for standard ANSI colors.
color('red', 'Oh no');
color('bgBlue', color('brightBlue', 'Hey'));
color('green', modifier('bold', 'Sup!'));
modifier
modifier(
ansiModifier: AnsiModifier,
...children: Array<string | IFragment>
): IFragment
Creates fragment for standard ANSI modifiers: dim
, bold
, hidden
, italic
, underline
, strikethrough
.
modifier('underline', 'Hello', 'World');
modifier('italic', modifier('bold', 'Hey'));
modifier('bold', color('green', 'Sup!'));
container
container(...children: Array<string | IFragment>): IFragment
Creates fragment, which sole purpose is to hold and build nested fragments.
container(
color('gray', '[08/01/18 12:00]'),
pad(1),
color('green', 'success'),
pad(1),
'Some message'
)
pad
pad(count: number, separator?: string): IFragment
Creates fragment, which repeats given separator (default:
) given number of times.
pad(1);
pad(2, '#')
pad(1, '\n')
fixed
fixed(
value: number,
bias: 'start' | 'end',
...children: Array<string | IFragment>
): IFragment
Creates fragment, which makes sure the children will always build to given number of non-ANSI characters. It will either trim the results or add necessary amount of spaces. The bias
control if trimming/padding should be done at the start of the string representing built children or at the end.
fixed(5, 'start', 'ERR'); // => ' ERR'
fixed(8, 'end', color('green', 'success')); // equals to color('green', 'success') + ' '
fixed(10, 'end', 'Hello', pad(2), 'World') // => 'Hello Wor'
ifElse
ifElse(
condition: Condition,
ifTrueFragment: string | IFragment,
elseFragment: string | IFragment
): IFragment
type ConditionValue = boolean | string | number | null | undefined;
type Condition = ConditionValue | (() => ConditionValue);
Change the output based on condition. Condition can ba a primitive value, which can be casted to boolean or a function. If conation or return value of condition is evaluated to true
, the first argument - ifTrueFragment
will be used, otherwise elseFragment
.
let condition = getConditionValue()
ifElse(
() => condition,
color('red', 'ERROR'),
color('yellow', 'WARNING')
)
provide
provide<T>(
value: T,
builder: (value: T) => string | IFragment
): IFragment
Provides given value to a builder function, which should return string
or fragment. Useful in situations when the output is connected with some calculated value - using provide
you only need to calculate final value once and forward it to custom styling logic.
provide(getMessageFromSomewhere(), value => {
switch (value.level) {
case 'error':
return container(
color('red', modifier('bold', value.level.toUpperCase())),
pad(2),
value.log
);
case 'info':
return container(
color('blue', value.level.toUpperCase()),
pad(2),
value.log
);
default:
return container(value.level.toUpperCase(), pad(2), value.log);
}
})
FAQs
A tiny library with builders to help making logs/CLI pretty with a nice DX.
The npm package ansi-fragments receives a total of 1,033,188 weekly downloads. As such, ansi-fragments popularity was classified as popular.
We found that ansi-fragments 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.