On this page:
make-tblock
build-tblock
happend
vappend
superimpose
frame-style/  c
frame
overline
underline
hflip
vflip
8.2

2 Text block combiners and basic operations

procedure

(make-tblock nrow ncol [char])  tblock

  nrow : exact-nonnegative-integer?
  ncol : exact-nonnegative-integer?
  char : char? = #\space
Returns a new tblock with nrow rows and ncol columns filled with the character char.

> (displayln (make-tblock 5 10 #\*))

********** ********** ********** ********** **********

procedure

(build-tblock nrow ncol get-char)  tblock?

  nrow : exact-nonnegative-integer?
  ncol : exact-nonnegative-integer?
  get-char : 
(-> exact-nonnegative-integer?
    exact-nonnegative-integer?
    char?)
Returns a new tblock with nrow rows and ncol columns and calls get-char for each (row, column) to obtain the corresponding character.

> (displayln
   (build-tblock 2 26 (λ (row col) (integer->char
                                    (+ 65 col (* row 32))))))
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz

procedure

(happend [#:align align    
  #:pad-char pad-char]    
  t ...)  tblock?
  align : (or/c 'top 'center 'bottom 'baseline) = 'baseline
  pad-char : char? = #\space
  t : tblock/any
Appends the tblocks t horizontally.

> (displayln (happend "A\nA"
                      " "
                      "B\nB\nB\nB"
                      ":\n:"
                      "C\nC"
                      #:align 'center))
B A B:C A B:C B

procedure

(vappend [#:align align    
  #:pad-char char?    
  #:baseline-of t-bl]    
  t ...)  tblock?
  align : (one-of/c 'left 'center 'right) = 'left
  char? : pad-char = #\space
  t-bl : (or/c #f tblock?) = #f
  t : tblock/any
Appends the tblocks t vertically.

If t-bl is not #f and t-bl is a member of t ... (according to eq?), then the baseline of the returned tblock is on the same line as the baseline of t-bl.

> (displayln (vappend "Twinkle, twinkle,"
                      "Little star,"
                      "How I wonder what you are!"
                      #:align 'center))
Twinkle, twinkle, Little star, How I wonder what you are!

procedure

(superimpose t1 t2 rpos cpos)  tblock?

  t1 : tblock/any
  t2 : block/any
  rpos : 
(or/c exact-integer?
      (-> exact-nonnegative-integer?
          exact-nonnegative-integer?
          exact-integer?)
      'top
      'center
      'bottom)
  cpos : 
(or/c exact-integer?
      (-> exact-nonnegative-integer?
          exact-nonnegative-integer?
          exact-integer?)
      'left
      'center
      'right)
Returns a new tblock where t2 is superimposed over t1. The position rpos can either be a number relative to the top left of t1, a procedure that takes the widths of both t1 and t2 and returns a number relative to the top left of t1, or a symbolic value—and similarly for cpos.

> (displayln (superimpose (make-tblock 8 4 #\.)
                          "AB\nCD"
                          'bottom 'right))
.... .... .... .... .... .... ..AB ..CD
> (displayln (superimpose (make-tblock 8 4 #\.)
                          "AB\nCD"
                          1 'left))
.... AB.. CD.. .... .... .... .... ....
> (displayln (superimpose (make-tblock 10 20 #\.)
                          "AB\nCD"
                          'center
                          (λ (w1 w2) (exact-truncate (* 3/4 (- w1 w2))))))
.................... .................... .................... .................... .............AB..... .............CD..... .................... .................... .................... ....................

value

frame-style/c : contract?

A contract for frame styles. Matches (or/c (one-of/c 'single 'round 'double) (list/c string? string? string?)) where the list of strings is for defining custom styles.

procedure

(frame t [#:style style #:inset inset])  tblock?

  t : tblock/any
  style : frame-style/c = 'single
  inset : exact-nonnegative-integer? = 0
Adds a frame aronud the tblock t.

> (displayln (frame "Someone framed me!\nI swear!"))

┌──────────────────┐ │Someone framed me!│ │I swear! │ └──────────────────┘

A custom style defines all characters of the frame as well as the padding character:
> (displayln (frame (lines->tblock "Someone framed me!\nI swear!"
                                 #:pad-char #\.)
                    #:inset 2
                    #:style '("(+)"
                              "[.]"
                              "{~}")))
(++++++++++++++++++++++) [......................] [......................] [..Someone framed me!..] [..I swear!............] [......................] [......................] {~~~~~~~~~~~~~~~~~~~~~~}

procedure

(overline t)  tblock?

  t : tblock/any
(underline t)  tblock?
  t : tblock/any
Draws a line above or below the text.

> (displayln (happend (overline "hello") " " (underline 'there) "!"))

_____ hello there! ‾‾‾‾‾

procedure

(hflip t)  tblock?

  t : tblock/any
(vflip t)  tblock?
  t : tblock/any
Returns a new tblock like t, flipped either horizontally or vertically.

> (displayln (vflip "ab\nAB"))

AB ab

> (displayln (hflip "ab\nAB"))

ba BA