fat_table 0.6.4 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.solargraph.yml +22 -0
- data/README.org +34 -29
- data/TODO.org +24 -0
- data/fat_table.gemspec +1 -1
- data/lib/fat_table/column.rb +3 -3
- data/lib/fat_table/convert.rb +10 -8
- data/lib/fat_table/formatters/formatter.rb +93 -74
- data/lib/fat_table/formatters/latex_formatter.rb +17 -15
- data/lib/fat_table/formatters/org_formatter.rb +6 -3
- data/lib/fat_table/formatters/term_formatter.rb +11 -11
- data/lib/fat_table/table.rb +2 -3
- data/lib/fat_table/version.rb +1 -1
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba8791fd0b89302cb5127adc325afd0a84bee337298955ecb48119805bcff53a
|
4
|
+
data.tar.gz: da6f4923fe75df0707c3fc21f96fef52127188e76f8a9ae1b923ea768689d31e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f9b047a93e55f7752173dc7f2c927db2bf45669ede7de1d652b89f1052054f90763c7910b850f84d80ea86a959c82e48f9f9d5b5fe9192ff6b15f9dabb104a7
|
7
|
+
data.tar.gz: 498eb355cce5f5fa8597ad23657d4699cfead201b6bbf5200386f50773c846061199de0373edbe41ff94c79e1a5d90da8c1f9a37d275c580a0a021109c2d8780
|
data/.solargraph.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
---
|
2
|
+
include:
|
3
|
+
- "**/*.rb"
|
4
|
+
exclude:
|
5
|
+
- spec/**/*
|
6
|
+
- test/**/*
|
7
|
+
- vendor/**/*
|
8
|
+
- ".bundle/**/*"
|
9
|
+
require: []
|
10
|
+
domains: []
|
11
|
+
reporters:
|
12
|
+
- rubocop
|
13
|
+
- require_not_found
|
14
|
+
formatter:
|
15
|
+
rubocop:
|
16
|
+
cops: safe
|
17
|
+
except: []
|
18
|
+
only: []
|
19
|
+
extra_args: []
|
20
|
+
require_paths: []
|
21
|
+
plugins: []
|
22
|
+
max_files: 5000
|
data/README.org
CHANGED
@@ -140,7 +140,7 @@ org-mode buffer as an org-table, ready for processing by other code blocks.
|
|
140
140
|
- [[#to-aoa-array-of-arrays][To AoA (Array of Arrays)]]
|
141
141
|
- [[#to-aoh-array-of-hashes][To AoH (Array of Hashes)]]
|
142
142
|
- [[#formatting-directives][Formatting Directives]]
|
143
|
-
- [[#
|
143
|
+
- [[#all-types-as-strings][All Types as Strings]]
|
144
144
|
- [[#numeric][Numeric]]
|
145
145
|
- [[#datetime][DateTime]]
|
146
146
|
- [[#boolean][Boolean]]
|
@@ -2389,7 +2389,7 @@ formats can be defined by adding additional classes.
|
|
2389
2389
|
**** Examples
|
2390
2390
|
***** To Text
|
2391
2391
|
|
2392
|
-
This formatter uses nothing
|
2392
|
+
This formatter uses nothing but ASCII characters to draw the table. Notice
|
2393
2393
|
that, unlike to ~to_org~ formatter shown below, the intersections of lines are
|
2394
2394
|
represented by a ~+~ character. Embelishments such as color, bold, and so
|
2395
2395
|
forth are ignored.
|
@@ -2421,7 +2421,7 @@ characters to draw the table, but, the intersections of lines are represented
|
|
2421
2421
|
by a ~|~ character. Embelishments such as color, bold, and so forth are
|
2422
2422
|
ignored. When working in Org Mode, note that Emacs will convert an Array of
|
2423
2423
|
Arrays into an Org Mode table, so when constructing tables programmatically,
|
2424
|
-
it may be better to use
|
2424
|
+
it may be better to use the =to_aoa= formatter shown below.
|
2425
2425
|
|
2426
2426
|
#+begin_SRC ruby :wrap EXAMPLE
|
2427
2427
|
tab_a.to_org
|
@@ -2528,28 +2528,28 @@ order, so '$R,' and ',$R' are equivalent.
|
|
2528
2528
|
|
2529
2529
|
Here is a list of all the formatting directives that apply to each cell type:
|
2530
2530
|
|
2531
|
-
****
|
2531
|
+
**** All Types as Strings
|
2532
2532
|
|
2533
|
-
For a string element,
|
2534
|
-
|
2535
|
-
|
2533
|
+
For a string element, or any an element of any type (since these are applied
|
2534
|
+
after the element has been converted to a String), the following instructions
|
2535
|
+
are valid.
|
2536
2536
|
|
2537
|
-
- u :: convert the element to all lowercase,
|
2538
|
-
- U :: convert the element to all uppercase,
|
2537
|
+
- u :: convert the element to all lowercase [default false],
|
2538
|
+
- U :: convert the element to all uppercase [default false],
|
2539
2539
|
- t :: title case the element, that is, upcase the initial letter in
|
2540
|
-
each word and lower case the other letters
|
2541
|
-
- B ~B :: make the element bold, or turn off bold
|
2542
|
-
- I ~I :: make the element italic, or turn off italic
|
2543
|
-
- R :: align the element on the right of the column
|
2544
|
-
- L :: align the element on the left of the column
|
2545
|
-
- C :: align the element in the center of the column
|
2540
|
+
each word and lower case the other letters [default false],
|
2541
|
+
- B ~B :: make the element bold, or turn off bold [default ~B]
|
2542
|
+
- I ~I :: make the element italic, or turn off italic [default ~I]
|
2543
|
+
- R :: align the element on the right of the column [default off]
|
2544
|
+
- L :: align the element on the left of the column [default on]
|
2545
|
+
- C :: align the element in the center of the column [default off]
|
2546
2546
|
- c[<color_spec>] :: render the element in the given color; the <color_spec>
|
2547
2547
|
can have the form fgcolor, fgcolor.bgcolor, or .bgcolor, to set the
|
2548
2548
|
foreground or background colors respectively, and each of those can be an
|
2549
2549
|
ANSI or X11 color name in addition to the special color, 'none', which keeps
|
2550
|
-
the output's default color.
|
2551
|
-
- _ ~_ :: underline the element, or turn off underline
|
2552
|
-
- * ~* :: cause the element to blink, or turn off blink
|
2550
|
+
the output's default color [default none].
|
2551
|
+
- _ ~_ :: underline the element, or turn off underline [default off]
|
2552
|
+
- * ~* :: cause the element to blink, or turn off blink [default off]
|
2553
2553
|
|
2554
2554
|
For example, the directive 'tCc[red.yellow]' would title-case the element,
|
2555
2555
|
center it, and color it red on a yellow background. The directives that are
|
@@ -2562,16 +2562,21 @@ particular columns.
|
|
2562
2562
|
For a numeric element, all the instructions valid for string are available, in
|
2563
2563
|
addition to the following:
|
2564
2564
|
|
2565
|
-
- , ~, :: insert grouping commas, or do not insert grouping commas
|
2566
|
-
|
2567
|
-
-
|
2568
|
-
|
2569
|
-
|
2570
|
-
|
2565
|
+
- , ~, :: insert grouping commas, or do not insert grouping commas [default
|
2566
|
+
~,],
|
2567
|
+
- $ ~$ :: format the number as currency according to the locale, or not
|
2568
|
+
[default ~$],
|
2569
|
+
- m.n :: include at least m digits before the decimal point, padding on the
|
2570
|
+
left with zeroes as needed, and round the number to the n decimal places and
|
2571
|
+
include n digits after the decimal point, padding on the right with zeroes
|
2572
|
+
as needed. If n is negative, the value will be rounded to the left of the
|
2573
|
+
decimal point: e.g., if n is -2, the number will be rounded to the nearest
|
2574
|
+
hundred, if -3, to the nearest thousand, etc. [default 0.0]
|
2571
2575
|
- H :: convert the number (assumed to be in units of seconds) to ~HH:MM:SS.ss~
|
2572
|
-
|
2573
|
-
|
2574
|
-
|
2576
|
+
form. So a column that is the result of subtracting two :datetime forms will
|
2577
|
+
result in a :numeric expressed as seconds and can be displayed in hours,
|
2578
|
+
minutes, and seconds with this formatting instruction. If this directive is
|
2579
|
+
included, all other numeric directives will be ignored. [default off]
|
2575
2580
|
|
2576
2581
|
For example, the directive 'R5.0c[blue]' would right-align the numeric
|
2577
2582
|
element, pad it on the left with zeros, and color it blue.
|
@@ -2598,7 +2603,7 @@ For a boolean cell, all the instructions valid for string are available, in
|
|
2598
2603
|
addition to the following:
|
2599
2604
|
|
2600
2605
|
- Y :: print true as ~Y~ and false as ~N~,
|
2601
|
-
- T :: print true as ~T~ and false as ~F
|
2606
|
+
- T :: print true as ~T~ and false as ~F~ [this is the default],
|
2602
2607
|
- X :: print true as ~X~ and false as an empty string '',
|
2603
2608
|
- b[xxx,yyy] :: print true as the string given as ~xxx~ and false as the string
|
2604
2609
|
given as ~yyy~,
|
@@ -2616,7 +2621,7 @@ By default, ~nil~ elements are rendered as blank cells, but you can make them
|
|
2616
2621
|
visible with the following, and in that case, all the formatting instructions
|
2617
2622
|
valid for strings are also available:
|
2618
2623
|
|
2619
|
-
- n[niltext] :: render a ~nil~ item with the given niltext.
|
2624
|
+
- n[niltext] :: render a ~nil~ item with the given niltext [default ''].
|
2620
2625
|
|
2621
2626
|
For example, you might want to use 'n[-]Cc[purple]' to make nils visible as a
|
2622
2627
|
centered purple hyphen.
|
data/TODO.org
CHANGED
@@ -1,4 +1,21 @@
|
|
1
1
|
|
2
|
+
* TODO Explicitly typed columns
|
3
|
+
Having FatTable infer column types from the content of a table is nice, but as
|
4
|
+
I learned from the need to have force_string!, sometimes we want to restrict
|
5
|
+
the type of the column and throw an error if the content does not conform.
|
6
|
+
So, perhaps when the table columns are specified, we can allow them to be
|
7
|
+
terminated with a "sigil" that restricts its type:
|
8
|
+
|
9
|
+
- @ :: at Date or DateTime column,
|
10
|
+
- # :: a Numeric column
|
11
|
+
- ? :: a Boolean column
|
12
|
+
- ~ :: a String Column,
|
13
|
+
- * :: a tolerant Column, i.e., it infers the type from the content, but
|
14
|
+
allows strings to be added as items in an otherwise typed column but treat
|
15
|
+
them a nil for calculation purposes.
|
16
|
+
- @*, #*, ?* :: a Date, Numeric, or Boolean column that is also tolerant.
|
17
|
+
`~*` could be treated the same as `~`.
|
18
|
+
|
2
19
|
* TODO Specify Column Widths
|
3
20
|
Allow a formatter to specify column widths. This could be a number of
|
4
21
|
characters, which would be interpreted as a number of "ems" for LaTeX.
|
@@ -23,6 +40,13 @@ Possible enhancements:
|
|
23
40
|
This is a [[https://github.com/westonganger/spreadsheet_architect][gem]] that I can include into the Table model to convert a table into
|
24
41
|
a spread-sheet, or even a sheet in a multi-sheet spreadsheet file.
|
25
42
|
|
43
|
+
* TODO Conversion to HTML
|
44
|
+
Perhaps an obvious formatter would be HTML tables, but CSS controls the
|
45
|
+
styling, so have to think on that one.
|
46
|
+
|
47
|
+
* TODO Conversion to JSON, YAML
|
48
|
+
These would need no color formatting and might prove useful.
|
49
|
+
|
26
50
|
* TODO Add Quandl or EODDATA Queries
|
27
51
|
Possible replacements for YQL.
|
28
52
|
|
data/fat_table.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.name = 'fat_table'
|
7
7
|
spec.version = FatTable::VERSION
|
8
8
|
spec.authors = ['Daniel E. Doherty']
|
9
|
-
spec.email = ['ded
|
9
|
+
spec.email = ['ded@ddoherty.net']
|
10
10
|
|
11
11
|
spec.summary = 'Provides tools for working with tables as a data type.'
|
12
12
|
spec.description = <<-DESC
|
data/lib/fat_table/column.rb
CHANGED
@@ -70,7 +70,7 @@ module FatTable
|
|
70
70
|
# except in the case of String columns, which retain them a blank
|
71
71
|
# strings.
|
72
72
|
#
|
73
|
-
#
|
73
|
+
# @example
|
74
74
|
#
|
75
75
|
# require 'fat_table'
|
76
76
|
# col = FatTable::Column.new(header: 'date')
|
@@ -83,6 +83,8 @@ module FatTable
|
|
83
83
|
# col.type #=> 'Numeric'
|
84
84
|
# col.header #=> :prices
|
85
85
|
# col.sum #=> 18376.75
|
86
|
+
#
|
87
|
+
# @param
|
86
88
|
def initialize(header:, items: [], type: 'NilClass', tolerant: false)
|
87
89
|
@raw_header = header
|
88
90
|
@header =
|
@@ -153,8 +155,6 @@ module FatTable
|
|
153
155
|
# Force the column to have String type and then convert all items to
|
154
156
|
# strings.
|
155
157
|
def force_string!
|
156
|
-
# msg = "Can only force an empty column to String type"
|
157
|
-
# raise UserError, msg unless empty?
|
158
158
|
@type = 'String'
|
159
159
|
unless empty?
|
160
160
|
@items = items.map(&:to_s)
|
data/lib/fat_table/convert.rb
CHANGED
@@ -102,23 +102,25 @@ module FatTable
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
-
ISO_DATE_RE = %r{(?<yr>\d\d\d\d)[-\/]
|
105
|
+
ISO_DATE_RE = %r{\A(?<yr>\d\d\d\d)[-\/]
|
106
106
|
(?<mo>\d\d?)[-\/]
|
107
107
|
(?<dy>\d\d?)\s*
|
108
|
-
(T?\s*\d\d:\d\d(:\d\d)
|
109
|
-
([-+](\d\d?)(:\d\d?))?)
|
108
|
+
(T?\s*\d\d:\d\d(:\d\d)?\s*
|
109
|
+
([-+](\d\d?)(:\d\d?))?)?\s*
|
110
|
+
((mon|tue|wed|thu|fri|sat|sun)[a-zA-z]*)?\s*
|
111
|
+
\z}xi
|
110
112
|
|
111
|
-
AMR_DATE_RE = %r{(?<dy>\d\d?)[-/](?<mo>\d\d?)[-/](?<yr>\d\d\d\d)\s*
|
112
|
-
(?<tm>T\d\d:\d\d:\d\d(\+\d\d:\d\d)?)
|
113
|
+
AMR_DATE_RE = %r{\A(?<dy>\d\d?)[-/](?<mo>\d\d?)[-/](?<yr>\d\d\d\d)\s*
|
114
|
+
(?<tm>T\d\d:\d\d:\d\d(\+\d\d:\d\d)?)?\z}xi
|
113
115
|
|
114
116
|
# A Date like 'Tue, 01 Nov 2016' or 'Tue 01 Nov 2016' or '01 Nov 2016'.
|
115
117
|
# These are emitted by Postgresql, so it makes from_sql constructor
|
116
118
|
# possible without special formatting of the dates.
|
117
|
-
INV_DATE_RE = %r{((mon|tue|wed|thu|fri|sat|sun)[a-zA-z]*,?)?\s+ # looks like dow
|
119
|
+
INV_DATE_RE = %r{\A((mon|tue|wed|thu|fri|sat|sun)[a-zA-z]*,?)?\s+ # looks like dow
|
118
120
|
(?<dy>\d\d?)\s+ # one or two-digit day
|
119
121
|
(?<mo_name>[jfmasondJFMASOND][A-Za-z]{2,})\s+ # looks like a month name
|
120
122
|
(?<yr>\d\d\d\d) # and a 4-digit year
|
121
|
-
}xi
|
123
|
+
\z}xi
|
122
124
|
|
123
125
|
# Convert the val to a DateTime if it is either a DateTime, a Date, a Time, or a
|
124
126
|
# String that can be parsed as a DateTime, otherwise return nil. It only
|
@@ -132,7 +134,7 @@ module FatTable
|
|
132
134
|
return val.to_datetime if val.is_a?(Time)
|
133
135
|
|
134
136
|
begin
|
135
|
-
str = val.to_s.clean
|
137
|
+
str = val.to_s.clean.sub(/\A[\[\(\{\<]\s*/, '').sub(/[\]\)\}\>]\s*\z/, '')
|
136
138
|
return nil if str.blank?
|
137
139
|
|
138
140
|
if str.match(ISO_DATE_RE)
|
@@ -53,35 +53,38 @@ module FatTable
|
|
53
53
|
# 'Average'.
|
54
54
|
attr_reader :gfooters
|
55
55
|
|
56
|
-
class_attribute :default_format
|
57
|
-
self.default_format = {
|
58
|
-
nil_text: '',
|
59
|
-
case: :none,
|
60
|
-
alignment: :left,
|
61
|
-
bold: false,
|
62
|
-
italic: false,
|
63
|
-
color: 'none',
|
64
|
-
bgcolor: 'none',
|
65
|
-
hms: false,
|
66
|
-
pre_digits: 0,
|
67
|
-
post_digits: -1,
|
68
|
-
commas: false,
|
69
|
-
currency: false,
|
70
|
-
datetime_fmt: '%F %H:%M:%S',
|
71
|
-
date_fmt: '%F',
|
72
|
-
true_text: 'T',
|
73
|
-
false_text: 'F',
|
74
|
-
true_color: 'none',
|
75
|
-
true_bgcolor: 'none',
|
76
|
-
false_color: 'none',
|
77
|
-
false_bgcolor: 'none',
|
78
|
-
underline: false,
|
79
|
-
blink: false,
|
80
|
-
}
|
81
|
-
|
82
56
|
class_attribute :valid_colors
|
83
57
|
self.valid_colors = ['none']
|
84
58
|
|
59
|
+
def self.default_format
|
60
|
+
{
|
61
|
+
nil_text: '',
|
62
|
+
case: :none,
|
63
|
+
alignment: :left,
|
64
|
+
bold: false,
|
65
|
+
italic: false,
|
66
|
+
color: 'none',
|
67
|
+
bgcolor: 'none',
|
68
|
+
hms: false,
|
69
|
+
pre_digits: 0,
|
70
|
+
post_digits: 0,
|
71
|
+
commas: false,
|
72
|
+
currency: false,
|
73
|
+
datetime_fmt: '%F %H:%M:%S',
|
74
|
+
date_fmt: '%F',
|
75
|
+
true_text: 'T',
|
76
|
+
false_text: 'F',
|
77
|
+
true_color: 'none',
|
78
|
+
true_bgcolor: 'none',
|
79
|
+
false_color: 'none',
|
80
|
+
false_bgcolor: 'none',
|
81
|
+
underline: false,
|
82
|
+
blink: false,
|
83
|
+
_h: nil,
|
84
|
+
_location: nil
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
85
88
|
# :category: Constructors
|
86
89
|
|
87
90
|
# Return a new Formatter for the given +table+ which must be of the class
|
@@ -659,7 +662,7 @@ module FatTable
|
|
659
662
|
# hash.
|
660
663
|
format_h =
|
661
664
|
if format_at[location][h].empty?
|
662
|
-
default_format
|
665
|
+
default_format
|
663
666
|
else
|
664
667
|
format_at[location][h].to_h
|
665
668
|
end
|
@@ -700,17 +703,17 @@ module FatTable
|
|
700
703
|
# format_for call with those locations.
|
701
704
|
if location == :body
|
702
705
|
format_h.each_pair do |k, v|
|
703
|
-
if format_at[:bfirst][h].send(k) == default_format[k]
|
706
|
+
if format_at[:bfirst][h].send(k) == self.class.default_format[k]
|
704
707
|
format_at[:bfirst][h].send("#{k}=", v)
|
705
708
|
end
|
706
|
-
if format_at[:gfirst][h].send(k) == default_format[k]
|
709
|
+
if format_at[:gfirst][h].send(k) == self.class.default_format[k]
|
707
710
|
format_at[:gfirst][h].send("#{k}=", v)
|
708
711
|
end
|
709
712
|
end
|
710
713
|
elsif location == :gfirst
|
711
714
|
# Copy :gfirst formatting to :bfirst if it is still the default
|
712
715
|
format_h.each_pair do |k, v|
|
713
|
-
if format_at[:bfirst][h].send(k) == default_format[k]
|
716
|
+
if format_at[:bfirst][h].send(k) == self.class.default_format[k]
|
714
717
|
format_at[:bfirst][h].send("#{k}=", v)
|
715
718
|
end
|
716
719
|
end
|
@@ -942,9 +945,10 @@ module FatTable
|
|
942
945
|
# Convert a value to a string based on the instructions in istruct,
|
943
946
|
# depending on the type of val. "Formatting," which changes the content of
|
944
947
|
# the string, such as adding commas, is always performed, except alignment
|
945
|
-
# which is only performed when the width parameter is
|
946
|
-
# which changes the appearance without changing the
|
947
|
-
# only if the decorate parameter is true.
|
948
|
+
# which is only performed when the width parameter is
|
949
|
+
# non-nil. "Decorating", which changes the appearance without changing the
|
950
|
+
# content, is performed only if the decorate parameter is true. Priority:
|
951
|
+
# lowest to highest: type, location, column_name
|
948
952
|
def format_cell(val, istruct, width: nil, decorate: false)
|
949
953
|
case val
|
950
954
|
when Numeric
|
@@ -982,8 +986,6 @@ module FatTable
|
|
982
986
|
end
|
983
987
|
end
|
984
988
|
|
985
|
-
private
|
986
|
-
|
987
989
|
# Add LaTeX control sequences, ANSI terminal escape codes, or other
|
988
990
|
# decorations to string to decorate it with the given attributes. None of
|
989
991
|
# the decorations may affect the displayed width of the string. Return the
|
@@ -992,6 +994,8 @@ module FatTable
|
|
992
994
|
str
|
993
995
|
end
|
994
996
|
|
997
|
+
private
|
998
|
+
|
995
999
|
# Convert a boolean to a string according to instructions in istruct, which
|
996
1000
|
# is assumed to be the result of parsing a formatting instruction string as
|
997
1001
|
# above. Only device-independent formatting is done here. Device dependent
|
@@ -1026,46 +1030,59 @@ module FatTable
|
|
1026
1030
|
# specializing this method.
|
1027
1031
|
def format_numeric(val, istruct)
|
1028
1032
|
return istruct.nil_text if val.nil?
|
1033
|
+
return val.secs_to_hms if istruct.hms
|
1029
1034
|
|
1030
|
-
|
1031
|
-
|
1032
|
-
result = val.
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
val.to_s(:currency, precision: istruct.post_digits, delimiter: delim,
|
1042
|
-
unit: FatTable.currency_symbol)
|
1043
|
-
end
|
1044
|
-
# istruct.commas = false
|
1045
|
-
elsif istruct.pre_digits.positive?
|
1046
|
-
if val.whole?
|
1047
|
-
# No fractional part, ignore post_digits
|
1048
|
-
result = sprintf("%0#{istruct.pre_digits}d", val)
|
1049
|
-
elsif istruct.post_digits >= 0
|
1050
|
-
# There's a fractional part and pre_digits. sprintf width includes
|
1051
|
-
# space for fractional part and decimal point, so add pre, post, and 1
|
1052
|
-
# to get the proper sprintf width.
|
1053
|
-
wid = istruct.pre_digits + 1 + istruct.post_digits
|
1054
|
-
result = sprintf("%0#{wid}.#{istruct.post_digits}f", val)
|
1055
|
-
else
|
1056
|
-
val = val.round(0)
|
1057
|
-
result = sprintf("%0#{istruct.pre_digits}d", val)
|
1035
|
+
if istruct.commas
|
1036
|
+
# Commify the whole number part if not done already.
|
1037
|
+
result = val.commas(istruct.post_digits)
|
1038
|
+
else
|
1039
|
+
result = val.round(istruct.post_digits).to_s
|
1040
|
+
match = result.match(/\.(\d+)\z/)
|
1041
|
+
if match && match[1]&.size < istruct.post_digits
|
1042
|
+
# Add trailing zeros to pad out post_digits
|
1043
|
+
n_zeros = [istruct.post_digits - match[1].size, 0].max
|
1044
|
+
zeros = '0' * n_zeros
|
1045
|
+
result = result + zeros
|
1058
1046
|
end
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1047
|
+
result
|
1048
|
+
end
|
1049
|
+
|
1050
|
+
if istruct.pre_digits.positive?
|
1051
|
+
match = result.match(/\A([\d,]+)(\.\d+)?\z/)
|
1052
|
+
whole_part = match[1]
|
1053
|
+
frac_part = match[2]
|
1054
|
+
n_zeros = [istruct.pre_digits - whole_part.gsub(',', '').size, 0].max
|
1055
|
+
result =
|
1056
|
+
if n_zeros.positive?
|
1057
|
+
if istruct.commas
|
1058
|
+
# Insert leading zeros with commas
|
1059
|
+
pre_comma_match = whole_part.match(/\A(\d+),/)
|
1060
|
+
if pre_comma_match
|
1061
|
+
n_partial_zeros = 3 - pre_comma_match[1].size
|
1062
|
+
whole_part = "0" * n_partial_zeros + whole_part
|
1063
|
+
n_zeros -= n_partial_zeros
|
1064
|
+
end
|
1065
|
+
zeros = ''
|
1066
|
+
if n_zeros.positive?
|
1067
|
+
zeros = "0" * n_zeros
|
1068
|
+
if n_zeros > 3 && istruct.commas
|
1069
|
+
zeros = zeros.reverse.gsub!(/([0-9]{3})/, "\\1,").reverse
|
1070
|
+
end
|
1071
|
+
end
|
1072
|
+
"#{zeros},#{whole_part}#{frac_part}"
|
1073
|
+
else
|
1074
|
+
# Insert leading zeros without commas
|
1075
|
+
zeros = "0" * n_zeros
|
1076
|
+
"#{zeros}#{whole_part}#{frac_part}"
|
1077
|
+
end
|
1078
|
+
else
|
1079
|
+
"#{whole_part}#{frac_part}"
|
1080
|
+
end
|
1063
1081
|
else
|
1064
|
-
result
|
1082
|
+
result
|
1065
1083
|
end
|
1066
|
-
if istruct.
|
1067
|
-
|
1068
|
-
result = result.commas
|
1084
|
+
if istruct.currency
|
1085
|
+
result = "#{FatTable.currency_symbol}#{result}"
|
1069
1086
|
end
|
1070
1087
|
result
|
1071
1088
|
end
|
@@ -1086,7 +1103,7 @@ module FatTable
|
|
1086
1103
|
val
|
1087
1104
|
end
|
1088
1105
|
if width && aligned?
|
1089
|
-
pad = width - width(val)
|
1106
|
+
pad = [width - width(val), 0].max
|
1090
1107
|
case istruct.alignment
|
1091
1108
|
when :left
|
1092
1109
|
val += ' ' * pad
|
@@ -1199,8 +1216,10 @@ module FatTable
|
|
1199
1216
|
result += post_header(widths)
|
1200
1217
|
end
|
1201
1218
|
new_rows.each do |loc_row|
|
1202
|
-
|
1203
|
-
|
1219
|
+
if loc_row.nil?
|
1220
|
+
result += hline(widths)
|
1221
|
+
next
|
1222
|
+
end
|
1204
1223
|
|
1205
1224
|
_loc, row = *loc_row
|
1206
1225
|
result += pre_row
|
@@ -95,18 +95,6 @@ module FatTable
|
|
95
95
|
result
|
96
96
|
end
|
97
97
|
|
98
|
-
private
|
99
|
-
|
100
|
-
def color_valid?(clr)
|
101
|
-
valid_colors.include?(clr)
|
102
|
-
end
|
103
|
-
|
104
|
-
def invalid_color_msg(clr)
|
105
|
-
valid_colors_list = valid_colors.join(' ').wrap
|
106
|
-
"LaTeXFormatter invalid color '#{clr}'. Valid colors are:\n" +
|
107
|
-
valid_colors_list
|
108
|
-
end
|
109
|
-
|
110
98
|
# Add LaTeX control sequences. Ignore background color, underline, and
|
111
99
|
# blink. Alignment needs to be done by LaTeX, so we have to take it into
|
112
100
|
# account unless it's the same as the body alignment, since that is the
|
@@ -119,15 +107,29 @@ module FatTable
|
|
119
107
|
result = "{\\textcolor{#{istruct.color}}{#{result}}}"
|
120
108
|
end
|
121
109
|
if istruct.bgcolor && istruct.bgcolor != 'none'
|
122
|
-
result = "\\cellcolor{#{istruct.bgcolor}}#{result}"
|
110
|
+
result = "{\\cellcolor{#{istruct.bgcolor}}{#{result}}}"
|
123
111
|
end
|
124
|
-
|
112
|
+
if (istruct._h && format_at[:body][istruct._h] &&
|
113
|
+
istruct.alignment != format_at[:body][istruct._h].alignment) ||
|
114
|
+
(istruct._h.nil? && istruct.alignment.to_sym != :left)
|
125
115
|
ac = alignment_code(istruct.alignment)
|
126
116
|
result = "\\multicolumn{1}{#{ac}}{#{result}}"
|
127
117
|
end
|
128
118
|
result
|
129
119
|
end
|
130
120
|
|
121
|
+
private
|
122
|
+
|
123
|
+
def color_valid?(clr)
|
124
|
+
valid_colors.include?(clr)
|
125
|
+
end
|
126
|
+
|
127
|
+
def invalid_color_msg(clr)
|
128
|
+
valid_colors_list = valid_colors.join(' ').wrap
|
129
|
+
"LaTeXFormatter invalid color '#{clr}'. Valid colors are:\n" +
|
130
|
+
valid_colors_list
|
131
|
+
end
|
132
|
+
|
131
133
|
# Return +str+ with quote marks oriented and special TeX characters quoted.
|
132
134
|
def quote(str)
|
133
135
|
# Replace single and double quotes with TeX oriented quotes.
|
@@ -159,7 +161,7 @@ module FatTable
|
|
159
161
|
end
|
160
162
|
|
161
163
|
def alignment_code(al_sym)
|
162
|
-
case al_sym
|
164
|
+
case al_sym.to_sym
|
163
165
|
when :center
|
164
166
|
'c'
|
165
167
|
when :right
|
@@ -6,9 +6,12 @@ module FatTable
|
|
6
6
|
# timestamps and the connector at the beginning of hlines is a '|' rather than
|
7
7
|
# a '+' as for text tables.
|
8
8
|
class OrgFormatter < Formatter
|
9
|
-
self.default_format
|
10
|
-
|
11
|
-
|
9
|
+
def self.default_format
|
10
|
+
fmt = super
|
11
|
+
fmt[:date_fmt] = '[%F %a]'
|
12
|
+
fmt[:datetime_fmt] = '[%F %a %H:%M:%S]'
|
13
|
+
fmt
|
14
|
+
end
|
12
15
|
|
13
16
|
private
|
14
17
|
|
@@ -38,6 +38,17 @@ module FatTable
|
|
38
38
|
self.valid_colors = ['none'] +
|
39
39
|
::Rainbow::X11ColorNames::NAMES.keys.map(&:to_s).sort
|
40
40
|
|
41
|
+
# Add ANSI codes to string to implement the given decorations
|
42
|
+
def decorate_string(str, istruct)
|
43
|
+
result = Rainbow(str)
|
44
|
+
result = colorize(result, istruct.color, istruct.bgcolor)
|
45
|
+
result = result.bold if istruct.bold
|
46
|
+
result = result.italic if istruct.italic
|
47
|
+
result = result.underline if istruct.underline
|
48
|
+
result = result.blink if istruct.blink
|
49
|
+
result
|
50
|
+
end
|
51
|
+
|
41
52
|
private
|
42
53
|
|
43
54
|
def color_valid?(clr)
|
@@ -66,17 +77,6 @@ module FatTable
|
|
66
77
|
str.gsub(/\e\[[0-9;]+m/, '')
|
67
78
|
end
|
68
79
|
|
69
|
-
# Add ANSI codes to string to implement the given decorations
|
70
|
-
def decorate_string(str, istruct)
|
71
|
-
result = Rainbow(str)
|
72
|
-
result = colorize(result, istruct.color, istruct.bgcolor)
|
73
|
-
result = result.bold if istruct.bold
|
74
|
-
result = result.italic if istruct.italic
|
75
|
-
result = result.underline if istruct.underline
|
76
|
-
result = result.blink if istruct.blink
|
77
|
-
result
|
78
|
-
end
|
79
|
-
|
80
80
|
def colorize(str, fg_color, bg_color)
|
81
81
|
fg_color = nil if fg_color == 'none'
|
82
82
|
bg_color = nil if bg_color == 'none'
|
data/lib/fat_table/table.rb
CHANGED
@@ -379,11 +379,10 @@ module FatTable
|
|
379
379
|
|
380
380
|
# :category: Attributes
|
381
381
|
|
382
|
-
# Set the column type for Column with the given +key+ as a String type
|
383
|
-
# but only if empty. Otherwise, we would have to worry about converting
|
384
|
-
# existing items in the column to String. Perhaps that's a TODO.
|
382
|
+
# Set the column type for Column with the given +key+ as a String type.
|
385
383
|
def force_string!(*keys)
|
386
384
|
keys.each do |h|
|
385
|
+
raise UserError, "force_string!: #{h} not a column in table" unless column(h)
|
387
386
|
column(h).force_string!
|
388
387
|
end
|
389
388
|
self
|
data/lib/fat_table/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fat_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel E. Doherty
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -264,7 +264,7 @@ description: |2
|
|
264
264
|
array of arrays with its .to_aoa output function and will be rendered in an
|
265
265
|
org-mode buffer as an org-table, ready for processing by other code blocks.
|
266
266
|
email:
|
267
|
-
- ded
|
267
|
+
- ded@ddoherty.net
|
268
268
|
executables:
|
269
269
|
- ft_console
|
270
270
|
extensions: []
|
@@ -274,6 +274,7 @@ files:
|
|
274
274
|
- ".rspec"
|
275
275
|
- ".rubocop.yml"
|
276
276
|
- ".simplecov"
|
277
|
+
- ".solargraph.yml"
|
277
278
|
- ".travis.yml"
|
278
279
|
- ".yardopts"
|
279
280
|
- Gemfile
|
@@ -319,7 +320,7 @@ licenses: []
|
|
319
320
|
metadata:
|
320
321
|
allowed_push_host: https://rubygems.org
|
321
322
|
yard.run: yri
|
322
|
-
post_install_message:
|
323
|
+
post_install_message:
|
323
324
|
rdoc_options: []
|
324
325
|
require_paths:
|
325
326
|
- lib
|
@@ -334,8 +335,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
334
335
|
- !ruby/object:Gem::Version
|
335
336
|
version: '0'
|
336
337
|
requirements: []
|
337
|
-
rubygems_version: 3.
|
338
|
-
signing_key:
|
338
|
+
rubygems_version: 3.4.1
|
339
|
+
signing_key:
|
339
340
|
specification_version: 4
|
340
341
|
summary: Provides tools for working with tables as a data type.
|
341
342
|
test_files: []
|