annotate src/print.sml @ 679:44f23712020d

Chat example working nicely, but without dead channel removal
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Mar 2009 18:26:50 -0400
parents 73456bfde988
children fd34210bc3e5
rev   line source
adamc@1 1 (* Copyright (c) 2008, Adam Chlipala
adamc@1 2 * All rights reserved.
adamc@1 3 *
adamc@1 4 * Redistribution and use in source and binary forms, with or without
adamc@1 5 * modification, are permitted provided that the following conditions are met:
adamc@1 6 *
adamc@1 7 * - Redistributions of source code must retain the above copyright notice,
adamc@1 8 * this list of conditions and the following disclaimer.
adamc@1 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@1 10 * this list of conditions and the following disclaimer in the documentation
adamc@1 11 * and/or other materials provided with the distribution.
adamc@1 12 * - The names of contributors may not be used to endorse or promote products
adamc@1 13 * derived from this software without specific prior written permission.
adamc@1 14 *
adamc@1 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@1 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@1 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@1 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@1 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@1 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@1 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@1 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@1 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@1 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@1 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@1 26 *)
adamc@1 27
adamc@244 28 (* Generic printing support code *)
adamc@1 29
adamc@1 30 structure Print :> PRINT = struct
adamc@1 31
adamc@1 32 structure SM = TextIOPP
adamc@1 33 structure PD = PPDescFn(SM)
adamc@1 34
adamc@1 35 type 'a printer = 'a -> PD.pp_desc
adamc@1 36
adamc@1 37 fun box ds = PD.hovBox (PD.PPS.Rel 1, ds)
adamc@1 38 fun parenIf b d =
adamc@1 39 if b then
adamc@1 40 box [PD.string "(", d, PD.string ")"]
adamc@1 41 else
adamc@1 42 d
adamc@1 43 val space = PD.space 1
adamc@1 44
adamc@1 45 val out = SM.openOut {dst = TextIO.stdOut, wid = 70}
adamc@1 46 val err = SM.openOut {dst = TextIO.stdErr, wid = 70}
adamc@1 47
adamc@1 48 fun p_list_sep sep f ls =
adamc@1 49 case ls of
adamc@1 50 [] => PD.string ""
adamc@1 51 | [x] => f x
adamc@1 52 | x :: rest =>
adamc@1 53 let
adamc@1 54 val tokens = foldr (fn (x, tokens) =>
adamc@223 55 sep :: PD.cut :: f x :: tokens)
adamc@1 56 [] rest
adamc@1 57 in
adamc@1 58 box (f x :: tokens)
adamc@1 59 end
adamc@1 60 fun p_list f = p_list_sep (box [PD.string ",", space]) f
adamc@1 61
adamc@275 62 fun p_list_sepi sep f ls =
adamc@275 63 case ls of
adamc@275 64 [] => PD.string ""
adamc@275 65 | [x] => f 0 x
adamc@275 66 | x :: rest =>
adamc@275 67 let
adamc@275 68 val tokens = ListUtil.foldri (fn (n, x, tokens) =>
adamc@275 69 sep :: PD.cut :: f (n + 1) x :: tokens)
adamc@275 70 [] rest
adamc@275 71 in
adamc@275 72 box (f 0 x :: tokens)
adamc@275 73 end
adamc@275 74
adamc@1 75 fun fprint f d = (PD.description (f, d);
adamc@1 76 PD.PPS.flushStream f)
adamc@1 77 val print = fprint out
adamc@1 78 val eprint = fprint err
adamc@1 79
adamc@1 80 fun fpreface f (s, d) =
adamc@1 81 fprint f (PD.hovBox (PD.PPS.Rel 0,
adamc@5 82 [PD.string s, PD.space 1, d, PD.newline]))
adamc@1 83
adamc@1 84 val preface = fpreface out
adamc@1 85 val epreface = fpreface err
adamc@1 86
adamc@3 87 fun fprefaces f s ls =
adamc@3 88 let
adamc@3 89 val len = foldl (fn ((s, _), best) =>
adamc@3 90 Int.max (size s, best)) 0 ls
adamc@3 91 in
adamc@3 92 fprint f (PD.string s);
adamc@3 93 fprint f PD.newline;
adamc@3 94 app (fn (s, d) =>
adamc@3 95 let
adamc@3 96 val s = CharVector.tabulate (len - size s,
adamc@3 97 fn _ => #" ")
adamc@3 98 ^ s ^ ": "
adamc@3 99 in
adamc@3 100 fpreface f (s, d)
adamc@3 101 end) ls
adamc@3 102 end
adamc@3 103
adamc@3 104 val prefaces = fprefaces out
adamc@3 105 val eprefaces = fprefaces err
adamc@3 106
adamc@3 107 fun fprefaces' f ls =
adamc@1 108 let
adamc@1 109 val len = foldl (fn ((s, _), best) =>
adamc@1 110 Int.max (size s, best)) 0 ls
adamc@1 111 in
adamc@1 112 app (fn (s, d) =>
adamc@1 113 let
adamc@1 114 val s = CharVector.tabulate (len - size s,
adamc@1 115 fn _ => #" ")
adamc@1 116 ^ s ^ ": "
adamc@1 117 in
adamc@1 118 fpreface f (s, d)
adamc@1 119 end) ls
adamc@1 120 end
adamc@1 121
adamc@3 122 val prefaces' = fprefaces' out
adamc@3 123 val eprefaces' = fprefaces' err
adamc@1 124
adamc@1 125 end