Skip to content

Commit 7ff0723

Browse files
authored
running-apps: update running-apps to use Duration.ago() (#182172)
Cleanup from #180098 #182155
1 parent fbfe04e commit 7ff0723

2 files changed

Lines changed: 10 additions & 60 deletions

File tree

packages/flutter_tools/lib/src/commands/running_apps.dart

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ class RunningAppsCommand extends FlutterCommand {
107107
final String deviceId = app.deviceId;
108108
final String platform = app.targetPlatform;
109109
final String vmServiceUri = app.wsUri;
110-
final String age = processAge(app.epoch, _systemClock);
110+
final String age = _systemClock
111+
.now()
112+
.difference(DateTime.fromMillisecondsSinceEpoch(app.epoch))
113+
.ago();
111114

112115
// If the device name and ID are effectively the same (e.g., "macos" and "macos"),
113116
// only show the name to avoid redundancy like "macos (macos)".
@@ -151,24 +154,3 @@ class RunningAppsCommand extends FlutterCommand {
151154
}
152155
}
153156
}
154-
155-
/// Formats the elapsed time since the given epoch.
156-
@visibleForTesting
157-
String processAge(int? epoch, SystemClock systemClock) {
158-
// TODO(jwren): Consider using [DurationAgo] from `lib/src/base/utils.dart`.
159-
// We need to decide on the width and precision, possibly modifying the utility
160-
// to support a shorter form (e.g. "5m" versus "5 minutes ago").
161-
if (epoch == null) {
162-
return 'unknown age';
163-
}
164-
final Duration elapsed = systemClock.now().difference(DateTime.fromMillisecondsSinceEpoch(epoch));
165-
if (elapsed.inDays > 0) {
166-
return '${elapsed.inDays}d';
167-
} else if (elapsed.inHours > 0) {
168-
return '${elapsed.inHours}h';
169-
} else if (elapsed.inMinutes > 0) {
170-
return '${elapsed.inMinutes}m';
171-
} else {
172-
return '${elapsed.inSeconds}s';
173-
}
174-
}

packages/flutter_tools/test/commands.shard/hermetic/running_apps_test.dart

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:flutter_tools/src/commands/running_apps.dart';
1010
import 'package:multicast_dns/multicast_dns.dart';
1111
import 'package:test/fake.dart';
1212

13-
import '../../src/common.dart';
1413
import '../../src/context.dart';
1514
import '../../src/test_flutter_command_runner.dart';
1615

@@ -194,11 +193,15 @@ dart_version=2.0.0''',
194193
// Verify formatting - 2 spaces indent, bullet separator
195194
expect(
196195
testLogger.statusText,
197-
contains(' app_one (debug) • macos • darwin-arm64 • ws://127.0.0.1:1234/ws • 1s'),
196+
contains(
197+
' app_one (debug) • macos • darwin-arm64 • ws://127.0.0.1:1234/ws • 0 minutes ago',
198+
),
198199
);
199200
expect(
200201
testLogger.statusText,
201-
contains(' app_two (release) • chrome • web-javascript • ws://127.0.0.1:5678/ws • 2s'),
202+
contains(
203+
' app_two (release) • chrome • web-javascript • ws://127.0.0.1:5678/ws • 0 minutes ago',
204+
),
202205
);
203206
});
204207

@@ -330,41 +333,6 @@ dart_version=2.0.0''',
330333
expect('app_one'.allMatches(testLogger.statusText), hasLength(1));
331334
expect(testLogger.statusText, contains('ws://127.0.0.1:1234/ws'));
332335
});
333-
334-
testWithoutContext('processAge', () {
335-
const kSecondMs = 1000;
336-
const int kMinuteMs = kSecondMs * 60;
337-
const int kHourMs = kMinuteMs * 60;
338-
const int kDayMs = kHourMs * 24;
339-
340-
const kNowMs = 10_000_000;
341-
const int kFiveSecondsMs = 5 * kSecondMs;
342-
const int kFiftyNineSecondsMs = 59 * kSecondMs;
343-
const int kFiftyNineMinutesMs = 59 * kMinuteMs;
344-
const int kTwentyThreeHoursMs = 23 * kHourMs;
345-
const int kTwoDaysMs = 2 * kDayMs;
346-
347-
final now = DateTime.fromMillisecondsSinceEpoch(kNowMs);
348-
final clock = SystemClock.fixed(now);
349-
350-
expect(processAge(null, clock), 'unknown age');
351-
352-
// Seconds
353-
expect(processAge(kNowMs - kFiveSecondsMs, clock), '5s');
354-
expect(processAge(kNowMs - kFiftyNineSecondsMs, clock), '59s');
355-
356-
// Minutes
357-
expect(processAge(kNowMs - kMinuteMs, clock), '1m');
358-
expect(processAge(kNowMs - kFiftyNineMinutesMs, clock), '59m');
359-
360-
// Hours
361-
expect(processAge(kNowMs - kHourMs, clock), '1h');
362-
expect(processAge(kNowMs - kTwentyThreeHoursMs, clock), '23h');
363-
364-
// Days
365-
expect(processAge(kNowMs - kDayMs, clock), '1d');
366-
expect(processAge(kNowMs - kTwoDaysMs, clock), '2d');
367-
});
368336
}
369337

370338
class FakeMDnsClient extends Fake implements MDnsClient {

0 commit comments

Comments
 (0)