print print primitives

Author: Chris Zheng  (z@caudate.me)
Date: 27 November 2018
Repository: https://github.com/zcaudate/hara
Version: 3.0.2

1    Introduction

hara.print provides methods for printing.

1.1    Installation

Add to project.clj dependencies:

[hara/base "3.0.2"]

All functions are in the hara.print namespace.

 (use (quote hara.print))

2    API



print-column ^

prints the column

v 3.0
(defn print-column
  [items name color]
  (let [ns-len  (or (->> items (map (comp count str first)) sort last)
                    20)
        display {:padding 1
                 :spacing 1
                 :columns [{:id :key   :length (inc ns-len) :align :left}
                           {:id name  :length 60 :color color}]}]
    (print-header [:key name] display)
    (doseq [[key m] items]
      (print-row [key (get m name)] display))
    (print "n")))
link
(-> (print-column [[:id.a {:data 100}] [:id.b {:data 200}]] :data #{}) (with-out-str))

print-compare ^

outputs a side by side comparison

v 3.0
(defn print-compare
  [output]
  (let [ns-len  (or (->> output (map (comp count str first)) sort last)
                    20)
        item-params {:padding 1
                     :spacing 1
                     :columns [{:id :ns    :length (inc ns-len) :align :left}
                               {:id :data  :length 50 :color #{:highlight}}
                               {:id :data  :length 50 :color #{:magenta}}]}]
    (doseq [[ns [src test]] output]
      (print-row [ns src (if (empty? test)
                           :no-tests
                           test)]
                 item-params))
    (print "n")))
link
(-> (print-compare [['hara.code [[:a :b :c] [:d :e :f]]]]) (with-out-str))

print-header ^

prints a header for the row

v 3.0
(defn print-header
  [titles {:keys [padding columns] :as params}]
  (let [pad (pad padding)
        header (-> (apply str
                          (mapv (fn [title {:keys [align length]}]
                                  (str pad
                                       (justify align
                                                (name title)
                                                length)))
                                titles
                                columns))
                   (ansi/style #{:bold}))]
    (println header)
    (print "n")))
link
(-> (print-header [:id :name :value] {:padding 0 :spacing 1 :columns [{:align :right :length 10} {:align :center :length 10} {:align :left :length 10}]}) (with-out-str))

print-row ^

prints a row to output

v 3.0
(defn print-row
  [row params]
  (apply mapv println (prepare-elements row params)))
link
(-> (print-row ["hello" :world (result/result {:data [:a :b :c :d :e :f] :status :info})] {:padding 0 :spacing 1 :columns [{:align :right :length 10} {:align :center :length 10} {:align :left :length 10}]}) (with-out-str))

print-subtitle ^

prints the subtitle

v 3.0
(defn print-subtitle
  [text]
  (print (ansi/style text #{:bold}) "n"))
link
(-> (print-subtitle "Hello Again") (with-out-str))

print-summary ^

outputs the summary of results

v 3.0
(defn print-summary
  [m]
  (let [ks (sort (keys m))]
    (print (ansi/style (str "SUMMARY " m "n")  #{:bold}))))
link
(-> (print-summary {:count 6 :files 2}) (with-out-str))

print-title ^

prints the title

v 3.0
(defn print-title
  [title]
  (let [line (apply str (repeat (count title) -))]
    (print (ansi/style (string/format "n%sn%sn%sn" line title line)
                       #{:bold}))))
link
(-> (print-title "Hello World") (with-out-str))

print-tree ^

outputs the result of `format-tree`

v 3.0
(defn print-tree
  ([tree]
   (print-tree tree "" nil?))
  ([tree prefix check]
   (let [output (->> (format-tree tree prefix check)
                     (string/split-lines))
         min-spaces (apply min (map (fn [s] (-> (re-find #"^(s*)" s)
                                                first
                                                count))
                                    output))
         output (->> output
                     (map #(subs % min-spaces))
                     (map (fn [s] (if (.startsWith s " ")
                                    (ansi/green "" s)
                                    (ansi/bold  "n" s)))))]
     (println (string/joinl output "n")))))
link
(print-tree '[{a "1.1"} [{b "1.2"} [{c "1.3"} {d "1.4"}]]])

progress ^

creates a structure representing progress

v 3.0
(defn progress
  ([]
   (progress nil))
  ([initial]
   (progress initial nil))
  ([initial options]
   (let [now (/ (System/nanoTime) 1000000000)
         default {:start-time  now
                  :update-time now
                  :total 100
                  :current 0
                  :label ""}]
     (Progress. (merge +progress-defaults+ options)
                (atom (merge default initial))))))
link
(-> (progress) :state deref) => (contains {:total 100, :current 0, :label ""})

progress-string ^

creates a string representation of the current progress

v 3.0
(defn progress-string
  ([progress]
   (progress-string @(:state progress)
                    (:options progress)))
  ([{:keys [total current label] :as state} {:keys [template bar] :as options}]
   (format "%s[%s] %ss %s"
           (if-not (empty? label) (str label " ") "")
           (progress-bar-string current total bar)
           (progress-eta total current (- (:update-time state)
                                          (:start-time state)))
           (progress-spinner-string current total))))
link
(progress-string (-> @(:state (progress)) (update :update-time + 10) (update :current + 9)) +progress-defaults+) => "[===== 9/100 ] 101s -"

progress-update ^

updates the progress meter

v 3.0
(defn progress-update
  ([progress]
   (progress-update progress 1))
  ([progress number]
   (swap! (:state progress)
          (fn [{:keys [total current] :as state}]
            (let [now (/ (System/nanoTime) 1000000000)
                  current (+ current number)
                  current (if (< current total)
                            current
                            total)]
              (-> state
                  (assoc :update-time now
                         :current current)))))))
link
(progress-update (progress) 10)