Skip to content

Commit

Permalink
Merge branch 'master' into no_auto_run_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ry authored Jan 24, 2019
2 parents 3955701 + d1a9605 commit 36bc5dc
Show file tree
Hide file tree
Showing 17 changed files with 611 additions and 257 deletions.
8 changes: 4 additions & 4 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
variables:
DENO_VERSION: 'v0.2.7'
DENO_VERSION: 'v0.2.8'

jobs:

- job: 'Linux'
pool:
vmImage: 'Ubuntu-16.04'
steps:
- script: curl -L https://deno.land/x/install/install.py | python - $(DENO_VERSION)
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
- script: deno test.ts --allow-run --allow-net --allow-write

- job: 'Mac'
pool:
vmImage: 'macOS-10.13'
steps:
- script: curl -L https://deno.land/x/install/install.py | python - $(DENO_VERSION)
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
- script: deno test.ts --allow-run --allow-net --allow-write

- job: 'Windows'
pool:
vmImage: 'vs2017-win2016'
steps:
- powershell: iwr https://deno.land/x/install/install.ps1 -Outfile 'install.ps1'; ./install.ps1 $(DENO_VERSION)
- powershell: iwr https://deno.land/x/install/install.ps1 -out install.ps1; .\install.ps1 $(DENO_VERSION)
- script: echo '##vso[task.prependpath]C:\Users\VssAdministrator\.deno\bin\'
- script: 'C:\Users\VssAdministrator\.deno\bin\deno.exe test.ts --allow-run --allow-net --allow-write'
11 changes: 6 additions & 5 deletions colors/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# colors

Is a basic console color module intended for [Deno](https://deno.land/). It is
inspired by [chalk](https://www.npmjs.com/package/chalk) and
inspired by [chalk](https://www.npmjs.com/package/chalk),
[kleur](https://www.npmjs.com/package/kleur), 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:
The main modules exports several functions which can color the output to the
console:

```ts
import { color } from "https://deno.land/x/std/colors/mod.ts";
import { bgBlue, red, bold } from "https://deno.land/x/std/colors/mod.ts";

console.log(color.bgBlue.red.bold("Hello world!"));
console.log(bgBlue(red(bold("Hello world!"))));
```

## TODO
Expand Down
4 changes: 2 additions & 2 deletions colors/example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { color } from "./mod.ts";
import { bgBlue, red, bold, italic } from "./mod.ts";

console.log(color.bgBlue.red.bold("Hello world!"));
console.log(bgBlue(italic(red(bold("Hello world!")))));
160 changes: 129 additions & 31 deletions colors/mod.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,131 @@
// Copyright 2018-2019 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
});

interface Code {
open: string;
close: string;
regexp: RegExp;
}

let enabled = true;

export function setEnabled(value: boolean) {
enabled = value;
}

export function getEnabled(): boolean {
return enabled;
}

function code(open: number, close: number): Code {
return {
open: `\x1b[${open}m`,
close: `\x1b[${close}m`,
regexp: new RegExp(`\\x1b\\[${close}m`, "g")
};
}

function run(str: string, code: Code) {
return enabled
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
: str;
}

export function reset(str: string): string {
return run(str, code(0, 0));
}

export function bold(str: string): string {
return run(str, code(1, 22));
}

export function dim(str: string): string {
return run(str, code(2, 22));
}

export function italic(str: string): string {
return run(str, code(3, 23));
}

export function underline(str: string): string {
return run(str, code(4, 24));
}

export function inverse(str: string): string {
return run(str, code(7, 27));
}

export function hidden(str: string): string {
return run(str, code(8, 28));
}

export function strikethrough(str: string): string {
return run(str, code(9, 29));
}

export function black(str: string): string {
return run(str, code(30, 39));
}

export function red(str: string): string {
return run(str, code(31, 39));
}

export function green(str: string): string {
return run(str, code(32, 39));
}

export function yellow(str: string): string {
return run(str, code(33, 39));
}

export function blue(str: string): string {
return run(str, code(34, 39));
}

export function magenta(str: string): string {
return run(str, code(35, 39));
}

export function cyan(str: string): string {
return run(str, code(36, 39));
}

export function white(str: string): string {
return run(str, code(37, 39));
}

export function gray(str: string): string {
return run(str, code(90, 39));
}

export function bgBlack(str: string): string {
return run(str, code(40, 49));
}

export function bgRed(str: string): string {
return run(str, code(41, 49));
}

export function bgGreen(str: string): string {
return run(str, code(42, 49));
}

export function bgYellow(str: string): string {
return run(str, code(43, 49));
}

export function bgBlue(str: string): string {
return run(str, code(44, 49));
}

export function bgMagenta(str: string): string {
return run(str, code(45, 49));
}

export function bgCyan(str: string): string {
return run(str, code(46, 49));
}

export function bgWhite(str: string): string {
return run(str, code(47, 49));
}
66 changes: 0 additions & 66 deletions colors/styles.ts

This file was deleted.

20 changes: 10 additions & 10 deletions colors/test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { assertEqual, test } from "../testing/mod.ts";
import { color } from "./mod.ts";
import { assert, test } from "../testing/mod.ts";
import { red, bgBlue, setEnabled, getEnabled } from "./mod.ts";
import "./example.ts";

test(function singleColor() {
assertEqual(color.red("Hello world"), "[31mHello world[39m");
assert.equal(red("Hello world"), "[31mHello world[39m");
});

test(function doubleColor() {
assertEqual(color.red.bgBlue("Hello world"), "[44m[31mHello world[39m[49m");
assert.equal(bgBlue(red("Hello world")), "[44m[31mHello world[39m[49m");
});

test(function newLinesContinueColors() {
assertEqual(color.red("Hello\nworld"), "Hello\nworld");
assertEqual(color.red("Hello\r\nworld"), "Hello\r\nworld");
assertEqual(color.red("Hello\n\nworld"), "Hello\n\nworld");
test(function replacesCloseCharacters() {
assert.equal(red("Hello"), "Hello");
});

test(function replacesCloseCharacters() {
assertEqual(color.red("Hello"), "Hello");
test(function enablingColors() {
assert.equal(getEnabled(), true);
setEnabled(false);
assert.equal(bgBlue(red("Hello world")), "Hello world");
});
6 changes: 1 addition & 5 deletions format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ async function main() {
await checkVersion();

const prettier = run({
args: [
"bash",
"-c",
"prettier --write *.ts */*.ts */**/*.ts *.md */*.md */**/*.md"
]
args: ["bash", "-c", "prettier --write '**/*.ts' '**/*.md'"]
});
const s = await prettier.status();
exit(s.code);
Expand Down
1 change: 1 addition & 0 deletions io/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Buffer, Reader } from "deno";
// from `src`.
// Returns the number of bytes copied.
export function copyBytes(dst: Uint8Array, src: Uint8Array, off = 0): number {
off = Math.max(0, Math.min(off, dst.byteLength));
const r = dst.byteLength - off;
if (src.byteLength > r) {
src = src.subarray(0, r);
Expand Down
36 changes: 36 additions & 0 deletions io/util_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { test, assert } from "../testing/mod.ts";
import { copyBytes } from "./util.ts";

test(function testCopyBytes() {
let dst = new Uint8Array(4);

dst.fill(0);
let src = Uint8Array.of(1, 2);
let len = copyBytes(dst, src, 0);
assert(len === 2);
assert.equal(dst, Uint8Array.of(1, 2, 0, 0));

dst.fill(0);
src = Uint8Array.of(1, 2);
len = copyBytes(dst, src, 1);
assert(len === 2);
assert.equal(dst, Uint8Array.of(0, 1, 2, 0));

dst.fill(0);
src = Uint8Array.of(1, 2, 3, 4, 5);
len = copyBytes(dst, src);
assert(len === 4);
assert.equal(dst, Uint8Array.of(1, 2, 3, 4));

dst.fill(0);
src = Uint8Array.of(1, 2);
len = copyBytes(dst, src, 100);
assert(len === 0);
assert.equal(dst, Uint8Array.of(0, 0, 0, 0));

dst.fill(0);
src = Uint8Array.of(3, 4);
len = copyBytes(dst, src, -2);
assert(len === 2);
assert.equal(dst, Uint8Array.of(3, 4, 0, 0));
});
Loading

0 comments on commit 36bc5dc

Please sign in to comment.