From 519150134892f4b03b2fb391c254cc208ccc99f7 Mon Sep 17 00:00:00 2001 From: Jaco Swarts Date: Mon, 24 May 2021 11:31:13 +0200 Subject: [PATCH] Migrate to nullsafety and clean up linting problems --- example/bool.dart | 2 +- example/choices.dart | 2 +- example/main.dart | 8 +++---- example/num.dart | 2 +- lib/prompts.dart | 51 ++++++++++++++++++++++---------------------- pubspec.yaml | 8 +++---- 6 files changed, 36 insertions(+), 37 deletions(-) diff --git a/example/bool.dart b/example/bool.dart index fccd61b..e445326 100644 --- a/example/bool.dart +++ b/example/bool.dart @@ -1,3 +1,3 @@ import 'package:prompts/prompts.dart' as prompts; -main() => prompts.getBool('Yes or no'); +bool main() => prompts.getBool('Yes or no'); diff --git a/example/choices.dart b/example/choices.dart index d370275..f97c9e1 100644 --- a/example/choices.dart +++ b/example/choices.dart @@ -1,6 +1,6 @@ import 'package:prompts/prompts.dart' as prompts; -main() { +void main() { var albums = [ 'Music of My Mind', 'Talking Book', diff --git a/example/main.dart b/example/main.dart index 2d5a54f..37b90aa 100644 --- a/example/main.dart +++ b/example/main.dart @@ -24,7 +24,7 @@ void main() { // "High-level" prompts are built upon [get]. // For example, we can prompt for confirmation trivially. - bool shouldDownload = prompts.getBool('Really download this package?'); + var shouldDownload = prompts.getBool('Really download this package?'); if (!shouldDownload) { print('Not downloading.'); @@ -33,13 +33,13 @@ void main() { } // Or, get an integer, WITH validation. - int age = prompts.getInt('How old are you?', defaultsTo: 23, chevron: false); + var age = prompts.getInt('How old are you?', defaultsTo: 23, chevron: false); print('$name, you\'re $age? Cool!'); // We can choose from various values. // There are two methods - shorthand and regular. var rgb = [Color.red, Color.green, Color.blue]; - Color color = prompts.chooseShorthand('Tell me your favorite color', rgb); + var color = prompts.chooseShorthand('Tell me your favorite color', rgb)!; print('You chose: ${color.about}'); // Displays an interactive selection in the terminal. @@ -50,7 +50,7 @@ void main() { // // You can also optionaly pass short `names`. color = prompts.choose('Choose another color', rgb, - defaultsTo: Color.blue, names: ['r', 'g', 'b']); + defaultsTo: Color.blue, names: ['r', 'g', 'b'])!; print(color.about); } diff --git a/example/num.dart b/example/num.dart index 3451b01..5179889 100644 --- a/example/num.dart +++ b/example/num.dart @@ -1,3 +1,3 @@ import 'package:prompts/prompts.dart'; -main() => getInt('Enter your age'); +int main() => getInt('Enter your age'); diff --git a/lib/prompts.dart b/lib/prompts.dart index 133acd9..b5bbffc 100644 --- a/lib/prompts.dart +++ b/lib/prompts.dart @@ -39,8 +39,8 @@ void clearLine() { /// backslash (`\`) will be interpreted as a signal that another line of /// input is to come. This is helpful for building REPL's. String get(String message, - {bool Function(String) validate, - String defaultsTo, + {bool Function(String)? validate, + String? defaultsTo, @deprecated bool colon = true, bool chevron = true, bool color = true, @@ -54,14 +54,14 @@ String get(String message, validate = (s) => s.trim().isEmpty || oldValidate(s); } - var prefix = "?"; + var prefix = '?'; var code = cyan; var currentChevron = '\u00BB'; var oldEchoMode = stdin.echoMode; void writeIt() { var msg = color - ? (code.wrap(prefix) + " " + wrapWith(message, [darkGray, styleBold])) + ? (code.wrap(prefix)! + ' ' + wrapWith(message, [darkGray, styleBold])!) : message; stdout.write(msg); if (defaultsTo != null) stdout.write(' ($defaultsTo)'); @@ -90,7 +90,7 @@ String get(String message, if (conceal) stdin.echoMode = false; while (true) { - var line = stdin.readLineSync().trim(); + var line = stdin.readLineSync()!.trim(); if (!line.endsWith('\\')) { buf.writeln(line); @@ -144,7 +144,7 @@ String get(String message, return out; } else { code = red; - prefix = "\u2717"; + prefix = '\u2717'; if (ansiOutputEnabled) stdout.add([$esc, $F]); // Clear the line. @@ -170,6 +170,7 @@ bool getBool(String message, AnsiCode inputColor = cyan}) { if (appendYesNo) { message += + // ignore: unnecessary_null_comparison defaultsTo == null ? ' (y/n)' : (defaultsTo ? ' (Y/n)' : ' (y/N)'); } var result = get( @@ -180,16 +181,14 @@ bool getBool(String message, chevron: chevron && colon, validate: (s) { s = s.trim().toLowerCase(); - return (defaultsTo != null && s.isEmpty) || - s.startsWith('y') || - s.startsWith('n'); + return (s.isEmpty) || s.startsWith('y') || s.startsWith('n'); }, ); result = result.toLowerCase(); - if (result.isEmpty) + if (result.isEmpty) { return defaultsTo; - else if (result == 'y') return true; + } else if (result == 'y') return true; return false; } @@ -199,7 +198,7 @@ bool getBool(String message, /// /// [color], [defaultsTo], [inputColor], [conceal], and [chevron] are forwarded to [get]. int getInt(String message, - {int defaultsTo, + {int? defaultsTo, int radix = 10, bool color = true, bool chevron = true, @@ -221,7 +220,7 @@ int getInt(String message, /// /// [color], [defaultsTo], [inputColor], [conceal], and [chevron] are forwarded to [get]. double getDouble(String message, - {double defaultsTo, + {double? defaultsTo, bool color = true, bool chevron = true, @deprecated bool colon = true, @@ -262,8 +261,8 @@ double getDouble(String message, /// 2) Blue /// 3) Green /// ``` -T choose(String message, Iterable options, - {T defaultsTo, +T? choose(String message, Iterable options, + {T? defaultsTo, String prompt = 'Enter your choice', // int defaultIndex = 0, bool chevron = true, @@ -272,7 +271,7 @@ T choose(String message, Iterable options, bool color = true, bool conceal = false, bool interactive = true, - Iterable names}) { + Iterable? names}) { if (options.isEmpty) { throw ArgumentError.value('`options` may not be empty.'); } @@ -301,7 +300,7 @@ T choose(String message, Iterable options, var b = StringBuffer(); - b..writeln(message); + b.writeln(message); if (interactive && ansiOutputEnabled && !Platform.isWindows) { var index = defaultsTo != null ? options.toList().indexOf(defaultsTo) : 0; @@ -318,13 +317,13 @@ T choose(String message, Iterable options, if (!needsClear) { needsClear = true; } else { - for (int i = 0; i < options.length; i++) { + for (var i = 0; i < options.length; i++) { goUpOneLine(); clearLine(); } } - for (int i = 0; i < options.length; i++) { + for (var i = 0; i < options.length; i++) { var key = map.keys.elementAt(i); var msg = map[key]; AnsiCode code; @@ -392,7 +391,7 @@ T choose(String message, Iterable options, } else { b.writeln(); - for (int i = 0; i < options.length; i++) { + for (var i = 0; i < options.length; i++) { var key = map.keys.elementAt(i); var indicator = names != null ? names.elementAt(i) : (i + 1).toString(); b.write('$indicator) ${map[key]}'); @@ -417,14 +416,14 @@ T choose(String message, Iterable options, if (s.isEmpty) return defaultsTo != null; if (map.values.contains(s)) return true; if (names != null && names.contains(s)) return true; - int i = int.tryParse(s); + var i = int.tryParse(s); if (i == null) return false; return i >= 1 && i <= options.length; }, ); if (line.isEmpty) return defaultsTo; - int i; + int? i; if (names != null && names.contains(line)) { i = names.toList().indexOf(line) + 1; } else { @@ -446,8 +445,8 @@ T choose(String message, Iterable options, /// A default option may be provided by means of [defaultsTo]. /// /// [color], [defaultsTo], [inputColor], and [chevron] are forwarded to [get]. -T chooseShorthand(String message, Iterable options, - {T defaultsTo, +T? chooseShorthand(String message, Iterable options, + {T? defaultsTo, bool chevron = true, @deprecated bool colon = true, AnsiCode inputColor = cyan, @@ -461,7 +460,7 @@ T chooseShorthand(String message, Iterable options, if (chevron && colon) b.write(':'); b.write(' ('); var firstChars = [], strings = []; - int i = 0; + var i = 0; for (var option in options) { var str = option.toString(); @@ -482,7 +481,7 @@ T chooseShorthand(String message, Iterable options, b.write(')'); - T value; + T? value; get( b.toString(), diff --git a/pubspec.yaml b/pubspec.yaml index 6a05fd3..e0011bb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,9 +4,9 @@ description: Rich, simple, synchronous command-line prompt library for Dart. homepage: https://github.com/thosakwe/prompts author: Tobe O environment: - sdk: ">=2.0.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: - charcode: ^1.0.0 - io: ^0.3.2 + charcode: ^1.2.0 + io: ^1.0.0 dev_dependencies: - pedantic: ^1.0.0 + pedantic: ^1.11.0