Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger): add link #517

Merged
merged 8 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/mason_logger/example/main.dart
Expand Up @@ -46,5 +46,10 @@ Future<void> main() async {
final canceling = logger.progress('Trying to cancel now!');
await Future<void>.delayed(const Duration(seconds: 1));
canceling.cancel();
logger.info('Done!');

final repoLink = link(
message: 'GitHub Repository',
uri: Uri.parse('https://github.com/felangel/mason'),
);
logger.info('To learn more, visit the $repoLink.');
}
1 change: 1 addition & 0 deletions packages/mason_logger/lib/mason_logger.dart
Expand Up @@ -2,4 +2,5 @@ library mason_logger;

export 'src/io.dart';
export 'src/level.dart';
export 'src/link.dart';
export 'src/mason_logger.dart' show Logger, Progress;
17 changes: 17 additions & 0 deletions packages/mason_logger/lib/src/link.dart
@@ -0,0 +1,17 @@
/// Wraps [uri] with an escape sequence so it's recognized as a hyperlink.
/// An optional message can be used in place of the [uri].
/// If no [message] is provided, the text content will be the full [uri].
///
/// ```dart
/// final plainLink = link(uri: Uri.parse('https://dart.dev'));
/// print(plainLink); // Equivalent to `[https://dart.dev](https://dart.dev)` in markdown
///
/// final richLink = link(uri: Uri.parse('https://dart.dev'), message: 'The Dart Website');
/// print(richLink); // Equivalent to `[The Dart Website](https://dart.dev)` in markdown
/// ```
String link({required Uri uri, String? message}) {
const leading = '\x1B]8;;';
const trailing = '\x1B\\';

return '$leading$uri$trailing${message ?? uri}$leading$trailing';
}
32 changes: 32 additions & 0 deletions packages/mason_logger/test/src/link_test.dart
@@ -0,0 +1,32 @@
import 'package:mason_logger/mason_logger.dart';
import 'package:test/test.dart';

void main() {
group('link', () {
final uri = Uri.parse('https://github.com/felangel/mason/issues/');
const lead = '\x1B]8;;';
const trail = '\x1B\\';

test(
'builds output with correct encodings: ' r'\x1B]8;;' ' and ' r'\x1B\\',
() {
const message = 'message';
final output = link(message: message, uri: uri);
final matcher = stringContainsInOrder(
[lead, '$uri', trail, message, lead, trail],
);

expect(output, matcher);
},
);

test('builds String with Uri when message is null: ', () {
final output = link(uri: uri);
final matcher = stringContainsInOrder(
[lead, '$uri', trail, '$uri', lead, trail],
);

expect(output, matcher);
});
});
}