diff --git a/README.md b/README.md index ab668e9130..73d48ab3af 100644 --- a/README.md +++ b/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. diff --git a/colors/README.md b/colors/README.md new file mode 100644 index 0000000000..f20d1939a9 --- /dev/null +++ b/colors/README.md @@ -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. diff --git a/colors/main.ts b/colors/main.ts new file mode 100644 index 0000000000..af4e1aace8 --- /dev/null +++ b/colors/main.ts @@ -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 + }); +} diff --git a/colors/main_test.ts b/colors/main_test.ts new file mode 100644 index 0000000000..596a592a66 --- /dev/null +++ b/colors/main_test.ts @@ -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"), "Hello world"); +}); + +test(function doubleColor() { + assertEqual(color.red.bgBlue("Hello world"), "Hello world"); +}); diff --git a/colors/styles.ts b/colors/styles.ts new file mode 100644 index 0000000000..74f609b83a --- /dev/null +++ b/colors/styles.ts @@ -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 = { + [S in keyof T]: { open: string; close: string; closeRe: RegExp } +}; + +export const styles: Styles = {} 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") + }; +} diff --git a/net/README.md b/net/README.md new file mode 100644 index 0000000000..65f2b9ad76 --- /dev/null +++ b/net/README.md @@ -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" +``` diff --git a/test.ts b/test.ts index 54413ecd95..1155202d14 100755 --- a/test.ts +++ b/test.ts @@ -1,6 +1,10 @@ #!/usr/bin/env deno --allow-run --allow-net -import { run, exit } from "deno"; +import { run } from "deno"; +// colors tests +import "colors/main_test.ts"; + +// net tests import "net/bufio_test.ts"; import "net/http_test.ts"; import "net/textproto_test.ts";