Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ColorTheme type #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

rindeal
Copy link

@rindeal rindeal commented Sep 9, 2024

Partial is needed to support:

  const theme = {
    ...defaultOptions.colors!,
    ...options.colors
  };

The callable type then allows to use multiple stylings like bold+red, or even completely custom stylings.

Explicit Record type enforces strict typing for defaultTheme:

defaultTheme: Record<TokenType, Color>

After these changes, one could write a theme such as:

const createFormatter = (hex: string, bold = false) => {
  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);
  const colorCode = `\x1b[38;2;${r};${g};${b}m`;
  const boldCode = bold ? '\x1b[1m' : '';
  const resetCode = '\x1b[0m';
  return (str: string) => `${boldCode}${colorCode}${str}${resetCode}`;
};

const JSON_THEME: ColorTheme = {
  Whitespace: (str: string) => str,
  Brace: createFormatter('#ADD8E6'), // Light Blue
  Bracket: createFormatter('#ADD8E6'), // Light Blue
  Colon: createFormatter('#FF69B4'), // Hot Pink
  Comma: createFormatter('#FF69B4'), // Hot Pink
  StringKey: createFormatter('#FFA500', true), // Bold Orange
  StringLiteral: createFormatter('#00BFFF'), // Deep Sky Blue
  NumberLiteral: createFormatter('#FF4500'), // Orange Red
  BooleanLiteral: createFormatter('#FF4500', true), // Bold Orange Red
  NullLiteral: createFormatter('#FF4500', true), // Bold Orange Red
};

Partial is needed to support:
```ts
  const theme = {
    ...defaultOptions.colors!,
    ...options.colors
  };
```

The callable type then allows to use multiple stylings like bold+red,
or even completely custom stylings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant