restemplate/UCRTHelper.pas

83 lines
1.9 KiB
Plaintext

{
This file is part of restemplate.
Copyright (C) 2015 Andreas Schneider
restemplate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
restemplate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with restemplate. If not, see <http://www.gnu.org/licenses/>.
}
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.