From 8302eb35405230120b53b6ec1bce2a3876963666 Mon Sep 17 00:00:00 2001 From: Vandad Nahavandipoor Date: Mon, 11 Oct 2021 17:29:08 +0200 Subject: [PATCH] Create dart-progress-for-futuret.dart --- source/dart-progress-for-futuret.dart | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 source/dart-progress-for-futuret.dart diff --git a/source/dart-progress-for-futuret.dart b/source/dart-progress-for-futuret.dart new file mode 100644 index 00000000..3b118749 --- /dev/null +++ b/source/dart-progress-for-futuret.dart @@ -0,0 +1,64 @@ +// Want to support my work 🤝? https://buymeacoffee.com/vandad + +import 'dart:io' show stdout; +import 'dart:async' show Future, Stream; + +const loadingSequence = ['⢿', '⣻', '⣽', '⣾', '⣷', '⣯', '⣟', '⡿']; +const escape = '\x1B[38;5;'; +const color = '${escape}1m'; +const textColor = '${escape}6m'; + +String progress({required int value, required String text}) { + final progress = '$color${loadingSequence[value % loadingSequence.length]}'; + final coloredText = '$textColor$text'; + return '$progress\t$coloredText'; +} + +Future performWithProgress({ + required Future task, + required String progressText, +}) { + final stream = Stream.periodic( + Duration(milliseconds: 100), + (value) => progress( + value: value, + text: progressText, + ), + ); + + final subscription = stream.listen( + (event) { + stdout.write('\r $event'); + }, + ); + + task.whenComplete(() { + stdout.write('\r ✅\t$progressText'); + stdout.write('\n'); + subscription.cancel(); + }); + + return task; +} + +final task1 = Future.delayed(Duration(seconds: 1), () => 'Result 1'); +final task2 = Future.delayed(Duration(seconds: 2), () => 'Result 2'); +final task3 = Future.delayed(Duration(seconds: 3), () => 'Result 3'); + +void main(List args) async { + var result = await performWithProgress( + task: task1, + progressText: 'Loading task 1', + ); + print('\tTask 1 result: $result'); + result = await performWithProgress( + task: task2, + progressText: 'Loading task 2', + ); + print('\tTask 2 result: $result'); + result = await performWithProgress( + task: task3, + progressText: 'Loading task 3', + ); + print('\tTask 3 result: $result'); +}