Skip to content

Instantly share code, notes, and snippets.

@adam-singer
Created March 7, 2013 02:25
Show Gist options
  • Save adam-singer/5105108 to your computer and use it in GitHub Desktop.
Save adam-singer/5105108 to your computer and use it in GitHub Desktop.
Example of how to prompt for password in dart console using streams. Not exactly secure since echo is not disabled. Taken from the dart-gde discovery project
/**
* $ cat /Applications/dart/dart-sdk/version
* 0.4.1.0_r19425
*/
import "dart:io";
import "dart:uri";
import "dart:async";
Future<String> promptPassword() {
var completer = new Completer<String>();
StreamSubscription stdinSubscription;
stdout.addString("enter password: ");
readLine(String line) {
stdout.addString("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
stdinSubscription.cancel();
completer.complete(line);
};
stdinSubscription = stdin
.transform(new StringDecoder())
.transform(new LineTransformer())
.listen(readLine);
return completer.future;
}
void main() {
promptPassword().then((password)=>print("password = $password"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment