Split into library.
This commit is contained in:
parent
c21191160f
commit
a29d8130a9
3 changed files with 145 additions and 132 deletions
119
bin/dcat.dart
119
bin/dcat.dart
|
@ -1,16 +1,15 @@
|
||||||
// Copyright (c) 2021, Erik C. Thauvin. All rights reserved.
|
// Copyright (c) 2021, Erik C. Thauvin. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style license that can be found
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
// in the LICENSE file.
|
// in the LICENSE file.
|
||||||
import 'dart:convert';
|
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:args/args.dart';
|
import 'package:args/args.dart';
|
||||||
|
import 'package:dcat/dcat.dart';
|
||||||
import 'package:indent/indent.dart';
|
import 'package:indent/indent.dart';
|
||||||
|
|
||||||
const appName = 'dcat';
|
const appName = libName;
|
||||||
const appVersion = '1.0.0';
|
const appVersion = '1.0.0';
|
||||||
const exitFailure = 1;
|
|
||||||
const exitSuccess = 0;
|
|
||||||
const helpFlag = 'help';
|
const helpFlag = 'help';
|
||||||
const nonBlankFlag = 'number-nonblank';
|
const nonBlankFlag = 'number-nonblank';
|
||||||
const numberFlag = 'number';
|
const numberFlag = 'number';
|
||||||
|
@ -19,69 +18,9 @@ const showTabsFlag = 'show-tabs';
|
||||||
const squeezeBlank = 'squeeze-blank';
|
const squeezeBlank = 'squeeze-blank';
|
||||||
const versionFlag = 'version';
|
const versionFlag = 'version';
|
||||||
|
|
||||||
/// Prints [message] and [path] to stderr.
|
|
||||||
Future<int> handleError(String message, {String path = ''}) async {
|
|
||||||
if (path.isNotEmpty) {
|
|
||||||
stderr.writeln('$appName: $path: $message');
|
|
||||||
} else {
|
|
||||||
stderr.write('$appName: $message');
|
|
||||||
}
|
|
||||||
return exitFailure;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Concatenates files in [paths].
|
|
||||||
Future<int> cat(List<String> paths,
|
|
||||||
{List<String>? log,
|
|
||||||
bool showEnds = false,
|
|
||||||
bool numberNonBlank = false,
|
|
||||||
bool showLineNumbers = false,
|
|
||||||
bool showTabs = false,
|
|
||||||
bool squeezeBlank = 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);
|
|
||||||
} 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);
|
|
||||||
} 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 handleError(message, path: path);
|
|
||||||
} on FormatException {
|
|
||||||
returnCode =
|
|
||||||
await handleError('Binary file not supported.', path: path);
|
|
||||||
} catch (e) {
|
|
||||||
returnCode = await handleError(e.toString(), path: path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return returnCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Concatenates files specified in [arguments].
|
/// Concatenates files specified in [arguments].
|
||||||
///
|
///
|
||||||
/// ```
|
/// Usage: `dcat [OPTION]... [FILE]...`
|
||||||
/// dcat [OPTION]... [FILE]...
|
|
||||||
/// ```
|
|
||||||
Future<int> main(List<String> arguments) async {
|
Future<int> main(List<String> arguments) async {
|
||||||
final parser = ArgParser();
|
final parser = ArgParser();
|
||||||
Future<int> returnCode;
|
Future<int> returnCode;
|
||||||
|
@ -109,8 +48,9 @@ Future<int> main(List<String> arguments) async {
|
||||||
try {
|
try {
|
||||||
argResults = parser.parse(arguments);
|
argResults = parser.parse(arguments);
|
||||||
} on FormatException catch (e) {
|
} on FormatException catch (e) {
|
||||||
return await handleError(
|
return await printError(
|
||||||
"${e.message}\nTry '$appName --$helpFlag' for more information.");
|
"${e.message}\nTry '$appName --$helpFlag' for more information.",
|
||||||
|
appName: appName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argResults[helpFlag]) {
|
if (argResults[helpFlag]) {
|
||||||
|
@ -131,21 +71,17 @@ Future<int> main(List<String> arguments) async {
|
||||||
return exitCode;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints version info.
|
/// Prints the version info.
|
||||||
Future<int> printVersion() async {
|
Future<int> printVersion() async {
|
||||||
print('''$appName (Dart cat) $appVersion
|
print('''$appName (Dart cat) $appVersion
|
||||||
Copyright (C) 2021 Erik C. Thauvin
|
Copyright (C) 2021 Erik C. Thauvin
|
||||||
License: 3-Clause BSD <https://opensource.org/licenses/BSD-3-Clause>
|
License: 3-Clause BSD <https://opensource.org/licenses/BSD-3-Clause>
|
||||||
|
|
||||||
Based on <https://dart.dev/tutorials/server/cmdline>
|
Inspired by <https://dart.dev/tutorials/server/cmdline>
|
||||||
Written by Erik C. Thauvin <https://erik.thauvin.net/>''');
|
Written by Erik C. Thauvin <https://erik.thauvin.net/>''');
|
||||||
return exitSuccess;
|
return exitSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads from stdin.
|
|
||||||
Future<Stream<String>> _readStdin() async =>
|
|
||||||
stdin.transform(utf8.decoder).transform(const LineSplitter());
|
|
||||||
|
|
||||||
/// Prints usage with [options].
|
/// Prints usage with [options].
|
||||||
Future<int> usage(String options) async {
|
Future<int> usage(String options) async {
|
||||||
print('''Usage: $appName [OPTION]... [FILE]...
|
print('''Usage: $appName [OPTION]... [FILE]...
|
||||||
|
@ -161,40 +97,3 @@ Examples:
|
||||||
Source and documentation: <https://github.com/ethauvin/dcat>''');
|
Source and documentation: <https://github.com/ethauvin/dcat>''');
|
||||||
return exitSuccess;
|
return exitSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes lines to stdout.
|
|
||||||
Future<int> _writeLines(Stream<String> lines, int lineNumber,
|
|
||||||
[List<String>? log,
|
|
||||||
bool showEnds = false,
|
|
||||||
bool showLineNumbers = false,
|
|
||||||
bool showNonBlank = false,
|
|
||||||
bool showTabs = false,
|
|
||||||
bool sqeezeBlank = false]) async {
|
|
||||||
var emptyLine = 0;
|
|
||||||
final sb = StringBuffer();
|
|
||||||
await for (final line in lines) {
|
|
||||||
sb.clear();
|
|
||||||
if (sqeezeBlank && line.isEmpty) {
|
|
||||||
if (++emptyLine >= 2) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
emptyLine = 0;
|
|
||||||
}
|
|
||||||
if (showNonBlank || showLineNumbers) {
|
|
||||||
sb.write('${lineNumber++}: ');
|
|
||||||
}
|
|
||||||
if (showTabs) {
|
|
||||||
sb.write(line.replaceAll('\t', '^I'));
|
|
||||||
} else {
|
|
||||||
sb.write(line);
|
|
||||||
}
|
|
||||||
if (showEnds) {
|
|
||||||
sb.write('\$');
|
|
||||||
}
|
|
||||||
|
|
||||||
log?.add(sb.toString());
|
|
||||||
stdout.writeln(sb);
|
|
||||||
}
|
|
||||||
return lineNumber;
|
|
||||||
}
|
|
||||||
|
|
114
lib/dcat.dart
Normal file
114
lib/dcat.dart
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
// Copyright (c) 2021, Erik C. Thauvin. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
library dcat;
|
||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
const libName = 'dcat';
|
||||||
|
const exitFailure = 1;
|
||||||
|
const exitSuccess = 0;
|
||||||
|
|
||||||
|
/// Concatenates files in [paths] to [stdout]
|
||||||
|
///
|
||||||
|
/// The parameters are similar to the [GNU cat utility](https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html#cat-invocation).
|
||||||
|
/// Specify a [log] for debugging purpose.
|
||||||
|
Future<int> cat(List<String> paths,
|
||||||
|
{List<String>? log,
|
||||||
|
bool showEnds = false,
|
||||||
|
bool numberNonBlank = false,
|
||||||
|
bool showLineNumbers = false,
|
||||||
|
bool showTabs = false,
|
||||||
|
bool squeezeBlank = 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);
|
||||||
|
} 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);
|
||||||
|
} 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, path: path);
|
||||||
|
} on FormatException {
|
||||||
|
returnCode = await printError('Binary file not supported.', path: path);
|
||||||
|
} catch (e) {
|
||||||
|
returnCode = await printError(e.toString(), path: path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prints the [appName], [path] and error [message] to [stderr].
|
||||||
|
Future<int> printError(String message,
|
||||||
|
{String appName = libName, String path = ''}) async {
|
||||||
|
if (path.isNotEmpty) {
|
||||||
|
stderr.writeln('$libName: $path: $message');
|
||||||
|
} else {
|
||||||
|
stderr.write('$libName: $message');
|
||||||
|
}
|
||||||
|
return exitFailure;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads from stdin.
|
||||||
|
Future<Stream<String>> _readStdin() async =>
|
||||||
|
stdin.transform(utf8.decoder).transform(const LineSplitter());
|
||||||
|
|
||||||
|
/// Writes lines to stdout.
|
||||||
|
Future<int> _writeLines(Stream<String> lines, int lineNumber,
|
||||||
|
[List<String>? log,
|
||||||
|
bool showEnds = false,
|
||||||
|
bool showLineNumbers = false,
|
||||||
|
bool showNonBlank = false,
|
||||||
|
bool showTabs = false,
|
||||||
|
bool squeezeBlank = false]) async {
|
||||||
|
var emptyLine = 0;
|
||||||
|
final sb = StringBuffer();
|
||||||
|
await for (final line in lines) {
|
||||||
|
sb.clear();
|
||||||
|
if (squeezeBlank && line.isEmpty) {
|
||||||
|
if (++emptyLine >= 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
emptyLine = 0;
|
||||||
|
}
|
||||||
|
if (showNonBlank || showLineNumbers) {
|
||||||
|
sb.write('${lineNumber++}: ');
|
||||||
|
}
|
||||||
|
if (showTabs) {
|
||||||
|
sb.write(line.replaceAll('\t', '^I'));
|
||||||
|
} else {
|
||||||
|
sb.write(line);
|
||||||
|
}
|
||||||
|
if (showEnds) {
|
||||||
|
sb.write('\$');
|
||||||
|
}
|
||||||
|
|
||||||
|
log?.add(sb.toString());
|
||||||
|
stdout.writeln(sb);
|
||||||
|
}
|
||||||
|
return lineNumber;
|
||||||
|
}
|
|
@ -1,46 +1,46 @@
|
||||||
|
import 'package:dcat/dcat.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
import '../bin/dcat.dart' as dcat;
|
import '../bin/dcat.dart' as app;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
final List<String> log = [];
|
final List<String> log = [];
|
||||||
int exitCode;
|
int exitCode;
|
||||||
|
|
||||||
test('Test Help', () async {
|
test('Test Help', () async {
|
||||||
expect(dcat.main(['-h']), completion(equals(0)));
|
expect(app.main(['-h']), completion(equals(0)));
|
||||||
expect(dcat.main(['--help']), completion(equals(0)));
|
expect(app.main(['--help']), completion(equals(0)));
|
||||||
exitCode = await dcat.main(['-h']);
|
exitCode = await app.main(['-h']);
|
||||||
expect(exitCode, equals(dcat.exitSuccess));
|
expect(exitCode, equals(exitSuccess));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test --version', () async {
|
test('Test --version', () async {
|
||||||
expect(dcat.main(['--version']), completion(equals(0)));
|
expect(app.main(['--version']), completion(equals(0)));
|
||||||
exitCode = await dcat.main(['--version']);
|
exitCode = await app.main(['--version']);
|
||||||
expect(exitCode, equals(dcat.exitSuccess));
|
expect(exitCode, equals(exitSuccess));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test directory', () async {
|
test('Test directory', () async {
|
||||||
exitCode = await dcat.main(['bin']);
|
exitCode = await app.main(['bin']);
|
||||||
expect(exitCode, equals(dcat.exitFailure));
|
expect(exitCode, equals(exitFailure));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test missing file', () async {
|
test('Test missing file', () async {
|
||||||
exitCode = await dcat.main(['foo']);
|
exitCode = await app.main(['foo']);
|
||||||
expect(exitCode, equals(dcat.exitFailure), reason: 'foo not found');
|
expect(exitCode, equals(exitFailure), reason: 'foo not found');
|
||||||
exitCode = await dcat.main(['bin/dcat.dart', 'foo']);
|
exitCode = await app.main(['bin/dcat.dart', 'foo']);
|
||||||
expect(exitCode, equals(dcat.exitFailure), reason: 'one missing file');
|
expect(exitCode, equals(exitFailure), reason: 'one missing file');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test cat source', () async {
|
test('Test cat source', () async {
|
||||||
await dcat.cat(['bin/dcat.dart'], log: log);
|
await cat(['bin/dcat.dart'], log: log);
|
||||||
expect(log.isEmpty, false, reason: 'log is empty');
|
expect(log.isEmpty, false, reason: 'log is empty');
|
||||||
expect(log.first, startsWith('// Copyright (c)'), reason: 'has copyright');
|
expect(log.first, startsWith('// Copyright (c)'), reason: 'has copyright');
|
||||||
expect(log.last, equals('}'));
|
expect(log.last, equals('}'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test cat -n source', () async {
|
test('Test cat -n source', () async {
|
||||||
exitCode =
|
exitCode = await cat(['bin/dcat.dart'], log: log, showLineNumbers: true);
|
||||||
await dcat.cat(['bin/dcat.dart'], log: log, showLineNumbers: true);
|
|
||||||
expect(exitCode, 0, reason: 'result code is 0');
|
expect(exitCode, 0, reason: 'result code is 0');
|
||||||
expect(log.first, startsWith('1: // Copyright (c)'),
|
expect(log.first, startsWith('1: // Copyright (c)'),
|
||||||
reason: 'has copyright');
|
reason: 'has copyright');
|
||||||
|
@ -51,7 +51,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test cat -E', () async {
|
test('Test cat -E', () async {
|
||||||
await dcat.cat(['test/test.txt'], log: log, showEnds: true);
|
await cat(['test/test.txt'], log: log, showEnds: true);
|
||||||
var hasBlank = false;
|
var hasBlank = false;
|
||||||
for (final String line in log) {
|
for (final String line in log) {
|
||||||
expect(line, endsWith('\$'));
|
expect(line, endsWith('\$'));
|
||||||
|
@ -63,8 +63,8 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test cat -bE', () async {
|
test('Test cat -bE', () async {
|
||||||
await dcat
|
await cat(['test/test.txt'],
|
||||||
.cat(['test/test.txt'], log: log, numberNonBlank: true, showEnds: true);
|
log: log, numberNonBlank: true, showEnds: true);
|
||||||
var hasBlank = false;
|
var hasBlank = false;
|
||||||
for (final String line in log) {
|
for (final String line in log) {
|
||||||
expect(line, endsWith('\$'));
|
expect(line, endsWith('\$'));
|
||||||
|
@ -76,7 +76,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test cat -T', () async {
|
test('Test cat -T', () async {
|
||||||
await dcat.cat(['test/test.txt'], log: log, showTabs: true);
|
await cat(['test/test.txt'], log: log, showTabs: true);
|
||||||
var hasTab = false;
|
var hasTab = false;
|
||||||
for (final String line in log) {
|
for (final String line in log) {
|
||||||
if (line.startsWith('^I')) {
|
if (line.startsWith('^I')) {
|
||||||
|
@ -88,7 +88,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test cat -s', () async {
|
test('Test cat -s', () async {
|
||||||
await dcat.cat(['test/test.txt'], log: log, squeezeBlank: true);
|
await cat(['test/test.txt'], log: log, squeezeBlank: true);
|
||||||
var hasSqueeze = true;
|
var hasSqueeze = true;
|
||||||
var prevLine = 'foo';
|
var prevLine = 'foo';
|
||||||
for (final String line in log) {
|
for (final String line in log) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue