} ;
HELP: wrap-lines
-{ $values { "lines" string } { "width" integer } { "newlines" "sequence of strings" } }
-{ $description "Given a string, divides it into a sequence of lines where each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
+{ $values { "string" string } { "width" integer } { "newlines" "sequence of strings" } }
+{ $description "Given a " { $snippet "string" } ", divides it into a sequence of lines where each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
HELP: wrap-string
{ $values { "string" string } { "width" integer } { "newstring" string } }
-{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
+{ $description "Given a " { $snippet "string" } ", alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
HELP: wrap-indented-string
-{ $values { "string" string } { "width" integer } { "indent" string } { "newstring" string } }
-{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space. Before each line, the indent string is added." } ;
+{ $values { "string" string } { "width" integer } { "indent" "string or integer" } { "newstring" string } }
+{ $description "Given a " { $snippet "string" } ", alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space. The " { $snippet "indent" } " can be either a " { $link string } " or a number of spaces to prepend to each line." } ;
"""This is a long piece of text that we wish to word wrap.""" 10
wrap-string
] unit-test
-
+
[
""" This is a
long piece
" " wrap-indented-string
] unit-test
+{ t } [
+ """This is a long piece of text that we wish to word wrap.""" 12
+ [ " " wrap-indented-string ] [ 2 wrap-indented-string ] 2bi =
+] unit-test
+
[ "this text\nhas lots of\nspaces" ]
[ "this text has lots of spaces" 12 wrap-string ] unit-test
! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
-USING: wrap kernel sequences fry splitting math ;
+USING: fry kernel math sequences splitting strings wrap ;
IN: wrap.strings
<PRIVATE
PRIVATE>
-: wrap-lines ( lines width -- newlines )
+: wrap-lines ( string width -- newlines )
[ split-lines ] dip '[ _ dup wrap join-elements ] map! concat ;
: wrap-string ( string width -- newstring )
wrap-lines join-lines ;
+<PRIVATE
+
+: make-indent ( indent -- indent' )
+ dup string? [ CHAR: \s <string> ] unless ; inline
+
+PRIVATE>
+
: wrap-indented-string ( string width indent -- newstring )
- [ length - wrap-lines ] keep '[ _ prepend ] map! join-lines ;
+ make-indent [ length - wrap-lines ] keep
+ '[ _ prepend ] map! join-lines ;