Adding word wrap vocab

db4
Daniel Ehrenberg 2008-01-31 11:51:38 -06:00
parent cea24feaa9
commit 9121354116
1 changed files with 30 additions and 0 deletions

30
extra/wrap/wrap.factor Normal file
View File

@ -0,0 +1,30 @@
USING: sequences kernel namespaces splitting math ;
IN: wrap
! Very stupid word wrapping/line breaking
! This will be replaced by a Unicode-aware method,
! which works with variable-width fonts
SYMBOL: width
: line-chunks ( string -- words-lines )
"\n" split [ " \t" split [ empty? not ] subset ] map ;
: (split-chunk) ( words -- )
-1 over [ length + 1+ dup width get > ] find drop nip
[ cut-slice swap , (split-chunk) ] [ , ] if* ;
: split-chunk ( words -- lines )
[ (split-chunk) ] { } make ;
: broken-lines ( string width -- lines )
width [
line-chunks
[ split-chunk [ " " join ] map ] map concat
] with-variable ;
: line-break ( string width -- newstring )
broken-lines "\n" join ;
: indented-break ( string width indent -- newstring )
[ length - broken-lines ] keep [ swap append ] curry map "\n" join ;