-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatted OutStreams #1816
Open
markuspf
wants to merge
2
commits into
gap-system:master
Choose a base branch
from
markuspf:formatted-out-stream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+210
−0
Open
Formatted OutStreams #1816
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Formatted output streams | ||
|
||
## <#GAPDoc Label="OutputTextStreamFormatter"> | ||
## <ManSection> | ||
## <Oper Name="OutputTextStreamFormatter" Arg='stream, indent'/> | ||
## <Oper Name="OutputTextStreamFormatter" Arg='stream'/> | ||
## | ||
## <Description> | ||
## returns an output stream that outputs received characters to the | ||
## stream <A>stream</A>, adding indentation by a number of spaces | ||
## after every newline character. | ||
## | ||
## The parameter <A>indent</A> specifie s an initial indentation, which defaults | ||
## to <M>0</M>. | ||
## | ||
## Printing the character <C>\\≤</C> to the stream decreases indentation, | ||
## printing <C>\\≥</C> increases indentation. | ||
## | ||
## <P/> | ||
## <Example><![CDATA[ | ||
## gap> output := OutputTextStreamFormatter(StandardOutput); | ||
## gap> WriteAll(output, "Hello, world\n"); | ||
## Hello, world | ||
## true | ||
## gap> WriteAll(output, "\>\>Hello, world\nSecond line\nthird\>\>\nfourth\n"); | ||
## Hello, world | ||
## Second line | ||
## third | ||
## fourth | ||
## true | ||
## ]]></Example> | ||
## </Description> | ||
## </ManSection> | ||
## <#/GAPDoc> | ||
## | ||
# Output stream wrapping another output stream | ||
DeclareCategory( "IsOutputTextStreamFormatter", IsOutputStream ); | ||
DeclareRepresentation( "IsOutputTextStreamFormatterRep", | ||
IsOutputTextStreamFormatter and IsPositionalObjectRep, | ||
["stream", "indent"] ); | ||
|
||
BindGlobal( "OutputTextStreamFormatterType", | ||
NewType( StreamsFamily, | ||
IsOutputTextStream and IsOutputTextStreamFormatterRep ) ); | ||
|
||
DeclareOperation( "OutputTextStreamFormatter", [ IsOutputStream, IsInt] ); | ||
DeclareOperation( "OutputTextStreamFormatter", [ IsOutputStream ] ); | ||
DeclareOperation( "SetIndentation", [ IsOutputTextStreamFormatter, IsInt ] ); | ||
DeclareOperation( "GetIndentation", [ IsOutputTextStreamFormatter ] ); | ||
|
||
## <#GAPDoc Label="OutputTextStreamPrefixer"> | ||
## <ManSection> | ||
## <Oper Name="OutputTextStreamPrefixer" Arg='stream, prefix'/> | ||
## | ||
## <Description> | ||
## returns an output stream that outputs received characters to the | ||
## stream <A>stream</A>, adding <A>prefix</A> in front of every | ||
## line. | ||
## | ||
## <P/> | ||
## <Example><![CDATA[ | ||
## gap> output := OutputTextStreamPrefixer(StandardOutput, "informational: ");; | ||
## gap> WriteAll(output, "Hello,\nworld"); | ||
## informational: Hello, | ||
## informational: worldtrue | ||
## ]]></Example> | ||
## </Description> | ||
## </ManSection> | ||
## <#/GAPDoc> | ||
## | ||
# Output stream prefixing every line | ||
DeclareCategory( "IsOutputTextStreamPrefixer", IsOutputStream ); | ||
DeclareRepresentation( "IsOutputTextStreamPrefixerRep", | ||
IsOutputTextStreamPrefixer and IsPositionalObjectRep, | ||
["stream", "prefix"] ); | ||
|
||
BindGlobal( "OutputTextStreamPrefixerType", | ||
NewType( StreamsFamily, | ||
IsOutputTextStream and IsOutputTextStreamPrefixerRep ) ); | ||
|
||
DeclareOperation( "OutputTextStreamPrefixer", [ IsOutputStream, IsString] ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Formatting output streams | ||
|
||
InstallMethod(OutputTextStreamFormatter, | ||
"for a stream, and an int", | ||
[ IsOutputStream, IsInt ], | ||
function( stream, ident ) | ||
local i; | ||
|
||
if ident < 0 then | ||
Error("initial identation has to be >= 0"); | ||
fi; | ||
return Objectify( OutputTextStreamFormatterType, [ stream, ident ] ); | ||
end); | ||
|
||
InstallOtherMethod( OutputTextStreamFormatter, | ||
"for an output stream", | ||
[ IsOutputStream ], | ||
function( stream ) | ||
return OutputTextStreamFormatter(stream, 0); | ||
end ); | ||
|
||
InstallMethod( ViewString, | ||
"for an output stream formatter", | ||
[ IsOutputTextStreamFormatterRep ], | ||
function( obj ) | ||
return "<wrapped output stream>"; | ||
end ); | ||
|
||
InstallMethod( WriteAll, | ||
"for an output stream formatter", | ||
[ IsOutputTextStream and IsOutputTextStreamFormatterRep, | ||
IsString ], | ||
function( stream, string ) | ||
local b; | ||
|
||
WriteAll(stream![1], ListWithIdenticalEntries(stream![2], ' ')); | ||
for b in string do | ||
WriteByte(stream, INT_CHAR(b)); | ||
od; | ||
return true; | ||
end ); | ||
|
||
InstallMethod( WriteByte, | ||
"output text string", | ||
[ IsOutputTextStream and IsOutputTextStreamFormatterRep, | ||
IsInt ], | ||
function( stream, byte ) | ||
if byte < 0 or 255 < byte then | ||
Error( "<byte> must an integer between 0 and 255" ); | ||
fi; | ||
if byte = INT_CHAR('\<') then | ||
if stream![2] > 0 then | ||
stream![2] := stream![2] - 1; | ||
fi; | ||
elif byte = INT_CHAR('\>') then | ||
stream![2] := stream![2] + 1; | ||
else | ||
WriteByte(stream![1], byte); | ||
if byte = INT_CHAR('\n') then | ||
WriteAll(stream![1], ListWithIdenticalEntries(stream![2], ' ')); | ||
fi; | ||
return true; | ||
fi; | ||
end ); | ||
|
||
InstallMethod( GetIndentation, | ||
"for an output stream formatter", | ||
[ IsOutputTextStreamFormatterRep ], | ||
x -> x![2] ); | ||
|
||
InstallMethod( SetIndentation, | ||
"for an output stream formatter", | ||
[ IsOutputTextStreamFormatterRep, IsInt ], | ||
function(stream, ident) | ||
if ident < 0 then | ||
Error("Indentation has to be >= 0"); | ||
fi; | ||
stream![2] := ident; | ||
return stream![2]; | ||
end); | ||
|
||
# prefix output | ||
InstallMethod( OutputTextStreamPrefixer, | ||
"for a stream, and an int", | ||
[ IsOutputStream, IsString ], | ||
function( stream, prefix ) | ||
return Objectify( OutputTextStreamPrefixerType, [ stream, prefix ] ); | ||
end); | ||
|
||
InstallMethod( ViewString, | ||
"for an output stream formatter", | ||
[ IsOutputTextStreamPrefixerRep ], | ||
function( obj ) | ||
return Concatenation("<wrapped output stream, prefixing ", obj![2], ">"); | ||
end ); | ||
|
||
InstallMethod( WriteAll, | ||
"for an output stream formatter", | ||
[ IsOutputTextStream and IsOutputTextStreamPrefixerRep, | ||
IsString ], | ||
function( stream, string ) | ||
local b; | ||
|
||
WriteAll(stream![1], stream![2]); | ||
for b in string do | ||
WriteByte(stream, INT_CHAR(b)); | ||
od; | ||
return true; | ||
end ); | ||
|
||
InstallMethod( WriteByte, | ||
"output text string", | ||
[ IsOutputTextStream and IsOutputTextStreamPrefixerRep, | ||
IsInt ], | ||
function( stream, byte ) | ||
if byte < 0 or 255 < byte then | ||
Error( "<byte> must an integer between 0 and 255" ); | ||
fi; | ||
WriteByte(stream![1], byte); | ||
if byte = INT_CHAR('\n') then | ||
WriteAll(stream![1], stream![2]); | ||
fi; | ||
return true; | ||
end ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No GAPDoc chunks with these names exist, hence the
makemanuals
tests fail.