add read from stdin
This commit is contained in:
@@ -1,13 +1,26 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
main(List<String> args) async {
|
main(List<String> args) async {
|
||||||
if (args.length == 0) {
|
final jsonContent = await readContent(args);
|
||||||
print('File name is not assigned\nUsage: prettyjson.dart <file.json>');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final jsonContent = await new File(args[0]).readAsString();
|
|
||||||
final jsonDecoded = json.decode(jsonContent);
|
final jsonDecoded = json.decode(jsonContent);
|
||||||
JsonEncoder encoder = new JsonEncoder.withIndent(' ');
|
JsonEncoder encoder = new JsonEncoder.withIndent(' ');
|
||||||
print(encoder.convert(jsonDecoded));
|
print(encoder.convert(jsonDecoded));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<String> readContent(List<String> args) async {
|
||||||
|
if (args.length == 0) {
|
||||||
|
return readFromStdin();
|
||||||
|
} else {
|
||||||
|
return new File(args[0]).readAsString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> readFromStdin() async {
|
||||||
|
final stdinContentLines = stdin.transform(utf8.decoder);
|
||||||
|
final stdinContentArray = new List<String>();
|
||||||
|
await for (var line in stdinContentLines) {
|
||||||
|
stdinContentArray.add(line);
|
||||||
|
}
|
||||||
|
return Future<String>.value(stdinContentArray.join('\n'));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user