prettyUri method
Returns a terse, human-readable representation of uri
.
uri
can be a String or a Uri. If it can be made relative to the
current working directory, that's done. Otherwise, it's returned as-is.
This gracefully handles non-file:
URIs for Style.posix and
Style.windows.
The returned value is meant for human consumption, and may be either URI- or path-formatted.
// POSIX
var context = new Context(current: '/root/path');
context.prettyUri('file:///root/path/a/b.dart'); // -> 'a/b.dart'
context.prettyUri('http://dartlang.org/'); // -> 'http://dartlang.org'
// Windows
var context = new Context(current: r'C:\root\path');
context.prettyUri('file:///C:/root/path/a/b.dart'); // -> r'a\b.dart'
context.prettyUri('http://dartlang.org/'); // -> 'http://dartlang.org'
// URL
var context = new Context(current: 'http://dartlang.org/root/path');
context.prettyUri('http://dartlang.org/root/path/a/b.dart');
// -> r'a/b.dart'
context.prettyUri('file:///root/path'); // -> 'file:///root/path'
Implementation
String prettyUri(uri) {
var typedUri = _parseUri(uri);
if (typedUri.scheme == 'file' && style == Style.url) {
return typedUri.toString();
} else if (typedUri.scheme != 'file' &&
typedUri.scheme != '' &&
style != Style.url) {
return typedUri.toString();
}
var path = normalize(fromUri(typedUri));
var rel = relative(path);
// Only return a relative path if it's actually shorter than the absolute
// path. This avoids ugly things like long "../" chains to get to the root
// and then go back down.
return split(rel).length > split(path).length ? path : rel;
}