restemplate/UCRTHelper.pas

65 lines
1.2 KiB
Plaintext

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
Result := 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);
SetLength(Result, Length(Result) - 1);
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 = ^C then
Result := ''; //Pretend that nothing happened.
Writeln; //Always end the line
end;
end.