~technomancy/polywell

1581a23f03378b4ad64b2b8bbe012450b340cc9a — Phil Hagelberg 8 months ago e63a211
Update fennelview to support metamethods.
1 files changed, 28 insertions(+), 8 deletions(-)

M polywell/lib/fennelview.fnl
M polywell/lib/fennelview.fnl => polywell/lib/fennelview.fnl +28 -8
@@ 7,7 7,7 @@
       {"\a" "\\a" "\b" "\\b" "\f" "\\f" "\n" "\\n"
        "\r" "\\r" "\t" "\\t" "\v" "\\v"})

(local long-control-char-esapes
(local long-control-char-escapes
       (let [long {}]
         (for [i 0 31]
           (let [ch (string.char i)]


@@ 18,7 18,7 @@

(fn escape [str]
  (let [str (: str :gsub "\\" "\\\\")
        str (: str :gsub "(%c)%f[0-9]" long-control-char-esapes)]
        str (: str :gsub "(%c)%f[0-9]" long-control-char-escapes)]
    (: str :gsub "%c" short-control-char-escapes)))

(fn sequence-key? [k len]


@@ 104,13 104,20 @@
      (puts self ":" k)
      (put-value self k)))

(fn put-kv-table [self t]
(fn put-kv-table [self t ordered-keys]
  (puts self "{")
  (set self.level (+ self.level 1))
  (each [k v (pairs t)]
  ;; first, output sorted nonsequential keys
  (each [_ k (ipairs ordered-keys)]
    (tabify self)
    (put-key self k)
    (puts self " ")
    (put-value self (. t k)))
  ;; next, output any sequential keys
  (each [i v (ipairs t)]
    (tabify self)
    (put-key self i)
    (puts self " ")
    (put-value self v))
  (set self.level (- self.level 1))
  (tabify self)


@@ 124,14 131,17 @@
      :else
      (let [(non-seq-keys len) (get-nonsequential-keys t)
            id (get-id self t)]
        (if (> (. self.appearances t) 1)
        ;; fancy metatable stuff can result in self.appearances not including a
        ;; table, so if it's not found, assume we haven't seen it; we can't do
        ;; cycle detection in that case.
        (if (and (. self.appearances t) (> (. self.appearances t) 1))
            (puts self "#<" id ">")
            (and (= (length non-seq-keys) 0) (= (length t) 0))
            (puts self "{}")
            (= (length non-seq-keys) 0)
            (put-sequential-table self t len)
            :else
            (put-kv-table self t)))))
            (put-kv-table self t non-seq-keys)))))

(set put-value (fn [self v]
                 (let [tv (type v)]


@@ 146,11 156,21 @@



(fn one-line [str]
  ;; save return value as local to ignore gsub's extra return value
  (let [ret (-> str
                (: :gsub "\n" " ")
                (: :gsub "%[ " "[") (: :gsub " %]" "]")
                (: :gsub "%{ " "{") (: :gsub " %}" "}")
                (: :gsub "%( " "(") (: :gsub " %)" ")"))]
    ret))

(fn fennelview [root options]
  (let [options (or options {})
        inspector {:appearances (count-table-appearances root {})
                   :depth (or options.depth 128)
                   :level 0 :buffer {} :ids {} :max-ids {}
                   :indent (or options.indent "  ")}]
                   :indent (or options.indent (if options.one-line "" "  "))}]
    (put-value inspector root)
    (table.concat inspector.buffer)))
    (let [str (table.concat inspector.buffer)]
      (if options.one-line (one-line str) str))))