railbow 0.6.2 → 0.8.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 +4 -4
- data/CHANGELOG.md +78 -0
- data/README.md +47 -6
- data/lib/railbow/about_formatter.rb +57 -21
- data/lib/railbow/calendar.rb +2 -4
- data/lib/railbow/clock.rb +72 -0
- data/lib/railbow/color_assigner.rb +6 -12
- data/lib/railbow/config.rb +1 -1
- data/lib/railbow/demo/status_demo.rb +4 -11
- data/lib/railbow/formatters/base.rb +11 -32
- data/lib/railbow/init.rb +7 -1
- data/lib/railbow/migration_formatter.rb +41 -25
- data/lib/railbow/migration_parser.rb +12 -19
- data/lib/railbow/multi_db.rb +15 -1
- data/lib/railbow/name_collision_resolver.rb +14 -27
- data/lib/railbow/name_formatter.rb +15 -16
- data/lib/railbow/notes_formatter.rb +42 -19
- data/lib/railbow/params.rb +33 -27
- data/lib/railbow/railtie.rb +8 -6
- data/lib/railbow/routes_formatter.rb +18 -6
- data/lib/railbow/stats_formatter.rb +139 -69
- data/lib/railbow/status/ghosts.rb +5 -6
- data/lib/railbow/status/git_data.rb +148 -87
- data/lib/railbow/status/help.rb +10 -1
- data/lib/railbow/status/printer.rb +49 -35
- data/lib/railbow/status/section.rb +116 -62
- data/lib/railbow/table/renderer.rb +27 -35
- data/lib/railbow/tasks/migrate_status.rake +33 -17
- data/lib/railbow/tasks/stats.rake +11 -75
- data/lib/railbow/version.rb +1 -1
- data/lib/railbow.rb +28 -4
- metadata +2 -2
|
@@ -12,16 +12,11 @@ require_relative "ghosts"
|
|
|
12
12
|
|
|
13
13
|
module Railbow
|
|
14
14
|
module Status
|
|
15
|
-
# One migrations directory's worth of db:migrate:status.
|
|
16
|
-
#
|
|
17
|
-
# Usually that is one database. Databases sharing a migrations_paths (the
|
|
18
|
-
# shape horizontal sharding takes) see the same files and differ only in
|
|
19
|
-
# what they have applied, so they merge into a single section carrying a
|
|
15
|
+
# One migrations directory's worth of db:migrate:status. Databases sharing
|
|
16
|
+
# a migrations_paths (horizontal sharding) merge into one section with a
|
|
20
17
|
# status per database.
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
# before the status cells can be built. A section never prints: Printer
|
|
24
|
-
# owns output, which is what lets sections share column widths.
|
|
18
|
+
# Rows are built lazily because merging must finish first. Printer owns
|
|
19
|
+
# output, which is what lets sections share column widths.
|
|
25
20
|
class Section
|
|
26
21
|
# Index of the Date column, where calendar week ticks are drawn.
|
|
27
22
|
TICK_COL = 2
|
|
@@ -42,8 +37,11 @@ module Railbow
|
|
|
42
37
|
attr_reader :databases, :migrations_key, :state, :hidden_count, :total_count,
|
|
43
38
|
:since_value, :entries, :ghosts
|
|
44
39
|
|
|
45
|
-
#
|
|
46
|
-
#
|
|
40
|
+
# What stock Rails prints for the databases in this section, shown if
|
|
41
|
+
# railbow fails to render it.
|
|
42
|
+
attr_accessor :fallback_output
|
|
43
|
+
|
|
44
|
+
# True when the floor topped the section back up, so the table shows
|
|
47
45
|
# migrations from outside the window it advertises.
|
|
48
46
|
def floor_applied?
|
|
49
47
|
@floor_applied == true
|
|
@@ -54,6 +52,8 @@ module Railbow
|
|
|
54
52
|
@databases = [{name: pool.db_config.name, database: pool.db_config.database}]
|
|
55
53
|
@migrations_key = migrations_key_for(pool)
|
|
56
54
|
@entries = {}
|
|
55
|
+
@all_entries = {}
|
|
56
|
+
@version_to_file = {}
|
|
57
57
|
@ghosts = {}
|
|
58
58
|
@hidden_count = 0
|
|
59
59
|
@total_count = 0
|
|
@@ -64,8 +64,7 @@ module Railbow
|
|
|
64
64
|
collect(pool)
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
-
#
|
|
68
|
-
# #databases; this is the single-database convenience.
|
|
67
|
+
# Single-database convenience; multi-database sections use #databases.
|
|
69
68
|
def database
|
|
70
69
|
databases.first[:database]
|
|
71
70
|
end
|
|
@@ -82,16 +81,14 @@ module Railbow
|
|
|
82
81
|
migrations_key == other.migrations_key
|
|
83
82
|
end
|
|
84
83
|
|
|
85
|
-
# Folds another database
|
|
86
|
-
#
|
|
87
|
-
# header, and anything already built is discarded.
|
|
84
|
+
# Folds in another database running the same migration files. Anything
|
|
85
|
+
# already built is discarded.
|
|
88
86
|
def merge!(other)
|
|
89
|
-
other.entries
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
mine[:statuses].merge!(entry[:statuses])
|
|
93
|
-
end
|
|
87
|
+
merge_entries(@entries, other.entries)
|
|
88
|
+
merge_entries(@all_entries, other.all_entries)
|
|
89
|
+
@version_to_file = other.version_to_file.merge(@version_to_file)
|
|
94
90
|
@databases.concat(other.databases)
|
|
91
|
+
@fallback_output = [fallback_output, other.fallback_output].compact.join
|
|
95
92
|
@ghosts = other.ghosts.merge(@ghosts)
|
|
96
93
|
@total_count = [@total_count, other.total_count].max
|
|
97
94
|
@hidden_count = [@hidden_count, other.hidden_count].max
|
|
@@ -107,10 +104,59 @@ module Railbow
|
|
|
107
104
|
databases.size > 1
|
|
108
105
|
end
|
|
109
106
|
|
|
110
|
-
#
|
|
111
|
-
#
|
|
112
|
-
#
|
|
113
|
-
|
|
107
|
+
# Versions the checked-out branch adds to this database, wherever they
|
|
108
|
+
# fall relative to the time window. Needs RBW_GIT=diff, which is what
|
|
109
|
+
# already makes railbow branch-aware.
|
|
110
|
+
def branch_versions
|
|
111
|
+
return @branch_versions if @branch_versions
|
|
112
|
+
return @branch_versions = [] unless Railbow::Params.git_diff?
|
|
113
|
+
|
|
114
|
+
files = GitData.branch_files(migrations_key, base_override: Railbow::Params.git_base)
|
|
115
|
+
@branch_versions = @all_entries.keys.select do |version|
|
|
116
|
+
file = version_to_file[version]
|
|
117
|
+
file && files.include?(File.basename(file))
|
|
118
|
+
end.sort
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Adds the branch's migrations the time window left out, so a drawn
|
|
122
|
+
# table never hides your own work.
|
|
123
|
+
def include_branch!
|
|
124
|
+
missing = branch_versions - @entries.keys
|
|
125
|
+
return self if missing.empty?
|
|
126
|
+
|
|
127
|
+
@entries = @entries.merge(@all_entries.slice(*missing))
|
|
128
|
+
@hidden_count -= missing.size
|
|
129
|
+
reset_built
|
|
130
|
+
self
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Keeps only the branch's migrations, for a database that would otherwise
|
|
134
|
+
# collapse to a summary line.
|
|
135
|
+
def narrow_to_branch!
|
|
136
|
+
@entries = @all_entries.slice(*branch_versions)
|
|
137
|
+
@hidden_count = total_count - @entries.size
|
|
138
|
+
@floor_applied = false
|
|
139
|
+
@branch_only = true
|
|
140
|
+
reset_built
|
|
141
|
+
self
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def branch_only?
|
|
145
|
+
@branch_only == true
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Pending migrations not among the rows drawn. A narrowed section still
|
|
149
|
+
# owes the reader what its summary line would have said.
|
|
150
|
+
def hidden_pending_count
|
|
151
|
+
pending_count - entries.count { |_, e| e[:statuses].value?("down") }
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def hidden_ghost_count
|
|
155
|
+
ghost_count - entries.count { |_, e| e[:name].to_s.include?(NO_FILE) }
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Answered from collected entries, so it costs no git work: it is asked
|
|
159
|
+
# to decide whether to build the rows at all.
|
|
114
160
|
def pending_in_view?
|
|
115
161
|
entries.any? { |_, entry| entry[:statuses].value?("down") }
|
|
116
162
|
end
|
|
@@ -135,6 +181,12 @@ module Railbow
|
|
|
135
181
|
|
|
136
182
|
attr_reader :latest_applied_version
|
|
137
183
|
|
|
184
|
+
protected
|
|
185
|
+
|
|
186
|
+
attr_reader :all_entries, :version_to_file
|
|
187
|
+
|
|
188
|
+
public
|
|
189
|
+
|
|
138
190
|
def latest_applied_date
|
|
139
191
|
version_date(latest_applied_version) if latest_applied_version
|
|
140
192
|
end
|
|
@@ -171,7 +223,7 @@ module Railbow
|
|
|
171
223
|
|
|
172
224
|
private
|
|
173
225
|
|
|
174
|
-
attr_reader :formatter, :git
|
|
226
|
+
attr_reader :formatter, :git
|
|
175
227
|
|
|
176
228
|
def migrations_key_for(pool)
|
|
177
229
|
paths = Array(pool.migration_context.migrations_paths)
|
|
@@ -186,6 +238,20 @@ module Railbow
|
|
|
186
238
|
@built = false
|
|
187
239
|
end
|
|
188
240
|
|
|
241
|
+
def merge_entries(into, from)
|
|
242
|
+
from.each do |version, entry|
|
|
243
|
+
mine = (into[version] ||= {name: entry[:name], statuses: {}})
|
|
244
|
+
mine[:name] = entry[:name] if mine[:name].include?(NO_FILE)
|
|
245
|
+
mine[:statuses].merge!(entry[:statuses])
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def entries_for(db_list)
|
|
250
|
+
db_list.to_h do |status, version, name|
|
|
251
|
+
[version.to_s, {name: name, statuses: {db_name => status}}]
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
189
255
|
def collect(pool)
|
|
190
256
|
db_list = pool.migration_context.migrations_status
|
|
191
257
|
@total_count = db_list.size
|
|
@@ -196,22 +262,20 @@ module Railbow
|
|
|
196
262
|
return
|
|
197
263
|
end
|
|
198
264
|
|
|
265
|
+
# The whole list, unfiltered: a summarized database can still show the
|
|
266
|
+
# branch's migrations, and those may sit outside the window.
|
|
267
|
+
@all_entries = entries_for(db_list)
|
|
268
|
+
pool.migration_context.migrations.each do |m|
|
|
269
|
+
@version_to_file[m.version.to_s] = m.filename
|
|
270
|
+
end
|
|
271
|
+
|
|
199
272
|
db_list = apply_since_filter(db_list)
|
|
200
273
|
|
|
201
274
|
# State tracks recency, not row count: a section whose only rows were
|
|
202
275
|
# pulled in by the floor still has nothing recent to say, and should
|
|
203
276
|
# still collapse in a multi-database run.
|
|
204
277
|
@state = @window_count.zero? ? :none_in_period : :ok
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
@entries = db_list.to_h do |status, version, name|
|
|
208
|
-
[version.to_s, {name: name, statuses: {db_name => status}}]
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
@version_to_file = {}
|
|
212
|
-
pool.migration_context.migrations.each do |m|
|
|
213
|
-
@version_to_file[m.version.to_s] = m.filename
|
|
214
|
-
end
|
|
278
|
+
@entries = entries_for(db_list)
|
|
215
279
|
end
|
|
216
280
|
|
|
217
281
|
# Counted over every migration, not just the ones in the window, so a
|
|
@@ -223,11 +287,9 @@ module Railbow
|
|
|
223
287
|
@latest_applied_version = applied.last&.dig(1)&.to_s
|
|
224
288
|
end
|
|
225
289
|
|
|
226
|
-
# The time window is a soft limit
|
|
227
|
-
#
|
|
228
|
-
#
|
|
229
|
-
# old. The window is still what decides whether the section reads as
|
|
230
|
-
# recent - see #collect.
|
|
290
|
+
# The time window is a soft limit: the floor tops the result back up to
|
|
291
|
+
# RBW_SINCE_MIN rows. The window alone still decides whether the section
|
|
292
|
+
# reads as recent (see #collect).
|
|
231
293
|
def apply_since_filter(db_list)
|
|
232
294
|
since_cutoff = Railbow::Params.parse_since(since_value, context: "migrations")
|
|
233
295
|
unless since_cutoff
|
|
@@ -235,7 +297,7 @@ module Railbow
|
|
|
235
297
|
return db_list
|
|
236
298
|
end
|
|
237
299
|
|
|
238
|
-
cutoff_version =
|
|
300
|
+
cutoff_version = Railbow::Clock.version_at(since_cutoff).to_i
|
|
239
301
|
@window_count = db_list.count { |_, v, _| v.to_i >= cutoff_version }
|
|
240
302
|
|
|
241
303
|
# Not Comparable#clamp: the floor can exceed the total, and clamp
|
|
@@ -294,8 +356,9 @@ module Railbow
|
|
|
294
356
|
@tables_enabled = Railbow::Params.view_tables?
|
|
295
357
|
end
|
|
296
358
|
|
|
297
|
-
# Git lookups and ghost recovery happen here rather than during
|
|
298
|
-
# so a section that ends up collapsed never pays for them.
|
|
359
|
+
# Git history lookups and ghost recovery happen here rather than during
|
|
360
|
+
# collect, so a section that ends up collapsed never pays for them. It
|
|
361
|
+
# pays only the cheap branch lookup (#branch_versions).
|
|
299
362
|
def build!
|
|
300
363
|
@built = true
|
|
301
364
|
@columns = []
|
|
@@ -313,13 +376,9 @@ module Railbow
|
|
|
313
376
|
@calendar = build_calendar
|
|
314
377
|
end
|
|
315
378
|
|
|
316
|
-
#
|
|
317
|
-
#
|
|
318
|
-
#
|
|
319
|
-
# On a terminal too narrow for the full row the width budget degrades the
|
|
320
|
-
# table instead of letting it wrap: the name column shrinks first, then
|
|
321
|
-
# Tables is dropped, then the formatted date (the raw Migration ID keeps
|
|
322
|
-
# the timestamp), then Who.
|
|
379
|
+
# On a narrow terminal the table degrades instead of wrapping: the name
|
|
380
|
+
# column shrinks first, then Tables, the date (the Migration ID keeps the
|
|
381
|
+
# timestamp) and Who are dropped, in that order.
|
|
323
382
|
def build_columns
|
|
324
383
|
@name_col_width = needs_name_truncation? ? NAME_COL_WIDTH : nil
|
|
325
384
|
|
|
@@ -377,9 +436,8 @@ module Railbow
|
|
|
377
436
|
name = entry[:name]
|
|
378
437
|
statuses = entry[:statuses]
|
|
379
438
|
|
|
380
|
-
#
|
|
381
|
-
#
|
|
382
|
-
# a row pending everywhere reads as inactive.
|
|
439
|
+
# Pending rows are greyed out as not in effect; in a shard group only
|
|
440
|
+
# a row pending everywhere.
|
|
383
441
|
@down_rows << idx if statuses.values.all? { |s| s == "down" }
|
|
384
442
|
|
|
385
443
|
ghost = name.include?(NO_FILE) ? ghosts[version] : nil
|
|
@@ -426,8 +484,7 @@ module Railbow
|
|
|
426
484
|
@status_aliases[status] || status
|
|
427
485
|
end
|
|
428
486
|
|
|
429
|
-
#
|
|
430
|
-
# carries. Yields a status indicator when the row earns one.
|
|
487
|
+
# Yields a status indicator when the row earns one.
|
|
431
488
|
def name_cell(name:, version:, ghost:, latest_mig_date:)
|
|
432
489
|
if ghost
|
|
433
490
|
return with_tags(Ghosts.display_name(ghost), Ghosts.tag(ghost), strip: false)
|
|
@@ -468,8 +525,8 @@ module Railbow
|
|
|
468
525
|
formatter.landed_tag(landed, fresh: latest_mig_date && landed >= latest_mig_date)
|
|
469
526
|
end
|
|
470
527
|
|
|
471
|
-
#
|
|
472
|
-
#
|
|
528
|
+
# Tags sit flush right in a capped column, two spaces after the name
|
|
529
|
+
# otherwise.
|
|
473
530
|
def with_tags(name, tags, strip: true)
|
|
474
531
|
return name if tags.nil? || tags.empty?
|
|
475
532
|
return "#{name} #{tags}" unless @name_col_width
|
|
@@ -553,10 +610,7 @@ module Railbow
|
|
|
553
610
|
end
|
|
554
611
|
|
|
555
612
|
def version_date(version)
|
|
556
|
-
|
|
557
|
-
Date.new(v[0..3].to_i, v[4..5].to_i, v[6..7].to_i)
|
|
558
|
-
rescue Date::Error
|
|
559
|
-
nil
|
|
613
|
+
Railbow::Clock.from_version(version)&.to_date
|
|
560
614
|
end
|
|
561
615
|
end
|
|
562
616
|
end
|
|
@@ -61,10 +61,9 @@ module Railbow
|
|
|
61
61
|
|
|
62
62
|
rows = prepare_rows(rows)
|
|
63
63
|
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
# again with what is left.
|
|
64
|
+
# When even fully shrunk columns overflow the terminal, drop the most
|
|
65
|
+
# expendable column and render again. One column per pass: the reduced
|
|
66
|
+
# renderer asks again with what is left.
|
|
68
67
|
if (drop = drop_index_to_fit(rows))
|
|
69
68
|
return dropped_renderer(drop).render(
|
|
70
69
|
rows.map { |row| row.reject.with_index { |_, i| i == drop } },
|
|
@@ -76,7 +75,6 @@ module Railbow
|
|
|
76
75
|
resolved = resolve_widths(rows)
|
|
77
76
|
shrink_to_fit!(resolved, rows)
|
|
78
77
|
|
|
79
|
-
# In oneline mode, truncate non-sticky non-last columns at resolved width
|
|
80
78
|
if @compact[:oneline]
|
|
81
79
|
rows = rows.map { |row|
|
|
82
80
|
row.each_with_index.map { |cell, i|
|
|
@@ -134,7 +132,7 @@ module Railbow
|
|
|
134
132
|
if col.fixed?
|
|
135
133
|
w = col.width
|
|
136
134
|
elsif i == last
|
|
137
|
-
#
|
|
135
|
+
# The last column flexes to the terminal, so it is never padded
|
|
138
136
|
w = 0
|
|
139
137
|
else
|
|
140
138
|
header_w = display_width(effective_label(col))
|
|
@@ -205,8 +203,6 @@ module Railbow
|
|
|
205
203
|
"#{pad}#{content}#{RESET}#{pad}"
|
|
206
204
|
}
|
|
207
205
|
|
|
208
|
-
# Join prefix parts with per-position separators.
|
|
209
|
-
# Loop index i joins column i-1 and column i (separator index = i-1).
|
|
210
206
|
# dup: appending to prefix_parts.first itself would corrupt the parts
|
|
211
207
|
# that render_last_cell measures for the wrapped-line indent.
|
|
212
208
|
prefix = prefix_parts.first.to_s.dup
|
|
@@ -216,7 +212,6 @@ module Railbow
|
|
|
216
212
|
|
|
217
213
|
last_cell_raw = row[last].to_s
|
|
218
214
|
|
|
219
|
-
# Separator before the last column has index (last - 1)
|
|
220
215
|
last_sep = separator_at(last - 1, tick_col)
|
|
221
216
|
render_last_cell(prefix, prefix_parts, last_cell_raw, widths, last, col_sep: last_sep, highlight: highlight, ghost: ghost, dim: dim)
|
|
222
217
|
end
|
|
@@ -225,7 +220,6 @@ module Railbow
|
|
|
225
220
|
pad = effective_padding
|
|
226
221
|
sep = col_sep || theme.col_separator
|
|
227
222
|
|
|
228
|
-
# Truncate if configured via column settings
|
|
229
223
|
if columns[last].truncate && columns[last].max_width
|
|
230
224
|
last_cell_raw = truncate_str(last_cell_raw, columns[last].max_width)
|
|
231
225
|
end
|
|
@@ -235,7 +229,7 @@ module Railbow
|
|
|
235
229
|
prefix_width = compute_prefix_width(widths, last)
|
|
236
230
|
last_col_max = term_w ? [term_w - prefix_width - display_width(pad), MIN_LAST_COL].max : nil
|
|
237
231
|
|
|
238
|
-
#
|
|
232
|
+
# A column's own truncate_fn knows its content (table tags fold into +N)
|
|
239
233
|
if columns[last].truncate_fn && last_col_max &&
|
|
240
234
|
display_width(last_cell_plain) > last_col_max
|
|
241
235
|
last_cell_raw = columns[last].truncate_fn.call(last_cell_raw, last_col_max)
|
|
@@ -243,7 +237,6 @@ module Railbow
|
|
|
243
237
|
return "#{prefix}#{sep}#{pad}#{last_cell_raw}#{RESET}#{pad}"
|
|
244
238
|
end
|
|
245
239
|
|
|
246
|
-
# Truncate to terminal width (by whole words) instead of wrapping
|
|
247
240
|
if columns[last].truncate && !columns[last].max_width && last_col_max &&
|
|
248
241
|
display_width(last_cell_plain) > last_col_max
|
|
249
242
|
last_cell_raw = truncate_by_words(last_cell_raw, last_col_max)
|
|
@@ -251,7 +244,6 @@ module Railbow
|
|
|
251
244
|
return "#{prefix}#{sep}#{pad}#{last_cell_raw}#{RESET}#{pad}"
|
|
252
245
|
end
|
|
253
246
|
|
|
254
|
-
# In oneline mode, truncate instead of wrapping
|
|
255
247
|
if @compact[:oneline] && last_col_max && display_width(last_cell_plain) > last_col_max
|
|
256
248
|
last_cell_raw = truncate_by_words(last_cell_raw, last_col_max)
|
|
257
249
|
last_cell_raw = style_cell(last_cell_raw, highlight: highlight, ghost: ghost, dim: dim, accent: columns[last].accent)
|
|
@@ -260,17 +252,21 @@ module Railbow
|
|
|
260
252
|
|
|
261
253
|
last_cell_raw = style_cell(last_cell_raw, highlight: highlight, ghost: ghost, dim: dim, accent: columns[last].accent)
|
|
262
254
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
255
|
+
whole = "#{prefix}#{sep}#{pad}#{last_cell_raw}#{RESET}#{pad}"
|
|
256
|
+
return whole unless last_col_max && display_width(strip_ansi(last_cell_raw)) > last_col_max
|
|
257
|
+
|
|
258
|
+
# Wrap only when every line fits beside the fixed columns. Otherwise the
|
|
259
|
+
# terminal re-wraps each padded continuation line, leaving a blank line
|
|
260
|
+
# before every fragment, so the value stays on its row whole.
|
|
261
|
+
room = term_w - prefix_width - display_width(pad)
|
|
262
|
+
wrapped = ansi_word_wrap(last_cell_raw, last_col_max)
|
|
263
|
+
return whole unless wrapped.all? { |line| display_width(strip_ansi(line)) <= room }
|
|
264
|
+
|
|
265
|
+
blank_prefix = prefix_parts.map { |part|
|
|
266
|
+
" " * display_width(strip_ansi(part))
|
|
267
|
+
}.join(sep)
|
|
268
|
+
"#{prefix}#{sep}#{pad}#{wrapped.first}#{RESET}#{pad}\n" +
|
|
269
|
+
wrapped[1..].map { |line| "#{blank_prefix}#{sep}#{pad}#{line}#{RESET}#{pad}" }.join("\n")
|
|
274
270
|
end
|
|
275
271
|
|
|
276
272
|
def style_cell(content, highlight: false, ghost: false, dim: false, accent: false)
|
|
@@ -294,14 +290,13 @@ module Railbow
|
|
|
294
290
|
def compute_prefix_width(widths, last)
|
|
295
291
|
pad_w = display_width(effective_padding)
|
|
296
292
|
sep_w = display_width(theme.col_separator)
|
|
297
|
-
#
|
|
293
|
+
# Padded fixed columns, the separator after each, and the last
|
|
294
|
+
# column's leading pad.
|
|
298
295
|
total = 0
|
|
299
296
|
(0...last).each do |i|
|
|
300
297
|
total += pad_w + widths[i] + pad_w
|
|
301
298
|
end
|
|
302
|
-
# Separators between columns + trailing separator before last column
|
|
303
299
|
total += sep_w * last
|
|
304
|
-
# Plus pad on last column
|
|
305
300
|
total += pad_w
|
|
306
301
|
total
|
|
307
302
|
end
|
|
@@ -413,11 +408,9 @@ module Railbow
|
|
|
413
408
|
widths
|
|
414
409
|
end
|
|
415
410
|
|
|
416
|
-
#
|
|
417
|
-
#
|
|
418
|
-
#
|
|
419
|
-
# too - it is drawn as-is, so a label wider than every cell would poke
|
|
420
|
-
# past the terminal edge otherwise.
|
|
411
|
+
# Every fixed column plus a reserve for the last column, which truncates
|
|
412
|
+
# down to MIN_LAST_COL but no further. Its header label counts too: it is
|
|
413
|
+
# drawn as-is, so a label wider than every cell would poke past the edge.
|
|
421
414
|
def required_width(widths, rows)
|
|
422
415
|
last = columns.size - 1
|
|
423
416
|
last_w = rows.map { |row| display_width(strip_ansi(row[last].to_s)) }.max || 0
|
|
@@ -567,14 +560,13 @@ module Railbow
|
|
|
567
560
|
value_aliases = @aliases[:values]
|
|
568
561
|
return rows unless value_aliases&.any?
|
|
569
562
|
|
|
570
|
-
# Build column index → value alias map
|
|
571
563
|
col_map = {}
|
|
572
564
|
columns.each_with_index do |col, i|
|
|
573
565
|
next unless col.aliased
|
|
574
566
|
|
|
575
567
|
label = col.label
|
|
576
568
|
col_map[i] = value_aliases[label] if value_aliases[label]
|
|
577
|
-
#
|
|
569
|
+
# Value aliases may name a renamed column by its original label
|
|
578
570
|
original = @reverse_col_aliases[label]
|
|
579
571
|
col_map[i] = value_aliases[original] if original && value_aliases[original]
|
|
580
572
|
end
|
|
@@ -595,7 +587,7 @@ module Railbow
|
|
|
595
587
|
|
|
596
588
|
def apply_cell_alias(cell, aliases_for_col)
|
|
597
589
|
plain = strip_ansi(cell)
|
|
598
|
-
#
|
|
590
|
+
# Exact match first, then a prefix match for cells with an indicator appended
|
|
599
591
|
replacement = aliases_for_col[plain]
|
|
600
592
|
if replacement
|
|
601
593
|
cell.sub(plain) { replacement }
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "stringio"
|
|
3
4
|
require_relative "../multi_db"
|
|
4
5
|
require_relative "../status/help"
|
|
5
6
|
require_relative "../status/printer"
|
|
6
7
|
require_relative "../status/section"
|
|
7
8
|
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
# the loop is what lets railbow render the databases together instead of one
|
|
13
|
-
# blind table each.
|
|
9
|
+
# Rails calls migrate_status once per database inside
|
|
10
|
+
# with_temporary_pool_for_each, for db:migrate:status and
|
|
11
|
+
# db:migrate:status:<database_name> alike. Wrapping that loop is what lets
|
|
12
|
+
# railbow render the databases together instead of one blind table each.
|
|
14
13
|
module Railbow
|
|
15
14
|
module MigrateStatusFormatter
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
# applying its own defaults.
|
|
15
|
+
# Rails 7.2 through 8.1 all route through here. Arguments pass verbatim so
|
|
16
|
+
# Rails keeps applying its own defaults.
|
|
19
17
|
def with_temporary_pool_for_each(*args, **kwargs, &block)
|
|
20
18
|
Railbow::MultiDb.batch { super(*args, **kwargs, &block) }
|
|
21
19
|
end
|
|
@@ -39,17 +37,35 @@ module Railbow
|
|
|
39
37
|
Kernel.abort "Schema migrations table does not exist yet."
|
|
40
38
|
end
|
|
41
39
|
|
|
42
|
-
|
|
40
|
+
# Outside the guard: an error here is Rails' own (a database that is
|
|
41
|
+
# down) and must raise as it would without railbow. Captured while the
|
|
42
|
+
# pool is still open, it is also the fallback.
|
|
43
|
+
stock = railbow_capture_stdout { super }
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
batch
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
Railbow.guard(-> { $stdout.write(stock) }) do
|
|
46
|
+
section = Railbow::Status::Section.new(migration_connection_pool)
|
|
47
|
+
section.fallback_output = stock
|
|
48
|
+
|
|
49
|
+
# No batch (a direct call, or a Rails that skips
|
|
50
|
+
# with_temporary_pool_for_each): the section prints on its own.
|
|
51
|
+
if batch
|
|
52
|
+
batch.add(section)
|
|
53
|
+
else
|
|
54
|
+
Railbow::Status::Printer.new([section]).print
|
|
55
|
+
end
|
|
51
56
|
end
|
|
52
57
|
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def railbow_capture_stdout
|
|
62
|
+
original = $stdout
|
|
63
|
+
$stdout = StringIO.new
|
|
64
|
+
yield
|
|
65
|
+
$stdout.string
|
|
66
|
+
ensure
|
|
67
|
+
$stdout = original
|
|
68
|
+
end
|
|
53
69
|
end
|
|
54
70
|
end
|
|
55
71
|
|
|
@@ -1,85 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
# CodeStatistics is autoloaded by Rails when the `stats` task runs,
|
|
5
|
-
# but is not available at rake-file load time.
|
|
6
|
-
#
|
|
7
|
-
# Rails 8.1 namespaced the class to Rails::CodeStatistics, removed
|
|
8
|
-
# the bare `require "code_statistics"` shim, and dropped the built-in
|
|
9
|
-
# `stats` rake task. We handle both old and new layouts.
|
|
10
|
-
Rails.application.config.after_initialize do
|
|
11
|
-
klass = nil
|
|
3
|
+
require_relative "../stats_formatter"
|
|
12
4
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
rescue LoadError
|
|
17
|
-
# fall through
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
unless klass
|
|
21
|
-
begin
|
|
22
|
-
require "code_statistics"
|
|
23
|
-
klass = CodeStatistics
|
|
24
|
-
rescue LoadError
|
|
25
|
-
# code_statistics not available (e.g., production without railties dev deps)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
5
|
+
# Rails 7.2 runs `rails stats` as a rake task that never boots the app, so the
|
|
6
|
+
# railtie initializer does not fire there: install from here as well.
|
|
7
|
+
Railbow::StatsFormatter.install!
|
|
28
8
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# Rails 8.1 removed the built-in `stats` task. Re-define it so that
|
|
36
|
-
# `rails stats` / `rake stats` keeps working with Railbow formatting.
|
|
37
|
-
unless Rake::Task.task_defined?(:stats)
|
|
9
|
+
# Rails 8.1 dropped the `stats` rake task for the `rails stats` command; this
|
|
10
|
+
# keeps `rake stats` working. Gated on the version, not task_defined?: railtie
|
|
11
|
+
# tasks load before Rails' own, so on older Rails both would run.
|
|
12
|
+
if Rails.gem_version >= Gem::Version.new("8.1")
|
|
38
13
|
desc "Report code statistics (lines / LOC / classes / methods / M/C / LOC/M)"
|
|
39
14
|
task stats: :environment do
|
|
40
|
-
|
|
41
|
-
Rails
|
|
42
|
-
elsif defined?(CodeStatistics)
|
|
43
|
-
CodeStatistics
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
unless klass
|
|
47
|
-
abort "CodeStatistics is not available. Make sure railties is loaded."
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
stat_directories = [
|
|
51
|
-
["Controllers", "app/controllers"],
|
|
52
|
-
["Helpers", "app/helpers"],
|
|
53
|
-
["Jobs", "app/jobs"],
|
|
54
|
-
["Models", "app/models"],
|
|
55
|
-
["Mailers", "app/mailers"],
|
|
56
|
-
["Channels", "app/channels"],
|
|
57
|
-
["JavaScripts", "app/javascript"],
|
|
58
|
-
["Libraries", "lib"],
|
|
59
|
-
["APIs", "app/apis"],
|
|
60
|
-
["Controller tests", "test/controllers"],
|
|
61
|
-
["Helper tests", "test/helpers"],
|
|
62
|
-
["Job tests", "test/jobs"],
|
|
63
|
-
["Model tests", "test/models"],
|
|
64
|
-
["Mailer tests", "test/mailers"],
|
|
65
|
-
["Channel tests", "test/channels"],
|
|
66
|
-
["Integration tests", "test/integration"],
|
|
67
|
-
["System tests", "test/system"],
|
|
68
|
-
["Controller specs", "spec/controllers"],
|
|
69
|
-
["Helper specs", "spec/helpers"],
|
|
70
|
-
["Job specs", "spec/jobs"],
|
|
71
|
-
["Model specs", "spec/models"],
|
|
72
|
-
["Mailer specs", "spec/mailers"],
|
|
73
|
-
["Channel specs", "spec/channels"],
|
|
74
|
-
["Request specs", "spec/requests"],
|
|
75
|
-
["System specs", "spec/system"]
|
|
76
|
-
]
|
|
77
|
-
|
|
78
|
-
root = Rails.root.to_s
|
|
79
|
-
pairs = stat_directories
|
|
80
|
-
.map { |name, dir| [name, File.join(root, dir)] }
|
|
15
|
+
stat_directories = Rails::CodeStatistics.directories
|
|
16
|
+
.map { |name, dir| [name, Rails.root.join(dir)] }
|
|
81
17
|
.select { |_, dir| File.directory?(dir) }
|
|
82
18
|
|
|
83
|
-
|
|
19
|
+
Rails::CodeStatistics.new(*stat_directories).to_s
|
|
84
20
|
end
|
|
85
21
|
end
|
data/lib/railbow/version.rb
CHANGED