From 05c26ed91f9c3fa1043fe9a50bcbd0e3db5fa299 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 28 Sep 2015 11:04:58 +0200 Subject: [PATCH] Improved prompting --- UCRTHelper.pas | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ restemplate.lpi | 6 ++++- restemplate.pas | 12 +++------- 3 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 UCRTHelper.pas diff --git a/UCRTHelper.pas b/UCRTHelper.pas new file mode 100644 index 0000000..d70e038 --- /dev/null +++ b/UCRTHelper.pas @@ -0,0 +1,64 @@ +unit UCRTHelper; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils, crt; + +type + TPromptMode = (pmNormal, pmPassword); + +function Prompt(ACaption: String; AMode: TPromptMode = pmNormal): String; overload; +function Prompt(ACaption, ADefault: String; AMode: TPromptMode = pmNormal): String; overload; + +implementation + +function Prompt(ACaption: String; AMode: TPromptMode): String; +begin + Prompt(ACaption, '', AMode); +end; + +function Prompt(ACaption, ADefault: String; AMode: TPromptMode): String; +var + c: Char; +begin + Write(ACaption); + if ADefault <> '' then + Write(' [', ADefault, ']'); + Write(': '); + + Result := ''; + repeat + c := ReadKey; + if (c = #8) and (Length(Result) > 0) then + begin + Write(#8, ' ', #8); + end + else if c > #31 then + begin + Result := Result + c; + + case AMode of + pmNormal: Write(c); + pmPassword: Write('*'); + end; + end; + until (c = #13) or (c = ^C); + + if (c = #13) and (Result = '') then + begin + Result := ADefault; + Write(ADefault); + end; + + if c = #13 then + Writeln; + + if c = ^C then + Result := ''; //Pretend that nothing happened. +end; + +end. + diff --git a/restemplate.lpi b/restemplate.lpi index 29d151f..ee187d7 100644 --- a/restemplate.lpi +++ b/restemplate.lpi @@ -55,11 +55,15 @@ - + + + + + diff --git a/restemplate.pas b/restemplate.pas index dae5847..f81f67b 100644 --- a/restemplate.pas +++ b/restemplate.pas @@ -28,7 +28,7 @@ uses JTemplate, fpjson, jsonparser, DOM, XMLRead, XMLWrite, - RegExpr, crt; + RegExpr, crt, UCRTHelper; type TContentType = (ctOther, ctJSON, ctXML); @@ -112,17 +112,11 @@ procedure CmdAskUser(AName: String); var value, default: String; begin - Write(AName); default := sessionIni.ReadString(templateName, AName, ''); - if default <> '' then - Write(' [', default, ']: ') - else - Write(': '); - - ReadLn(value); + value := Prompt(AName, default); if value = '' then - value := default; + Halt(3); //Cancelled parser.Fields.Add(AName, value);