Removed exitCode parameter from CatResult.addMessage().
Renamed _printError to _getErrorMessage.
This commit is contained in:
parent
3603d873b2
commit
0ad6ff7d10
1 changed files with 24 additions and 24 deletions
|
@ -32,8 +32,8 @@ class CatResult {
|
||||||
bool get isSuccess => exitCode == exitSuccess;
|
bool get isSuccess => exitCode == exitSuccess;
|
||||||
|
|
||||||
/// Add a message with an optional path.
|
/// Add a message with an optional path.
|
||||||
void addMessage(int exitCode, String message, {String? path}) {
|
void addMessage(String message, {String? path}) {
|
||||||
this.exitCode = exitCode;
|
exitCode = exitFailure;
|
||||||
if (path != null && path.isNotEmpty) {
|
if (path != null && path.isNotEmpty) {
|
||||||
messages.add('$path: $message');
|
messages.add('$path: $message');
|
||||||
} else {
|
} else {
|
||||||
|
@ -68,10 +68,10 @@ Future<CatResult> cat(List<String> paths, IOSink output,
|
||||||
if (paths.isEmpty) {
|
if (paths.isEmpty) {
|
||||||
if (input != null) {
|
if (input != null) {
|
||||||
try {
|
try {
|
||||||
await _writeStream(input, lastLine, output, numberNonBlank, showEnds,
|
await _copyStream(input, lastLine, output, numberNonBlank, showEnds,
|
||||||
showLineNumbers, showNonPrinting, showTabs, squeezeBlank);
|
showLineNumbers, showNonPrinting, showTabs, squeezeBlank);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
result.addMessage(exitFailure, _formatError(e));
|
result.addMessage(_getErrorMessage(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -83,34 +83,18 @@ Future<CatResult> cat(List<String> paths, IOSink output,
|
||||||
} else {
|
} else {
|
||||||
stream = File(path).openRead();
|
stream = File(path).openRead();
|
||||||
}
|
}
|
||||||
await _writeStream(stream, lastLine, output, numberNonBlank, showEnds,
|
await _copyStream(stream, lastLine, output, numberNonBlank, showEnds,
|
||||||
showLineNumbers, showNonPrinting, showTabs, squeezeBlank);
|
showLineNumbers, showNonPrinting, showTabs, squeezeBlank);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
result.addMessage(exitFailure, _formatError(e), path: path);
|
result.addMessage(_getErrorMessage(e), path: path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formats error message.
|
// Copies (and formats) a stream to an IO sink.
|
||||||
String _formatError(Object e) {
|
Future<void> _copyStream(
|
||||||
final String message;
|
|
||||||
if (e is FileSystemException) {
|
|
||||||
final String? osMessage = e.osError?.message;
|
|
||||||
if (osMessage != null && osMessage.isNotEmpty) {
|
|
||||||
message = osMessage;
|
|
||||||
} else {
|
|
||||||
message = e.message;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
message = '$e';
|
|
||||||
}
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Writes parsed data from a stream
|
|
||||||
Future<void> _writeStream(
|
|
||||||
Stream<List<int>> stream,
|
Stream<List<int>> stream,
|
||||||
_LastLine lastLine,
|
_LastLine lastLine,
|
||||||
IOSink out,
|
IOSink out,
|
||||||
|
@ -217,3 +201,19 @@ Future<void> _writeStream(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the message (platform specific, if available) for an exception.
|
||||||
|
String _getErrorMessage(Object e) {
|
||||||
|
final String message;
|
||||||
|
if (e is FileSystemException) {
|
||||||
|
final String? osMessage = e.osError?.message;
|
||||||
|
if (osMessage != null && osMessage.isNotEmpty) {
|
||||||
|
message = osMessage;
|
||||||
|
} else {
|
||||||
|
message = e.message;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
message = '$e';
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue