Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Created October 31, 2019 23:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slightfoot/5870fbb15d24ab37e761f4fffbea78dd to your computer and use it in GitHub Desktop.
Save slightfoot/5870fbb15d24ab37e761f4fffbea78dd to your computer and use it in GitHub Desktop.
Little example of creating a coloured terminal progress bar in Dart.
import 'dart:io' show stdout;
Future<void> main() async {
print('\n\x1b[38;5;39mBUILDING CODE\x1b[0m');
for (int j = 0; j < 3; j++) {
for (int i = 0; i <= 100; i++) {
drawProgressBar(i / 100.0, 20);
await Future.delayed(const Duration(milliseconds: 25));
}
}
stdout.write('\n');
}
void drawProgressBar(double amount, int size) {
final limit = (size * amount).toInt();
stdout.write(
'\r\x1b[38;5;75;51m' +
String.fromCharCodes(List.generate(size, (int index) {
if (index < limit) {
return 0x2593;
}
return 0x2591;
})) +
'\x1b[0m',
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment