wrap.strings: allow wrap-indented-string to have a numbered indent provided.

db4
John Benediktsson 2013-07-27 21:17:37 -07:00
parent 2f2e04ca45
commit 8198a6f547
3 changed files with 22 additions and 9 deletions

View File

@ -14,14 +14,14 @@ ARTICLE: "wrap.strings" "String word wrapping"
} ;
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." } ;

View File

@ -14,7 +14,7 @@ word wrap."""
"""This is a long piece of text that we wish to word wrap.""" 10
wrap-string
] unit-test
[
""" This is a
long piece
@ -27,6 +27,11 @@ word wrap."""
" " 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

View File

@ -1,6 +1,6 @@
! 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
@ -19,11 +19,19 @@ IN: wrap.strings
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 ;