Skip to content

Commit

Permalink
Add functions to View, Display, and Code (formerly Print) to a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
markuspf committed Sep 1, 2016
1 parent 8f5e7f4 commit 1e011df
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/read1.g
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ReadLib( "info.gd" );
ReadLib( "assert.gd" );
ReadLib( "files.gd" );
ReadLib( "streams.gd" );
ReadLib( "show.gd" );

ReadLib( "matobj1.gd" );
ReadLib( "vecmat.gd" );
Expand Down
1 change: 1 addition & 0 deletions lib/read2.g
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ ReadLib( "process.gd" );

ReadLib( "files.gi" );
ReadLib( "streams.gi" );
ReadLib( "show.gi" );
ReadLib( "process.gi" );

67 changes: 67 additions & 0 deletions lib/show.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#############################################################################
##
#W show.gd
##
## Contains the declarations of operations used for outputting objects
## to streams
##

############################################################################
##
#O ViewObjStream(<stream>, <obj>)
##
## <ManSection>
## <Oper Name="ViewObjStream" Arg='stream, obj'/>
##
## <Description>
## <Ref Oper="ViewObjStream"/> prints information about the object <A>obj</A>
## to the stream <A>stream</A>.
## This information should be concise and human readable,
## in particular <E>not</E> necessarily detailed enough for defining <A>obj</A>,
## an in general <E>not</E> &GAP; readable.
## <P/>
## More detailed information can be obtained by <Ref Func="ShowObj"/>
## </Description>
## </ManSection>
##
DeclareOperation("ViewObjStream", [IsOutputStream, IsObject]);

############################################################################
##
#O DisplayObjStream(<stream>, <obj>)
##
## <ManSection>
## <Oper Name="DisplayObjStream" Arg='stream, obj'/>
##
## <Description>
## <Ref Oper="DisplayObjStream"/> prints information about the object
## <A>obj</A> to the stream <A>stream</A> in a nicely formatted way.
## <P/>
## More detailed information can be obtained by <Ref Func="ShowObj"/>
## </Description>
## </ManSection>
## Nicely formatted output
##
DeclareOperation("DisplayObjStream", [IsOutputStream, IsObject]);

############################################################################
##
#O CodeObjStream(<stream>, <obj>)
##
## <ManSection>
## <Oper Name="CodeObjStream" Arg='stream, obj'/>
##
## <Description>
## <Ref Oper="CodeObjStream"/> prints GAP code to recreate the object
## <A>obj</A> to the stream <A>stream</A>.
## <P/>
## More detailed information can be obtained by <Ref Func="ShowObj"/>
## </Description>
## </ManSection>
##
DeclareOperation("CodeObjStream", [IsOutputStream, IsObject]);

############################################################################
##
#E

70 changes: 70 additions & 0 deletions lib/show.gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#############################################################################
##
#W show.gi
##
## Contains implementations of standard functions for outputting objects
## to streams.
##
## These functions should replace all the View/Print/Display variants, and
## in particular get rid of a lot of code duplication, when there are separate
## implementations for View and ViewString for instance, and lack of
## implementations when View exists, but ViewString doesn't (probably for the
## reason above)
##
## output streams will be free to ignore/strip formatting helpers
##
## The naming is still topic of discussion. I renamed Print to Code, because
## this makes the requirement to output Code clearer.
##

############################################################################
##
#O ViewObjStream(<stream>, <obj>)
##
InstallMethod(ViewObjStream, "for an output stream, and an object",
[IsOutputStream, IsObject],
function(stream, obj)
PrintTo(stream, ViewString(obj));
end);

# A possible implementation for ViewString
# (once ViewObjStream is the default)
NewViewString := function(obj)
local res, stream;
res := "";
stream := OutputTextString(res, true);
ViewObjStream(stream, obj);
return res;
end;

# A possible implementation for View if ViewObjStream has a method installed
NewView := function(obj)
local stream;
stream := OutputTextFile("*stdout*", true);
ViewObjStream(stream, obj);
end;

############################################################################
##
#O DisplayObjStream(<stream>, <obj>)
##
InstallMethod(DisplayObjStream, "for an output stream, and an object",
[IsOutputStream, IsObject],
function(stream, obj)
PrintTo(stream, DisplayString(obj));
end);

############################################################################
##
#O CodeObjStream(<stream>, <obj>)
##
InstallMethod(CodeObjStream, "for an output stream, and an object",
[IsOutputStream, IsObject],
function(stream, obj)
PrintTo(stream, String(obj));
end);

############################################################################
##
#E

0 comments on commit 1e011df

Please sign in to comment.