diff options
Diffstat (limited to 'libs/fstring.neb')
| -rw-r--r-- | libs/fstring.neb | 73 |
1 files changed, 57 insertions, 16 deletions
diff --git a/libs/fstring.neb b/libs/fstring.neb index 32ce56c..f4967d6 100644 --- a/libs/fstring.neb +++ b/libs/fstring.neb @@ -18,41 +18,82 @@ (branch ((eq? "}" (first str)) (list (rest str) (->string (eval (first (parse-neb cur)))))) + ;(list (rest str) (eval (first (parse-neb cur))))) ((eq? "," (first str)) (.special-parse (rest str) "" (->string (eval (first (parse-neb cur)))))) + ;(.special-parse (rest str) "" (eval (first (parse-neb cur))))) (#true (.brace-parse (rest str) (concat cur (first str)))))) +(func .fill-parse (str) + (if + (or + (eq? 0 (length str)) + (in? (first str) (split "^<>")) ; these are aligns + (try + (block + (string->int (first str)) ; these are widths + #true) + #false)) + (list str " ") + (list (rest str) (first str)))) + +(func .align-parse (str) + (if (in? (first str) (split "^<>")) + (list (rest str) (first str)) + (list str ">"))) + +(func .number-parse (str cur) + (def ret + (try + (string->int (first str)) + _panic_)) + (if (eq? :int (typeof ret)) + (.number-parse (rest str) (concat cur (first str))) + (branch + ((eq? 0 (length cur)) (list str 0)) + (#true (list str (string->int cur)))))) + (func .special-parse (str cur val) (if (eq? "}" (first str)) (block - ; [align][width] (to start) - (def align-width - (if (in? (first cur) (split "^<>")) - (list (first cur) (string->int (rest cur))) - (list "<" (string->int cur)))) + + ; ,[[fill]align][width] + ; where fill cannot be a number or ^<> + + ; there's gotta be a more elegant way to do this + (def fill-parse (.fill-parse cur)) + (def fill (last fill-parse)) + (def align-parse (.align-parse (first fill-parse))) + (def align (last align-parse)) + (def width-parse (.number-parse (first align-parse) "")) + (def width (last width-parse)) + (branch - ((>= (length val) (last align-width)) + ((>= (length val) width) (list (rest str) val)) - ((eq? "<" (first align-width)) ; align left - (list (rest str) (concat val (.repeat (- (last align-width) (length val)))))) - ((eq? ">" (first align-width)) ; align right - (list (rest str) (concat (.repeat (- (last align-width) (length val))) val))) + ((eq? "<" align) ; align left + (list (rest str) (concat val (.repeat (- width (length val)) fill)))) + ((eq? ">" align) ; align right + (list (rest str) (concat (.repeat (- width (length val)) fill) val))) (#true ; align center (block - (def half (/ (- (last align-width) (length val)) 2)) + (def half (/ (- width (length val)) 2)) (if (int? half) - (list (rest str) (concat (.repeat half) val (.repeat half))) - (list (rest str) (concat (.repeat (floor half)) val (.repeat (+ 1 (floor half)))))))))) + (list (rest str) (concat (.repeat half fill) val (.repeat half fill))) + (list (rest str) (concat (.repeat (floor half) fill) val (.repeat (+ 1 (floor half)) fill)))))))) (.special-parse (rest str) (concat cur (first str)) val))) -(func .repeat (cnt) +(func .repeat (cnt) (.repeat cnt " ")) + +(func .repeat (cnt char) (def out "") (for-count cnt - (redef out (concat out " "))) + (redef out (concat out char))) out) + (func fmt :string (inp :string) - ; { <neb code> [, [[^<>][:digits:]]]} + ; { <neb code> , [[fill character][^<>]][:digits:]} (.fmt-parse inp "")) |
