cat function Null safety

Future<int> cat(
  1. List<String> paths,
  2. {String appName = '',
  3. List<String>? log,
  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 stdout

The parameters are similar to the GNU cat utility. Specify a log for debugging or testing purpose.

Implementation

Future<int> cat(List<String> paths,
    {String appName = '',
    List<String>? log,
    bool showEnds = false,
    bool numberNonBlank = false,
    bool showLineNumbers = false,
    bool showTabs = false,
    bool squeezeBlank = false,
    bool showNonPrinting = false}) async {
  var lineNumber = 1;
  var returnCode = 0;
  log?.clear();
  if (paths.isEmpty) {
    final lines = await _readStdin();
    await _writeLines(lines, lineNumber, log, showEnds, showLineNumbers,
        numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
  } else {
    for (final path in paths) {
      try {
        final Stream<String> lines;
        if (path == '-') {
          lines = await _readStdin();
        } else {
          lines = utf8.decoder
              .bind(File(path).openRead())
              .transform(const LineSplitter());
        }
        lineNumber = await _writeLines(
            lines,
            lineNumber,
            log,
            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;
        }
        returnCode = await printError(message, appName: appName, path: path);
      } on FormatException {
        returnCode = await printError('Binary file not supported.',
            appName: appName, path: path);
      } catch (e) {
        returnCode =
            await printError(e.toString(), appName: appName, path: path);
      }
    }
  }
  return returnCode;
}