Skip to content

Commit

Permalink
feat(mason_logger): add link (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luckey-Elijah committed Sep 16, 2022
1 parent 978aa42 commit 38a525b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
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);
});
});
}

0 comments on commit 38a525b

Please sign in to comment.