Skip to content

Commit 4b384b6

Browse files
author
Jason Duncan
committed
Merge remote-tracking branch 'origin/melpa'
2 parents a7c3440 + b3da869 commit 4b384b6

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

TODO.org

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
** DONE Some docstrings are quite long which lets you pass package-lint and makes for good enough M-x apropos output, but they wrap poorly in M-x help.
2+
** DONE "delimiater" -> "delimiter"
3+
** DONE Since you're using Emacs 25 you can use the string-trim functions (could format-table-trim-row be rewritten as just (string-trim row begin-row end-row)?
4+
** DONE Could the first conditional in format-table-remove-noise be written either as:
5+
6+
#+BEGIN_SRC emacs-lisp
7+
(unless (or (string-equal "" cur-line)
8+
(and regexp (string-match regexp cur-line)))
9+
(push cur-line ret))
10+
#+END_SRC
11+
12+
*** or, even more aggressively (perhaps even using string-blank-p instead of string-empty-p?):
13+
14+
#+BEGIN_SRC emacs-lisp
15+
(and (not (string-empty-p cur-line))
16+
(not (and regexp (string-match regexp cur-line)))
17+
(push cur-line ret))
18+
#+END_SRC
19+
20+
*** NOTE The version using `and' is slightly more concise, but I believe the compromise I made between the two suggestions to be more readable.
21+
*** NOTE2 I'm wary of going all the way to string-blank-p as I wouldn't want to break potential compatibility with a table format which uses whitespace in a way I hadn't thought of.
22+
23+
** DONE Could the last conditional in format-table-remove-noise be written with (when ret ...) instead of (if (not ret) nil ...)?
24+
** DONE Rather than
25+
26+
#+BEGIN_SRC emacs-lisp
27+
(let* ((lines (split-string str "[
28+
29+
]+"))...)
30+
#+END_SRC
31+
32+
which incidentally contains two newlines, why not:
33+
34+
#+BEGIN_SRC emacs-lisp
35+
(let* ((lines (split-string str "[\n]+")) ...)
36+
#+END_SRC

format-table.el

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@
7373
(json . json)))
7474

7575
(defun format-table-pad-right (value length)
76-
"Pad the string VALUE to the length specified by LENGTH using spaces to the right."
76+
"Pad the string VALUE to the LENGTH specified using spaces to the right."
7777
(format (concat "%-" (number-to-string length) "s") value))
7878

7979
(defun format-table-pad-center (value length)
80-
"Pad the string VALUE to the length specified by LENGTH by surrounding with spaces."
80+
"Pad the string VALUE to the LENGTH specified by surrounding with spaces."
8181
(let* ((value-length (length value))
8282
(left-length (/ (- length value-length) 2))
8383
(right-length (+ left-length
@@ -93,44 +93,42 @@
9393
(make-string right-length ? ))))
9494

9595
(defun format-table-remove-noise (lines input-mode)
96+
"Remove lines which constitute noise, such as empty lines or results count.
97+
LINES is the list of source lines, split on newlines. INPUT-MODE is used to
98+
determine what the results count should look like."
9699
"Given the set of table LINES and some extra information in INPUT-MODE, filter out any empty lines or lines which otherwise do not belong to the table of values."
97100
(let* ((row-count-format (plist-get input-mode :row-count-format))
98-
(regexp (if (not row-count-format) nil (format row-count-format "[[:digit:]]+")))
101+
(regexp (when row-count-format (format row-count-format "[[:digit:]]+")))
99102
ret)
100103
(dolist (cur-line (reverse lines) ret)
101-
(if (not (or (string-equal "" cur-line)
102-
(if (not regexp) nil (string-match regexp cur-line))))
103-
(push cur-line ret)))
104-
(if (not ret) nil
104+
(unless (or (string-empty-p cur-line)
105+
(and regexp (string-match regexp cur-line)))
106+
(push cur-line ret)))
107+
(when ret
105108
(setq ret (if (plist-get input-mode :top-border-fn) (-slice ret 1) ret))
106109
(if (plist-get input-mode :top-border-fn)
107110
(-slice ret 0 (1- (length ret))) ret))))
108111

109-
(defun format-table-trim-row (row begin-row end-row)
110-
"Given the string ROW trim the string BEGIN-ROW from the beginning and END-ROW from the end."
111-
(replace-regexp-in-string
112-
(concat "^" (regexp-quote begin-row))
113-
""
114-
(replace-regexp-in-string
115-
(concat (regexp-quote end-row) "$")
116-
""
117-
row)))
118-
119112
(defun format-table-get-col-widths (dashes input-mode)
120-
"Using the line of all DASHES from the table and the INPUT-MODE, determine the widths of the columns and return them in a list."
121-
(let* ((separator-begin-row (plist-get input-mode :separator-begin-row))
122-
(separator-end-row (plist-get input-mode :separator-end-row))
113+
"Determine the widths of each column in the table.
114+
DASHES is a string containing the row from the source table which separates the
115+
header from the body of the table. INPUT-MODE is used to determine what column
116+
separators and such should look like."
117+
(let* ((separator-begin-row (regexp-quote (plist-get input-mode :separator-begin-row)))
118+
(separator-end-row (regexp-quote (plist-get input-mode :separator-end-row)))
123119
(separator-col-separator (plist-get input-mode :separator-col-separator))
124-
(dashes (format-table-trim-row dashes separator-begin-row separator-end-row)))
120+
(dashes (string-trim dashes separator-begin-row separator-end-row)))
125121
(-map #'length (split-string dashes
126122
(regexp-quote separator-col-separator)))))
127123

128124
(defun format-table-split-row (row col-widths input-mode)
129-
"Split the given string ROW based on the fixed positions listed in COL-WIDTHS and any additional information in INPUT-MODE."
125+
"Split the given string ROW based on the fixed positions listed in COL-WIDTHS.
126+
INPUT-MODE is used to determine what beginning of row, end of row, and column
127+
separators should look like."
130128
(let* ((begin-row (plist-get input-mode :begin-row))
131129
(end-row (plist-get input-mode :end-row))
132130
(col-separator (plist-get input-mode :col-separator))
133-
(row (format-table-trim-row row begin-row end-row))
131+
(row (string-trim row begin-row end-row))
134132
split)
135133
(reverse
136134
(dolist (cur-width col-widths split)
@@ -145,29 +143,28 @@
145143
cur-width))))))))
146144

147145
(defun format-table-assemble-table (header body)
148-
"Given the HEADER list of column names and nested list of values BODY, return an alist with both and some extra meta information about same."
146+
"Assemble the HEADER and BODY of the table into an alist with some extra info."
149147
(let ((max-col-widths (format-table-get-max-col-widths (cons header body))))
150148
(list :header header
151149
:body body
152150
:max-col-widths max-col-widths
153151
:row-count (length body))))
154152

155153
(defun format-table-parse-table (lines col-widths input-mode)
156-
"Given the list of table LINES, set of COL-WIDTHS, and INPUT-MODE, build a table of values as a plist."
154+
"Parse the list of table LINES into a plist."
157155
(let* ((header (format-table-split-row (car lines) col-widths input-mode))
158156
(body (--map (format-table-split-row it col-widths input-mode) (-slice lines 2))))
159157
(format-table-assemble-table header body)))
160158

161159
(defun format-table-get-max-col-widths (table)
162-
"Given the nested list TABLE of values, determine the length of the longest value in each column and return each in a list."
160+
"List the length of the longest value in each column from TABLE."
163161
(let ((last (make-list (length (car table)) 0)))
164162
(dolist (cur-row table last)
165163
(setq last
166164
(-zip-with #'max (-map #'length cur-row) last)))))
167165

168166
(defun format-table-render-row (row max-col-widths output-mode &optional pad-fn)
169167
"Render a table row with the proper column separators and a newline.
170-
171168
Arguments are the list of values ROW, the list of MAX-COL-WIDTHS, and delimiter
172169
information in OUTPUT-MODE. Optionally use PAD-FN to pad each column value,
173170
otherwise values will be padded to the right with spaces."
@@ -186,7 +183,9 @@ otherwise values will be padded to the right with spaces."
186183
(make-string length ?-))
187184

188185
(defun format-table-render-separator-row (max-col-widths output-mode)
189-
"Given the list of MAX-COL-WIDTHS and delimiter information in OUTPUT-MODE, render a row which separates the header row from the rest of the rows."
186+
"Render a row which separates the header row from the rest of the rows.
187+
Columns will be spaced accrding to MAX-COL-WIDTHS and delimited using strings
188+
from OUTPUT-MODE."
190189
(append
191190
(list
192191
(plist-get output-mode :separator-begin-row))
@@ -205,7 +204,7 @@ otherwise values will be padded to the right with spaces."
205204
(json-encode vec)))
206205

207206
(defun format-table-render-table (table output-mode)
208-
"Given the TABLE of values and delimiter information in OUTPUT-MODE, re-render the table as a string."
207+
"Re-render the TABLE of values as a string using delimiter information in OUTPUT-MODE."
209208
(if (equal output-mode 'json)
210209
(list (format-table-render-json table))
211210
(let ((top-border-fn (plist-get output-mode :top-border-fn))
@@ -251,18 +250,20 @@ otherwise values will be padded to the right with spaces."
251250
(format-table-assemble-table header body)))
252251

253252
(defun format-table-cleanup-and-parse (str input-mode)
254-
"Parse the given string STR using delimiater information in INPUT-MODE to a table of values as a plist."
253+
"Parse the given STR using delimiter information in INPUT-MODE to a plist."
255254
(if (equal input-mode 'json)
256255
(format-table-parse-json str)
257-
(let* ((lines (split-string str "[
258-
]+"))
256+
(let* ((lines (split-string str "[\n]+"))
259257
(lines (format-table-remove-noise lines input-mode)))
260-
(if (not lines) nil
258+
(when lines
261259
(let* ((col-widths (format-table-get-col-widths (car (cdr lines)) input-mode)))
262260
(format-table-parse-table lines col-widths input-mode))))))
263261

264262
(defun format-table (str input-mode output-mode)
265-
"Process the given string STR containing a table in a format specified by INPUT-MODE, gather and reformat the table contained within to the format specified by OUTPUT-MODE."
263+
"Reformat tabular data.
264+
Process the given string STR containing a table in a format specified by
265+
INPUT-MODE, gather and reformat the table contained within to the format
266+
specified by OUTPUT-MODE."
266267
(let* ((input-mode (format-table-get-format input-mode))
267268
(output-mode (format-table-get-format output-mode))
268269
(table (format-table-cleanup-and-parse str input-mode)))

0 commit comments

Comments
 (0)