Skip to content

A peer-to-peer stack for code collaboration

Extract pad-to from issue/diff app (#518)

Merle Breitkreuz34bddd42571a
diff --git a/bin/rad-diff b/bin/rad-diff
index 57fcf4f..a5017c4 100755
--- a/bin/rad-diff
+++ b/bin/rad-diff
@@ -167,21 +167,10 @@
:author 20
:modified-at 20})
-(def pad-to
- (fn [l x]
- (if (< l 1)
- ""
- (do
- (def pad (string/intercalate "" (map (fn [x] " ") (range 1 l))))
- (def x-length (length x))
- (if (> x-length (- l 1))
- (take l x)
- (string-append x (take (- l x-length) pad)))))))
-
(def wrap-cell
(fn [phrase cell-key]
(def c-length (lookup cell-key cells))
- (string-append (pad-to c-length phrase) " ")))
+ (string-append (take c-length (string/pad-right-to c-length phrase)) " ")))
(def pretty-table-header
(fn []
@@ -200,12 +189,12 @@
:state s
:author-name an
:modified-at m-a}
- (string-append
- (pretty-state s) " "
- (show n) " "
- tit " "
- an " "
- m-a " "))))
+ (string/unwords
+ [(pretty-state s)
+ (show n)
+ tit
+ an
+ m-a]))))
(def pretty-row
(fn [i]
diff --git a/bin/rad-issue b/bin/rad-issue
index e5b518e..1b3877d 100755
--- a/bin/rad-issue
+++ b/bin/rad-issue
@@ -84,21 +84,10 @@
:author 20
:modified-at 20})
-(def pad-to
- (fn [l x]
- (if (< l 1)
- ""
- (do
- (def pad (string/intercalate "" (map (fn [x] " ") (range 1 l))))
- (def x-length (length x))
- (if (> x-length (- l 1))
- (take l x)
- (string-append x (take (- l x-length) pad)))))))
-
(def wrap-cell
(fn [phrase cell-key]
(def c-length (lookup cell-key cells))
- (string-append (pad-to c-length phrase) " ")))
+ (string-append (take c-length (string/pad-right-to c-length phrase)) " ")))
(def pretty-table-header
(fn []
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index aa10e1f..0fbe642 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -785,6 +785,21 @@ sequence to ``rest-pat``. Also works for strings.
Concatenate a sequence of sequences.
+``prelude/list``
+----------------
+
+Functions for creating lists. See also ``prelude/seq``.
+
+``nil``
+~~~~~~~
+
+The empty list.
+
+``(range from to)``
+~~~~~~~~~~~~~~~~~~~
+
+Returns a list with all integers from ``from`` to ``to``, inclusive.
+
``prelude/strings``
-------------------
@@ -838,6 +853,12 @@ Reverses ``str``. E.g.: ``(reverse-string "abc")`` == ``"cba"``.
True if ``str`` ends with ``substr``
+``(pad-right-to l word)``
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Appends the ``word`` with whitespace to get to length ``l``. If ``word``
+is longer than ``l``, the whole word is returned without padding.
+
``prelude/dict``
----------------
@@ -1004,21 +1025,6 @@ Ask for user input with a prompt.
Tests for exceptions.
-``prelude/list``
-----------------
-
-Functions for creating lists. See also ``prelude/seq``.
-
-``nil``
-~~~~~~~
-
-The empty list.
-
-``(range from to)``
-~~~~~~~~~~~~~~~~~~~
-
-Returns a list with all integers from ``from`` to ``to``, inclusive.
-
``prelude/set``
---------------
diff --git a/rad/prelude.rad b/rad/prelude.rad
index bdc07de..507f089 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -9,12 +9,12 @@
(import prelude/patterns :unqualified)
(file-module! "prelude/bool.rad")
(file-module! "prelude/seq.rad")
+(file-module! "prelude/list.rad")
(file-module! "prelude/strings.rad")
(file-module! "prelude/dict.rad")
(file-module! "prelude/io.rad")
(file-module! "prelude/maybe.rad")
(file-module! "prelude/exception.rad")
-(file-module! "prelude/list.rad")
(file-module! "prelude/set.rad")
(file-module! "prelude/ref.rad")
(file-module! "prelude/lens.rad")
diff --git a/rad/prelude/machine.rad b/rad/prelude/machine.rad
index 5882033..9a82ea3 100644
--- a/rad/prelude/machine.rad
+++ b/rad/prelude/machine.rad
@@ -139,10 +139,10 @@
"prelude/patterns.rad"
"prelude/bool.rad"
"prelude/seq.rad"
+ "prelude/list.rad"
"prelude/strings.rad"
"prelude/dict.rad"
"prelude/maybe.rad"
- "prelude/list.rad"
"prelude/set.rad"
"prelude/ref.rad"
"prelude/lens.rad"
diff --git a/rad/prelude/strings.rad b/rad/prelude/strings.rad
index eaf9749..b991ce4 100644
--- a/rad/prelude/strings.rad
+++ b/rad/prelude/strings.rad
@@ -1,10 +1,11 @@
{:module 'prelude/strings
:doc "String manipulation functions."
:exports '[intercalate unlines unwords split-by words lines map-string
- reverse-string ends-with?] }
+ reverse-string ends-with? pad-right-to] }
(import prelude/basic :unqualified)
(import prelude/seq :unqualified)
+(import prelude/list :unqualified)
(import prelude/patterns :unqualified)
(def intercalate
@@ -107,6 +108,26 @@
[ (lines " \n \t") ==> [" " " \t"]]
[ (lines "") ==> []])
+;; pad-to
+
+(def pad-right-to
+ "Appends the `word` with whitespace to get to length `l`. If `word` is longer
+ than `l`, the whole word is returned without padding."
+ (fn [l word]
+ (def w-length (length word))
+ (if (< (- l 1) w-length)
+ word
+ (do
+ (def pad (intercalate "" (map (fn [x] " ") (range 1 l))))
+ (string-append word (take (- l w-length) pad))))))
+
+(:test "pad-right-to"
+ [ (pad-right-to 0 "foobar") ==> "foobar"]
+ [ (pad-right-to 3 "foobar") ==> "foobar"]
+ [ (pad-right-to 6 "") ==> " "]
+ [ (pad-right-to 6 "foobar") ==> "foobar"]
+ [ (pad-right-to 7 "foobar") ==> "foobar "])
+
;; map-string
(def map-string
diff --git a/reference-doc.yaml b/reference-doc.yaml
index d95aef5..3603281 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -136,11 +136,11 @@ modules:
- prelude/patterns
- prelude/bool
- prelude/seq
+- prelude/list
- prelude/strings
- prelude/dict
- prelude/io
- prelude/exception
-- prelude/list
- prelude/set
- prelude/ref
- prelude/lens