From 5a2800fb0e25ce72250f3684cb20beb3dbf180c2 Mon Sep 17 00:00:00 2001 From: danilomo Date: Sat, 25 Jan 2025 12:18:06 +0100 Subject: [PATCH] Adds option to omit headers and style factory method for custom separator without borders --- src/table/core.clj | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/table/core.clj b/src/table/core.clj index 8f8f40e..4280840 100644 --- a/src/table/core.clj +++ b/src/table/core.clj @@ -1,6 +1,6 @@ (ns table.core (:require table.width) - (:use [clojure.string :only [join]] )) + (:use [clojure.string :only [join blank?]] )) (declare style-for format-cell render-rows-with-fields escape-newline render-rows table-str) @@ -18,8 +18,11 @@ :top-dash "─" :dash "─" :bottom-dash "═" :header-walls ["│ " " │ " " ║"] :body-walls ["│ " " │ " " ║"] } :github-markdown {:top ["" "" ""] :middle ["|-" " | " "-|"] :bottom ["" "" ""] - :top-dash "" :dash "-" :bottom-dash "" :header-walls walls :body-walls walls } - }) + :top-dash "" :dash "-" :bottom-dash "" :header-walls walls :body-walls walls }}) + +(defn with-separator [separator] + {:top ["" "" ""] :middle ["" "" ""] :bottom ["" "" ""] + :top-dash "" :dash "" :bottom-dash "" :header-walls ["" "" ""] :body-walls ["" separator ""] }) (defn table "Generates an ascii table for almost any input that fits in your terminal. @@ -47,6 +50,7 @@ "Returns rows and fields. Rows are a vector of vectors containing string cell values." [table options] (let [ + skip-header (:skip-header options) top-level-vec (not (coll? (first table))) fields (cond top-level-vec [:value] @@ -54,7 +58,8 @@ (distinct (vec (flatten (map keys table))))) (map? table) [:key :value] :else (first table)) - rows (cond + rows (cond + skip-header table top-level-vec (map #(vector %) table) (map? (first table)) (map #(map (fn [k] (get % k)) fields) table) (map? table) table @@ -79,6 +84,7 @@ (defn- render-rows-with-fields [rows fields options] (let [ + skip-header (:skip-header options) headers (map #(if (keyword? %) (name %) (str %)) fields) widths (table.width/get-widths (cons headers rows)) fmt-row (fn [row] @@ -96,8 +102,10 @@ header (wrap-row headers (style-for :header-walls)) body (map #(wrap-row (fmt-row %) (style-for :body-walls)) rows) ] - (concat [(border-for :top :top-dash) header (border-for :middle :dash)] - body [( border-for :bottom :bottom-dash)]))) + (remove blank? (concat [(border-for :top :top-dash)] + (if skip-header [] [header (border-for :middle :dash)]) + body + [( border-for :bottom :bottom-dash)])))) (defn- escape-newline [string] (clojure.string/replace string (str \newline) (char-escape-string \newline)))