diff --git a/README.md b/README.md index fa9a18c..0b2802b 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,9 @@ dart compile exe bin/dcat.dart ```dart import 'package:dcat/dcat.dart'; -final result = await cat(['path/to/file', 'path/to/otherfile]'], File('path/to/outfile')); -if (result.exitCode == exitFailure) { +final result = await cat(['path/to/file', 'path/to/otherfile]'], + File('path/to/outfile').openWrite()); +if (result.isFailure) { for (final message in result.messages) { print("Error: $message"); } @@ -66,9 +67,8 @@ The `cat` function supports the following parameters: Parameter | Description | Type :--------------- |:----------------------------- | :------------------- paths | The file paths. | String[] -output | The standard output or file. | IOSink or File +output | The standard output or file. | [IOSink](https://api.dart.dev/dart-io/IOSink-class.html) input | The standard input. | Stream\>? -log | The log for debugging. | List\? showEnds | Same as `-e` | bool numberNonBlank | Same as `-b` | bool showLineNumbers | Same as `-n` | bool @@ -77,9 +77,8 @@ squeezeBlank | Same as `-s` | bool showNonPrinting | Same as `-v` | bool * `paths` and `output` are required. -* `output` should be an `IOSink` like `stdout` or a `File`. -* `input` can be `stdin`. -* `log` is used for debugging/testing purposes. +* `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. +* `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. @@ -89,11 +88,13 @@ A `CatResult` object is returned which contains the `exitCode` (`exitSuccess` or final result = await cat(['path/to/file'], stdout); if (result.exitCode == exitSuccess) { ... +} else { + for (final message in result.messages) { + stderr.writeln("Error: $message"); + } } ``` ## Differences from [GNU cat](https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html#cat-invocation) - No binary file support. - - A line is considered terminated by either a `CR` (carriage return), a `LF` (line feed), a `CR+LF` sequence (DOS line ending). - - A line ending is automatically appended to the last line of any read file. - - The `U+` notation is used instead of `M-` for non-printing characters. \ No newline at end of file + - The [U+](https://en.wikipedia.org/wiki/Unicode) notation is used instead of `M-` for non-printing characters. \ No newline at end of file diff --git a/doc/api/dcat/CatResult-class.html b/doc/api/dcat/CatResult-class.html index 0b9dff6..3afea57 100644 --- a/doc/api/dcat/CatResult-class.html +++ b/doc/api/dcat/CatResult-class.html @@ -75,7 +75,7 @@
exitCode - int + int
@@ -85,19 +85,41 @@
- hashCode - int + hashCode + int
- The hash code for this object. [...] + The hash code for this object. [...]
read-only, inherited
+
+ +
+ isFailure + bool + +
+
+ Returns true if the exitCode is exitFailure. +
read-only
+ +
+ +
+ isSuccess + bool + +
+
+ Returns true if the exitCode is exitSuccess. +
read-only
+
messages - List<String> + List<String>
@@ -107,8 +129,8 @@
- runtimeType - Type + runtimeType + Type
@@ -124,40 +146,40 @@

Methods

- addMessage(int exitCode, String message, {String? path}) + addMessage(int exitCode, String message, {String? path}) → void
- Add a message. + Add a message with an optional path.
- noSuchMethod(Invocation invocation) + noSuchMethod(Invocation invocation) → dynamic
- Invoked when a non-existent method or property is accessed. [...] + Invoked when a non-existent method or property is accessed. [...]
inherited
- toString() - String + toString() + String
- A string representation of this object. [...] + A string representation of this object. [...]
inherited
@@ -169,14 +191,14 @@

Operators

- operator ==(Object other) - bool + operator ==(Object other) + bool
- The equality operator. [...] + The equality operator. [...]
inherited
@@ -235,17 +257,19 @@ Properties
  • exitCode
  • -
  • hashCode
  • +
  • hashCode
  • +
  • isFailure
  • +
  • isSuccess
  • messages
  • -
  • runtimeType
  • +
  • runtimeType
  • Methods
  • addMessage
  • -
  • noSuchMethod
  • -
  • toString
  • +
  • noSuchMethod
  • +
  • toString
  • Operators
  • -
  • operator ==
  • +
  • operator ==
  • diff --git a/doc/api/dcat/CatResult/CatResult.html b/doc/api/dcat/CatResult/CatResult.html index 00dc26c..30b1bcc 100644 --- a/doc/api/dcat/CatResult/CatResult.html +++ b/doc/api/dcat/CatResult/CatResult.html @@ -91,17 +91,19 @@ Properties
  • exitCode
  • -
  • hashCode
  • +
  • hashCode
  • +
  • isFailure
  • +
  • isSuccess
  • messages
  • -
  • runtimeType
  • +
  • runtimeType
  • Methods
  • addMessage
  • -
  • noSuchMethod
  • -
  • toString
  • +
  • noSuchMethod
  • +
  • toString
  • Operators
  • -
  • operator ==
  • +
  • operator ==
  • diff --git a/doc/api/dcat/CatResult/addMessage.html b/doc/api/dcat/CatResult/addMessage.html index ceb40a2..7daeea5 100644 --- a/doc/api/dcat/CatResult/addMessage.html +++ b/doc/api/dcat/CatResult/addMessage.html @@ -53,9 +53,9 @@ void -addMessage(
    1. int exitCode,
    2. -
    3. String message,
    4. -
    5. {String? path}
    6. +addMessage(
      1. int exitCode,
      2. +
      3. String message,
      4. +
      5. {String? path}
      ) @@ -63,7 +63,7 @@
      -

      Add a message.

      +

      Add a message with an optional path.

      @@ -109,17 +109,19 @@ Properties
    7. exitCode
    8. -
    9. hashCode
    10. +
    11. hashCode
    12. +
    13. isFailure
    14. +
    15. isSuccess
    16. messages
    17. -
    18. runtimeType
    19. +
    20. runtimeType
    21. Methods
    22. addMessage
    23. -
    24. noSuchMethod
    25. -
    26. toString
    27. +
    28. noSuchMethod
    29. +
    30. toString
    31. Operators
    32. -
    33. operator ==
    34. +
    35. operator ==
    36. diff --git a/doc/api/dcat/CatResult/exitCode.html b/doc/api/dcat/CatResult/exitCode.html index 78902db..5fcbb8e 100644 --- a/doc/api/dcat/CatResult/exitCode.html +++ b/doc/api/dcat/CatResult/exitCode.html @@ -50,7 +50,7 @@
      - int + int exitCode
      read / write
      @@ -97,17 +97,19 @@ Properties
    37. exitCode
    38. -
    39. hashCode
    40. +
    41. hashCode
    42. +
    43. isFailure
    44. +
    45. isSuccess
    46. messages
    47. -
    48. runtimeType
    49. +
    50. runtimeType
    51. Methods
    52. addMessage
    53. -
    54. noSuchMethod
    55. -
    56. toString
    57. +
    58. noSuchMethod
    59. +
    60. toString
    61. Operators
    62. -
    63. operator ==
    64. +
    65. operator ==
    66. diff --git a/doc/api/dcat/CatResult/isFailure.html b/doc/api/dcat/CatResult/isFailure.html new file mode 100644 index 0000000..125425f --- /dev/null +++ b/doc/api/dcat/CatResult/isFailure.html @@ -0,0 +1,150 @@ + + + + + + + + isFailure property - CatResult class - dcat library - Dart API + + + + + + + + + + + + + + + + + +
      + +
      + + +
      isFailure
      + +
      + +
      + + +
      +
      +

      isFailure property + Null safety +

      + + + +
      + +
      + bool + isFailure + + +
      + + +
      +

      Returns true if the exitCode is exitFailure.

      +
      + + +
      +

      Implementation

      +
      bool get isFailure => exitCode == exitFailure;
      +
      + +
      + + +
      + + + + + +
      + +
      + + dcat + 1.0.0 + + + +
      + + + + + + + + + + + + diff --git a/doc/api/dcat/CatResult/isSuccess.html b/doc/api/dcat/CatResult/isSuccess.html new file mode 100644 index 0000000..3d8e6b8 --- /dev/null +++ b/doc/api/dcat/CatResult/isSuccess.html @@ -0,0 +1,150 @@ + + + + + + + + isSuccess property - CatResult class - dcat library - Dart API + + + + + + + + + + + + + + + + + +
      + +
      + + +
      isSuccess
      + +
      + +
      + + +
      +
      +

      isSuccess property + Null safety +

      + + + +
      + +
      + bool + isSuccess + + +
      + + +
      +

      Returns true if the exitCode is exitSuccess.

      +
      + + +
      +

      Implementation

      +
      bool get isSuccess => exitCode == exitSuccess;
      +
      + +
      + + +
      + + + + + +
      + +
      + + dcat + 1.0.0 + + + +
      + + + + + + + + + + + + diff --git a/doc/api/dcat/CatResult/messages.html b/doc/api/dcat/CatResult/messages.html index 0163b3a..91ad77e 100644 --- a/doc/api/dcat/CatResult/messages.html +++ b/doc/api/dcat/CatResult/messages.html @@ -50,7 +50,7 @@
      - List<String> + List<String> messages
      final
      @@ -97,17 +97,19 @@ Properties
    67. exitCode
    68. -
    69. hashCode
    70. +
    71. hashCode
    72. +
    73. isFailure
    74. +
    75. isSuccess
    76. messages
    77. -
    78. runtimeType
    79. +
    80. runtimeType
    81. Methods
    82. addMessage
    83. -
    84. noSuchMethod
    85. -
    86. toString
    87. +
    88. noSuchMethod
    89. +
    90. toString
    91. Operators
    92. -
    93. operator ==
    94. +
    95. operator ==
    96. diff --git a/doc/api/dcat/cat.html b/doc/api/dcat/cat.html index 975ead7..45e1714 100644 --- a/doc/api/dcat/cat.html +++ b/doc/api/dcat/cat.html @@ -52,27 +52,25 @@
      -Future<CatResult> -cat(
      1. List<String> paths,
      2. -
      3. Object output,
      4. -
      5. {Stream<List<int>>? input,
      6. -
      7. List<String>? log,
      8. -
      9. bool showEnds = false,
      10. -
      11. bool numberNonBlank = false,
      12. -
      13. bool showLineNumbers = false,
      14. -
      15. bool showTabs = false,
      16. -
      17. bool squeezeBlank = false,
      18. -
      19. bool showNonPrinting = false}
      20. +Future<CatResult> +cat(
        1. List<String> paths,
        2. +
        3. IOSink output,
        4. +
        5. {Stream<List<int>>? input,
        6. +
        7. bool showEnds = false,
        8. +
        9. bool numberNonBlank = false,
        10. +
        11. bool showLineNumbers = false,
        12. +
        13. bool showTabs = false,
        14. +
        15. bool squeezeBlank = false,
        16. +
        17. bool showNonPrinting = false}
        )
      -

      Concatenates files in paths to stdout or File.

      +

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

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

      @@ -81,33 +79,21 @@

      Implementation

      -
      Future<CatResult> cat(List<String> paths, Object output,
      +  
      Future<CatResult> cat(List<String> paths, IOSink output,
           {Stream<List<int>>? input,
      -    List<String>? log,
           bool showEnds = false,
           bool numberNonBlank = false,
           bool showLineNumbers = false,
           bool showTabs = false,
           bool squeezeBlank = false,
           bool showNonPrinting = false}) async {
      -  var result = CatResult();
      -  var lineNumber = 1;
      -  log?.clear();
      +  final result = CatResult();
      +  final lastLine = _LastLine(0, _lineFeed);
         if (paths.isEmpty) {
           if (input != null) {
      -      final lines = await _readStream(input);
             try {
      -        await _writeLines(
      -            lines,
      -            lineNumber,
      -            output,
      -            log,
      -            showEnds,
      -            showLineNumbers,
      -            numberNonBlank,
      -            showTabs,
      -            squeezeBlank,
      -            showNonPrinting);
      +        await _writeStream(input, lastLine, output, showEnds, showLineNumbers,
      +            numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
             } catch (e) {
               result.addMessage(exitFailure, '$e');
             }
      @@ -115,25 +101,14 @@
         } else {
           for (final path in paths) {
             try {
      -        final Stream<String> lines;
      +        final Stream<List<int>> stream;
               if (path == '-' && input != null) {
      -          lines = await _readStream(input);
      +          stream = input;
               } else {
      -          lines = utf8.decoder
      -              .bind(File(path).openRead())
      -              .transform(const LineSplitter());
      +          stream = File(path).openRead();
               }
      -        lineNumber = await _writeLines(
      -            lines,
      -            lineNumber,
      -            output,
      -            log,
      -            showEnds,
      -            showLineNumbers,
      -            numberNonBlank,
      -            showTabs,
      -            squeezeBlank,
      -            showNonPrinting);
      +        await _writeStream(stream, lastLine, output, showEnds, showLineNumbers,
      +            numberNonBlank, showTabs, squeezeBlank, showNonPrinting);
             } on FileSystemException catch (e) {
               final String? osMessage = e.osError?.message;
               final String message;
      diff --git a/doc/api/dcat/dcat-library.html b/doc/api/dcat/dcat-library.html
      index 5ab06b8..1aaeb3f 100644
      --- a/doc/api/dcat/dcat-library.html
      +++ b/doc/api/dcat/dcat-library.html
      @@ -77,12 +77,12 @@
             
      exitFailure - → const int + → const int
      - + Failure exit code.
      @@ -92,12 +92,12 @@
      exitSuccess - → const int + → const int
      - + Success exit code.
      @@ -114,14 +114,14 @@
      - cat(List<String> paths, Object output, {Stream<List<int>>? input, List<String>? log, bool showEnds = false, bool numberNonBlank = false, bool showLineNumbers = false, bool showTabs = false, bool squeezeBlank = false, bool showNonPrinting = false}) - 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}) + Future<CatResult>
      - Concatenates files in paths to stdout or File. [...] + Concatenates files in paths to the standard output or a file. [...]
      diff --git a/doc/api/dcat/exitFailure-constant.html b/doc/api/dcat/exitFailure-constant.html index cad2799..bd27d23 100644 --- a/doc/api/dcat/exitFailure-constant.html +++ b/doc/api/dcat/exitFailure-constant.html @@ -50,12 +50,15 @@
      - int + int const exitFailure
      +
      +

      Failure exit code.

      +
      diff --git a/doc/api/dcat/exitSuccess-constant.html b/doc/api/dcat/exitSuccess-constant.html index 887e38e..9e8c765 100644 --- a/doc/api/dcat/exitSuccess-constant.html +++ b/doc/api/dcat/exitSuccess-constant.html @@ -50,12 +50,15 @@
      - int + int const exitSuccess
      +
      +

      Success exit code.

      +
      diff --git a/doc/api/index.html b/doc/api/index.html index ae696be..e26d37a 100644 --- a/doc/api/index.html +++ b/doc/api/index.html @@ -85,34 +85,36 @@ Examples:

      Library Usage

      import 'package:dcat/dcat.dart';
       
      -final result = await cat(['path/to/file', 'path/to/otherfile]'], File('path/to/outfile'));
      -if (result.exitCode == exitFailure) {
      +final result = await cat(['path/to/file', 'path/to/otherfile]'],
      +    File('path/to/outfile').openWrite());
      +if (result.isFailure) {
         for (final message in result.messages) {
           print("Error: $message");
         }
       }
       

      The cat function supports the following parameters:

      -
      ParameterDescriptionType
      pathsThe file paths.String[]
      outputThe standard output or file.IOSink or File
      inputThe standard input.Stream<List<int>>?
      logThe log for debugging.List<String>?
      showEndsSame as -ebool
      numberNonBlankSame as -bbool
      showLineNumbersSame as -nbool
      showTabsSame as -Tbool
      squeezeBlankSame as -sbool
      showNonPrintingSame as -vbool
      +
      ParameterDescriptionType
      pathsThe file paths.String[]
      outputThe standard output or file.IOSink
      inputThe standard input.Stream<List<int>>?
      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.
      • -
      • input can be stdin.
      • -
      • log is used for debugging/testing purposes.
      • +
      • output should be an IOSink like stdout or a File stream.
      • +
      • input can be stdin.

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

      A CatResult object is returned which contains the exitCode (exitSuccess or exitFailure) and error messages if any:

      final result = await cat(['path/to/file'], stdout);
       if (result.exitCode == exitSuccess) {
         ...
      +} else {
      +  for (final message in result.messages) {
      +    stderr.writeln("Error: $message");
      +  }
       }
       

      Differences from GNU cat

      • No binary file support.
      • -
      • A line is considered terminated by either a CR (carriage return), a LF (line feed), a CR+LF sequence (DOS line ending).
      • -
      • A line ending is automatically appended to the last line of any read file.
      • -
      • The U+ notation is used instead of M- for non-printing characters.
      • +
      • The U+ notation is used instead of M- for non-printing characters.
      diff --git a/doc/api/index.json b/doc/api/index.json index 8909c48..846fa27 100644 --- a/doc/api/index.json +++ b/doc/api/index.json @@ -1 +1 @@ -[{"name":"dcat","qualifiedName":"dcat","href":"dcat/dcat-library.html","type":"library","overriddenDepth":0,"packageName":"dcat"},{"name":"CatResult","qualifiedName":"dcat.CatResult","href":"dcat/CatResult-class.html","type":"class","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}},{"name":"CatResult","qualifiedName":"dcat.CatResult.CatResult","href":"dcat/CatResult/CatResult.html","type":"constructor","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"addMessage","qualifiedName":"dcat.CatResult.addMessage","href":"dcat/CatResult/addMessage.html","type":"method","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"exitCode","qualifiedName":"dcat.CatResult.exitCode","href":"dcat/CatResult/exitCode.html","type":"property","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"messages","qualifiedName":"dcat.CatResult.messages","href":"dcat/CatResult/messages.html","type":"property","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"cat","qualifiedName":"dcat.cat","href":"dcat/cat.html","type":"function","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}},{"name":"exitFailure","qualifiedName":"dcat.exitFailure","href":"dcat/exitFailure-constant.html","type":"top-level constant","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}},{"name":"exitSuccess","qualifiedName":"dcat.exitSuccess","href":"dcat/exitSuccess-constant.html","type":"top-level constant","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}}] +[{"name":"dcat","qualifiedName":"dcat","href":"dcat/dcat-library.html","type":"library","overriddenDepth":0,"packageName":"dcat"},{"name":"CatResult","qualifiedName":"dcat.CatResult","href":"dcat/CatResult-class.html","type":"class","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}},{"name":"CatResult","qualifiedName":"dcat.CatResult.CatResult","href":"dcat/CatResult/CatResult.html","type":"constructor","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"addMessage","qualifiedName":"dcat.CatResult.addMessage","href":"dcat/CatResult/addMessage.html","type":"method","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"exitCode","qualifiedName":"dcat.CatResult.exitCode","href":"dcat/CatResult/exitCode.html","type":"property","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"isFailure","qualifiedName":"dcat.CatResult.isFailure","href":"dcat/CatResult/isFailure.html","type":"property","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"isSuccess","qualifiedName":"dcat.CatResult.isSuccess","href":"dcat/CatResult/isSuccess.html","type":"property","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"messages","qualifiedName":"dcat.CatResult.messages","href":"dcat/CatResult/messages.html","type":"property","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"CatResult","type":"class"}},{"name":"cat","qualifiedName":"dcat.cat","href":"dcat/cat.html","type":"function","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}},{"name":"exitFailure","qualifiedName":"dcat.exitFailure","href":"dcat/exitFailure-constant.html","type":"top-level constant","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}},{"name":"exitSuccess","qualifiedName":"dcat.exitSuccess","href":"dcat/exitSuccess-constant.html","type":"top-level constant","overriddenDepth":0,"packageName":"dcat","enclosedBy":{"name":"dcat","type":"library"}}]