cat function Null safety
Concatenates files in paths
to the standard output or a file.
output
should be an IOSink such as stdout or File.openWrite.input
can be stdin.
The remaining optional parameters are similar to the GNU cat utility.
Implementation
Future<CatResult> cat(List<String> paths, IOSink output,
{Stream<List<int>>? input,
bool showEnds = false,
bool numberNonBlank = false,
bool showLineNumbers = false,
bool showTabs = false,
bool squeezeBlank = false,
bool showNonPrinting = false}) async {
final result = CatResult();
final lastLine = _LastLine(0, _lineFeed);
if (paths.isEmpty) {
if (input != null) {
try {
await _writeStream(input, lastLine, output, showEnds, showLineNumbers,
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
} catch (e) {
result.addMessage(exitFailure, '$e');
}
}
} else {
for (final path in paths) {
try {
final Stream<List<int>> stream;
if (path == '-' && input != null) {
stream = input;
} else {
stream = File(path).openRead();
}
await _writeStream(stream, lastLine, output, showEnds, showLineNumbers,
numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
} on FileSystemException catch (e) {
final String? osMessage = e.osError?.message;
final String message;
if (osMessage != null && osMessage.isNotEmpty) {
message = osMessage;
} else {
message = e.message;
}
result.addMessage(exitFailure, message, path: path);
} on FormatException {
result.addMessage(exitFailure, 'Binary file not supported.',
path: path);
} catch (e) {
result.addMessage(exitFailure, '$e', path: path);
}
}
}
return result;
}