colorino 0.1.0 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -34
- package/dist/browser.cjs +1 -1
- package/dist/browser.d.cts +1 -1
- package/dist/browser.d.mts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.mjs +1 -1
- package/dist/node.cjs +2 -2
- package/dist/node.d.cts +1 -1
- package/dist/node.d.mts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.mjs +2 -2
- package/dist/shared/{colorino.V5I9NhYv.d.cts → colorino.Du-FJF5k.d.cts} +2 -2
- package/dist/shared/{colorino.V5I9NhYv.d.mts → colorino.Du-FJF5k.d.mts} +2 -2
- package/dist/shared/{colorino.V5I9NhYv.d.ts → colorino.Du-FJF5k.d.ts} +2 -2
- package/dist/shared/{colorino.BCUN0J9y.cjs → colorino.gkk2eRGY.cjs} +54 -45
- package/dist/shared/{colorino.xxLMfJFV.mjs → colorino.iQv4mlpl.mjs} +54 -45
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -76,7 +76,7 @@ colorino.trace('Tracing app start...')
|
|
|
76
76
|
|
|
77
77
|
### Creating a Custom Logger
|
|
78
78
|
|
|
79
|
-
Need your own colors or different settings?
|
|
79
|
+
Need your own colors or different settings?
|
|
80
80
|
Use the factory to create as many loggers as you want (each with its own palette and options):
|
|
81
81
|
|
|
82
82
|
```typescript
|
|
@@ -97,12 +97,12 @@ myLogger.info('Rebranded info!')
|
|
|
97
97
|
|
|
98
98
|
### Options & Theme Overrides
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
`createColorino(palette?, options?)` accepts:
|
|
101
101
|
|
|
102
|
-
| Option
|
|
103
|
-
|
|
104
|
-
| `disableWarnings`
|
|
105
|
-
| `theme`
|
|
102
|
+
| Option | Type | Default | Description |
|
|
103
|
+
|-------------------|------------------------------|---------|--------------------------------------------------------------------------------|
|
|
104
|
+
| `disableWarnings` | `boolean` | `false` | Suppress warnings when color support can't be detected or is disabled |
|
|
105
|
+
| `theme` | `'dark' \| 'light' \| 'unknown'` | *(auto)*| Override auto-theme detection: force `'dark'` or `'light'` for palette selection |
|
|
106
106
|
|
|
107
107
|
#### Example: Forcing a theme
|
|
108
108
|
|
|
@@ -117,23 +117,23 @@ const forcedDarkLogger = createColorino({}, { theme: 'dark' })
|
|
|
117
117
|
forcedDarkLogger.info('This will always use dark-friendly colors.')
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
-
> **Tip:**
|
|
120
|
+
> **Tip:**
|
|
121
121
|
> Forcing `'dark'` or `'light'` bypasses automatic theming, ensuring predictable colors in environments with unknown or unsupported theme detection (like some CI pipelines, dumb terminals, or minimal browsers).
|
|
122
122
|
|
|
123
123
|
***
|
|
124
124
|
|
|
125
125
|
### Customization
|
|
126
126
|
|
|
127
|
-
Use your brand colors
|
|
127
|
+
Use your brand colors by passing a partial palette to the `createColorino` factory. Any log levels you don't specify will use the smart theme defaults.
|
|
128
128
|
|
|
129
129
|
```typescript
|
|
130
|
-
import {
|
|
130
|
+
import { createColorino } from 'colorino'
|
|
131
131
|
|
|
132
132
|
// Custom error color; others use theme defaults
|
|
133
|
-
const myLogger =
|
|
133
|
+
const myLogger = createColorino({ error: '#ff007b' })
|
|
134
134
|
|
|
135
|
-
myLogger.error('Oh no!')
|
|
136
|
-
myLogger.info('Still styled by theme.')
|
|
135
|
+
myLogger.error('Oh no!') // Uses your custom color
|
|
136
|
+
myLogger.info('Still styled by theme.') // Uses the default theme color
|
|
137
137
|
```
|
|
138
138
|
|
|
139
139
|
***
|
|
@@ -142,7 +142,7 @@ myLogger.info('Still styled by theme.')
|
|
|
142
142
|
|
|
143
143
|
Colorino auto-detects your environment and color support, but you can override behavior using these standard environment variables (compatible with Chalk):
|
|
144
144
|
|
|
145
|
-
| Variable | Effect
|
|
145
|
+
| Variable | Effect | Example |
|
|
146
146
|
|------------------|---------------------------------------------------|--------------------------|
|
|
147
147
|
| `NO_COLOR` | Forces *no color* output | `NO_COLOR=1 node app.js` |
|
|
148
148
|
| `FORCE_COLOR` | Forces color (`1`=ANSI, `2`=256, `3`=truecolor) | `FORCE_COLOR=3 node app.js` |
|
|
@@ -157,21 +157,23 @@ Colorino auto-detects your environment and color support, but you can override b
|
|
|
157
157
|
|
|
158
158
|
## Colorino vs. Chalk
|
|
159
159
|
|
|
160
|
-
| Feature
|
|
161
|
-
|
|
162
|
-
| Out-of-box logs
|
|
163
|
-
| Zero-config
|
|
164
|
-
| Node + browser
|
|
165
|
-
| CSS console logs
|
|
166
|
-
|
|
|
160
|
+
| Feature | 🎨 **Colorino** | 🖍️ **Chalk** |
|
|
161
|
+
|--------------------------|----------------------------|-----------------|
|
|
162
|
+
| Out-of-box logs | ✔ themed, all log levels | ✘ string styling|
|
|
163
|
+
| Zero-config | ✔ | ✘ manual, per-use|
|
|
164
|
+
| Node + browser | ✔ | ✘ (Node only) |
|
|
165
|
+
| CSS console logs | ✔ | ✘ |
|
|
166
|
+
| Extensible / Composable | ✔ (via factory) | ✘ |
|
|
167
167
|
|
|
168
168
|
***
|
|
169
169
|
|
|
170
170
|
## API Reference
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
The `colorino` package exports two main items:
|
|
173
|
+
|
|
174
|
+
### 1. `colorino` (default instance)
|
|
173
175
|
|
|
174
|
-
|
|
176
|
+
A pre-configured, zero-setup logger instance. Just import and use.
|
|
175
177
|
|
|
176
178
|
- `.log(...args)`
|
|
177
179
|
- `.info(...args)`
|
|
@@ -180,10 +182,14 @@ Preconfigured logger.
|
|
|
180
182
|
- `.debug(...args)`
|
|
181
183
|
- `.trace(...args)`
|
|
182
184
|
|
|
183
|
-
###
|
|
185
|
+
### 2. `createColorino(palette?, options?)` (factory)
|
|
186
|
+
|
|
187
|
+
A factory function to create your own customized logger instances.
|
|
184
188
|
|
|
185
|
-
- `palette` (`Partial<Palette>`):
|
|
186
|
-
- `options` (`
|
|
189
|
+
- `palette` (`Partial<Palette>`): An object to override default colors for specific log levels (e.g., `{ error: '#ff007b' }`).
|
|
190
|
+
- `options` (`ColorinoOptions`): An object to control behavior:
|
|
191
|
+
- `disableWarnings: boolean` (default `false`): Suppress warnings on environments with no color support.
|
|
192
|
+
- `theme: 'dark' | 'light'` (default `auto`): Force a specific theme instead of auto-detecting.
|
|
187
193
|
|
|
188
194
|
***
|
|
189
195
|
|
|
@@ -191,31 +197,53 @@ Preconfigured logger.
|
|
|
191
197
|
|
|
192
198
|
Example: Add a `fatal()` logger for critical errors.
|
|
193
199
|
|
|
200
|
+
Since colorino uses a factory pattern, extend it by creating your own factory that composes the base logger with additional methods:
|
|
201
|
+
|
|
194
202
|
```typescript
|
|
195
|
-
import {
|
|
203
|
+
import { createColorino, type ColorinoOptions, type Palette } from 'colorino'
|
|
196
204
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
205
|
+
// Create a factory for your custom logger
|
|
206
|
+
export function createMyLogger(palette?: Partial<Palette>, options?: ColorinoOptions) {
|
|
207
|
+
// Get the base logger instance
|
|
208
|
+
const baseLogger = createColorino(palette, options)
|
|
209
|
+
|
|
210
|
+
// Define your custom method
|
|
211
|
+
function fatal(...args: unknown[]): void {
|
|
212
|
+
// Reuse the base logger's error method
|
|
213
|
+
baseLogger.error(...args)
|
|
214
|
+
|
|
215
|
+
// Add your custom behavior
|
|
203
216
|
if (typeof process !== 'undefined' && process.exit) {
|
|
204
217
|
process.exit(1)
|
|
205
218
|
}
|
|
206
219
|
}
|
|
220
|
+
|
|
221
|
+
// Return a new object with all base methods + your custom ones
|
|
222
|
+
// This preserves type safety and the original API
|
|
223
|
+
return {
|
|
224
|
+
...baseLogger,
|
|
225
|
+
fatal,
|
|
226
|
+
}
|
|
207
227
|
}
|
|
208
228
|
```
|
|
209
229
|
|
|
210
230
|
Usage:
|
|
211
231
|
|
|
212
232
|
```typescript
|
|
213
|
-
const logger =
|
|
233
|
+
const logger = createMyLogger({ error: '#d92626' })
|
|
214
234
|
|
|
215
235
|
logger.info('Starting!')
|
|
216
236
|
logger.fatal('Missing config: Exiting')
|
|
217
237
|
```
|
|
218
238
|
|
|
239
|
+
### Why This Pattern?
|
|
240
|
+
|
|
241
|
+
- **Composition > Inheritance**: No fragile base class problems
|
|
242
|
+
- **Type Safe**: TypeScript infers the return type correctly
|
|
243
|
+
- **Future Proof**: Works even if colorino's internal implementation changes
|
|
244
|
+
- **Clean**: No messing with `super()` or constructor parameters
|
|
245
|
+
- **Composable**: You can layer multiple extensions
|
|
246
|
+
|
|
219
247
|
***
|
|
220
248
|
|
|
221
249
|
## Contributing
|
|
@@ -225,7 +253,7 @@ PRs and issues welcome!
|
|
|
225
253
|
1. Fork the repo.
|
|
226
254
|
2. Make a branch (`git checkout -b feat/my-feature`)
|
|
227
255
|
3. Add your change, with tests.
|
|
228
|
-
4. Run `npm test` to ensure all tests pass in both Node and browser.
|
|
256
|
+
4. Run `npm test:all` to ensure all tests pass in both Node and browser.
|
|
229
257
|
5. Open a Pull Request.
|
|
230
258
|
|
|
231
259
|
***
|
package/dist/browser.cjs
CHANGED
package/dist/browser.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.
|
|
1
|
+
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.Du-FJF5k.cjs';
|
|
2
2
|
import 'neverthrow';
|
|
3
3
|
|
|
4
4
|
declare function createColorino(palette: Palette, options?: ColorinoOptions): Colorino;
|
package/dist/browser.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.
|
|
1
|
+
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.Du-FJF5k.mjs';
|
|
2
2
|
import 'neverthrow';
|
|
3
3
|
|
|
4
4
|
declare function createColorino(palette: Palette, options?: ColorinoOptions): Colorino;
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.
|
|
1
|
+
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.Du-FJF5k.js';
|
|
2
2
|
import 'neverthrow';
|
|
3
3
|
|
|
4
4
|
declare function createColorino(palette: Palette, options?: ColorinoOptions): Colorino;
|
package/dist/browser.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as ColorLevel, a as Colorino, I as InputValidator, d as darkDraculaPalette } from './shared/colorino.
|
|
1
|
+
import { C as ColorLevel, a as Colorino, I as InputValidator, d as darkDraculaPalette } from './shared/colorino.iQv4mlpl.mjs';
|
|
2
2
|
import 'neverthrow';
|
|
3
3
|
|
|
4
4
|
class BrowserColorSupportDetector {
|
package/dist/node.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const theme = require('./shared/colorino.
|
|
3
|
+
const theme = require('./shared/colorino.gkk2eRGY.cjs');
|
|
4
4
|
const neverthrow = require('neverthrow');
|
|
5
5
|
|
|
6
6
|
class OscThemeQuerier {
|
|
@@ -161,7 +161,7 @@ class NodeColorSupportDetector {
|
|
|
161
161
|
if (this._envCliColor !== void 0 && this._envCliColor !== "0") {
|
|
162
162
|
return theme.ColorLevel.ANSI;
|
|
163
163
|
}
|
|
164
|
-
return this._isTTY ? theme.ColorLevel.ANSI : theme.ColorLevel.NO_COLOR;
|
|
164
|
+
return this._isTTY || isForced ? theme.ColorLevel.ANSI : theme.ColorLevel.NO_COLOR;
|
|
165
165
|
}
|
|
166
166
|
async getTheme() {
|
|
167
167
|
if (this._overrideTheme) {
|
package/dist/node.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.
|
|
1
|
+
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.Du-FJF5k.cjs';
|
|
2
2
|
import 'neverthrow';
|
|
3
3
|
|
|
4
4
|
declare function createColorino(palette: Palette, options?: ColorinoOptions): Colorino;
|
package/dist/node.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.
|
|
1
|
+
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.Du-FJF5k.mjs';
|
|
2
2
|
import 'neverthrow';
|
|
3
3
|
|
|
4
4
|
declare function createColorino(palette: Palette, options?: ColorinoOptions): Colorino;
|
package/dist/node.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.
|
|
1
|
+
import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.Du-FJF5k.js';
|
|
2
2
|
import 'neverthrow';
|
|
3
3
|
|
|
4
4
|
declare function createColorino(palette: Palette, options?: ColorinoOptions): Colorino;
|
package/dist/node.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { O as OscQueryError, C as ColorLevel, a as Colorino, I as InputValidator, d as darkDraculaPalette } from './shared/colorino.
|
|
1
|
+
import { O as OscQueryError, C as ColorLevel, a as Colorino, I as InputValidator, d as darkDraculaPalette } from './shared/colorino.iQv4mlpl.mjs';
|
|
2
2
|
import { err, ok } from 'neverthrow';
|
|
3
3
|
|
|
4
4
|
class OscThemeQuerier {
|
|
@@ -159,7 +159,7 @@ class NodeColorSupportDetector {
|
|
|
159
159
|
if (this._envCliColor !== void 0 && this._envCliColor !== "0") {
|
|
160
160
|
return ColorLevel.ANSI;
|
|
161
161
|
}
|
|
162
|
-
return this._isTTY ? ColorLevel.ANSI : ColorLevel.NO_COLOR;
|
|
162
|
+
return this._isTTY || isForced ? ColorLevel.ANSI : ColorLevel.NO_COLOR;
|
|
163
163
|
}
|
|
164
164
|
async getTheme() {
|
|
165
165
|
if (this._overrideTheme) {
|
|
@@ -71,9 +71,9 @@ declare class Colorino {
|
|
|
71
71
|
private readonly _nodeColorSupportDetector?;
|
|
72
72
|
private readonly _options;
|
|
73
73
|
private _alreadyWarned;
|
|
74
|
-
private _colorLevel;
|
|
74
|
+
private readonly _colorLevel;
|
|
75
|
+
private readonly isBrowser;
|
|
75
76
|
constructor(_palette: Palette, _validator: InputValidator, _browserColorSupportDetector?: BrowserColorSupportDetector | undefined, _nodeColorSupportDetector?: NodeColorSupportDetector | undefined, _options?: ColorinoOptions);
|
|
76
|
-
color(level: LogLevel, text: string): string;
|
|
77
77
|
log(...args: unknown[]): void;
|
|
78
78
|
info(...args: unknown[]): void;
|
|
79
79
|
warn(...args: unknown[]): void;
|
|
@@ -71,9 +71,9 @@ declare class Colorino {
|
|
|
71
71
|
private readonly _nodeColorSupportDetector?;
|
|
72
72
|
private readonly _options;
|
|
73
73
|
private _alreadyWarned;
|
|
74
|
-
private _colorLevel;
|
|
74
|
+
private readonly _colorLevel;
|
|
75
|
+
private readonly isBrowser;
|
|
75
76
|
constructor(_palette: Palette, _validator: InputValidator, _browserColorSupportDetector?: BrowserColorSupportDetector | undefined, _nodeColorSupportDetector?: NodeColorSupportDetector | undefined, _options?: ColorinoOptions);
|
|
76
|
-
color(level: LogLevel, text: string): string;
|
|
77
77
|
log(...args: unknown[]): void;
|
|
78
78
|
info(...args: unknown[]): void;
|
|
79
79
|
warn(...args: unknown[]): void;
|
|
@@ -71,9 +71,9 @@ declare class Colorino {
|
|
|
71
71
|
private readonly _nodeColorSupportDetector?;
|
|
72
72
|
private readonly _options;
|
|
73
73
|
private _alreadyWarned;
|
|
74
|
-
private _colorLevel;
|
|
74
|
+
private readonly _colorLevel;
|
|
75
|
+
private readonly isBrowser;
|
|
75
76
|
constructor(_palette: Palette, _validator: InputValidator, _browserColorSupportDetector?: BrowserColorSupportDetector | undefined, _nodeColorSupportDetector?: NodeColorSupportDetector | undefined, _options?: ColorinoOptions);
|
|
76
|
-
color(level: LogLevel, text: string): string;
|
|
77
77
|
log(...args: unknown[]): void;
|
|
78
78
|
info(...args: unknown[]): void;
|
|
79
79
|
warn(...args: unknown[]): void;
|
|
@@ -72,46 +72,17 @@ class Colorino {
|
|
|
72
72
|
this._browserColorSupportDetector = _browserColorSupportDetector;
|
|
73
73
|
this._nodeColorSupportDetector = _nodeColorSupportDetector;
|
|
74
74
|
this._options = _options;
|
|
75
|
+
this.isBrowser = !!this._browserColorSupportDetector;
|
|
75
76
|
this._colorLevel = this._detectColorSupport();
|
|
76
77
|
const validatePaletteResult = this._validator.validatePalette(this._palette);
|
|
77
78
|
if (validatePaletteResult.isErr()) throw validatePaletteResult.error;
|
|
78
|
-
if ((this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") && !this._options.disableWarnings) {
|
|
79
|
+
if (!this.isBrowser && (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") && !this._options.disableWarnings) {
|
|
79
80
|
this._maybeWarnUser();
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
83
|
_alreadyWarned = false;
|
|
83
84
|
_colorLevel;
|
|
84
|
-
|
|
85
|
-
const hex = this._palette[level];
|
|
86
|
-
let ansiCode;
|
|
87
|
-
switch (this._colorLevel) {
|
|
88
|
-
case ColorLevel.TRUECOLOR: {
|
|
89
|
-
const [r, g, b] = colorConverter.hex.toRgb(hex);
|
|
90
|
-
ansiCode = `\x1B[38;2;${r};${g};${b}m`;
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
case ColorLevel.ANSI256: {
|
|
94
|
-
const code = colorConverter.hex.toAnsi256(hex);
|
|
95
|
-
ansiCode = `\x1B[38;5;${code}m`;
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
case ColorLevel.ANSI: {
|
|
99
|
-
const code = colorConverter.hex.toAnsi16(hex);
|
|
100
|
-
if (code < 38) {
|
|
101
|
-
ansiCode = `\x1B[${code}m`;
|
|
102
|
-
} else {
|
|
103
|
-
ansiCode = `\x1B[${code}m`;
|
|
104
|
-
}
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
case "UnknownEnv": {
|
|
108
|
-
return text;
|
|
109
|
-
}
|
|
110
|
-
default:
|
|
111
|
-
return text;
|
|
112
|
-
}
|
|
113
|
-
return `${ansiCode}${text}\x1B[0m`;
|
|
114
|
-
}
|
|
85
|
+
isBrowser;
|
|
115
86
|
log(...args) {
|
|
116
87
|
this._out("log", args);
|
|
117
88
|
}
|
|
@@ -131,15 +102,11 @@ class Colorino {
|
|
|
131
102
|
this._out("debug", args);
|
|
132
103
|
}
|
|
133
104
|
_detectColorSupport() {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
this._colorLevel = this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
137
|
-
return this._colorLevel;
|
|
105
|
+
if (this.isBrowser) {
|
|
106
|
+
return this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
138
107
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
this._colorLevel = this._nodeColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
142
|
-
return this._colorLevel;
|
|
108
|
+
if (this._nodeColorSupportDetector?.isNodeEnv()) {
|
|
109
|
+
return this._nodeColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
143
110
|
}
|
|
144
111
|
return "UnknownEnv";
|
|
145
112
|
}
|
|
@@ -147,15 +114,57 @@ class Colorino {
|
|
|
147
114
|
if (this._alreadyWarned) return;
|
|
148
115
|
this._alreadyWarned = true;
|
|
149
116
|
console.warn(
|
|
150
|
-
"[Colorino] No ANSI color support detected in this terminal. See https://github.com/chalk/supports-color#support-matrix to learn how to enable terminal color."
|
|
117
|
+
"[Colorino] No ANSI color support detected in this terminal. See [https://github.com/chalk/supports-color#support-matrix](https://github.com/chalk/supports-color#support-matrix) to learn how to enable terminal color."
|
|
151
118
|
);
|
|
152
119
|
}
|
|
153
120
|
_out(level, args) {
|
|
154
|
-
const
|
|
155
|
-
|
|
121
|
+
const consoleMethod = isConsoleMethod(level) ? level : "log";
|
|
122
|
+
if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
|
|
123
|
+
if (level === "trace") console.trace(...args);
|
|
124
|
+
else console[consoleMethod](...args);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (this.isBrowser) {
|
|
128
|
+
const hex2 = this._palette[level];
|
|
129
|
+
if (typeof args[0] === "string") {
|
|
130
|
+
console[consoleMethod](`%c${args[0]}`, `color:${hex2}`, ...args.slice(1));
|
|
131
|
+
} else {
|
|
132
|
+
console[consoleMethod](...args);
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const hex = this._palette[level];
|
|
137
|
+
let ansiCode;
|
|
138
|
+
switch (this._colorLevel) {
|
|
139
|
+
case ColorLevel.TRUECOLOR: {
|
|
140
|
+
const [r, g, b] = colorConverter.hex.toRgb(hex);
|
|
141
|
+
ansiCode = `\x1B[38;2;${r};${g};${b}m`;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case ColorLevel.ANSI256: {
|
|
145
|
+
const code = colorConverter.hex.toAnsi256(hex);
|
|
146
|
+
ansiCode = `\x1B[38;5;${code}m`;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case ColorLevel.ANSI:
|
|
150
|
+
default: {
|
|
151
|
+
const code = colorConverter.hex.toAnsi16(hex);
|
|
152
|
+
ansiCode = `\x1B[${code}m`;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const processedArgs = [...args];
|
|
157
|
+
const firstStringIndex = processedArgs.findIndex(
|
|
158
|
+
(arg) => typeof arg === "string"
|
|
156
159
|
);
|
|
157
|
-
if (
|
|
158
|
-
|
|
160
|
+
if (firstStringIndex !== -1) {
|
|
161
|
+
processedArgs[firstStringIndex] = `${ansiCode}${processedArgs[firstStringIndex]}\x1B[0m`;
|
|
162
|
+
}
|
|
163
|
+
if (level === "trace") {
|
|
164
|
+
console.trace(...processedArgs);
|
|
165
|
+
} else {
|
|
166
|
+
console[consoleMethod](...processedArgs);
|
|
167
|
+
}
|
|
159
168
|
}
|
|
160
169
|
}
|
|
161
170
|
|
|
@@ -70,46 +70,17 @@ class Colorino {
|
|
|
70
70
|
this._browserColorSupportDetector = _browserColorSupportDetector;
|
|
71
71
|
this._nodeColorSupportDetector = _nodeColorSupportDetector;
|
|
72
72
|
this._options = _options;
|
|
73
|
+
this.isBrowser = !!this._browserColorSupportDetector;
|
|
73
74
|
this._colorLevel = this._detectColorSupport();
|
|
74
75
|
const validatePaletteResult = this._validator.validatePalette(this._palette);
|
|
75
76
|
if (validatePaletteResult.isErr()) throw validatePaletteResult.error;
|
|
76
|
-
if ((this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") && !this._options.disableWarnings) {
|
|
77
|
+
if (!this.isBrowser && (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") && !this._options.disableWarnings) {
|
|
77
78
|
this._maybeWarnUser();
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
_alreadyWarned = false;
|
|
81
82
|
_colorLevel;
|
|
82
|
-
|
|
83
|
-
const hex = this._palette[level];
|
|
84
|
-
let ansiCode;
|
|
85
|
-
switch (this._colorLevel) {
|
|
86
|
-
case ColorLevel.TRUECOLOR: {
|
|
87
|
-
const [r, g, b] = colorConverter.hex.toRgb(hex);
|
|
88
|
-
ansiCode = `\x1B[38;2;${r};${g};${b}m`;
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
case ColorLevel.ANSI256: {
|
|
92
|
-
const code = colorConverter.hex.toAnsi256(hex);
|
|
93
|
-
ansiCode = `\x1B[38;5;${code}m`;
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
case ColorLevel.ANSI: {
|
|
97
|
-
const code = colorConverter.hex.toAnsi16(hex);
|
|
98
|
-
if (code < 38) {
|
|
99
|
-
ansiCode = `\x1B[${code}m`;
|
|
100
|
-
} else {
|
|
101
|
-
ansiCode = `\x1B[${code}m`;
|
|
102
|
-
}
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
case "UnknownEnv": {
|
|
106
|
-
return text;
|
|
107
|
-
}
|
|
108
|
-
default:
|
|
109
|
-
return text;
|
|
110
|
-
}
|
|
111
|
-
return `${ansiCode}${text}\x1B[0m`;
|
|
112
|
-
}
|
|
83
|
+
isBrowser;
|
|
113
84
|
log(...args) {
|
|
114
85
|
this._out("log", args);
|
|
115
86
|
}
|
|
@@ -129,15 +100,11 @@ class Colorino {
|
|
|
129
100
|
this._out("debug", args);
|
|
130
101
|
}
|
|
131
102
|
_detectColorSupport() {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
this._colorLevel = this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
135
|
-
return this._colorLevel;
|
|
103
|
+
if (this.isBrowser) {
|
|
104
|
+
return this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
136
105
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
this._colorLevel = this._nodeColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
140
|
-
return this._colorLevel;
|
|
106
|
+
if (this._nodeColorSupportDetector?.isNodeEnv()) {
|
|
107
|
+
return this._nodeColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
|
|
141
108
|
}
|
|
142
109
|
return "UnknownEnv";
|
|
143
110
|
}
|
|
@@ -145,15 +112,57 @@ class Colorino {
|
|
|
145
112
|
if (this._alreadyWarned) return;
|
|
146
113
|
this._alreadyWarned = true;
|
|
147
114
|
console.warn(
|
|
148
|
-
"[Colorino] No ANSI color support detected in this terminal. See https://github.com/chalk/supports-color#support-matrix to learn how to enable terminal color."
|
|
115
|
+
"[Colorino] No ANSI color support detected in this terminal. See [https://github.com/chalk/supports-color#support-matrix](https://github.com/chalk/supports-color#support-matrix) to learn how to enable terminal color."
|
|
149
116
|
);
|
|
150
117
|
}
|
|
151
118
|
_out(level, args) {
|
|
152
|
-
const
|
|
153
|
-
|
|
119
|
+
const consoleMethod = isConsoleMethod(level) ? level : "log";
|
|
120
|
+
if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
|
|
121
|
+
if (level === "trace") console.trace(...args);
|
|
122
|
+
else console[consoleMethod](...args);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (this.isBrowser) {
|
|
126
|
+
const hex2 = this._palette[level];
|
|
127
|
+
if (typeof args[0] === "string") {
|
|
128
|
+
console[consoleMethod](`%c${args[0]}`, `color:${hex2}`, ...args.slice(1));
|
|
129
|
+
} else {
|
|
130
|
+
console[consoleMethod](...args);
|
|
131
|
+
}
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const hex = this._palette[level];
|
|
135
|
+
let ansiCode;
|
|
136
|
+
switch (this._colorLevel) {
|
|
137
|
+
case ColorLevel.TRUECOLOR: {
|
|
138
|
+
const [r, g, b] = colorConverter.hex.toRgb(hex);
|
|
139
|
+
ansiCode = `\x1B[38;2;${r};${g};${b}m`;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case ColorLevel.ANSI256: {
|
|
143
|
+
const code = colorConverter.hex.toAnsi256(hex);
|
|
144
|
+
ansiCode = `\x1B[38;5;${code}m`;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case ColorLevel.ANSI:
|
|
148
|
+
default: {
|
|
149
|
+
const code = colorConverter.hex.toAnsi16(hex);
|
|
150
|
+
ansiCode = `\x1B[${code}m`;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const processedArgs = [...args];
|
|
155
|
+
const firstStringIndex = processedArgs.findIndex(
|
|
156
|
+
(arg) => typeof arg === "string"
|
|
154
157
|
);
|
|
155
|
-
if (
|
|
156
|
-
|
|
158
|
+
if (firstStringIndex !== -1) {
|
|
159
|
+
processedArgs[firstStringIndex] = `${ansiCode}${processedArgs[firstStringIndex]}\x1B[0m`;
|
|
160
|
+
}
|
|
161
|
+
if (level === "trace") {
|
|
162
|
+
console.trace(...processedArgs);
|
|
163
|
+
} else {
|
|
164
|
+
console[consoleMethod](...processedArgs);
|
|
165
|
+
}
|
|
157
166
|
}
|
|
158
167
|
}
|
|
159
168
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "colorino",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "A super simple colorized logger that gets the most out of your terminal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,22 +39,21 @@
|
|
|
39
39
|
"dist",
|
|
40
40
|
"README.md",
|
|
41
41
|
"package.json",
|
|
42
|
-
"LICENSE"
|
|
42
|
+
"LICENSE.md"
|
|
43
43
|
],
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "unbuild",
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"format": "npm run lint:fix && oxfmt",
|
|
49
|
-
"format:check": "prettier --check .",
|
|
50
|
-
"check": "npm run lint && npm run format:check",
|
|
51
|
-
"fix": "npm run lint:fix && npm run format",
|
|
52
|
-
"test": "npm run test:node && npm run test:browser",
|
|
46
|
+
"fix": "oxlint --fix && oxfmt",
|
|
47
|
+
"test:all": "npm run test:node && npm run test:browser",
|
|
53
48
|
"test:node": "vitest run --config vitest.config.ts",
|
|
54
49
|
"test:browser": "vitest run --config vitest.browser.config.ts",
|
|
55
50
|
"test:coverage": "vitest run --config vitest.config.ts --coverage",
|
|
56
51
|
"test:ui": "vitest --ui --config vitest.config.ts",
|
|
57
|
-
"
|
|
52
|
+
"version:patch": "npm version patch",
|
|
53
|
+
"version:minor": "npm version minor",
|
|
54
|
+
"version:major": "npm version major",
|
|
55
|
+
"prepublishOnly": "npm run build && npm run test:all",
|
|
56
|
+
"postversion": "npm publish --access public",
|
|
58
57
|
"prepare": "husky"
|
|
59
58
|
},
|
|
60
59
|
"dependencies": {
|
|
@@ -78,7 +77,8 @@
|
|
|
78
77
|
"*.{ts,json,md,yml,yaml}": [
|
|
79
78
|
"oxlint --fix",
|
|
80
79
|
"oxfmt"
|
|
81
|
-
]
|
|
80
|
+
],
|
|
81
|
+
"*.ts": "vitest related --run"
|
|
82
82
|
},
|
|
83
83
|
"engines": {
|
|
84
84
|
"node": ">=16.0.0",
|