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[]
|
paths | The file paths. | String[]
|
||||||
output | The standard output or file. | [IOSink](https://api.dart.dev/dart-io/IOSink-class.html)
|
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
|
showEnds | Same as `-e` | bool
|
||||||
numberNonBlank | Same as `-b` | bool
|
numberNonBlank | Same as `-b` | bool
|
||||||
showLineNumbers | Same as `-n` | bool
|
showLineNumbers | Same as `-n` | bool
|
||||||
|
|
|
@ -94,7 +94,7 @@ if (result.isFailure) {
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>The <code>cat</code> function supports the following parameters:</p>
|
<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>
|
<ul>
|
||||||
<li><code>paths</code> and <code>output</code> are required.</li>
|
<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>
|
<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 {
|
bool showNonPrinting) async {
|
||||||
const tab = 9;
|
const tab = 9;
|
||||||
int squeeze = 0;
|
int squeeze = 0;
|
||||||
|
final noFlags = !showEnds &&
|
||||||
|
!showLineNumbers &&
|
||||||
|
!numberNonBlank &&
|
||||||
|
!showTabs &&
|
||||||
|
!squeezeBlank &&
|
||||||
|
!showNonPrinting;
|
||||||
final sb = StringBuffer();
|
final sb = StringBuffer();
|
||||||
await stream.forEach((data) {
|
await stream.forEach((data) {
|
||||||
sb.clear();
|
sb.clear();
|
||||||
for (final ch in utf8.decode(data).runes) {
|
for (final ch in utf8.decode(data).runes) {
|
||||||
if (lastLine.lastChar == _lineFeed) {
|
if (noFlags) {
|
||||||
if (squeezeBlank) {
|
sb.writeCharCode(ch);
|
||||||
if (ch == _lineFeed) {
|
} else {
|
||||||
if (squeeze >= 1) {
|
if (lastLine.lastChar == _lineFeed) {
|
||||||
lastLine.lastChar = ch;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
squeeze++;
|
|
||||||
} else {
|
} else {
|
||||||
squeeze = 0;
|
sb
|
||||||
}
|
..write('^')
|
||||||
}
|
..writeCharCode(ch + 64);
|
||||||
if (showLineNumbers || numberNonBlank) {
|
continue;
|
||||||
if (!numberNonBlank || ch != _lineFeed) {
|
|
||||||
sb.write('${++lastLine.lineNumber}'.padLeft(6) + '\t');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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) {
|
if (sb.isNotEmpty) {
|
||||||
out.write(sb);
|
out.write(sb);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -269,5 +269,13 @@ void main() {
|
||||||
lines = await tmp.readAsLines();
|
lines = await tmp.readAsLines();
|
||||||
expect(lines.length, 2, reason: "two lines");
|
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