1. Yapp (yet another pretty print)

I've just cleaned up my version of pretty print.

If anyone wants to give it a whirl let me know if there is anything
you don't like: There's not much benefit if you throw it a short
string, it needs a big table type thing to shine.

Pete

--
-- Yapp. Yet another pretty print
-- Author Pete Lomax
--
-- usage(1): prnt(object,nest)	outputs to screen, breaks lines longer
--								than 78 characters, pauses every 23=20
--								lines. See examples for use of "nest".
--
-- usage(2): prntEx(file,object,maxlen,pause,format,nest)
--
-- where file 	specifies the output, typically 1 to screen, or a file
--				opened by the calling application, or if 0 is passed,=20
--				the result is left (with embedded \n characters) in=20
--				the global sequence pres,
-- 		 object is the item to be printed,
--		 maxlen is the line length to split long lines, typically 78=20
--				(the default if 0 is passed)
--		 pause  is the number of lines displayed between pauses,=20
--				typically 23, or if 0 is passed it never pauses, and
--       format 0 prints strings as is, 1 number only, 3 number&text.
--				 			  eg "abc"  {97, 98, 99}   {97a, 98b, 99c}
--		 nest	indicates nesting levels to force new lines eg:
--
-- obj=3D{1,{2,{3,3},2},1}
--	  nest=3D0
--    prnt(obj,nest) gives:=20
--=20
-- {1, {2, {3, 3}, 2}, 1}
--
-- same, with nest=3D1 gives:
--
-- { 1,
--   { 2, { 3, 3}, 2},
--   1}
--
-- same, with nest=3D2 gives:
--
-- { 1,
--   { 2,
--     { 3, 3},
--     2},
--   1}
--
-- nest may also be passed as a sequence of {indent,nest_level}.
--  In the above examples, note how the 1, {, and 1 display in the=20
--  same column. If an indent level is specified, spaces are inserted=20
--  in the "{1" to maintain alignment.
--
-- eg same, with nest=3D{3,2} gives:
--
-- {  1,
--    {  2,
--       {  3, 3},
--       2},
--    1}
--
-- same, with nest=3D{1,2} gives:
--
-- {1,
--  {2,
--   {3, 3},
--   2},
--  1}
--
-- The default indent level is 2, unless the atom 0 is passed
-- it is one (see the first example)
--
-- Also, specifying a nest-level of -1 suppresses the trailing \n
--
without trace

integer maxlen		-- break lines longer than this
integer nindent		-- indent increment when nesting
integer pause		-- pause display after this many lines
integer format		-- 0=3Dtext as strings,=20
					-- 1 as numbers,=20
					-- 3 as txt&number

sequence pline				-- output line, as built by sput()
integer  plen	plen=3D0		-- used part of pline; rest is garbage
integer  sline	sline=3D0		-- counter for screen line

--
-- bundle "puts(1," calls together and output one line at a time...
--
integer ofile
global sequence pres	-- print result if file is passed as 0

procedure spurge()
	if ofile then
		puts(ofile,pline[1..plen])
	else
		pres&=3Dpline[1..plen]
	end if
	plen=3D0
end procedure

procedure sput(object txt)
integer p
	p=3Dplen+1
	if atom(txt) then
		plen+=3D1
	else
		plen+=3Dlength(txt)
	end if
	if plen>length(pline) then
		pline&=3Drepeat(0,plen-length(pline))
	end if
	pline[p..plen]=3Dtxt
	if pline[plen]=3D'\n' then
		spurge()
		sline+=3D1+(plen>maxlen)
		if pause and sline >=3D pause then
			if getc(0) then end if
			sline=3D0
		end if
	end if
end procedure


without warning -- suppress short-circuit warning
function prnf(object cl, integer col, integer indent, integer prnt,
integer break)
integer len
integer aschar
sequence sep,txt
	if sequence(cl) then
		if format=3D0 then
			aschar=3Dlength(cl)
			for i=3D1 to length(cl) do
				if sequence(cl[i]) or cl[i]<' ' or cl[i]>'~' then=20
					aschar=3D0=20
					exit=20
				end if
			end for
			if aschar then
				if prnt then sput('\"'&cl&'\"') end if
				return length(cl)+2
			end if
		end if
		len=3Dnindent=09
		if prnt then sput('{'&repeat(' ',nindent-1)) end if
		sep=3D""
		for i=3D1 to length(cl) do
			if (i>1 and break>0)
			or prnf(cl[i],col+len,col+nindent,0,break-1)+
			   col+len+length(sep)+2>=3Dmaxlen then
				if not prnt then return maxlen+1 end if
				sput(",\n")
				sput(repeat(' ',indent+nindent))
				len=3Dnindent
				col=3Dindent
				sep=3D""
			end if
			if prnt then sput(sep) end if
			len+=3Dlength(sep)
			len+=3Dprnf(cl[i],col+len,col+nindent,prnt,break-1)
			sep=3D", "
		end for
		if prnt then sput('}') end if
		return len+1
	end if
	if integer(cl) then
		if format=3D3 and cl>=3D' ' and cl<=3D'~' then
			txt=3Dsprintf("%d%s",{cl,cl})
		else
			txt=3Dsprintf("%d",cl)
		end if
	else
		txt=3Dsprintf("%3.2f",cl)
	end if
	if prnt then sput(txt) end if
	return length(txt)
end function
with warning


global procedure prntEx(integer file, object o, integer maxln,=20
						integer pauze, integer fmt, object nest)
-- file: 1 for screen, 0 leaves result in global sequence 'pres'.
-- o is the object to be printed
-- maxln (defaults to 78 if 0 is passed) for splitting long lines
-- pauze (suggested 23, or 0 for no pausing) prompts every n lines
-- fmt 0 prints text strings as eg "abc",=20
--     1 as {97, 98, 99},=20
--     3 as {97a, 98b, 99c}
-- nest	indicates nesting levels to force new lines,
--      or can be passed as {indent, nest_level}
--
	ofile=3Dfile
	if maxln=3D0 then maxln=3D78 end if
	maxlen=3Dmaxln
	pline=3Drepeat(9,maxlen+1)	-- space for \n
	pause=3Dpauze
	format=3Dfmt
	pres=3D""
	nindent=3D2
	if sequence(nest) then
		nindent=3Dnest[1]
		nest=3Dnest[2]
	elsif nest=3D0 then
		nindent=3D1
	end if
	if prnf(o,0,0,1,nest) then end if
	if nest>=3D0 then=20
		sput("\n")
	elsif plen then
		spurge()
	end if
end procedure
=09
global procedure prnt(object o, object nest)
	prntEx(1,o,78,23,0,nest)
end procedure

new topic     » topic index » view message » categorize

2. Re: Yapp (yet another pretty print)

Hello Pete, you wrote:

> I've just cleaned up my version of pretty print.
>
> If anyone wants to give it a whirl let me know if there is anything
> you don't like: There's not much benefit if you throw it a short
> string, it needs a big table type thing to shine.
>
> Pete

It's very nice. With your version it is e.g. possible, to print a big
table in a way, so that much information can be seen at the same time,
which makes comparison much easier (faster and more reliable). It seems
to be near to the version of pretty print, I've been looking for.
But: Without putting it on the User Contributions Page, you can't get
Micro $$ for it. smile

<snip>

> global procedure prntEx(integer file, object o, integer maxln,
> 						integer pauze, integer fmt, object nest)
> -- file: 1 for screen, 0 leaves result in global sequence 'pres'.
> -- o is the object to be printed
> -- maxln (defaults to 78 if 0 is passed) for splitting long lines
> -- pauze (suggested 23, or 0 for no pausing) prompts every n lines
> -- fmt 0 prints text strings as eg "abc",
> --     1 as {97, 98, 99},
> --     3 as {97a, 98b, 99c}
> -- nest	indicates nesting levels to force new lines,
> --      or can be passed as {indent, nest_level}
> --
> 	ofile=file
> 	if maxln=0 then maxln=78 end if
> 	maxlen=maxln
> 	pline=repeat(9,maxlen+1)	-- space for \n
> 	pause=pauze
> 	format=fmt
> 	pres=""
> 	nindent=2
                ^
I changed this to 1.

> 	if sequence(nest) then
> 		nindent=nest[1]
> 		nest=nest[2]
> 	elsif nest=0 then
> 		nindent=1
> 	end if
> 	if prnf(o,0,0,1,nest) then end if
> 	if nest>=0 then
> 		sput("\n")
> 	elsif plen then
> 		spurge()
> 	end if
> end procedure
> 	
> global procedure prnt(object o, object nest)
                    ^
I changed this to pp. smile

> 	prntEx(1,o,78,23,0,nest)
> end procedure

I don't have the time to make extensive tests at the moment. But I
will use your version on a regular basis, instead of the official
pretty_print(), to gather some experience.

Many thanks, and best regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |    |\      _,,,---,,_
 \ /  against HTML in       |    /,`.-'`'    -.  ;-;;,_
  X   e-mail and news,      |   |,4-  ) )-,_..;\ (  `'-'
 / \  and unneeded MIME     |  '---''(_/--'  `-'\_)

new topic     » goto parent     » topic index » view message » categorize

3. Re: Yapp (yet another pretty print)

On Mon, 19 May 2003 22:35:58 +0200, Juergen Luethje <j.lue at gmx.de>
wrote:

>
>Hello Pete, you wrote:
>
>> I've just cleaned up my version of pretty print.
>>
>> If anyone wants to give it a whirl let me know if there is anything
>> you don't like: There's not much benefit if you throw it a short
>> string, it needs a big table type thing to shine.
>>
>> Pete
>
>It's very nice. With your version it is e.g. possible, to print a big
>table in a way, so that much information can be seen at the same time,
>which makes comparison much easier (faster and more reliable).
If you use prnt(object,{1,0})  it should cram almost to the character,
but without mucking up indent and/or line wrapping too much.
I don't want to bash Rob's pretty print, but I wrote this precisely
because his version didn't put enuf on one screen for my taste.
I've put a space after each comma, do you want an option to drop that?
>It seems to be near to the version of pretty print, I've been looking =
for.
>But: Without putting it on the User Contributions Page, you can't get
>Micro $$ for it. smile
Its alpha, if people like it, it'll end up on there. Besides, if Rob
likes it, I won't need micro$ blink)
>
><snip>
>> 	nindent=3D2
>                ^
>I changed this to 1.
Yep, or use {1,0}. It is only a default, if people want 1 as the
default instead, no problem
>
>> global procedure prnt(object o, object nest)
>                    ^
>I changed this to pp. smile
>
>> 	prntEx(1,o,78,23,0,nest)
>> end procedure
Is there a name clash or was that just personal preference?
>
>I don't have the time to make extensive tests at the moment. But I
>will use your version on a regular basis, instead of the official
>pretty_print(), to gather some experience.
Cuh! You've had nine hours already! You just can't get the staff these
days blink)) Seriously, though, thats _exactly_ what I want you to do.

Thanks,
Pete

new topic     » goto parent     » topic index » view message » categorize

4. Re: Yapp (yet another pretty print)

Hello Pete, you wrote:

> On Mon, 19 May 2003 22:35:58 +0200, Juergen Luethje <j.lue at gmx.de>
> wrote:
>
>>
>> Hello Pete, you wrote:
>>
>>> I've just cleaned up my version of pretty print.

<snip>

> If you use prnt(object,{1,0})  it should cram almost to the character,
> but without mucking up indent and/or line wrapping too much.
> I don't want to bash Rob's pretty print, but I wrote this precisely
> because his version didn't put enuf on one screen for my taste.
> I've put a space after each comma, do you want an option to drop that?

I personally like it the way it is. With a space after each comma,
I can perceive especially big tables faster and more reliable than
without. Maybe other people want an option to drop that. On the other
hand, to keep it simple, IMHO there shouldn't be too many options.

<snip>

>>> global procedure prnt(object o, object nest)
>>                    ^
>> I changed this to pp. smile
>>
>>> 	prntEx(1,o,78,23,0,nest)
>>> end procedure
> Is there a name clash or was that just personal preference?

Just personal preference. "pp" was on the tip of my tongue as acronym
for "pretty print", but mainly I like it, if frequently used names are
short and simple to type.

>> I don't have the time to make extensive tests at the moment. But I
>> will use your version on a regular basis, instead of the official
>> pretty_print(), to gather some experience.
> Cuh! You've had nine hours already! You just can't get the staff these
> days blink))

You can say that again, it's scandalous! blink))

> Seriously, though, thats _exactly_ what I want you to do.

Okey-dokey.

> Thanks,
> Pete

Best regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |    |\      _,,,---,,_
 \ /  against HTML in       |    /,`.-'`'    -.  ;-;;,_
  X   e-mail and news,      |   |,4-  ) )-,_..;\ (  `'-'
 / \  and unneeded MIME     |  '---''(_/--'  `-'\_)

new topic     » goto parent     » topic index » view message » categorize

5. Re: Yapp (yet another pretty print)

I wrote:

> Hello Pete, you wrote:

 <snip>

>> I've put a space after each comma, do you want an option to drop that?
>
> I personally like it the way it is. With a space after each comma,
> I can perceive especially big tables faster and more reliable than
> without. Maybe other people want an option to drop that. On the other
> hand, to keep it simple, IMHO there shouldn't be too many options.

 <snip>

Another idea. I just got this output ...

{{1, 2, 3, 4}, {1, 2, 4, 3}, {1, 3, 2, 4}, {1, 3, 4, 2}, {1, 4, 2, 3},
 {1, 4, 3, 2}, {2, 1, 3, 4}, {2, 1, 4, 3}, {2, 3, 1, 4}, {2, 3, 4, 1},
 {2, 4, 1, 3}, {2, 4, 3, 1}, {3, 1, 2, 4}, {3, 1, 4, 2}, {3, 2, 1, 4},
 {3, 2, 4, 1}, {3, 4, 1, 2}, {3, 4, 2, 1}, {4, 1, 2, 3}, {4, 1, 3, 2},
 {4, 2, 1, 3}, {4, 2, 3, 1}, {4, 3, 1, 2}, {4, 3, 2, 1}}

... which is not so clear for me after all. Without a space after each
comma, it would be:

{{1,2,3,4},{1,2,4,3},{1,3,2,4},{1,3,4,2},{1,4,2,3},
 {1,4,3,2},{2,1,3,4},{2,1,4,3},{2,3,1,4},{2,3,4,1},
 {2,4,1,3},{2,4,3,1},{3,1,2,4},{3,1,4,2},{3,2,1,4},
 {3,2,4,1},{3,4,1,2},{3,4,2,1},{4,1,2,3},{4,1,3,2},
 {4,2,1,3},{4,2,3,1},{4,3,1,2},{4,3,2,1}}

Also not too clear, IMHO.
How about putting a space after each comma, except in the innermost level
of a sequence? smile  That would give:

{{1,2,3,4}, {1,2,4,3}, {1,3,2,4}, {1,3,4,2}, {1,4,2,3},
 {1,4,3,2}, {2,1,3,4}, {2,1,4,3}, {2,3,1,4}, {2,3,4,1},
 {2,4,1,3}, {2,4,3,1}, {3,1,2,4}, {3,1,4,2}, {3,2,1,4},
 {3,2,4,1}, {3,4,1,2}, {3,4,2,1}, {4,1,2,3}, {4,1,3,2},
 {4,2,1,3}, {4,2,3,1}, {4,3,1,2}, {4,3,2,1}}


Best regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  | "Everything should be made as simple
 \ /  against HTML in       |  as possible, but not simpler."
  X   e-mail and news,      |
 / \  and unneeded MIME     |  http://www.sfheart.com/einstein.html

new topic     » goto parent     » topic index » view message » categorize

6. Re: Yapp (yet another pretty print)

On Thu, 22 May 2003 18:30:36 +0200, Juergen Luethje <j.lue at gmx.de>
wrote:

<snip>
>How about putting a space after each comma, except in the innermost =
level
>of a sequence? smile

Sure, just put this around line 163(ish):

if atom(cl[i]) and (i=3Dlength(cl) or atom(cl[i+1])) then
	sep=3D","
else
			sep=3D", "
end if

Pete

new topic     » goto parent     » topic index » view message » categorize

7. Re: Yapp (yet another pretty print)

Hello Pete, you wrote:

> On Thu, 22 May 2003 18:30:36 +0200, Juergen Luethje <j.lue at gmx.de>
> wrote:
>
> <snip>
>> How about putting a space after each comma, except in the innermost leve=
l
>> of a sequence? smile
>
> Sure, just put this around line 163(ish):
>
> if atom(cl[i]) and (i=3Dlength(cl) or atom(cl[i+1])) then
> 	sep=3D","
> else
> 			sep=3D", "
> end if
>
> Pete

Thank you, that works fine!

I think I've found a bug. It was alredy there, before I applied the
modification mentioned above. Please try:

-------=3D-------------------------=3D-------
include yapp.e
sequence s

s =3D {{{"blue", "red"}, {"cyan", "white"}},
     {{"blue", "red"}, {"cyan", "white"}}}

prnt(s, 2)
puts(1, "---------------------------\n")
prnt(s, 3)
-------=3D-------------------------=3D-------

Unfortunately, I don't have sufficient understanding of the algorithm,
to tweak it myself.


                 ~~~=B0~~~=B0~~~=B0~~~=B0~~~


Another suggestion. The current version doesn't print special characters
above ASCII 126. The German language has the "umlauts"
(196,214,220,228,246,252) and the "sz" (223, looking like the Greek
"beta"). There are also special characters in other languages, such as
Spanish, French, Danish, Czech, etc.

Therefore, in function prnf() I removed
    or cl[i]>'~'
    and cl<=3D'~'

I believe this is not critical, because AFAIK the characters above ASCII
126 are no control characters, which might change the output in an
unwanted way (e.g. like Carriage Return).
The only disadvantage I'm aware of, is that this increases the chance,
that a sequence, that is meant as "array" of numbers, unintentionally is
printed as a string.

On the other hand, without this change, not all strings are printed as
strings, and (at least in the German language) there are *quite many*
words containing those special characters. So if you don't like my
"radical" blink solution, IMHO at least there should be an option for the
procedure prntEx(), to achieve this behaviour (like the appropriate
option in pretty_print() by RDS).

Thanks again for these pretty procedures.

Best regards,
   Juergen

--=20
 /"\  ASCII ribbon campain  |
 \ /  against HTML in       |  This message has been ROT-13 encrypted
  X   e-mail and news,      |  twice for higher security.
 / \  and unneeded MIME     |

new topic     » goto parent     » topic index » view message » categorize

8. Re: Yapp (yet another pretty print)

On Fri, 23 May 2003 13:38:07 +0200, Juergen Luethje <j.lue at gmx.de>
wrote:

>I think I've found a bug. It was alredy there, before I applied the
>modification mentioned above. Please try:
>
>s =3D {{{"blue", "red"}, {"cyan", "white"}},
>     {{"blue", "red"}, {"cyan", "white"}}}
>
>prnt(s, 2)
>puts(1, "---------------------------\n")
>prnt(s, 3)
>
>Unfortunately, I don't have sufficient understanding of the algorithm,
>to tweak it myself.
I may have to think about this one a while.

>Another suggestion. The current version doesn't print special characters
>above ASCII 126.
<snip>
>Therefore, in function prnf() I removed
>    or cl[i]>'~'
>    and cl<=3D3D'~'
No, replace them with #FF instead. I'll make that the default.

Thanks,
Pete

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu