; bugs: ;#16> (fmt "{x} x {") ;exception! name 'ev' is not defined (func .fmt-parse (str cur) (branch ((eq? 0 (length str)) cur) ((eq? "{" (first str)) (block (def tmp (.brace-parse (rest str) "")) (.fmt-parse (first tmp) (concat cur (first (rest tmp)))))) (#true (.fmt-parse (rest str) (concat cur (first str)))))) (func .brace-parse :[:string] (str :string cur :string) ; returns (remaining value) (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 ; ,[[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) width) (list (rest str) 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 (/ (- width (length val)) 2)) (if (int? 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) (.repeat cnt " ")) (func .repeat (cnt char) (def out "") (for-count cnt (redef out (concat out char))) out) (func fmt :string (inp :string) ; { , [[fill character][^<>]][:digits:]} (.fmt-parse inp ""))