8000 Migrate to nullsafety and clean up linting problems by productiveme · Pull Request #7 · thosakwe/prompts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Migrate to nullsafety and clean up linting problems #7

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/bool.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import 'package:prompts/prompts.dart' as prompts;

main() => prompts.getBool('Yes or no');
bool main() => prompts.getBool('Yes or no');
2 changes: 1 addition & 1 deletion example/choices.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:prompts/prompts.dart' as prompts;

main() {
void main() {
var albums = [
'Music of My Mind',
'Talking Book',
Expand Down
8 changes: 4 additions & 4 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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.
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion example/num.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import 'package:prompts/prompts.dart';

main() => getInt('Enter your age');
int main() => getInt('Enter your age');
51 changes: 25 additions & 26 deletions lib/prompts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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;
}

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -262,8 +261,8 @@ double getDouble(String message,
/// 2) Blue
/// 3) Green
/// ```
T choose<T>(String message, Iterable<T> options,
{T defaultsTo,
T? choose<T>(String message, Iterable<T> options,
{T? defaultsTo,
String prompt = 'Enter your choice',
// int defaultIndex = 0,
bool chevron = true,
Expand All @@ -272,7 +271,7 @@ T choose<T>(String message, Iterable<T> options,
bool color = true,
bool conceal = false,
bool interactive = true,
Iterable<String> names}) {
Iterable<String>? names}) {
if (options.isEmpty) {
throw ArgumentError.value('`options` may not be empty.');
}
Expand Down Expand Up @@ -301,7 +300,7 @@ T choose<T>(String message, Iterable<T> options,

var b = StringBuffer();

b..writeln(message);
b.writeln(message);

if (interactive && ansiOutputEnabled && !Platform.isWindows) {
var index = defaultsTo != null ? options.toList().indexOf(defaultsTo) : 0;
Expand All @@ -318,13 +317,13 @@ T choose<T>(String message, Iterable<T> 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;
Expand Down Expand Up @@ -392,7 +391,7 @@ T choose<T>(String message, Iterable<T> 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]}');
Expand All @@ -417,14 +416,14 @@ T choose<T>(String message, Iterable<T> 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 {
Expand All @@ -446,8 +445,8 @@ T choose<T>(String message, Iterable<T> options,
/// A default option may be provided by means of [defaultsTo].
///
/// [color], [defaultsTo], [inputColor], and [chevron] are forwarded to [get].
T chooseShorthand<T>(String message, Iterable<T> options,
{T defaultsTo,
T? chooseShorthand<T>(String message, Iterable<T> options,
{T? defaultsTo,
bool chevron = true,
@deprecated bool colon = true,
AnsiCode inputColor = cyan,
Expand All @@ -461,7 +460,7 @@ T chooseShorthand<T>(String message, Iterable<T> options,
if (chevron && colon) b.write(':');
b.write(' (');
var firstChars = <String>[], strings = <String>[];
int i = 0;
var i = 0;

for (var option in options) {
var str = option.toString();
Expand All @@ -482,7 +481,7 @@ T chooseShorthand<T>(String message, Iterable<T> options,

b.write(')');

T value;
T? value;

get(
b.toString(),
Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Rich, simple, synchronous command-line prompt library for Dart.
homepage: https://github.com/thosakwe/prompts
author: Tobe O <thosakwe@gmail.com>
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
0