Was trying to make a pretty proggy and needed something to format either side of a string so the ends would be even regardless of the info presented. I have no idea how to do this in lape or if you even can, so I made a function to format strings so they're either aligned to the left, right, or center. It fills the sides of the string with whatever character you choose depending on how you want it aligned.
If that didn't make sense it's cause I'm terrible at explaining things , so I will let the code do the explaining.
fstring():
Simba Code:
type
alignType = (left, right, center);
(*
fstring()
by: HKbotz
Formats the string so it is either aligned to the left, right, or center.
Fills sides based on align with whatever character you choose for specified
length.
-str: the string to format
-len: the length of the formatted string
-align: the way you wish to align the text compared to the fill char
-fill: the character you wish to fill the sides with
usage:
fstring('Some text', 30);
fstring('Some text', 30, center, '-');
example:
writeln('// ' + fstring('SomeStat', 20) + '\\');
writeln('// ' + fstring('SomeOtherStat', 20) + '\\');
Output:
// SomeStat \\
// SomeOtherStat \\
*)
function fstring(str: string; len: integer; align: alignType = left; fill: char = ' '): string;
var
i, c, n, strLen: integer;
begin
setLength(result, len);
strLen := length(str);
if (align = left) then
begin
for (i := 1) to (len) do
begin
if (i <= strLen) then
result[i] := str[i]
else
result[i] := fill;
end;
end else if (align = right) then
begin
n := len - strLen;
c := 1;
for (i := 1) to (len) do
begin
if (i <= n) then
result[i] := fill
else
begin
result[i] := str[c];
inc(c);
end;
end;
end else if (align = center) then
begin
n := round((len - strLen) / 2);
c := 1;
for (i := 1) to (len) do
begin
if (i <= n) then
result[i] := fill
else if (c <= strLen) then
begin
result[i] := str[c];
inc(c);
end else
result[i] := fill;
end;
end else
begin
writeln('Passed incorrect align param. Use right, left, or center.');
writeln('Returning your original string...');
setLength(result, strLen);
result := str;
end;
end;
Example:
Simba Code:
procedure exampleProggy();
var
logType: string = 'Willow';
begin
writeln('//' + fstring('', 30, , '-') + '\\');
writeln('//' + fstring('Fake log chopping script', 30, center) + '\\');
writeln('//' + fstring('', 30) + '\\');
writeln('//' + fstring(' Log type: ' + logType, 30) + '\\');
writeln('//' + fstring(' Logs chopped: ' + intToStr(230), 30) + '\\');
writeln('//' + fstring(' Runs done: ' + intToStr(8), 30) + '\\');
writeln('//' + fstring(' Time running: ' + timeRunning(TIME_SHORT), 30) + '\\');
writeln('//' + fstring('', 30, , '-') + '\\');
end;
begin
exampleProggy();
end.
Output from exampleProggy():
Code:
//------------------------------\\
// Fake log chopping script \\
// \\
// Log type: Willow \\
// Logs chopped: 230 \\
// Runs done: 8 \\
// Time running: 02s \\
//------------------------------\\
If the string you supply is longer than len, it will just return the first len charaters.
So if you have a string that is 'This is a really long string', but only supply a len of 10, it will return 'This is a '.
Hope that makes sense and that someone can get some use out of this(so far I'm just using for proggies).
If there's a better way to do the formatting thing please let me know .