|
| 1 | +// ignore_for_file: invalid_use_of_protected_member |
| 2 | + |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +import 'package:args/args.dart'; |
| 6 | +import 'package:barcode/barcode.dart'; |
| 7 | +import 'package:barcode/src/barcode_1d.dart'; |
| 8 | +import 'package:barcode/src/qrcode.dart'; |
| 9 | +import 'package:quiver/iterables.dart'; |
| 10 | + |
| 11 | +final _parser = ArgParser() |
| 12 | + ..addFlag( |
| 13 | + 'help', |
| 14 | + abbr: 'h', |
| 15 | + negatable: false, |
| 16 | + help: 'Shows this help message.', |
| 17 | + ) |
| 18 | + ..addFlag( |
| 19 | + 'version', |
| 20 | + abbr: 'v', |
| 21 | + negatable: false, |
| 22 | + help: 'Shows the current version.', |
| 23 | + ) |
| 24 | + ..addOption( |
| 25 | + 'type', |
| 26 | + abbr: 't', |
| 27 | + defaultsTo: '128', |
| 28 | + help: 'Options: 128, qr.', |
| 29 | + ) |
| 30 | + ..addOption( |
| 31 | + 'height', |
| 32 | + abbr: 'y', |
| 33 | + defaultsTo: '8', |
| 34 | + help: 'Height for 1D barcodes.', |
| 35 | + ); |
| 36 | + |
| 37 | +void main(List<String> aargs) { |
| 38 | + final args = _parser.parse(aargs); |
| 39 | + if (args['help'] as bool) { |
| 40 | + print(_parser.usage); |
| 41 | + return; |
| 42 | + } |
| 43 | + if (args['version'] as bool) { |
| 44 | + print('barcoder 0.0.1'); |
| 45 | + return; |
| 46 | + } |
| 47 | + final data = args.rest.first; |
| 48 | + final type = args['type'] as String; |
| 49 | + final height = int.parse(args['height']); |
| 50 | + final output = switch (type) { |
| 51 | + '128' => _barcode128(data, height: height), |
| 52 | + 'qr' => _qr(data), |
| 53 | + _ => 'Unknown type: $type', |
| 54 | + }; |
| 55 | + print(output); |
| 56 | +} |
| 57 | + |
| 58 | +String _pixel(bool pixel) => pixel ? '█' : ' '; |
| 59 | + |
| 60 | +String _barcode128(String data, {int height = 8}) { |
| 61 | + final line = |
| 62 | + (Barcode.code128() as Barcode1D).convert(data).map(_pixel).join(); |
| 63 | + return List.filled(height, line).join('\n'); |
| 64 | +} |
| 65 | + |
| 66 | +String _qr(String data) { |
| 67 | + final qr = |
| 68 | + (Barcode.qrCode() as BarcodeQR).convert(utf8.encoder.convert(data)); |
| 69 | + final rows = partition(qr.pixels, qr.width); |
| 70 | + final pixels = [ |
| 71 | + for (final row in rows) row.map((e) => e ? '█' : ' ').join(), |
| 72 | + ]; |
| 73 | + return pixels.join('\n'); |
| 74 | +} |
0 commit comments