Skip to content

Commit

Permalink
feat: Change Deno.stdout.writable.getWriter() to writeAll
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfuxing committed Oct 30, 2024
1 parent 32cd98a commit dbd8a86
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import { MultiProgressBar } from "jsr:@deno-library/progress";
import { delay } from "jsr:@std/async";

// or JSR (with version)
// import { MultiProgressBar } from "jsr:@deno-library/[email protected].0";
// import { MultiProgressBar } from "jsr:@deno-library/[email protected].1";
// import { delay } from "jsr:@std/[email protected]";

// or JSR (no prefix, run `deno add @deno-library/progress` and `deno add @std/async`)
// import { MultiProgressBar } from "@deno-library/progress";
// import { delay } from "@std/async";

// or
// import { MultiProgressBar } from "https://deno.land/x/[email protected].0/mod.ts";
// import { MultiProgressBar } from "https://deno.land/x/[email protected].1/mod.ts";
// import { delay } from "https://deno.land/[email protected]/async/delay.ts";

const title = "download files";
Expand Down Expand Up @@ -168,15 +168,15 @@ import ProgressBar from "jsr:@deno-library/progress";
import { delay } from "jsr:@std/async";

// or JSR (with version)
// import ProgressBar from "jsr:@deno-library/[email protected].0";
// import ProgressBar from "jsr:@deno-library/[email protected].1";
// import { delay } from "jsr:@std/[email protected]";

// or JSR (no prefix, run `deno add @deno-library/progress` and `deno add @std/async`)
// import ProgressBar from "@deno-library/progress";
// import { delay } from "@std/async";

// or
// import ProgressBar from "https://deno.land/x/[email protected].0/mod.ts";
// import ProgressBar from "https://deno.land/x/[email protected].1/mod.ts";
// import { delay } from "@std/async/delay";

const title = "downloading:";
Expand All @@ -203,15 +203,15 @@ import ProgressBar from "jsr:@deno-library/progress";
import { delay } from "jsr:@std/async";

// or JSR (with version)
// import ProgressBar from "jsr:@deno-library/[email protected].0";
// import ProgressBar from "jsr:@deno-library/[email protected].1";
// import { delay } from "jsr:@std/[email protected]";

// or JSR (no prefix, run `deno add @deno-library/progress` and `deno add @std/async`)
// import ProgressBar from "@deno-library/progress";
// import { delay } from "@std/async";

// or
// import ProgressBar from "https://deno.land/x/[email protected].0/mod.ts";
// import ProgressBar from "https://deno.land/x/[email protected].1/mod.ts";
// import { delay } from "@std/async/delay";

const total = 100;
Expand Down
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
## Changelog

### v1.5.0 - 2024.04.02
### v1.5.1 - 2024.10.30

Changed `Deno.stdout.writable.getWriter()` to `writeAll` in `@std/io/write-all`.

Initially, I was using `writeAllSync`, but on December 27, 2023, I discovered that `writeAllSync` was deprecated, and the documentation recommended using the `stream API`. As a result, I released version 1.4.1 (Remove deprecated writeAllSync, Use WritableStream instead). However, both `writeAll` and `writeAllSync` are now available again, and I plan to switch back. Currently using the promise style, so I changed to use `writeAll`.

### v1.5.0 - 2024.10.30

[Allow to configure the writer to something other than stdout](https://github.com/deno-library/progress/issues/30)

### v1.4.9 - 2024.04.02
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deno-library/progress",
"version": "1.5.0",
"version": "1.5.1",
"exports": {
".": "./mod.ts",
"./time": "./time.ts"
Expand Down
4 changes: 3 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { bgGreen, bgWhite, stripAnsiCode } from "jsr:@std/[email protected]/colors";
export { bgGreen, bgWhite, stripAnsiCode } from "jsr:@std/[email protected]/colors";
export { writeAll } from "jsr:@std/[email protected]/write-all";
// export type { Writer } from "jsr:@std/[email protected]/types";
14 changes: 10 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { bgGreen, bgWhite, stripAnsiCode } from "./deps.ts";
import { writeAll } from "./deps.ts";
// import type { Writer } from "./deps.ts";
import { prettyTime, type prettyTimeOptions } from "./time.ts";
export { MultiProgressBar } from "./multi.ts";

Expand Down Expand Up @@ -55,7 +57,9 @@ export default class ProgressBar {
private start = Date.now();
private lastRenderTime = 0;
private encoder = new TextEncoder();
private writer: WritableStreamDefaultWriter<Uint8Array>;
// private writer: WritableStreamDefaultWriter<Uint8Array>;
// private writer: Writer;
private writer: typeof Deno.stdout | typeof Deno.stderr;

/**
* Title, total, complete, incomplete, can also be set or changed in the render method
Expand Down Expand Up @@ -95,7 +99,8 @@ export default class ProgressBar {
this.display = display ??
":title :percent :bar :time :completed/:total :text";
this.prettyTime = prettyTime;
this.writer = output.writable.getWriter();
// this.writer = output.writable.getWriter();
this.writer = output;
}

/**
Expand Down Expand Up @@ -209,7 +214,7 @@ export default class ProgressBar {
await this.breakLine();
}
await this.showCursor();
this.writer.releaseLock();
// this.writer.releaseLock();
}

/**
Expand Down Expand Up @@ -238,7 +243,8 @@ export default class ProgressBar {
}

private stdoutWrite(msg: string): Promise<void> {
return this.writer.write(this.encoder.encode(msg));
// return this.writer.write(this.encoder.encode(msg));
return writeAll(this.writer, this.encoder.encode(msg))
}

private clearLine(direction: Direction = Direction.all): Promise<void> {
Expand Down
14 changes: 10 additions & 4 deletions multi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { bgGreen, bgWhite, stripAnsiCode } from "./deps.ts";
import { writeAll } from "./deps.ts";
// import type { Writer } from "./deps.ts";
import { prettyTime, type prettyTimeOptions } from "./time.ts";

const isTerminal = Deno.stdout.isTerminal;
Expand Down Expand Up @@ -50,7 +52,9 @@ export class MultiProgressBar {
private start = Date.now();
private lastRenderTime = 0;
private encoder = new TextEncoder();
private writer: WritableStreamDefaultWriter<Uint8Array>;
// private writer: WritableStreamDefaultWriter<Uint8Array>;
// private writer: Writer;
private writer: typeof Deno.stdout | typeof Deno.stderr;

/**
* Title, total, complete, incomplete, can also be set or changed in the render method
Expand Down Expand Up @@ -87,7 +91,8 @@ export class MultiProgressBar {
this.interval = interval ?? 16;
this.display = display ?? ":bar :text :percent :time :completed/:total";
this.prettyTime = prettyTime;
this.writer = output.writable.getWriter();
// this.writer = output.writable.getWriter();
this.writer = output;
}

/**
Expand Down Expand Up @@ -199,7 +204,7 @@ export class MultiProgressBar {
await this.breakLine();
}
await this.showCursor();
this.writer.releaseLock();
// this.writer.releaseLock();
}

/**
Expand Down Expand Up @@ -234,7 +239,8 @@ export class MultiProgressBar {
}

private stdoutWrite(msg: string): Promise<void> {
return this.writer.write(this.encoder.encode(msg));
// return this.writer.write(this.encoder.encode(msg));
return writeAll(this.writer, this.encoder.encode(msg))
}

private showCursor(): Promise<void> {
Expand Down

0 comments on commit dbd8a86

Please sign in to comment.