Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Unified Diff: pkg/path/lib/src/context.dart

Issue 296233002: Add a path.formatUri method and release path 1.2.0. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/path/lib/path.dart ('k') | pkg/path/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/path/lib/src/context.dart
diff --git a/pkg/path/lib/src/context.dart b/pkg/path/lib/src/context.dart
index 9c8e1e46465b489ccfa7c13d6610d80c0562b489..88ca61fa5eee43d2478fd5f68616c3261d55fd0f 100644
--- a/pkg/path/lib/src/context.dart
+++ b/pkg/path/lib/src/context.dart
@@ -472,6 +472,48 @@ class Context {
}
}
+ /// 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'
+ String prettyUri(uri) {
+ if (uri is String) uri = Uri.parse(uri);
+ if (uri.scheme == 'file' && style == Style.url) return uri.toString();
+ if (uri.scheme != 'file' && uri.scheme != '' && style != Style.url) {
+ return uri.toString();
+ }
+
+ var path = normalize(fromUri(uri));
+ var rel = relative(path);
+ var components = split(rel);
+
+ // 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;
+ }
+
ParsedPath _parse(String path) => new ParsedPath.parse(path, style);
}
« no previous file with comments | « pkg/path/lib/path.dart ('k') | pkg/path/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698