restemplate/UCRTHelper.pas

65 lines
1.2 KiB
Plaintext
Raw Normal View History

2015-09-28 11:04:58 +02:00
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
2015-09-28 11:09:59 +02:00
Result := Prompt(ACaption, '', AMode);
2015-09-28 11:04:58 +02:00
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);
2015-09-28 11:06:43 +02:00
SetLength(Result, Length(Result) - 1);
2015-09-28 11:04:58 +02:00
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.
2015-09-28 11:13:04 +02:00
Writeln; //Always end the line
2015-09-28 11:04:58 +02:00
end;
end.