restemplate/UCRTHelper.pas

83 lines
1.9 KiB
Plaintext
Raw Normal View History

2015-09-29 11:54:28 +02:00
{
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/>.
}
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.