Improved prompting

This commit is contained in:
Andreas Schneider 2015-09-28 11:04:58 +02:00
parent d1d371afb4
commit 05c26ed91f
3 changed files with 72 additions and 10 deletions

64
UCRTHelper.pas Normal file
View File

@ -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.

View File

@ -55,11 +55,15 @@
<FormatVersion Value="1"/> <FormatVersion Value="1"/>
</local> </local>
</RunParams> </RunParams>
<Units Count="1"> <Units Count="2">
<Unit0> <Unit0>
<Filename Value="restemplate.pas"/> <Filename Value="restemplate.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
</Unit0> </Unit0>
<Unit1>
<Filename Value="UCRTHelper.pas"/>
<IsPartOfProject Value="True"/>
</Unit1>
</Units> </Units>
</ProjectOptions> </ProjectOptions>
<CompilerOptions> <CompilerOptions>

View File

@ -28,7 +28,7 @@ uses
JTemplate, JTemplate,
fpjson, jsonparser, fpjson, jsonparser,
DOM, XMLRead, XMLWrite, DOM, XMLRead, XMLWrite,
RegExpr, crt; RegExpr, crt, UCRTHelper;
type type
TContentType = (ctOther, ctJSON, ctXML); TContentType = (ctOther, ctJSON, ctXML);
@ -112,17 +112,11 @@ procedure CmdAskUser(AName: String);
var var
value, default: String; value, default: String;
begin begin
Write(AName);
default := sessionIni.ReadString(templateName, AName, ''); default := sessionIni.ReadString(templateName, AName, '');
if default <> '' then value := Prompt(AName, default);
Write(' [', default, ']: ')
else
Write(': ');
ReadLn(value);
if value = '' then if value = '' then
value := default; Halt(3); //Cancelled
parser.Fields.Add(AName, value); parser.Fields.Add(AName, value);