csv_preview 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +8 -0
- data/CHANGELOG.md +25 -0
- data/LICENSE +21 -0
- data/README.md +587 -0
- data/SECURITY.md +51 -0
- data/fonts/Inter-Regular.ttf +0 -0
- data/fonts/Inter-SemiBold.ttf +0 -0
- data/fonts/OFL.txt +92 -0
- data/lib/csv_preview/active_storage_previewer.rb +97 -0
- data/lib/csv_preview/cell_kind.rb +75 -0
- data/lib/csv_preview/configuration.rb +183 -0
- data/lib/csv_preview/deadline.rb +31 -0
- data/lib/csv_preview/errors.rb +30 -0
- data/lib/csv_preview/layout.rb +590 -0
- data/lib/csv_preview/painter.rb +194 -0
- data/lib/csv_preview/parser.rb +456 -0
- data/lib/csv_preview/railtie.rb +54 -0
- data/lib/csv_preview/reader.rb +259 -0
- data/lib/csv_preview/sample.rb +85 -0
- data/lib/csv_preview/sanitizer.rb +31 -0
- data/lib/csv_preview/text.rb +621 -0
- data/lib/csv_preview/theme.rb +207 -0
- data/lib/csv_preview/version.rb +6 -0
- data/lib/csv_preview.rb +149 -0
- data/skills/csv-preview/SKILL.md +171 -0
- metadata +121 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CsvPreview
|
|
4
|
+
# Everything about a preview except the pixels: which columns and rows are shown, their widths, the
|
|
5
|
+
# exact display strings, the footer and the output size (rendering.md section 6). It does all the
|
|
6
|
+
# measuring, with one libvips text call per drawn string, and keeps the rendered text for the
|
|
7
|
+
# painter. Built by {CsvPreview.layout}; for debugging and tests, and its shape may change before 1.0.
|
|
8
|
+
#
|
|
9
|
+
# Internally every value is device px. {Column#width} is the one value reported in logical px.
|
|
10
|
+
class Layout
|
|
11
|
+
# Every string the image shows that does not come from the file, in one place so translation is a
|
|
12
|
+
# one-file change. Counts are formatted with {Layout.format_count}.
|
|
13
|
+
STRINGS = {
|
|
14
|
+
empty: "Empty file",
|
|
15
|
+
nothing_read: "No data in the first %<size>s",
|
|
16
|
+
no_rows: "No rows",
|
|
17
|
+
row: "row",
|
|
18
|
+
rows: "rows",
|
|
19
|
+
column: "column",
|
|
20
|
+
columns: "columns",
|
|
21
|
+
count: "%<count>s %<noun>s",
|
|
22
|
+
count_of: "%<shown>s of %<total>s %<noun>s",
|
|
23
|
+
count_at_least: "%<count>s+ %<noun>s",
|
|
24
|
+
count_of_at_least: "%<shown>s of %<total>s+ %<noun>s",
|
|
25
|
+
separator: " · ",
|
|
26
|
+
megabytes: "%<size>s MB",
|
|
27
|
+
kilobytes: "%<size>s KB",
|
|
28
|
+
byte: "%<size>s byte",
|
|
29
|
+
bytes: "%<size>s bytes"
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
# The true minus sign (U+2212) that number cells show instead of a hyphen-minus.
|
|
33
|
+
MINUS = "−"
|
|
34
|
+
# Bytes in a kibibyte and a mebibyte, for the "No data in the first 2 MB" message.
|
|
35
|
+
KIB = 1_024
|
|
36
|
+
# (see KIB)
|
|
37
|
+
MIB = 1_048_576
|
|
38
|
+
|
|
39
|
+
# One fully visible column, or the peek column.
|
|
40
|
+
#
|
|
41
|
+
# @!attribute index [Integer] the column's index in the {Sample}
|
|
42
|
+
# @!attribute kind [Symbol] :number or :text
|
|
43
|
+
# @!attribute align [Symbol] :left or :right (always :left for the peek column)
|
|
44
|
+
# @!attribute width [Float] logical px, `width_px / scale`
|
|
45
|
+
# @!attribute width_px [Integer] device px, padding included (for the peek: up to the frame)
|
|
46
|
+
# @!attribute x_px [Integer] device px from the image's left edge
|
|
47
|
+
Column = Data.define(:index, :kind, :align, :width, :width_px, :x_px)
|
|
48
|
+
|
|
49
|
+
# @api private
|
|
50
|
+
# A rectangle in device px.
|
|
51
|
+
Box = Data.define(:x, :y, :width, :height)
|
|
52
|
+
|
|
53
|
+
# @api private
|
|
54
|
+
# One rendered string at its final device-px position: the image's top-left corner is at
|
|
55
|
+
# (`x`, `y`), and the painter clips it to `box` (its cell, or the footer or message band, without
|
|
56
|
+
# rules). The painter needs nothing else from {Text}.
|
|
57
|
+
Placement = Data.define(:run, :x, :y, :box)
|
|
58
|
+
|
|
59
|
+
# @api private
|
|
60
|
+
# Device-px bands and rules for the painter (rendering.md 2.3). `body_rows` is the number of row
|
|
61
|
+
# bands below the header (`rows_shown`, or the message rows of the special layouts); row rules
|
|
62
|
+
# are drawn only between data rows, so at the top of rows 1 to `rows_shown` - 1. `footer_top` is
|
|
63
|
+
# nil without a footer, `fade` nil without a peek column.
|
|
64
|
+
Geometry = Data.define(:frame, :header_height, :row_height, :footer_height, :header_rule, :row_rule,
|
|
65
|
+
:footer_rule, :body_rows, :footer_top, :fade)
|
|
66
|
+
|
|
67
|
+
# @return [Sample]
|
|
68
|
+
attr_reader :sample
|
|
69
|
+
# @return [Integer] fully visible columns (the peek column is not counted)
|
|
70
|
+
attr_reader :visible_columns
|
|
71
|
+
# @return [Array<Column>] the fully visible columns, left to right
|
|
72
|
+
attr_reader :columns
|
|
73
|
+
# @return [Column, nil] the partly visible column under the fade, drawn left-aligned
|
|
74
|
+
attr_reader :peek_column
|
|
75
|
+
# @return [Integer] data rows drawn
|
|
76
|
+
attr_reader :rows_shown
|
|
77
|
+
# @return [String, nil] what the footer says (the drawn footer is cut with "…" if the image is
|
|
78
|
+
# narrower), nil when nothing is hidden and no footer is drawn
|
|
79
|
+
attr_reader :footer_text
|
|
80
|
+
# @return [String, nil] "Empty file", "No data in the first 2 MB", "No rows" or nil
|
|
81
|
+
attr_reader :message
|
|
82
|
+
# @return [Integer] output width in pixels
|
|
83
|
+
attr_reader :width_px
|
|
84
|
+
# @return [Integer] output height in pixels
|
|
85
|
+
attr_reader :height_px
|
|
86
|
+
# @return [Integer]
|
|
87
|
+
attr_reader :scale
|
|
88
|
+
# @return [Theme]
|
|
89
|
+
attr_reader :theme
|
|
90
|
+
# @return [Integer] libvips text calls made for this layout (instrumentation for tests)
|
|
91
|
+
attr_reader :text_calls
|
|
92
|
+
# @api private
|
|
93
|
+
# @return [Array<Placement>] every drawn string in paint order: column by column (each visible
|
|
94
|
+
# column's header, then its cells), then the peek column's header and cells, then the message,
|
|
95
|
+
# then the footer
|
|
96
|
+
attr_reader :placements
|
|
97
|
+
# @api private
|
|
98
|
+
# @return [Geometry]
|
|
99
|
+
attr_reader :geometry
|
|
100
|
+
# @api private
|
|
101
|
+
# @return [Integer] the `max_width_px` cap the layout was built under (the painter checks it again)
|
|
102
|
+
attr_reader :max_width_px
|
|
103
|
+
# @api private
|
|
104
|
+
# @return [Integer] the `max_height_px` cap
|
|
105
|
+
attr_reader :max_height_px
|
|
106
|
+
|
|
107
|
+
# @api private
|
|
108
|
+
# @param sample [Sample]
|
|
109
|
+
# @param config [Configuration] a resolved snapshot
|
|
110
|
+
# @param theme [Theme]
|
|
111
|
+
# @param deadline [Deadline]
|
|
112
|
+
# @return [Layout]
|
|
113
|
+
# @raise [RenderFailed] when libvips cannot draw text, or the result would break the size caps
|
|
114
|
+
# @raise [TimeoutExceeded]
|
|
115
|
+
def self.build(sample, config:, theme:, deadline:)
|
|
116
|
+
Builder.new(sample, config, theme, deadline).call
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# @param index [Integer] 0-based column index
|
|
120
|
+
# @return [String] spreadsheet column letters: A ... Z, AA, AB, ...
|
|
121
|
+
def self.column_letters(index)
|
|
122
|
+
letters = +""
|
|
123
|
+
n = index + 1
|
|
124
|
+
while n.positive?
|
|
125
|
+
n, r = (n - 1).divmod(26)
|
|
126
|
+
letters.prepend((r + 65).chr)
|
|
127
|
+
end
|
|
128
|
+
letters
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# @param n [Integer]
|
|
132
|
+
# @return [String] ASCII digits with "," every three, independent of locale
|
|
133
|
+
def self.format_count(n)
|
|
134
|
+
n.to_s.reverse.scan(/\d{1,3}/).join(",").reverse
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# @param bytes [Integer]
|
|
138
|
+
# @return [String] whole or one-decimal MB (of 1,048,576 bytes), KB below that, bytes below 1 KB
|
|
139
|
+
def self.format_size(bytes)
|
|
140
|
+
if bytes >= MIB
|
|
141
|
+
format(STRINGS[:megabytes], size: one_decimal(bytes, MIB))
|
|
142
|
+
elsif bytes >= KIB
|
|
143
|
+
format(STRINGS[:kilobytes], size: one_decimal(bytes, KIB))
|
|
144
|
+
else
|
|
145
|
+
format(STRINGS[bytes == 1 ? :byte : :bytes], size: bytes)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def self.one_decimal(value, unit)
|
|
150
|
+
whole, tenth = (value * 10.0 / unit).round.divmod(10)
|
|
151
|
+
tenth.zero? ? format_count(whole) : "#{format_count(whole)}.#{tenth}"
|
|
152
|
+
end
|
|
153
|
+
private_class_method :one_decimal
|
|
154
|
+
|
|
155
|
+
# @api private
|
|
156
|
+
def initialize(sample:, visible_columns:, columns:, peek_column:, rows_shown:, footer_text:, message:,
|
|
157
|
+
width_px:, height_px:, scale:, theme:, text_calls:, placements:, geometry:, texts:, lookup:,
|
|
158
|
+
max_width_px:, max_height_px:)
|
|
159
|
+
@sample = sample
|
|
160
|
+
@visible_columns = visible_columns
|
|
161
|
+
@columns = columns.freeze
|
|
162
|
+
@peek_column = peek_column
|
|
163
|
+
@rows_shown = rows_shown
|
|
164
|
+
@footer_text = footer_text
|
|
165
|
+
@message = message
|
|
166
|
+
@width_px = width_px
|
|
167
|
+
@height_px = height_px
|
|
168
|
+
@scale = scale
|
|
169
|
+
@theme = theme
|
|
170
|
+
@text_calls = text_calls
|
|
171
|
+
@placements = placements.freeze
|
|
172
|
+
@geometry = geometry
|
|
173
|
+
@texts = texts.freeze
|
|
174
|
+
@lookup = lookup.freeze
|
|
175
|
+
@max_width_px = max_width_px
|
|
176
|
+
@max_height_px = max_height_px
|
|
177
|
+
freeze
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# @param row [Integer] 0-based data row
|
|
181
|
+
# @param col [Integer] 0-based column index
|
|
182
|
+
# @return [String, nil] the display string of a drawn cell (with "…" and U+2212 where applied; for
|
|
183
|
+
# the peek column, untruncated), "" for a blank cell, nil outside the drawn area
|
|
184
|
+
def cell_text(row, col)
|
|
185
|
+
@texts[[:cell, row, col]]
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# @param col [Integer]
|
|
189
|
+
# @return [String, nil] the same for the header (column letters when the file has no header row)
|
|
190
|
+
def header_text(col)
|
|
191
|
+
@texts[[:header, col]]
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# @api private
|
|
195
|
+
# @return [Placement, nil] nil when the cell draws nothing or is not drawn
|
|
196
|
+
def cell_placement(row, col) = @lookup[[:cell, row, col]]
|
|
197
|
+
|
|
198
|
+
# @api private
|
|
199
|
+
# @return [Placement, nil]
|
|
200
|
+
def header_placement(col) = @lookup[[:header, col]]
|
|
201
|
+
|
|
202
|
+
# @api private
|
|
203
|
+
# @return [Placement, nil]
|
|
204
|
+
def footer_placement = @lookup[:footer]
|
|
205
|
+
|
|
206
|
+
# @api private
|
|
207
|
+
# @return [Placement, nil]
|
|
208
|
+
def message_placement = @lookup[:message]
|
|
209
|
+
|
|
210
|
+
# @return [String] a summary of the sizes and counts (never cell text or libvips objects)
|
|
211
|
+
def inspect
|
|
212
|
+
"#<CsvPreview::Layout #{width_px}x#{height_px} visible_columns=#{visible_columns} " \
|
|
213
|
+
"peek_column=#{peek_column&.index.inspect} rows_shown=#{rows_shown} footer_text=#{footer_text.inspect} " \
|
|
214
|
+
"message=#{message.inspect} text_calls=#{text_calls}>"
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Runs the algorithm of rendering.md section 6 once. Not shared between threads.
|
|
218
|
+
# @api private
|
|
219
|
+
class Builder
|
|
220
|
+
# One measured column: display strings, the runs that measured them, and (for visible columns)
|
|
221
|
+
# the runs fitted to the column, which are drawn.
|
|
222
|
+
Measured = Struct.new(:index, :kind, :align, :header_style, :cell_style, :header, :header_run, :cells,
|
|
223
|
+
:runs, :natural, :header_fit, :fits)
|
|
224
|
+
|
|
225
|
+
def initialize(sample, config, theme, deadline)
|
|
226
|
+
@sample = sample
|
|
227
|
+
@config = config
|
|
228
|
+
@theme = theme
|
|
229
|
+
@deadline = deadline
|
|
230
|
+
@scale = config.scale
|
|
231
|
+
@renderer = Text::Renderer.new(deadline: deadline)
|
|
232
|
+
@styles = theme.styles(scale: @scale, font: config.font)
|
|
233
|
+
@metrics = Hash.new { |cache, style| cache[style] = Text.metrics(style) }
|
|
234
|
+
tokens!
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def call
|
|
238
|
+
return empty_layout if @sample.empty?
|
|
239
|
+
|
|
240
|
+
@n = @sample.column_count
|
|
241
|
+
@rows_shown = rows_shown
|
|
242
|
+
@message = STRINGS[:no_rows] if @sample.rows.empty?
|
|
243
|
+
@measured = []
|
|
244
|
+
measure_until_overflow
|
|
245
|
+
arrange
|
|
246
|
+
footer
|
|
247
|
+
fill
|
|
248
|
+
truncate
|
|
249
|
+
finish
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
private
|
|
253
|
+
|
|
254
|
+
def tokens!
|
|
255
|
+
@frame = @theme.rule_px(:frame_width, @scale)
|
|
256
|
+
@header_rule = @theme.rule_px(:header_rule_width, @scale)
|
|
257
|
+
@row_rule = @theme.rule_px(:row_rule_width, @scale)
|
|
258
|
+
@footer_rule = @theme.rule_px(:footer_rule_width, @scale)
|
|
259
|
+
@header_h = px(:header_height)
|
|
260
|
+
@row_h = px(:row_height)
|
|
261
|
+
@footer_h = px(:footer_height)
|
|
262
|
+
@pad = px(:cell_padding)
|
|
263
|
+
@edge = px(:edge_padding)
|
|
264
|
+
@inset = @edge - @pad
|
|
265
|
+
@fade_w = px(:fade_width)
|
|
266
|
+
@peek_w = px(:peek_width)
|
|
267
|
+
@min_col = px(:min_column_width)
|
|
268
|
+
@max_col = px(:max_column_width)
|
|
269
|
+
@min_w = px(:min_width)
|
|
270
|
+
@w_cap = [px(:max_width), @config.max_width_px].min
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def px(token) = @theme.px(token, @scale)
|
|
274
|
+
|
|
275
|
+
def check! = @deadline.check!(:layout)
|
|
276
|
+
|
|
277
|
+
# Step 2 (6.3): the footer is always reserved when counting.
|
|
278
|
+
def rows_shown
|
|
279
|
+
fit = (@config.max_height_px - (2 * @frame) - @header_h - @footer_h) / @row_h
|
|
280
|
+
[@sample.rows.size, [fit, 0].max].min
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# Step 3 (6.4): natural widths, left to right, stopping at the first column that overflows.
|
|
284
|
+
def measure_until_overflow
|
|
285
|
+
x = @inset
|
|
286
|
+
(0...@n).each do |j|
|
|
287
|
+
x += measured(j).natural
|
|
288
|
+
break if x + @inset > @w_cap
|
|
289
|
+
end
|
|
290
|
+
@all_fit = @measured.size == @n && x + @inset <= @w_cap
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def measured(j)
|
|
294
|
+
@measured[j] ||= measure(j)
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def measure(j)
|
|
298
|
+
check!
|
|
299
|
+
kind = @sample.column_kinds[j]
|
|
300
|
+
number = kind == :number
|
|
301
|
+
m = Measured.new(index: j, kind: kind, align: number ? :right : :left,
|
|
302
|
+
header_style: number ? @styles.header_number : @styles.header,
|
|
303
|
+
cell_style: number ? @styles.body_number : @styles.body)
|
|
304
|
+
m.header = header_display(j)
|
|
305
|
+
m.header_run = render(m.header, m.header_style, m.align)
|
|
306
|
+
m.cells = Array.new(@rows_shown) { |r| cell_display(@sample.rows[r][j], kind) }
|
|
307
|
+
m.runs = m.cells.map do |cell|
|
|
308
|
+
check!
|
|
309
|
+
render(cell, m.cell_style, m.align)
|
|
310
|
+
end
|
|
311
|
+
m.natural = natural_width(m)
|
|
312
|
+
m
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def natural_width(m)
|
|
316
|
+
cells = m.runs.compact.map(&:extent).max || 0
|
|
317
|
+
header = m.header_run&.extent || 0
|
|
318
|
+
if m.kind == :number
|
|
319
|
+
[[cells, [header, @max_col - (2 * @pad)].min].max + (2 * @pad), @min_col].max
|
|
320
|
+
else
|
|
321
|
+
([cells, header].max + (2 * @pad)).clamp(@min_col, @max_col)
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# 6.2 and 4.1: column letters when there is no header row; blank for columns past the header.
|
|
326
|
+
def header_display(j)
|
|
327
|
+
return Layout.column_letters(j) if @sample.headers.nil?
|
|
328
|
+
|
|
329
|
+
@sample.headers[j] || ""
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# 4.1 item 2: in a number column, a cell that is a number has its hyphens drawn as U+2212.
|
|
333
|
+
def cell_display(cell, kind)
|
|
334
|
+
return cell unless kind == :number && @theme.true_minus && cell.include?("-")
|
|
335
|
+
|
|
336
|
+
CellKind.cell(cell) == :number ? cell.tr("-", MINUS) : cell
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def render(string, style, align) = @renderer.render(string, style: style, align: align)
|
|
340
|
+
|
|
341
|
+
# Step 4 (6.5): visible columns, the peek column and the width before step 5.
|
|
342
|
+
def arrange
|
|
343
|
+
if @all_fit
|
|
344
|
+
@widths = @measured.map(&:natural)
|
|
345
|
+
@peek = nil
|
|
346
|
+
@width = (2 * @inset) + @widths.sum
|
|
347
|
+
return
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
@width = @w_cap
|
|
351
|
+
@widths, @peek = visible_widths(@width - @frame - @fade_w)
|
|
352
|
+
cap_peek if @peek
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# Columns whose text ends before the fade starts are fully visible; the first that is not is the
|
|
356
|
+
# peek. Column 0 is always visible, clamped when it alone is too wide (the only case where a
|
|
357
|
+
# number is truncated).
|
|
358
|
+
def visible_widths(limit)
|
|
359
|
+
widths = []
|
|
360
|
+
x = @inset
|
|
361
|
+
while widths.size < @n
|
|
362
|
+
natural = measured(widths.size).natural
|
|
363
|
+
break if x + natural - @pad > limit
|
|
364
|
+
|
|
365
|
+
widths << natural
|
|
366
|
+
x += natural
|
|
367
|
+
end
|
|
368
|
+
return [widths, widths.size < @n ? widths.size : nil] unless widths.empty?
|
|
369
|
+
return [[@width - (2 * @inset)], nil] if @n == 1
|
|
370
|
+
|
|
371
|
+
measured(1)
|
|
372
|
+
[[limit - @inset + @pad], 1]
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# The peek gets at most peek_width; any more space goes to the visible columns.
|
|
376
|
+
def cap_peek
|
|
377
|
+
space = (@width - @frame) - (@inset + @widths.sum)
|
|
378
|
+
distribute(space - @peek_w) if space > @peek_w
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# Shares `extra` px evenly across the visible columns, the remainder to the last.
|
|
382
|
+
def distribute(extra)
|
|
383
|
+
share, rest = extra.divmod(@widths.size)
|
|
384
|
+
@widths.map! { |w| w + share }
|
|
385
|
+
@widths[-1] += rest
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
# Step 7 (6.8), rendered before step 5 because its width can widen a small table.
|
|
389
|
+
def footer
|
|
390
|
+
visible = @widths.size
|
|
391
|
+
rows_hidden = @rows_shown < @sample.rows.size || @sample.rows_truncated?
|
|
392
|
+
total_columns = [@sample.total_columns, visible].max
|
|
393
|
+
columns_hidden = visible < total_columns
|
|
394
|
+
return unless rows_hidden || columns_hidden
|
|
395
|
+
|
|
396
|
+
@footer_text = rows_part(rows_hidden) + STRINGS[:separator] +
|
|
397
|
+
count_part(visible, total_columns, columns_hidden, :column, :columns)
|
|
398
|
+
@footer_run = render(@footer_text, @styles.footer, :left)
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def rows_part(hidden)
|
|
402
|
+
shown = Layout.format_count(@rows_shown)
|
|
403
|
+
return count_part(@rows_shown, @rows_shown, false, :row, :rows) unless hidden
|
|
404
|
+
|
|
405
|
+
total = @sample.total_rows
|
|
406
|
+
if total
|
|
407
|
+
format(STRINGS[:count_of], shown: shown, total: Layout.format_count(total), noun: STRINGS[:rows])
|
|
408
|
+
elsif @sample.rows_read > @rows_shown
|
|
409
|
+
format(STRINGS[:count_of_at_least], shown: shown, total: Layout.format_count(@sample.rows_read),
|
|
410
|
+
noun: STRINGS[:rows])
|
|
411
|
+
else
|
|
412
|
+
format(STRINGS[:count_at_least], count: shown, noun: STRINGS[:rows])
|
|
413
|
+
end
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def count_part(shown, total, hidden, singular, plural)
|
|
417
|
+
if hidden
|
|
418
|
+
format(STRINGS[:count_of], shown: Layout.format_count(shown), total: Layout.format_count(total),
|
|
419
|
+
noun: STRINGS[plural])
|
|
420
|
+
else
|
|
421
|
+
format(STRINGS[:count], count: Layout.format_count(shown), noun: STRINGS[shown == 1 ? singular : plural])
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# Step 5 (6.6): small tables fill a minimum width, or the footer's width; never above the cap.
|
|
426
|
+
def fill
|
|
427
|
+
footer_need = @footer_run ? @footer_run.extent + (2 * @edge) : 0
|
|
428
|
+
target = [[@width, @min_w, footer_need].max, @w_cap].min
|
|
429
|
+
if target > @width
|
|
430
|
+
distribute(target - @width)
|
|
431
|
+
@width = target
|
|
432
|
+
end
|
|
433
|
+
@footer_run = fit(@footer_run, @styles.footer, @width - (2 * @edge)) if @footer_run
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
def fit(run, style, max_extent) = @renderer.fit(run, style: style, max_extent: max_extent)
|
|
437
|
+
|
|
438
|
+
# Step 6 (6.7): every visible header and cell is cut to its column's text width.
|
|
439
|
+
def truncate
|
|
440
|
+
@widths.each_with_index do |width, j|
|
|
441
|
+
check!
|
|
442
|
+
m = @measured[j]
|
|
443
|
+
max = width - (2 * @pad)
|
|
444
|
+
m.header_fit = fit(m.header_run, m.header_style, max)
|
|
445
|
+
m.fits = m.runs.map do |run|
|
|
446
|
+
check!
|
|
447
|
+
fit(run, m.cell_style, max)
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
def finish
|
|
453
|
+
@texts = {}
|
|
454
|
+
@lookup = {}
|
|
455
|
+
@placements = []
|
|
456
|
+
columns = place_columns
|
|
457
|
+
peek = place_peek
|
|
458
|
+
body_rows = @message ? 1 : @rows_shown
|
|
459
|
+
footer_top = @frame + @header_h + (body_rows * @row_h)
|
|
460
|
+
place_message(@frame + @header_h, @row_h, @edge) if @message
|
|
461
|
+
place_footer(footer_top) if @footer_run
|
|
462
|
+
height = (2 * @frame) + @header_h + (body_rows * @row_h) + (@footer_text ? @footer_h : 0)
|
|
463
|
+
fade = peek && Box.new(x: @width - @frame - @fade_w, y: @frame, width: @fade_w,
|
|
464
|
+
height: @header_h + (@rows_shown * @row_h))
|
|
465
|
+
layout(visible_columns: columns.size, columns: columns, peek_column: peek, rows_shown: @rows_shown,
|
|
466
|
+
footer_text: @footer_text, message: @message, width_px: @width, height_px: height,
|
|
467
|
+
geometry: geometry(body_rows, @footer_text ? footer_top : nil, fade))
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
def place_columns
|
|
471
|
+
x = @inset
|
|
472
|
+
@widths.each_with_index.map do |width, j|
|
|
473
|
+
m = @measured[j]
|
|
474
|
+
place_column(m, m.header_fit, m.fits, x, width) { |run| run.x_in(x + @pad, x + width - @pad) }
|
|
475
|
+
column = Column.new(index: j, kind: m.kind, align: m.align, width: width / @scale.to_f, width_px: width,
|
|
476
|
+
x_px: x)
|
|
477
|
+
x += width
|
|
478
|
+
column
|
|
479
|
+
end
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
# 6.5 item 4: the peek is drawn left-aligned whatever its kind, untruncated, and clipped at the
|
|
483
|
+
# frame. A right-aligned run (a number, measured right-aligned) has its ink start at the text edge.
|
|
484
|
+
def place_peek
|
|
485
|
+
return nil unless @peek
|
|
486
|
+
|
|
487
|
+
m = measured(@peek)
|
|
488
|
+
x = @inset + @widths.sum
|
|
489
|
+
width = (@width - @frame) - x
|
|
490
|
+
place_column(m, m.header_run, m.runs, x, width) { |run| x + @pad + (run.align == :left ? run.xoffset : 0) }
|
|
491
|
+
Column.new(index: m.index, kind: m.kind, align: :left, width: width / @scale.to_f, width_px: width, x_px: x)
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Records the display strings and placements of one column's header and cells. `header_run` and
|
|
495
|
+
# `runs` are what is drawn: fitted runs for a visible column, the measured runs for the peek.
|
|
496
|
+
def place_column(m, header_run, runs, x, width, &place_x)
|
|
497
|
+
key = [:header, m.index]
|
|
498
|
+
@texts[key] = text_for(m.header, m.header_run, header_run)
|
|
499
|
+
if header_run
|
|
500
|
+
box = Box.new(x: x, y: @frame, width: width, height: @header_h - @header_rule)
|
|
501
|
+
add(key, header_run, place_x.call(header_run), band_y(header_run, m.header_style, @frame, @header_h), box)
|
|
502
|
+
end
|
|
503
|
+
runs.each_with_index do |run, r|
|
|
504
|
+
key = [:cell, r, m.index]
|
|
505
|
+
@texts[key] = text_for(m.cells[r], m.runs[r], run)
|
|
506
|
+
next unless run
|
|
507
|
+
|
|
508
|
+
top = @frame + @header_h + (r * @row_h)
|
|
509
|
+
rule = r.zero? ? 0 : @row_rule
|
|
510
|
+
box = Box.new(x: x, y: top + rule, width: width, height: @row_h - rule)
|
|
511
|
+
add(key, run, place_x.call(run), band_y(run, m.cell_style, top, @row_h), box)
|
|
512
|
+
end
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
# The display string: as built for a blank or untruncated string, the drawn run's when it was cut
|
|
516
|
+
# with "…", and "" when not even "…" fits.
|
|
517
|
+
def text_for(display, full, drawn)
|
|
518
|
+
return display if full.nil? || full.equal?(drawn)
|
|
519
|
+
|
|
520
|
+
drawn ? drawn.string : ""
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
def place_message(top, height, x_left)
|
|
524
|
+
style = @styles.message
|
|
525
|
+
run = fit(render(@message, style, :left), style, @width - (2 * @edge))
|
|
526
|
+
return unless run
|
|
527
|
+
|
|
528
|
+
x = x_left ? x_left + run.xoffset : ((@width - run.image.width) / 2.0).round
|
|
529
|
+
box = Box.new(x: @frame, y: top, width: @width - (2 * @frame), height: height)
|
|
530
|
+
add(:message, run, x, band_y(run, style, top, height), box)
|
|
531
|
+
end
|
|
532
|
+
|
|
533
|
+
def place_footer(top)
|
|
534
|
+
run = @footer_run
|
|
535
|
+
box = Box.new(x: @frame, y: top + @footer_rule, width: @width - (2 * @frame), height: @footer_h - @footer_rule)
|
|
536
|
+
add(:footer, run, @edge + run.xoffset, band_y(run, @styles.footer, top, @footer_h), box)
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
# 4.6: every run in a band shares one baseline, with cap height centred in the band.
|
|
540
|
+
def band_y(run, style, top, height) = @metrics[style].line_top(top, height) + run.yoffset
|
|
541
|
+
|
|
542
|
+
def add(key, run, x, y, box)
|
|
543
|
+
placement = Placement.new(run: run, x: x, y: y, box: box)
|
|
544
|
+
@lookup[key] = placement
|
|
545
|
+
@placements << placement
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
def geometry(body_rows, footer_top, fade)
|
|
549
|
+
Geometry.new(frame: @frame, header_height: @header_h, row_height: @row_h, footer_height: @footer_h,
|
|
550
|
+
header_rule: @header_rule, row_rule: @row_rule, footer_rule: @footer_rule,
|
|
551
|
+
body_rows: body_rows, footer_top: footer_top, fade: fade)
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
# 6.9: the empty-file layout. The message is centred in the body; the body loses rows, down to
|
|
555
|
+
# one, when the height cap is small.
|
|
556
|
+
def empty_layout
|
|
557
|
+
@width = [@min_w, @w_cap].min
|
|
558
|
+
body_rows = @theme.empty_body_rows
|
|
559
|
+
body_rows -= 1 while body_rows > 1 && (2 * @frame) + @header_h + (body_rows * @row_h) > @config.max_height_px
|
|
560
|
+
@message = if @sample.total_rows
|
|
561
|
+
STRINGS[:empty]
|
|
562
|
+
else
|
|
563
|
+
format(STRINGS[:nothing_read], size: Layout.format_size(@config.max_input_bytes))
|
|
564
|
+
end
|
|
565
|
+
@texts = {}
|
|
566
|
+
@lookup = {}
|
|
567
|
+
@placements = []
|
|
568
|
+
place_message(@frame + @header_h, body_rows * @row_h, nil)
|
|
569
|
+
layout(visible_columns: 0, columns: [], peek_column: nil, rows_shown: 0, footer_text: nil, message: @message,
|
|
570
|
+
width_px: @width, height_px: (2 * @frame) + @header_h + (body_rows * @row_h),
|
|
571
|
+
geometry: geometry(body_rows, nil, nil))
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
# 6.10: the size never exceeds the caps; a bug that would break them fails loudly instead.
|
|
575
|
+
def layout(width_px:, height_px:, **members)
|
|
576
|
+
unless width_px.positive? && height_px.positive? && width_px <= @config.max_width_px &&
|
|
577
|
+
height_px <= @config.max_height_px
|
|
578
|
+
raise RenderFailed, "layout of #{width_px} by #{height_px} px breaks the size caps " \
|
|
579
|
+
"(#{@config.max_width_px} by #{@config.max_height_px} px). This is a bug in " \
|
|
580
|
+
"csv_preview; please report it at #{Help::ISSUES}"
|
|
581
|
+
end
|
|
582
|
+
|
|
583
|
+
Layout.new(sample: @sample, width_px: width_px, height_px: height_px, scale: @scale, theme: @theme,
|
|
584
|
+
text_calls: @renderer.calls, placements: @placements, texts: @texts, lookup: @lookup,
|
|
585
|
+
max_width_px: @config.max_width_px, max_height_px: @config.max_height_px, **members)
|
|
586
|
+
end
|
|
587
|
+
end
|
|
588
|
+
private_constant :Builder
|
|
589
|
+
end
|
|
590
|
+
end
|