1 /* 2 * hunt-console eases the creation of beautiful and testable command line interfaces. 3 * 4 * Copyright (C) 2018-2019, HuntLabs 5 * 6 * Website: https://www.huntlabs.net 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.console.helper.QuestionHelper; 13 14 import hunt.console.input.Input; 15 import hunt.console.output.Output; 16 import hunt.console.question.ChoiceQuestion; 17 import hunt.console.question.Question; 18 import hunt.console.helper.AbstractHelper; 19 20 import hunt.Exceptions; 21 import hunt.stream.Common; 22 import hunt.stream.FileInputStream; 23 24 import std.stdio; 25 26 class QuestionHelper : AbstractHelper 27 { 28 private InputStream inputStream; 29 30 public string ask(Input input, Output output, Question question) 31 { 32 if (!input.isInteractive()) { 33 return question.getDefaultValue(); 34 } 35 36 return doAsk(output, question); 37 } 38 39 protected string doAsk(Output output, Question question) 40 { 41 InputStream inputStream = this.inputStream is null ? new FileInputStream(stdin) : this.inputStream; 42 43 string message = question.getQuestion(); 44 if (cast(ChoiceQuestion)question !is null) { 45 46 } 47 48 output.writeln(message); 49 50 string answer; 51 52 if (question.isHidden()) { 53 implementationMissing(); 54 } else { 55 implementationMissing(); 56 } 57 58 if (answer is null || answer.length == 0) { 59 answer = question.getDefaultValue(); 60 } 61 62 return answer; 63 } 64 65 public InputStream getInputStream() 66 { 67 return inputStream; 68 } 69 70 public void setInputStream(InputStream inputStream) 71 { 72 this.inputStream = inputStream; 73 } 74 75 /* override */ public string getName() 76 { 77 return "question"; 78 } 79 }