Skip to content

Commit

Permalink
Handle failure of parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Feb 29, 2024
1 parent 7261e85 commit 09afb07
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions lib/src/utils/parsers/git_clone_update_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,42 @@ String lastMatchedEntry = '';
final maxLabelLength =
regexes.keys.map((e) => e.length).reduce((a, b) => a > b ? a : b);

var _hasFailedPrint = false;

void updateProgress(String line) {
final matchedEntry = regexes.entries.firstWhereOrNull(
(entry) => line.contains(entry.key),
);

if (matchedEntry != null) {
final label = matchedEntry.key.padRight(maxLabelLength);
final match = matchedEntry.value.firstMatch(line);
final percentVaue = match?.group(1);
int? percentage = int.tryParse(percentVaue ?? '');

if (percentage != lastPercentage) {
if (percentage == null) return;

if (lastMatchedEntry.isNotEmpty && lastMatchedEntry != label) {
printProgressBar(lastMatchedEntry, 100);
logger.write('\n');
}
if (_hasFailedPrint) {
logger.info('\n');

return;
}
try {
final matchedEntry = regexes.entries.firstWhereOrNull(
(entry) => line.contains(entry.key),
);

printProgressBar(label, percentage);
if (matchedEntry != null) {
final label = matchedEntry.key.padRight(maxLabelLength);
final match = matchedEntry.value.firstMatch(line);
final percentVaue = match?.group(1);
int? percentage = int.tryParse(percentVaue ?? '');

lastPercentage = percentage;
lastMatchedEntry = label;
if (percentage != lastPercentage) {
if (percentage == null) return;

if (lastMatchedEntry.isNotEmpty && lastMatchedEntry != label) {
printProgressBar(lastMatchedEntry, 100);
logger.write('\n');
}

printProgressBar(label, percentage);

lastPercentage = percentage;
lastMatchedEntry = label;
}
}
} catch (e) {
logger.detail('Failed to update progress bar $e');
_hasFailedPrint = true;
}
}

Expand All @@ -60,7 +72,7 @@ void printProgressBar(String label, int percentage) {

// Create a custom Process.start, that prints using the progress bar
Future<void> runGitCloneUpdate(List<String> args) async {
final process = await Process.start('git', args);
final process = await Process.start('git', args, runInShell: true);

// ignore: avoid-unassigned-stream-subscriptions
process.stderr.transform(utf8.decoder).listen((line) {
Expand Down

0 comments on commit 09afb07

Please sign in to comment.