Skip to content

Commit

Permalink
move styles from options into utils, start terminal color support wor…
Browse files Browse the repository at this point in the history
…k, update rslib
  • Loading branch information
aprilsbloom committed Nov 30, 2024
1 parent 2e16e98 commit c77cfa9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"devDependencies": {
"@microsoft/api-extractor": "^7.48.0",
"@rslib/core": "^0.1.0",
"@rslib/core": "^0.1.1",
"@types/bun": "latest",
"@types/lodash": "^4.17.13",
"@types/strftime": "^0.9.8",
Expand All @@ -33,6 +33,7 @@
"dependencies": {
"cli-highlight": "^2.1.11",
"lodash": "^4.17.21",
"strftime": "^0.10.3"
"strftime": "^0.10.3",
"supports-color": "^9.4.0"
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,5 @@ export class Consol {
export const consol = new Consol();
export const createConsol = (options: Partial<LoggerOptions> = {}) => new Consol(options);

export { LogLevel, logTypeToLogLevel, logLevelToLogType } from './enums';
export { LogLevel, logTypeToLogLevel, logLevelToLogType } from './enums';
export { hexToAnsi, hexToRGB, styles, ANSI_ESCAPE, SUPPORTED_LANGUAGES } from './utils';
18 changes: 5 additions & 13 deletions src/optionsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Theme } from 'cli-highlight';
import { LogLevel } from "./enums";
import type { Format, LoggerOptions, LogType, StringifyFunc, Style } from "./types";
import { Formatter } from "./formatter";
import { ANSI_ESCAPE } from "./utils";
import { styles } from "./utils";

export class OptionsManager {
private options: LoggerOptions = {
Expand All @@ -19,14 +19,6 @@ export class OptionsManager {
themes: {},
},

styles: {
reset: `${ANSI_ESCAPE}[0m`,
bold: `${ANSI_ESCAPE}[1m`,
italic: `${ANSI_ESCAPE}[3m`,
underline: `${ANSI_ESCAPE}[4m`,
strikethrough: `${ANSI_ESCAPE}[9m`,
},

format: {
log: "!{date:%Y/%m/%d %H:%M:%S}! !{level}! !{message}!",
path: 'logs/!{date:%Y-%m-%d}!.log',
Expand Down Expand Up @@ -205,12 +197,12 @@ export class OptionsManager {
}

// Styles
public getStyles(): LoggerOptions['styles'] {
return this.options.styles;
public getStyles(): typeof styles {
return styles;
}

public getStyle(style: Style): string {
return this.options.styles[style];
return styles[style];
}

// Base formats
Expand All @@ -227,7 +219,7 @@ export class OptionsManager {
}

public getFormat(format: Exclude<keyof LoggerOptions['format'], 'level'>): string {
return this.options.format[format] + this.options.styles.reset;
return this.options.format[format] + styles.reset;
}

public setBaseLogFormat(value: string) {
Expand Down
1 change: 0 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface LoggerOptions {
themes: Record<string, Theme>;
}

styles: Record<Style, string>;
format: {
log: string;
path: string;
Expand Down
36 changes: 32 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import supportsColor from 'supports-color';
import type { Style } from './types';

export const ANSI_ESCAPE = '\x1b';
export const styles: Record<Style, string> = {
reset: `${ANSI_ESCAPE}[0m`,
bold: `${ANSI_ESCAPE}[1m`,
italic: `${ANSI_ESCAPE}[3m`,
underline: `${ANSI_ESCAPE}[4m`,
strikethrough: `${ANSI_ESCAPE}[9m`,
}

export function hexToAnsi(hex: string, background: boolean = false): string {
if (!supportsColor.stdout) return styles.reset;
if (!supportsColor.stdout.hasBasic) return styles.reset;

// deal w 16m colors first
if (supportsColor.stdout.has16m) {
const [r, g, b] = hexToRGB(hex);
return `${ANSI_ESCAPE}[${background ? '48' : '38'};2;${r};${g};${b}m`;
}

// deal w 256 colors
if (supportsColor.stdout.has256) {
// use this lookup table: https://stackoverflow.com/a/60392218
}

return styles.reset;
}

export function hexToRGB(hex: string): [number, number, number] {
hex = hex.toUpperCase();
if (hex.startsWith('#')) hex = hex.slice(1);
if (hex.length === 3) hex = hex.split('').map(c => c + c).join('');
if (!/^[0-9A-F]{6}$/i.test(hex)) throw new Error('Invalid hex string!');

const r = Number.parseInt(hex.slice(0, 2), 16).toString();
const g = Number.parseInt(hex.slice(2, 4), 16).toString();
const b = Number.parseInt(hex.slice(4, 6), 16).toString();
const r = Number.parseInt(hex.slice(0, 2), 16);
const g = Number.parseInt(hex.slice(2, 4), 16);
const b = Number.parseInt(hex.slice(4, 6), 16);

return `${ANSI_ESCAPE}[${background ? '48' : '38'};2;${r};${g};${b}m`;
return [r, g, b];
}

export const SUPPORTED_LANGUAGES = [
Expand Down

0 comments on commit c77cfa9

Please sign in to comment.