mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
Add colors module (#30)
This commit is contained in:
parent
1a35f9daf5
commit
54787f172c
30
README.md
30
README.md
@ -2,27 +2,15 @@
|
||||
|
||||
[![Build Status](https://travis-ci.com/denoland/deno_std.svg?branch=master)](https://travis-ci.com/denoland/deno_std)
|
||||
|
||||
Usage:
|
||||
This repository contains collections of modules that create a standard library
|
||||
for Deno.
|
||||
|
||||
```typescript
|
||||
import { serve } from "https://deno.land/x/net/http.ts";
|
||||
const s = serve("0.0.0.0:8000");
|
||||
| Collection | Description |
|
||||
| ---------------------------- | --------------------------------------------------------------- |
|
||||
| [colors](./colors/README.md) | Modules that generate ANSI color codes for the console. |
|
||||
| [net](./net/README.md) | A framework for creating HTTP/HTTPS servers inspired by GoLang. |
|
||||
| [path](./path/README.md) | A path manipulation library. |
|
||||
|
||||
async function main() {
|
||||
for await (const req of s) {
|
||||
req.respond({ body: new TextEncoder().encode("Hello World\n") });
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
```
|
||||
|
||||
## File Server
|
||||
|
||||
A small program for serving local files over HTTP.
|
||||
|
||||
Add the following to your `.bash_profile`
|
||||
```
|
||||
alias file_server="deno https://deno.land/x/net/file_server.ts --allow-net"
|
||||
```
|
||||
---
|
||||
|
||||
Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
|
28
colors/README.md
Normal file
28
colors/README.md
Normal file
@ -0,0 +1,28 @@
|
||||
# colors
|
||||
|
||||
Is a basic console color library intended for [Deno](https://deno.land/). It is
|
||||
inspired by packages like [chalk](https://www.npmjs.com/package/chalk) and
|
||||
[colors](https://www.npmjs.com/package/colors) on npm.
|
||||
|
||||
## Usage
|
||||
|
||||
The main modules exports a single function name `color` which is a function
|
||||
that provides chaining to stack colors. Basic usage looks like this:
|
||||
|
||||
```ts
|
||||
import { color } from "https://deno.land/x/colors/main.ts";
|
||||
|
||||
console.log(color.bgBlue.red.bold("Hello world!"));
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
- Currently, it just assumes it is running in an environment that supports ANSI
|
||||
escape code terminal coloring. It should actually detect, specifically
|
||||
windows and adjust properly.
|
||||
|
||||
- Test coverage is very basic at the moment.
|
||||
|
||||
---
|
||||
|
||||
Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
33
colors/main.ts
Normal file
33
colors/main.ts
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
import { styles } from "./styles.ts";
|
||||
|
||||
type Styles = { readonly [S in keyof typeof styles]: Color };
|
||||
|
||||
type Color = Styles & {
|
||||
(str: string): string;
|
||||
};
|
||||
|
||||
const styleStack: string[] = [];
|
||||
|
||||
export const color = function color(str: string): string {
|
||||
styleStack.reverse();
|
||||
while (styleStack.length) {
|
||||
const style = styleStack.pop();
|
||||
const code = styles[style];
|
||||
str = `${code.open}${str.replace(code.closeRe, code.open)}${
|
||||
code.close
|
||||
}`.replace(/\r?\n/g, `${code.close}$&${code.open}`);
|
||||
}
|
||||
return str;
|
||||
} as Color;
|
||||
|
||||
for (const style of Object.keys(styles)) {
|
||||
Object.defineProperty(color, style, {
|
||||
get() {
|
||||
styleStack.push(style);
|
||||
return color;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: false
|
||||
});
|
||||
}
|
10
colors/main_test.ts
Normal file
10
colors/main_test.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { assertEqual, test } from "https://deno.land/x/testing/testing.ts";
|
||||
import { color } from "./main";
|
||||
|
||||
test(function singleColor() {
|
||||
assertEqual(color.red("Hello world"), "[31mHello world[39m");
|
||||
});
|
||||
|
||||
test(function doubleColor() {
|
||||
assertEqual(color.red.bgBlue("Hello world"), "[44m[31mHello world[39m[49m");
|
||||
});
|
66
colors/styles.ts
Normal file
66
colors/styles.ts
Normal file
@ -0,0 +1,66 @@
|
||||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
const matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
|
||||
function escapeStringRegexp(str: string): string {
|
||||
return str.replace(matchOperatorsRe, "\\$&");
|
||||
}
|
||||
|
||||
const codes = {
|
||||
reset: [0, 0],
|
||||
bold: [1, 22],
|
||||
dim: [2, 22],
|
||||
italic: [3, 23],
|
||||
underline: [4, 24],
|
||||
inverse: [7, 27],
|
||||
hidden: [8, 28],
|
||||
strikethrough: [9, 29],
|
||||
|
||||
black: [30, 39],
|
||||
red: [31, 39],
|
||||
green: [32, 39],
|
||||
yellow: [33, 39],
|
||||
blue: [34, 39],
|
||||
magenta: [35, 39],
|
||||
cyan: [36, 39],
|
||||
white: [37, 39],
|
||||
|
||||
blackBright: [90, 39],
|
||||
redBright: [91, 39],
|
||||
greenBright: [92, 39],
|
||||
yellowBright: [93, 39],
|
||||
blueBright: [94, 39],
|
||||
magentaBright: [95, 39],
|
||||
cyanBright: [96, 39],
|
||||
whiteBright: [97, 39],
|
||||
|
||||
bgBlack: [40, 49],
|
||||
bgRed: [41, 49],
|
||||
bgGreen: [42, 49],
|
||||
bgYellow: [43, 49],
|
||||
bgBlue: [44, 49],
|
||||
bgMagenta: [45, 49],
|
||||
bgCyan: [46, 49],
|
||||
bgWhite: [47, 49],
|
||||
|
||||
bgBlackBright: [100, 49],
|
||||
bgRedBright: [101, 49],
|
||||
bgGreenBright: [102, 49],
|
||||
bgYellowBright: [103, 49],
|
||||
bgBlueBright: [104, 49],
|
||||
bgMagentaBright: [105, 49],
|
||||
bgCyanBright: [106, 49],
|
||||
bgWhiteBright: [107, 49]
|
||||
};
|
||||
|
||||
type Styles<T> = {
|
||||
[S in keyof T]: { open: string; close: string; closeRe: RegExp }
|
||||
};
|
||||
|
||||
export const styles: Styles<typeof codes> = {} as any;
|
||||
|
||||
for (const [style, [open, close]] of Object.entries(codes)) {
|
||||
styles[style] = {
|
||||
open: `\u001b[${open}m`,
|
||||
close: `\u001b[${close}m`,
|
||||
closeRe: new RegExp(escapeStringRegexp(`\u001b[${close}m`), "g")
|
||||
};
|
||||
}
|
26
net/README.md
Normal file
26
net/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
# net
|
||||
|
||||
Usage:
|
||||
|
||||
```typescript
|
||||
import { serve } from "https://deno.land/x/net/http.ts";
|
||||
const s = serve("0.0.0.0:8000");
|
||||
|
||||
async function main() {
|
||||
for await (const req of s) {
|
||||
req.respond({ body: new TextEncoder().encode("Hello World\n") });
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
```
|
||||
|
||||
### File Server
|
||||
|
||||
A small program for serving local files over HTTP.
|
||||
|
||||
Add the following to your `.bash_profile`
|
||||
|
||||
```
|
||||
alias file_server="deno https://deno.land/x/net/file_server.ts --allow-net"
|
||||
```
|
Loading…
Reference in New Issue
Block a user