Skip to content

Commit

Permalink
feat: add fg and bg styles
Browse files Browse the repository at this point in the history
  • Loading branch information
m15a committed Jan 25, 2024
1 parent e90cedc commit 02923f4
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions spago.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ workspace:
- aff
- console
- effect
- functions
- maybe
- prelude
test_dependencies:
Expand Down
1 change: 1 addition & 0 deletions spago.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package:
- aff
- console
- effect
- functions
- maybe
- prelude
test:
Expand Down
9 changes: 9 additions & 0 deletions src/Chalk/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ export const bgBlueBright = function(chalk_) { return chalk_.bgBlueBright; };
export const bgMagentaBright = function(chalk_) { return chalk_.bgMagentaBright; };
export const bgCyanBright = function(chalk_) { return chalk_.bgCyanBright; };
export const bgWhiteBright = function(chalk_) { return chalk_.bgWhiteBright; };

/* 256 and Truecolor support */

export const _rgb = function(r_, g_, b_) { return function(chalk_) { return chalk_.rgb(r_, g_, b_); }; };
export const _hex = function(hex_) { return function(chalk_) { return chalk_.hex(hex_); }; };
export const _ansi256 = function(i_) { return function(chalk_) { return chalk_.ansi256(i_); }; };
export const _bgRgb = function(r_, g_, b_) { return function(chalk_) { return chalk_.bgRgb(r_, g_, b_); }; };
export const _bgHex = function(hex_) { return function(chalk_) { return chalk_.bgHex(hex_); }; };
export const _bgAnsi256 = function(i_) { return function(chalk_) { return chalk_.bgAnsi256(i_); }; };
24 changes: 23 additions & 1 deletion src/Chalk/Style.purs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ module Chalk.Style
, bgMagentaBright
, bgCyanBright
, bgWhiteBright
, fg
, bg
) where

import Chalk.Types (Style)
import Chalk.Types (Color(..), Style)
import Data.Function.Uncurried (Fn3, runFn3)

{- Modifiers -}

Expand Down Expand Up @@ -101,3 +104,22 @@ foreign import bgBlueBright :: Style
foreign import bgMagentaBright :: Style
foreign import bgCyanBright :: Style
foreign import bgWhiteBright :: Style

{- 256 and Truecolor support -}

foreign import _rgb :: Fn3 Int Int Int Style
foreign import _hex :: String -> Style
foreign import _ansi256 :: Int -> Style
foreign import _bgRgb :: Fn3 Int Int Int Style
foreign import _bgHex :: String -> Style
foreign import _bgAnsi256 :: Int -> Style

fg :: Color -> Style
fg (RGB { r, g, b }) = runFn3 _rgb r g b
fg (Hex hex) = _hex hex
fg (ANSI256 i) = _ansi256 i

bg :: Color -> Style
bg (RGB { r, g, b }) = runFn3 _bgRgb r g b
bg (Hex hex) = _bgHex hex
bg (ANSI256 i) = _bgAnsi256 i
15 changes: 14 additions & 1 deletion src/Chalk/Types.purs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Chalk.Types
( Chalk
, Color(..)
, Style
, SupportsColor
) where

import Prelude (Unit)
import Prelude

newtype Chalk = Chalk Unit

Expand All @@ -16,3 +17,15 @@ type SupportsColor =
, has256 :: Boolean
, has16m :: Boolean
}

data Color
= RGB { r :: Int, g :: Int, b :: Int }
| Hex String
| ANSI256 Int

derive instance Eq Color

instance Show Color where
show (RGB { r, g, b }) = "(RGB " <> show r <> " " <> show g <> " " <> show b <> ")"
show (Hex hex) = "(Hex " <> hex <> ")"
show (ANSI256 i) = "(ANSI256 " <> show i <> ")"
9 changes: 9 additions & 0 deletions tools/generate-style-js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ for (const name of backgroundColorNames) {
stdout.write(exportStyle(name));
}

stdout.write("\n/* 256 and Truecolor support */\n\n");

stdout.write("export const _rgb = function(r_, g_, b_) { return function(chalk_) { return chalk_.rgb(r_, g_, b_); }; };\n");
stdout.write("export const _hex = function(hex_) { return function(chalk_) { return chalk_.hex(hex_); }; };\n");
stdout.write("export const _ansi256 = function(i_) { return function(chalk_) { return chalk_.ansi256(i_); }; };\n");
stdout.write("export const _bgRgb = function(r_, g_, b_) { return function(chalk_) { return chalk_.bgRgb(r_, g_, b_); }; };\n");
stdout.write("export const _bgHex = function(hex_) { return function(chalk_) { return chalk_.bgHex(hex_); }; };\n");
stdout.write("export const _bgAnsi256 = function(i_) { return function(chalk_) { return chalk_.bgAnsi256(i_); }; };\n");

// vim: tw=88 nowrap
27 changes: 25 additions & 2 deletions tools/generate-style-purs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
import { modifierNames, foregroundColorNames, backgroundColorNames } from 'chalk';
const stdout = process.stdout;

const allNames = modifierNames.concat(foregroundColorNames, backgroundColorNames);
const allNames = modifierNames.concat(
foregroundColorNames,
backgroundColorNames,
[ "fg", "bg" ]
);
stdout.write("module Chalk.Style\n ( ");
stdout.write(allNames.join("\n , "));
stdout.write("\n ) where\n\n");

stdout.write("import Chalk.Types (Style)\n\n");
stdout.write("import Chalk.Types (Color(..), Style)\n");
stdout.write("import Data.Function.Uncurried (Fn3, runFn3)\n\n")

const importStyle = function(name) {
return `foreign import ${name} :: Style\n`;
Expand All @@ -35,4 +40,22 @@ for (const name of backgroundColorNames) {
stdout.write(importStyle(name));
}

stdout.write("\n{- 256 and Truecolor support -}\n\n");

stdout.write("foreign import _rgb :: Fn3 Int Int Int Style\n");
stdout.write("foreign import _hex :: String -> Style\n");
stdout.write("foreign import _ansi256 :: Int -> Style\n");
stdout.write("foreign import _bgRgb :: Fn3 Int Int Int Style\n");
stdout.write("foreign import _bgHex :: String -> Style\n");
stdout.write("foreign import _bgAnsi256 :: Int -> Style\n\n");

stdout.write("fg :: Color -> Style\n");
stdout.write("fg (RGB { r, g, b }) = runFn3 _rgb r g b\n");
stdout.write("fg (Hex hex) = _hex hex\n");
stdout.write("fg (ANSI256 i) = _ansi256 i\n\n");
stdout.write("bg :: Color -> Style\n");
stdout.write("bg (RGB { r, g, b }) = runFn3 _bgRgb r g b\n");
stdout.write("bg (Hex hex) = _bgHex hex\n");
stdout.write("bg (ANSI256 i) = _bgAnsi256 i\n");

// vim: tw=88 nowrap

0 comments on commit 02923f4

Please sign in to comment.