Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfuxing committed Jan 5, 2024
1 parent 68c30d4 commit 2b42daa
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 60 deletions.
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ProgressBar in terminal for deno

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

const title = "download files";
const total = 100;
Expand All @@ -27,29 +28,26 @@ const bars = new MultiProgressBar({
let completed1 = 0;
let completed2 = 0;

function downloading() {
if (completed1 <= total || completed2 <= total) {
async function download() {
while (completed1 <= total || completed2 <= total) {
completed1 += 1;
completed2 += 2;
bars.render([
await bars.render([
{
completed: completed1,
total,
text: "file1",
// You can also change the style of the progress bar
// complete: "*",
// incomplete: ".",
complete: "*",
incomplete: ".",
},
{ completed: completed2, total, text: "file2" },
]);

setTimeout(function () {
downloading();
}, 100);
await delay(50);
}
}

downloading();
await download();
```

#### interface
Expand Down Expand Up @@ -149,6 +147,7 @@ What is displayed and display order, default: ':bar :text :percent :time

```ts
import ProgressBar from "https://deno.land/x/[email protected]/mod.ts";
export { delay } from "https://deno.land/[email protected]/async/delay.ts";

const title = "downloading:";
const total = 100;
Expand All @@ -157,22 +156,21 @@ const progress = new ProgressBar({
total,
});
let completed = 0;
function downloading() {
if (completed <= total) {
progress.render(completed++);
async function download() {
while (completed <= total) {
await progress.render(completed++);

setTimeout(function () {
downloading();
}, 100);
await delay(50);
}
}
downloading();
await download();
```

#### complex example

```ts
import ProgressBar from "https://deno.land/x/[email protected]/mod.ts";
export { delay } from "https://deno.land/[email protected]/async/delay.ts";

const total = 100;
const progress = new ProgressBar({
Expand All @@ -188,16 +186,14 @@ const progress = new ProgressBar({
// ...
});
let completed = 0;
function run() {
if (completed <= total) {
progress.render(completed++);
async function download() {
while (completed <= total) {
await progress.render(completed++);

setTimeout(function () {
run();
}, 100);
await delay(50);
}
}
run();
await download();
```

More examples in the `examples` folder.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/backward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function forward() {
async function backward() {
while (completed > 0) {
// ==> here
progress.render(--completed);
await progress.render(--completed);
// <== here
await delay(20);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/changeBgColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
if (completed >= 20) {
await progress.render(completed++, {
Expand All @@ -26,4 +26,4 @@ async function run() {
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/changeColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
if (completed >= 20) {
await progress.render(completed++, {
Expand All @@ -28,4 +28,4 @@ async function run() {
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
await progress.render(completed++);

await delay(20);
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/colorProgression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
await progress.render(completed++, {
// ==> here
Expand All @@ -21,4 +21,4 @@ async function run() {
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
progress.render(completed++);

await delay(20);
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const progress = new ProgressBar({

let completed = 0;

async function downloading() {
async function download() {
while (completed <= total) {
await progress.render(completed++);

Expand All @@ -23,4 +23,4 @@ async function downloading() {
}
}

await downloading();
await download();
4 changes: 2 additions & 2 deletions examples/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
await progress.render(completed++);

await delay(20);
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/eta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
await progress.render(completed++);

await delay(20);
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function* log() {

const info = log();

async function run() {
async function download() {
while (completed <= total) {
await progress.render(completed++, {
title: completed % 20 === 0 ? info.next().value + "" : "",
Expand All @@ -32,4 +32,4 @@ async function run() {
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const bars = new MultiProgressBar({
let completed1 = 0;
let completed2 = 0;

async function downloading() {
async function download() {
while (completed1 <= total || completed2 <= total) {
completed1 += 1;
completed2 += 2;
Expand All @@ -34,4 +34,4 @@ async function downloading() {
}
}

await downloading();
await download();
4 changes: 2 additions & 2 deletions examples/multiPrettyTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const bars = new MultiProgressBar({
let completed1 = 0;
let completed2 = 0;

async function downloading() {
async function download() {
if (completed1 <= total || completed2 <= total) {
completed1 += 1;
completed2 += 2;
Expand Down Expand Up @@ -49,4 +49,4 @@ async function downloading() {
}
}

await downloading();
await download();
4 changes: 2 additions & 2 deletions examples/preciseBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const progress = new ProgressBar({

let completed = 0;

async function downloading() {
async function download() {
while (completed <= total) {
await progress.render(completed++);

await delay(50);
}
}

await downloading();
await download();
4 changes: 2 additions & 2 deletions examples/prettyTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const progress = new ProgressBar({

let completed = 0;

async function run() {
async function download() {
while (completed <= total) {
await progress.render(completed++, {
prettyTimeOptions: {
Expand All @@ -29,4 +29,4 @@ async function run() {
}
}

await run();
await download();
4 changes: 2 additions & 2 deletions examples/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const progress = new ProgressBar({

let completed = 0;

async function downloading() {
async function download() {
while (completed <= total) {
await progress.render(completed++);

await delay(50);
}
}

await downloading();
await download();
4 changes: 2 additions & 2 deletions examples/total.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const progress = new ProgressBar({

let completed = 0;

async function downloading() {
async function download() {
while (completed <= total) {
// Can also be set in the constructor
// ==> here
Expand All @@ -23,4 +23,4 @@ async function downloading() {
}
}

await downloading();
await download();
4 changes: 2 additions & 2 deletions examples/width.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ const progress = new ProgressBar({

let completed = 0;

async function downloading() {
async function download() {
while (completed <= total) {
await progress.render(completed++);

await delay(20);
}
}

await downloading();
await download();
2 changes: 1 addition & 1 deletion tests/mod.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ProgressBar from "../mod.ts";
import { simpleTimerStream } from "../deps_dev.ts";
import { simpleTimerStream } from "../deps_test.ts";

Deno.test(`Use ProgressBar in a deno test`, async () => {
const progress = new ProgressBar({ title: "downloading: ", total: 50 });
Expand Down
2 changes: 1 addition & 1 deletion tests/multi.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MultiProgressBar } from "../mod.ts";
import { delay } from "../deps_dev.ts";
import { delay } from "../deps_test.ts";

Deno.test(`Use MultiProgressBar in a deno test`, async () => {
const title = "download files";
Expand Down
2 changes: 1 addition & 1 deletion tests/time.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prettyTime } from "../time.ts";
import { assertEquals } from "../deps_dev.ts";
import { assertEquals } from "../deps_test.ts";

Deno.test(`test prettyTime: default`, () => {
// assertEquals(prettyTime(10), "10.0s");
Expand Down

0 comments on commit 2b42daa

Please sign in to comment.