cat function

Future<CatResult> cat(
  1. List<String> paths,
  2. IOSink output, {
  3. Stream<List<int>>? input,
  4. bool numberNonBlank = false,
  5. bool showEnds = false,
  6. bool showLineNumbers = false,
  7. bool showNonPrinting = false,
  8. bool showTabs = false,
  9. bool squeezeBlank = 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 numberNonBlank = false,
    bool showEnds = false,
    bool showLineNumbers = false,
    bool showNonPrinting = false,
    bool showTabs = false,
    bool squeezeBlank = false}) async {
  final result = CatResult();
  final lastLine = _LastLine();

  if (paths.isEmpty) {
    if (input != null) {
      try {
        await _copyStream(input, lastLine, output, numberNonBlank, showEnds,
            showLineNumbers, showNonPrinting, showTabs, squeezeBlank);
      } catch (e) {
        result.addError(_getErrorMessage(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 _copyStream(stream, lastLine, output, numberNonBlank, showEnds,
            showLineNumbers, showNonPrinting, showTabs, squeezeBlank);
      } catch (e) {
        result.addError(_getErrorMessage(e), path: path);
      }
    }
  }
  return result;
}