cat function Null safety

Future<CatResult> cat(
  1. List<String> paths,
  2. IOSink output,
  3. {Stream<List<int>>? input,
  4. bool showEnds = false,
  5. bool numberNonBlank = false,
  6. bool showLineNumbers = false,
  7. bool showTabs = false,
  8. bool squeezeBlank = false,
  9. bool showNonPrinting = false}
)

Concatenates files in paths to the standard output or a file.

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;
}