sema-fmt

0.1.0

Pretty-printing, table formatting, and tree rendering

$ sema pkg install sema-fmt
Readme Versions 1 Dependencies 0

sema-fmt

Pretty-printing, table formatting, and tree rendering.

Install

sema pkg add sema-fmt

Quick start

(import "sema-fmt")

(fmt/pprint {:users (list {:name "Alice" :role "admin"}
                          {:name "Bob"   :role "user"})
             :count 2}
            {:width 40})
; {:count 2
;  :users ({:name "Alice" :role "admin"}
;          {:name "Bob" :role "user"})}

API

Function Description
(fmt/pp value opts?) Pretty-print to a string. Options: :width (default 80)
(fmt/pprint value opts?) Pretty-print to stdout; same options
(fmt/table rows opts?) Format a list of maps as an ASCII table. Options: :columns
(fmt/tree label children) Render a tree with box-drawing characters
(fmt/bullet items) Render a bulleted list
(fmt/indent s n) Indent every line of s by n spaces

fmt/pp

(fmt/pp (list 1 2 3))
; => "(1 2 3)"

(fmt/pp {:xs (list 1 2 3)} {:width 10})
; {:xs (1
;       2
;       3)}

Returns a string. Collections that fit within :width columns stay on one line; otherwise they break with items aligned under the opening bracket. Strings inside the output keep their quotes (with \" and \\ escaped), so (fmt/pp (list "a b" "c")) is the unambiguous "(\"a b\" \"c\")". Map keys render in sorted order.

fmt/pprint

(fmt/pprint {:name "Alice" :age 30})
; {:age 30 :name "Alice"}

Prints (fmt/pp value opts?) to stdout and returns nil.

fmt/table

(println (fmt/table
  (list {:name "Alice" :age 30 :city "Oslo"}
        {:name "Bob"   :age 25 :city "Bergen"})))
; | age | city   | name  |
; |-----|--------|-------|
; |  30 | Oslo   | Alice |
; |  25 | Bergen | Bob   |

(println (fmt/table
  (list {:name "Alice" :age 30 :city "Oslo"}
        {:name "Bob"   :age 25 :city "Bergen"})
  {:columns (list :name :age)}))
; | name  | age |
; |-------|-----|
; | Alice |  30 |
; | Bob   |  25 |

Formats a list of maps, one row per map. Column order defaults to the sorted keys of the first row; pass {:columns (list :name :age)} to choose the columns and their order explicitly (this is also how you include keys missing from the first row). Keys missing from a row render as empty cells. Columns whose present values are all numbers are right-aligned. Empty rows returns "".

fmt/tree

(println (fmt/tree "root"
  (list (list "src" (list "main.sema" "lib.sema"))
        {:label "tests" :children (list "test.sema")})))
; root
; ├── src
; │   ├── main.sema
; │   └── lib.sema
; └── tests
;     └── test.sema

Each node in children is one of:

  • a 2-element list (label children)children is a list of nodes, or nil for a leaf,
  • a map {:label "x" :children (...)}:children optional,
  • a bare value — rendered as a leaf via str.

The three shapes can be mixed at any depth.

fmt/bullet

(println (fmt/bullet (list "First" "Second" "Third")))
; • First
; • Second
; • Third

fmt/indent

(fmt/indent "hello\nworld" 4)
; => "    hello\n    world"

Testing

sema pkg add sema-test   # once
sema tests.sema

License

MIT

VersionSizePublished
0.1.0 5 KB 2026-07-07 21:28:02
No dependencies