From 293a29c7a256b4a14a94edd28b66b697048d83ff Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 16 Oct 2021 14:59:09 -0700 Subject: [PATCH] Cleaned up errors reporting. --- README.md | 2 +- bin/dcat.dart | 18 +++++++++------- doc/api/dcat/cat.html | 19 ++++------------ doc/api/index.html | 2 +- lib/dcat.dart | 50 ++++++++++++++++++++++++------------------- 5 files changed, 44 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 9fa113c..9bfb5dd 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ squeezeBlank | Same as `-s` | bool showNonPrinting | Same as `-v` | bool * `paths` and `output` are required. -* `output` should be an [IOSink](https://api.dart.dev/dart-io/IOSink-class.html) like `stdout` or a [File](https://api.dart.dev/dart-io/File/openWrite.html) stream. +* `output` should be an [IOSink](https://api.dart.dev/dart-io/IOSink-class.html) such as `stdout` or a [File](https://api.dart.dev/dart-io/File/openWrite.html) stream. * `input` can be [stdin](https://api.dart.dev/dart-io/Stdin-class.html). The remaining optional parameters are similar to the [GNU cat](https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html#cat-invocation) utility. diff --git a/bin/dcat.dart b/bin/dcat.dart index 7820519..2c939db 100644 --- a/bin/dcat.dart +++ b/bin/dcat.dart @@ -22,6 +22,8 @@ const showTabsFlag = 'show-tabs'; const squeezeBlank = 'squeeze-blank'; const versionFlag = 'version'; +const _homePage = 'https://github.com/ethauvin/dcat'; + /// Concatenates files specified in [arguments]. /// /// Usage: `dcat [option] [file]…` @@ -33,8 +35,9 @@ Future main(List arguments) async { try { argResults = parser.parse(arguments); } on FormatException catch (e) { - return printError( + await printError( "${e.message}\nTry '$appName --$helpFlag' for more information."); + return exitFailure; } if (argResults[helpFlag]) { @@ -121,20 +124,19 @@ Future setupArgsParser() async { return parser; } -/// Prints the error [message] to [stderr]. -Future printError(String message) async { +/// Prints an error [message] to [stderr]. +Future printError(String message) async { stderr.writeln("$appName: $message"); - return exitFailure; } /// Prints the version info. Future printVersion() async { print('''$appName (Dart cat) $appVersion Copyright (C) 2021 Erik C. Thauvin -License: 3-Clause BSD +License 3-Clause BSD: -Inspired by -Written by Erik C. Thauvin '''); +Written by Erik C. Thauvin +Source: $_homePage'''); return exitSuccess; } @@ -150,6 +152,6 @@ Examples: $appName f - g Output f's contents, then standard input, then g's contents. $appName Copy standard input to standard output. -Source and documentation: '''); +Source and documentation: <$_homePage>'''); return exitSuccess; } diff --git a/doc/api/dcat/cat.html b/doc/api/dcat/cat.html index 45e1714..163c36e 100644 --- a/doc/api/dcat/cat.html +++ b/doc/api/dcat/cat.html @@ -88,14 +88,15 @@ bool squeezeBlank = false, bool showNonPrinting = false}) async { final result = CatResult(); - final lastLine = _LastLine(0, _lineFeed); + final lastLine = _LastLine(); + if (paths.isEmpty) { if (input != null) { try { await _writeStream(input, lastLine, output, showEnds, showLineNumbers, numberNonBlank, showTabs, squeezeBlank, showNonPrinting); } catch (e) { - result.addMessage(exitFailure, '$e'); + result.addMessage(exitFailure, _formatError(e)); } } } else { @@ -109,20 +110,8 @@ } 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); + result.addMessage(exitFailure, _formatError(e), path: path); } } } diff --git a/doc/api/index.html b/doc/api/index.html index 317b307..9de1c2a 100644 --- a/doc/api/index.html +++ b/doc/api/index.html @@ -97,7 +97,7 @@ if (result.isFailure) {
ParameterDescriptionType
pathsThe file paths.String[]
outputThe standard output or file.IOSink
inputThe standard input.Stream
showEndsSame as -ebool
numberNonBlankSame as -bbool
showLineNumbersSame as -nbool
showTabsSame as -Tbool
squeezeBlankSame as -sbool
showNonPrintingSame as -vbool
  • paths and output are required.
  • -
  • output should be an IOSink like stdout or a File stream.
  • +
  • output should be an IOSink such as stdout or a File stream.
  • input can be stdin.

The remaining optional parameters are similar to the GNU cat utility.

diff --git a/lib/dcat.dart b/lib/dcat.dart index 8580b4d..1209711 100644 --- a/lib/dcat.dart +++ b/lib/dcat.dart @@ -45,10 +45,8 @@ class CatResult { // Holds the current line number and last character. class _LastLine { - int lineNumber; - int lastChar; - - _LastLine(this.lineNumber, this.lastChar); + int lineNumber = 0; + int lastChar = _lineFeed; } /// Concatenates files in [paths] to the standard output or a file. @@ -66,14 +64,15 @@ Future cat(List paths, IOSink output, bool squeezeBlank = false, bool showNonPrinting = false}) async { final result = CatResult(); - final lastLine = _LastLine(0, _lineFeed); + final lastLine = _LastLine(); + if (paths.isEmpty) { if (input != null) { try { await _writeStream(input, lastLine, output, showEnds, showLineNumbers, numberNonBlank, showTabs, squeezeBlank, showNonPrinting); } catch (e) { - result.addMessage(exitFailure, '$e'); + result.addMessage(exitFailure, _formatError(e)); } } } else { @@ -87,26 +86,32 @@ Future cat(List paths, IOSink output, } 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); + result.addMessage(exitFailure, _formatError(e), path: path); } } } return result; } +// Formats error message. +String _formatError(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 if (e is FormatException) { + message = 'Binary file not supported.'; + } else { + message = '$e'; + } + return message; +} + // Writes parsed data from a stream Future _writeStream( Stream stream, @@ -118,9 +123,8 @@ Future _writeStream( bool showTabs, bool squeezeBlank, bool showNonPrinting) async { - const tab = 9; - int squeeze = 0; - final noFlags = !showEnds && + // No flags + if (!showEnds && !showLineNumbers && !numberNonBlank && !showTabs && @@ -162,6 +166,7 @@ Future _writeStream( } } else if (ch == tab) { if (showTabs) { + // TAB sb.write('^I'); continue; } @@ -183,6 +188,7 @@ Future _writeStream( continue; } } else { + // CTRL sb ..write('^') ..writeCharCode(ch + 64);