Search code examples
clojure

Print Table from DataBase in Clojure


my goal is to print my Table as close with even spaces between each col.

(defn PrintTable [tableName]
  "prints table in clear format"
  (let [tableRef (get (deref dataBase) tableName) ; get refrence for table
        keyList (keys @tableRef)] ; get key list of table
    (doseq [tableKeys (range (count keyList))] ; print the keys of the table
      (let [key (nth (keys @tableRef) tableKeys)]
        (print key "\t|"))
    )
    (println)
    (doseq [rows (range (count @(tableRef (nth (keys @tableRef) 0))))] ; print for each rows all the values
      (doseq [cols (range (count keyList))]
        (let [key (nth (keys @tableRef) cols)]
          (print (@(tableRef key) rows) "\t|")
        )
      )
      (println)
    )
  )
  (println)
)

i have tried using tab however this is the out come i get:

P_Id    |LastName   |FirstName  |Address    |City   |
1   |Darmon     |Gilad  |ishayahu   |Haifa  |
2   |SM     |Shiran     |erez   |RamatIshay     |

D_Id    |Name   |OwnerLastName  |OwnerFirstName     |
a   |Bono   |Darmon     |Gilad  |
b   |Bony   |SM     |Shiran     |

any suggestion for a nicer and aligned print?


Solution

  • use format to make the cols line up:

    user> (println (format "%20s %20s %20s\n%20s %20s %20s" 
                     "short" "medium" "reallylong" 
                     "reallylong" "medium" "short"))
    
                   short               medium           reallylong
              reallylong               medium                short
    nil
    user> 
    

    or left justify with %-20s

    user> (println (format "%-20s %-20s %-20s\n%-20s %-20s %-20s" 
                            "short" "medium" "reallylong" 
                            "reallylong" "medium" "short")) 
    
    short                medium               reallylong 
    reallylong           medium               short 
    nil 
    user>