]> gitweb.factorcode.org Git - factor.git/commitdiff
wrap.strings: allow wrap-indented-string to have a numbered indent provided.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 28 Jul 2013 04:17:37 +0000 (21:17 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 28 Jul 2013 04:18:10 +0000 (21:18 -0700)
basis/wrap/strings/strings-docs.factor
basis/wrap/strings/strings-tests.factor
basis/wrap/strings/strings.factor

index c8c08d3db16e043cb4b693697ac6f58f2a13900e..adce6f0d5569c2b4cce091bc164968dd56209f33 100644 (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." } ;
 
index b9abedc4c455dac9c63061731857afc739904b23..bf6fb6749b10132d10f24a38c3889ff4ae62c794 100644 (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
 
index f5ab7774442b03e6497df06be62d2e5a001736b9..0825edcc4bdbfb8cf88f3dc91e4633224c6fc797 100644 (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 ;