Skip to content

Commit 6f4d903

Browse files
Extend StreamFormatting to seperate linewrap and indenting
1 parent 076b3e9 commit 6f4d903

File tree

7 files changed

+224
-103
lines changed

7 files changed

+224
-103
lines changed

lib/custom_streams.gi

+3-6
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,8 @@ InstallMethod( PrintFormattingStatus, "output text custom",
343343
##
344344
InstallMethod( SetPrintFormattingStatus, "output text custom",
345345
[IsOutputTextCustomRep and IsOutputTextStream,
346-
IsBool],
346+
IsObject],
347347
function( str, stat)
348-
if stat = fail then
349-
Error("Print formatting status must be true or false");
350-
else
351-
str!.formatting := stat;
352-
fi;
348+
CheckValidPrintFormattingStatus(stat);
349+
str!.formatting := stat;
353350
end);

lib/streams.gd

+3-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ DeclareGlobalFunction( "InputOutputLocalProcess" );
10151015
## </ManSection>
10161016
## <#/GAPDoc>
10171017
##
1018-
DeclareOperation( "SetPrintFormattingStatus", [IsOutputStream, IsBool] );
1018+
DeclareOperation( "SetPrintFormattingStatus", [IsOutputStream, IsObject] );
10191019
DeclareOperation( "PrintFormattingStatus", [IsOutputStream] );
10201020

10211021

@@ -1259,3 +1259,5 @@ DeclareGlobalFunction( "InputFromUser" );
12591259
## <#/GAPDoc>
12601260
##
12611261
DeclareGlobalFunction( "OpenExternal" );
1262+
1263+
DeclareGlobalFunction( "CheckValidPrintFormattingStatus");

lib/streams.gi

+33-20
Original file line numberDiff line numberDiff line change
@@ -929,13 +929,10 @@ InstallMethod( PrintFormattingStatus, "output text string",
929929
##
930930
InstallMethod( SetPrintFormattingStatus, "output text string",
931931
[IsOutputTextStringRep and IsOutputTextStream,
932-
IsBool],
932+
IsObject],
933933
function( str, stat)
934-
if stat = fail then
935-
Error("Print formatting status must be true or false");
936-
else
937-
str![2] := stat;
938-
fi;
934+
CheckValidPrintFormattingStatus(stat);
935+
str![2] := stat;
939936
end);
940937

941938

@@ -1130,13 +1127,10 @@ InstallMethod( PrintFormattingStatus, "output text file",
11301127
##
11311128
InstallMethod( SetPrintFormattingStatus, "output text file",
11321129
[IsOutputTextFileRep and IsOutputTextStream,
1133-
IsBool],
1130+
IsObject],
11341131
function( str, stat)
1135-
if stat = fail then
1136-
Error("Print formatting status must be true or false");
1137-
else
1138-
str![3] := stat;
1139-
fi;
1132+
CheckValidPrintFormattingStatus(stat);
1133+
str![3] := stat;
11401134
end);
11411135

11421136
## formatting status for stdout or current output
@@ -1151,7 +1145,7 @@ function(str)
11511145
fi;
11521146
end);
11531147

1154-
InstallOtherMethod( SetPrintFormattingStatus, "for stdout", [IsString, IsBool],
1148+
InstallOtherMethod( SetPrintFormattingStatus, "for stdout", [IsString, IsObject],
11551149
function(str, status)
11561150
if str = "*stdout*" then
11571151
SET_PRINT_FORMATTING_STDOUT(status);
@@ -1255,9 +1249,7 @@ InstallMethod( SetPrintFormattingStatus, "output text none",
12551249
[IsOutputTextNoneRep and IsOutputTextNone,
12561250
IsBool],
12571251
function( str, stat)
1258-
if stat = fail then
1259-
Error("Print formatting status must be true or false");
1260-
fi;
1252+
CheckValidPrintFormattingStatus(stat);
12611253
end);
12621254

12631255

@@ -1676,14 +1668,35 @@ InstallMethod( SetPrintFormattingStatus, "for non-text output stream",
16761668
TryNextMethod();
16771669
fi;
16781670

1679-
if stat = true then
1680-
Error("non-text streams support onlyPrint formatting status false");
1681-
elif stat = fail then
1682-
Error("Print formatting status must be true or false");
1671+
CheckValidPrintFormattingStatus(stat);
1672+
if (( IsBool(stat) and stat = true ) or
1673+
( IsRecord(stat) and (stat.linewrap or stat.indent) ) ) then
1674+
Error("non-text streams do not support print formatting");
16831675
fi;
16841676
end);
16851677

16861678

1679+
InstallGlobalFunction( "CheckValidPrintFormattingStatus",
1680+
function(fs)
1681+
local r;
1682+
if IsBool(fs) then
1683+
if fs = fail then
1684+
Error("Formatting status cannot be 'fail'");
1685+
fi;
1686+
elif IsRecord(fs) then
1687+
if Set(RecNames(fs)) <> ["indent", "linewrap"] then
1688+
Error("Formatting status records must contain only 'indent' and 'linewrap'");
1689+
fi;
1690+
for r in ["indent","linewrap"] do
1691+
if not fs.(r) in [false, true] then
1692+
Error(Concatenation(r, " must be a Boolean in Formatting status"));
1693+
fi;
1694+
od;
1695+
else
1696+
Error("Formatting status must be a boolean or a record");
1697+
fi;
1698+
end);
1699+
16871700
#############################################################################
16881701
##
16891702
#M FileDescriptorOfStream( <iostream-by-pty> )

0 commit comments

Comments
 (0)