Don't parse if there are no flags.
This commit is contained in:
parent
fd3661b21b
commit
edb77726f1
4 changed files with 71 additions and 49 deletions
|
@ -68,7 +68,7 @@ Parameter | Description | Type
|
|||
:--------------- |:----------------------------- | :-------------------
|
||||
paths | The file paths. | String[]
|
||||
output | The standard output or file. | [IOSink](https://api.dart.dev/dart-io/IOSink-class.html)
|
||||
input | The standard input. | Stream<List\<int\>\>?
|
||||
input | The standard input. | [Stream](https://api.dart.dev/dart-io/Stdin-class.html)
|
||||
showEnds | Same as `-e` | bool
|
||||
numberNonBlank | Same as `-b` | bool
|
||||
showLineNumbers | Same as `-n` | bool
|
||||
|
|
|
@ -94,7 +94,7 @@ if (result.isFailure) {
|
|||
}
|
||||
</code></pre>
|
||||
<p>The <code>cat</code> function supports the following parameters:</p>
|
||||
<table><thead><tr><th style="text-align: left;">Parameter</th><th style="text-align: left;">Description</th><th style="text-align: left;">Type</th></tr></thead><tbody><tr><td style="text-align: left;">paths</td><td style="text-align: left;">The file paths.</td><td style="text-align: left;">String[]</td></tr><tr><td style="text-align: left;">output</td><td style="text-align: left;">The standard output or file.</td><td style="text-align: left;"><a href="https://api.dart.dev/dart-io/IOSink-class.html">IOSink</a></td></tr><tr><td style="text-align: left;">input</td><td style="text-align: left;">The standard input.</td><td style="text-align: left;">Stream<List<int>>?</td></tr><tr><td style="text-align: left;">showEnds</td><td style="text-align: left;">Same as <code>-e</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">numberNonBlank</td><td style="text-align: left;">Same as <code>-b</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showLineNumbers</td><td style="text-align: left;">Same as <code>-n</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showTabs</td><td style="text-align: left;">Same as <code>-T</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">squeezeBlank</td><td style="text-align: left;">Same as <code>-s</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showNonPrinting</td><td style="text-align: left;">Same as <code>-v</code></td><td style="text-align: left;">bool</td></tr></tbody></table>
|
||||
<table><thead><tr><th style="text-align: left;">Parameter</th><th style="text-align: left;">Description</th><th style="text-align: left;">Type</th></tr></thead><tbody><tr><td style="text-align: left;">paths</td><td style="text-align: left;">The file paths.</td><td style="text-align: left;">String[]</td></tr><tr><td style="text-align: left;">output</td><td style="text-align: left;">The standard output or file.</td><td style="text-align: left;"><a href="https://api.dart.dev/dart-io/IOSink-class.html">IOSink</a></td></tr><tr><td style="text-align: left;">input</td><td style="text-align: left;">The standard input.</td><td style="text-align: left;"><a href="https://api.dart.dev/dart-io/Stdin-class.html">Stream</a></td></tr><tr><td style="text-align: left;">showEnds</td><td style="text-align: left;">Same as <code>-e</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">numberNonBlank</td><td style="text-align: left;">Same as <code>-b</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showLineNumbers</td><td style="text-align: left;">Same as <code>-n</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showTabs</td><td style="text-align: left;">Same as <code>-T</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">squeezeBlank</td><td style="text-align: left;">Same as <code>-s</code></td><td style="text-align: left;">bool</td></tr><tr><td style="text-align: left;">showNonPrinting</td><td style="text-align: left;">Same as <code>-v</code></td><td style="text-align: left;">bool</td></tr></tbody></table>
|
||||
<ul>
|
||||
<li><code>paths</code> and <code>output</code> are required.</li>
|
||||
<li><code>output</code> should be an <a href="https://api.dart.dev/dart-io/IOSink-class.html">IOSink</a> like <code>stdout</code> or a <a href="https://api.dart.dev/dart-io/File/openWrite.html">File</a> stream.</li>
|
||||
|
|
106
lib/dcat.dart
106
lib/dcat.dart
|
@ -120,64 +120,78 @@ Future<void> _writeStream(
|
|||
bool showNonPrinting) async {
|
||||
const tab = 9;
|
||||
int squeeze = 0;
|
||||
final noFlags = !showEnds &&
|
||||
!showLineNumbers &&
|
||||
!numberNonBlank &&
|
||||
!showTabs &&
|
||||
!squeezeBlank &&
|
||||
!showNonPrinting;
|
||||
final sb = StringBuffer();
|
||||
await stream.forEach((data) {
|
||||
sb.clear();
|
||||
for (final ch in utf8.decode(data).runes) {
|
||||
if (lastLine.lastChar == _lineFeed) {
|
||||
if (squeezeBlank) {
|
||||
if (ch == _lineFeed) {
|
||||
if (squeeze >= 1) {
|
||||
lastLine.lastChar = ch;
|
||||
if (noFlags) {
|
||||
sb.writeCharCode(ch);
|
||||
} else {
|
||||
if (lastLine.lastChar == _lineFeed) {
|
||||
if (squeezeBlank) {
|
||||
if (ch == _lineFeed) {
|
||||
if (squeeze >= 1) {
|
||||
lastLine.lastChar = ch;
|
||||
continue;
|
||||
}
|
||||
squeeze++;
|
||||
} else {
|
||||
squeeze = 0;
|
||||
}
|
||||
}
|
||||
if (showLineNumbers || numberNonBlank) {
|
||||
if (!numberNonBlank || ch != _lineFeed) {
|
||||
sb
|
||||
..write('${++lastLine.lineNumber}'.padLeft(6))
|
||||
..write('\t');
|
||||
}
|
||||
}
|
||||
}
|
||||
lastLine.lastChar = ch;
|
||||
if (ch == _lineFeed) {
|
||||
if (showEnds) {
|
||||
sb.write('\$');
|
||||
}
|
||||
} else if (ch == tab) {
|
||||
if (showTabs) {
|
||||
sb.write('^I');
|
||||
continue;
|
||||
}
|
||||
} else if (showNonPrinting) {
|
||||
if (ch >= 32) {
|
||||
if (ch < 127) {
|
||||
// ASCII
|
||||
sb.writeCharCode(ch);
|
||||
continue;
|
||||
} else if (ch == 127) {
|
||||
// NULL
|
||||
sb.write('^?');
|
||||
continue;
|
||||
} else {
|
||||
// UNICODE
|
||||
sb
|
||||
..write('U+')
|
||||
..write(ch.toRadixString(16).padLeft(4, '0').toUpperCase());
|
||||
continue;
|
||||
}
|
||||
squeeze++;
|
||||
} else {
|
||||
squeeze = 0;
|
||||
}
|
||||
}
|
||||
if (showLineNumbers || numberNonBlank) {
|
||||
if (!numberNonBlank || ch != _lineFeed) {
|
||||
sb.write('${++lastLine.lineNumber}'.padLeft(6) + '\t');
|
||||
sb
|
||||
..write('^')
|
||||
..writeCharCode(ch + 64);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
sb.writeCharCode(ch);
|
||||
}
|
||||
lastLine.lastChar = ch;
|
||||
if (ch == _lineFeed) {
|
||||
if (showEnds) {
|
||||
sb.write('\$');
|
||||
}
|
||||
} else if (ch == tab) {
|
||||
if (showTabs) {
|
||||
sb.write('^I');
|
||||
continue;
|
||||
}
|
||||
} else if (showNonPrinting) {
|
||||
if (ch >= 32) {
|
||||
if (ch < 127) {
|
||||
// ASCII
|
||||
sb.writeCharCode(ch);
|
||||
continue;
|
||||
} else if (ch == 127) {
|
||||
// NULL
|
||||
sb.write('^?');
|
||||
continue;
|
||||
} else {
|
||||
// UNICODE
|
||||
sb.write('U+' + ch.toRadixString(16).padLeft(4, '0').toUpperCase());
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
sb
|
||||
..write('^')
|
||||
..writeCharCode(ch + 64);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
sb.writeCharCode(ch);
|
||||
}
|
||||
if (sb.isNotEmpty) {
|
||||
out.write(sb);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -269,5 +269,13 @@ void main() {
|
|||
lines = await tmp.readAsLines();
|
||||
expect(lines.length, 2, reason: "two lines");
|
||||
});
|
||||
|
||||
test('Test cat file -', () async {
|
||||
var tmp = tmpFile();
|
||||
await cat([sampleFile, '-'], tmp.openWrite(),
|
||||
input: mockStdin(text: '\n$sampleText'));
|
||||
var lines = await tmp.readAsLines();
|
||||
expect(lines.last, equals(sampleText));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue