Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-dldc committed Dec 14, 2024
0 parents commit 1e2f5f6
Show file tree
Hide file tree
Showing 14 changed files with 582 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish

on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v2.x # Run with latest stable Deno.

- run: deno task check
- run: npx jsr publish
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"deno.enable": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"npm.autoDetect": "off",
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Etienne Dldc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CSS console

Print styled text in the console using CSS styles
([using `%c` flag](https://developer.mozilla.org/en-US/docs/Web/API/console#styling_console_output)).
11 changes: 11 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@dldc/console-colors",
"version": "0.1.0",
"exports": "./mod.ts",
"tasks": {
"update": "deno run -A jsr:@molt/cli",
"update:commit": "deno task -q update --commit",
"check": "deno check **/*.ts && deno fmt --check && deno lint"
},
"imports": {}
}
3 changes: 3 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./src/applyStyles.ts";
export * from "./src/base.ts";
export * from "./src/colors.ts";
export * from "./src/css.ts";
export * from "./src/styledString.ts";
25 changes: 25 additions & 0 deletions src/applyStyles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CSS_SUBSTITUTIONS } from "./constants.ts";
import type { TConsoleStyles } from "./css.ts";
import { styledString, type TStyledString } from "./styledString.ts";

export function applyStyles(
value: string | TStyledString,
styles: TConsoleStyles,
): TStyledString {
if (typeof value === "string") {
if (value.includes(CSS_SUBSTITUTIONS)) {
throw new Error("Unexpected CSS_SUBSTITUTIONS in content");
}
return styledString(CSS_SUBSTITUTIONS + value, [styles]);
}
if (value.content.startsWith(CSS_SUBSTITUTIONS)) {
return styledString(
value.content,
value.styles.map((s) => ({ ...s, ...styles })),
);
}
return styledString(CSS_SUBSTITUTIONS + value.content, [
styles,
...value.styles.map((s) => ({ ...s, ...styles })),
]);
}
50 changes: 50 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { CSS_SUBSTITUTIONS } from "./constants.ts";
import type { TConsoleStyles } from "./css.ts";
import { styledString, type TStyledString } from "./styledString.ts";

// template literal fn
export function base(
strings: TemplateStringsArray,
...values: (string | TStyledString)[]
): TStyledString {
const parts: (string | TStyledString)[] = [];
const addPart = (part: string | TStyledString) => {
if (part === "") {
return;
}
const lastPart = parts[parts.length - 1];
if (typeof lastPart === "string") {
parts[parts.length - 1] = lastPart + part;
} else {
parts.push(part);
}
};

for (let i = 0; i < strings.length; i++) {
addPart(strings[i]);
if (i < values.length) {
addPart(values[i]);
}
}

let content = "";
const styles: TConsoleStyles[] = [];
let onlyString = true;

parts.forEach((part) => {
if (typeof part === "string") {
if (onlyString) {
content += part;
return;
}
content += CSS_SUBSTITUTIONS + part;
styles.push({});
return;
}
onlyString = false;
content += part.content;
styles.push(...part.styles);
});

return styledString(content, styles);
}
Loading

0 comments on commit 1e2f5f6

Please sign in to comment.