From 2b42daa09f6637705c278f3d1fa8aaf617c40ee6 Mon Sep 17 00:00:00 2001 From: zhangfuxing Date: Fri, 5 Jan 2024 11:29:41 +0800 Subject: [PATCH] optimize code --- README.md | 44 ++++++++++++++++-------------------- deps_dev.ts => deps_test.ts | 0 examples/backward.ts | 2 +- examples/changeBgColor.ts | 4 ++-- examples/changeColor.ts | 4 ++-- examples/clear.ts | 4 ++-- examples/colorProgression.ts | 4 ++-- examples/complete.ts | 4 ++-- examples/console.ts | 4 ++-- examples/display.ts | 4 ++-- examples/eta.ts | 4 ++-- examples/info.ts | 4 ++-- examples/multi.ts | 4 ++-- examples/multiPrettyTime.ts | 4 ++-- examples/preciseBar.ts | 4 ++-- examples/prettyTime.ts | 4 ++-- examples/title.ts | 4 ++-- examples/total.ts | 4 ++-- examples/width.ts | 4 ++-- tests/mod.test.ts | 2 +- tests/multi.test.ts | 2 +- tests/time.test.ts | 2 +- 22 files changed, 56 insertions(+), 60 deletions(-) rename deps_dev.ts => deps_test.ts (100%) diff --git a/README.md b/README.md index c83a732..493ff6f 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ ProgressBar in terminal for deno ```ts import { MultiProgressBar } from "https://deno.land/x/progress@v1.4.4/mod.ts"; +export { delay } from "https://deno.land/std@0.210.0/async/delay.ts"; const title = "download files"; const total = 100; @@ -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 @@ -149,6 +147,7 @@ What is displayed and display order, default: ':bar :text :percent :time ```ts import ProgressBar from "https://deno.land/x/progress@v1.4.4/mod.ts"; +export { delay } from "https://deno.land/std@0.210.0/async/delay.ts"; const title = "downloading:"; const total = 100; @@ -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/progress@v1.4.4/mod.ts"; +export { delay } from "https://deno.land/std@0.210.0/async/delay.ts"; const total = 100; const progress = new ProgressBar({ @@ -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. diff --git a/deps_dev.ts b/deps_test.ts similarity index 100% rename from deps_dev.ts rename to deps_test.ts diff --git a/examples/backward.ts b/examples/backward.ts index 3e08ffc..a2fdfca 100644 --- a/examples/backward.ts +++ b/examples/backward.ts @@ -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); } diff --git a/examples/changeBgColor.ts b/examples/changeBgColor.ts index d20e844..4789a5e 100644 --- a/examples/changeBgColor.ts +++ b/examples/changeBgColor.ts @@ -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++, { @@ -26,4 +26,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/changeColor.ts b/examples/changeColor.ts index 71ae16b..9898607 100644 --- a/examples/changeColor.ts +++ b/examples/changeColor.ts @@ -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++, { @@ -28,4 +28,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/clear.ts b/examples/clear.ts index 3f55305..dd04452 100644 --- a/examples/clear.ts +++ b/examples/clear.ts @@ -12,7 +12,7 @@ const progress = new ProgressBar({ let completed = 0; -async function run() { +async function download() { while (completed <= total) { await progress.render(completed++); @@ -20,4 +20,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/colorProgression.ts b/examples/colorProgression.ts index ae6feb5..2540fb0 100644 --- a/examples/colorProgression.ts +++ b/examples/colorProgression.ts @@ -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 @@ -21,4 +21,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/complete.ts b/examples/complete.ts index a43cefa..645b9da 100644 --- a/examples/complete.ts +++ b/examples/complete.ts @@ -13,7 +13,7 @@ const progress = new ProgressBar({ let completed = 0; -async function run() { +async function download() { while (completed <= total) { progress.render(completed++); @@ -21,4 +21,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/console.ts b/examples/console.ts index 08fbea1..b615d0b 100644 --- a/examples/console.ts +++ b/examples/console.ts @@ -11,7 +11,7 @@ const progress = new ProgressBar({ let completed = 0; -async function downloading() { +async function download() { while (completed <= total) { await progress.render(completed++); @@ -23,4 +23,4 @@ async function downloading() { } } -await downloading(); +await download(); diff --git a/examples/display.ts b/examples/display.ts index 8dc9445..38a93d4 100644 --- a/examples/display.ts +++ b/examples/display.ts @@ -18,7 +18,7 @@ const progress = new ProgressBar({ let completed = 0; -async function run() { +async function download() { while (completed <= total) { await progress.render(completed++); @@ -26,4 +26,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/eta.ts b/examples/eta.ts index 853055e..4e5d68f 100644 --- a/examples/eta.ts +++ b/examples/eta.ts @@ -14,7 +14,7 @@ const progress = new ProgressBar({ let completed = 0; -async function run() { +async function download() { while (completed <= total) { await progress.render(completed++); @@ -22,4 +22,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/info.ts b/examples/info.ts index 92baf9a..02b0d78 100644 --- a/examples/info.ts +++ b/examples/info.ts @@ -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 + "" : "", @@ -32,4 +32,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/multi.ts b/examples/multi.ts index d2301f0..166a14c 100644 --- a/examples/multi.ts +++ b/examples/multi.ts @@ -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; @@ -34,4 +34,4 @@ async function downloading() { } } -await downloading(); +await download(); diff --git a/examples/multiPrettyTime.ts b/examples/multiPrettyTime.ts index 472c811..5f6d681 100644 --- a/examples/multiPrettyTime.ts +++ b/examples/multiPrettyTime.ts @@ -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; @@ -49,4 +49,4 @@ async function downloading() { } } -await downloading(); +await download(); diff --git a/examples/preciseBar.ts b/examples/preciseBar.ts index d1810ae..c223d22 100644 --- a/examples/preciseBar.ts +++ b/examples/preciseBar.ts @@ -21,7 +21,7 @@ const progress = new ProgressBar({ let completed = 0; -async function downloading() { +async function download() { while (completed <= total) { await progress.render(completed++); @@ -29,4 +29,4 @@ async function downloading() { } } -await downloading(); +await download(); diff --git a/examples/prettyTime.ts b/examples/prettyTime.ts index fe1c3f6..afa1e98 100644 --- a/examples/prettyTime.ts +++ b/examples/prettyTime.ts @@ -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: { @@ -29,4 +29,4 @@ async function run() { } } -await run(); +await download(); diff --git a/examples/title.ts b/examples/title.ts index 0cffcca..4381f6b 100644 --- a/examples/title.ts +++ b/examples/title.ts @@ -13,7 +13,7 @@ const progress = new ProgressBar({ let completed = 0; -async function downloading() { +async function download() { while (completed <= total) { await progress.render(completed++); @@ -21,4 +21,4 @@ async function downloading() { } } -await downloading(); +await download(); diff --git a/examples/total.ts b/examples/total.ts index a466dd1..6f577f8 100644 --- a/examples/total.ts +++ b/examples/total.ts @@ -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 @@ -23,4 +23,4 @@ async function downloading() { } } -await downloading(); +await download(); diff --git a/examples/width.ts b/examples/width.ts index 52eec22..34b4cc9 100644 --- a/examples/width.ts +++ b/examples/width.ts @@ -15,7 +15,7 @@ const progress = new ProgressBar({ let completed = 0; -async function downloading() { +async function download() { while (completed <= total) { await progress.render(completed++); @@ -23,4 +23,4 @@ async function downloading() { } } -await downloading(); +await download(); diff --git a/tests/mod.test.ts b/tests/mod.test.ts index c831adf..36440b2 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -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 }); diff --git a/tests/multi.test.ts b/tests/multi.test.ts index 5bffd1a..64e5e8e 100644 --- a/tests/multi.test.ts +++ b/tests/multi.test.ts @@ -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"; diff --git a/tests/time.test.ts b/tests/time.test.ts index ea000d3..db580a6 100644 --- a/tests/time.test.ts +++ b/tests/time.test.ts @@ -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");