Skip to content

Commit d01011c

Browse files
authored
Merge pull request #21451 from Homebrew/move-formatting-methods-to-formatter
formatter: move formatting methods from kernel
2 parents b5c9928 + b95d9ce commit d01011c

File tree

13 files changed

+137
-82
lines changed

13 files changed

+137
-82
lines changed

Library/Homebrew/cask/info.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def self.installation_info(cask, installed:)
7272
tab = Tab.for_cask(cask)
7373

7474
info = ["Installed"]
75-
info << "#{versioned_staged_path} (#{disk_usage_readable(path_details)})"
75+
info << "#{versioned_staged_path} (#{Formatter.disk_usage_readable(path_details)})"
7676
info << " #{tab}" if tab.tabfile&.exist?
7777
info.join("\n")
7878
end

Library/Homebrew/cmd/cleanup.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def run
5252
cleanup.clean!(quiet: args.quiet?, periodic: false)
5353

5454
unless cleanup.disk_cleanup_size.zero?
55-
disk_space = disk_usage_readable(cleanup.disk_cleanup_size)
55+
disk_space = Formatter.disk_usage_readable(cleanup.disk_cleanup_size)
5656
if args.dry_run?
5757
ohai "This operation would free approximately #{disk_space} of disk space."
5858
else

Library/Homebrew/cmd/info.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ def info_formula(formula)
338338
bottle.fetch_tab(quiet: !args.debug?) if args.fetch_manifest?
339339
bottle_size = bottle.bottle_size
340340
installed_size = bottle.installed_size
341-
puts "Bottle Size: #{disk_usage_readable(bottle_size)}" if bottle_size
342-
puts "Installed Size: #{disk_usage_readable(installed_size)}" if installed_size
341+
puts "Bottle Size: #{Formatter.disk_usage_readable(bottle_size)}" if bottle_size
342+
puts "Installed Size: #{Formatter.disk_usage_readable(installed_size)}" if installed_size
343343
rescue RuntimeError => e
344344
odebug e
345345
end
@@ -432,14 +432,16 @@ def print_sizes_table(title, items)
432432
ohai title
433433

434434
total_size = items.sum(&:size)
435-
total_size_str = disk_usage_readable(total_size)
435+
total_size_str = Formatter.disk_usage_readable(total_size)
436436

437437
name_width = (items.map { |item| item.name.length } + [5]).max
438-
size_width = (items.map { |item| disk_usage_readable(item.size).length } + [total_size_str.length]).max
438+
size_width = (items.map do |item|
439+
Formatter.disk_usage_readable(item.size).length
440+
end + [total_size_str.length]).max
439441

440442
items.each do |item|
441443
puts format("%-#{name_width}s %#{size_width}s", item.name,
442-
disk_usage_readable(item.size))
444+
Formatter.disk_usage_readable(item.size))
443445
end
444446

445447
puts format("%-#{name_width}s %#{size_width}s", "Total", total_size_str)

Library/Homebrew/download_queue.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ def message_with_progress(downloadable, future, message, message_length_max)
301301
size_length = 5
302302
unit_length = 2
303303
size_formatting_string = "%<size>#{size_length}.#{precision}f%<unit>#{unit_length}s"
304-
size, unit = disk_usage_readable_size_unit(fetched_size, precision:)
304+
size, unit = Formatter.disk_usage_readable_size_unit(fetched_size, precision:)
305305
formatted_fetched_size = format(size_formatting_string, size:, unit:)
306306

307307
total_size = downloadable.total_size
308308
formatted_total_size = if future.fulfilled?
309309
formatted_fetched_size
310310
elsif total_size
311-
size, unit = disk_usage_readable_size_unit(total_size, precision:)
311+
size, unit = Formatter.disk_usage_readable_size_unit(total_size, precision:)
312312
format(size_formatting_string, size:, unit:)
313313
else
314314
# fill in the missing spaces for the size if we don't have it yet.

Library/Homebrew/exceptions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ def initialize(cmd, status:, output: nil, secrets: [])
692692
status.termsig
693693
end
694694

695-
redacted_cmd = redact_secrets(cmd.shelljoin.gsub('\=', "="), secrets)
695+
redacted_cmd = Formatter.redact_secrets(cmd.shelljoin.gsub('\=', "="), secrets)
696696

697697
reason = if exitstatus
698698
"exited with #{exitstatus}"

Library/Homebrew/extend/kernel.rb

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -195,42 +195,6 @@ def ensure_executable!(name, formula_name = nil, reason: "", latest: false)
195195
Formulary.factory_stub(formula_name).ensure_installed!(reason:, latest:).opt_bin/name
196196
end
197197

198-
sig {
199-
params(
200-
size_in_bytes: T.any(Integer, Float),
201-
precision: T.nilable(Integer),
202-
).returns([T.any(Integer, Float), String])
203-
}
204-
def disk_usage_readable_size_unit(size_in_bytes, precision: nil)
205-
size = size_in_bytes
206-
unit = "B"
207-
%w[KB MB GB].each do |next_unit|
208-
break if (precision ? size.abs.round(precision) : size.abs) < 1000
209-
210-
size /= 1000.0
211-
unit = next_unit
212-
end
213-
[size, unit]
214-
end
215-
216-
sig { params(size_in_bytes: T.any(Integer, Float)).returns(String) }
217-
def disk_usage_readable(size_in_bytes)
218-
size, unit = disk_usage_readable_size_unit(size_in_bytes)
219-
# avoid trailing zero after decimal point
220-
if ((size * 10).to_i % 10).zero?
221-
"#{size.to_i}#{unit}"
222-
else
223-
"#{format("%<size>.1f", size:)}#{unit}"
224-
end
225-
end
226-
227-
sig { params(number: Integer).returns(String) }
228-
def number_readable(number)
229-
numstr = number.to_i.to_s
230-
(numstr.size - 3).step(1, -3) { |i| numstr.insert(i.to_i, ",") }
231-
numstr
232-
end
233-
234198
# Calls the given block with the passed environment variables
235199
# added to `ENV`, then restores `ENV` afterwards.
236200
#
@@ -281,11 +245,4 @@ def tap_and_name_comparison
281245
end
282246
end
283247
end
284-
285-
sig { params(input: String, secrets: T::Array[String]).returns(String) }
286-
def redact_secrets(input, secrets)
287-
secrets.compact
288-
.reduce(input) { |str, secret| str.gsub secret, "******" }
289-
.freeze
290-
end
291248
end

Library/Homebrew/extend/pathname/disk_usage_extension.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# typed: strict
22
# frozen_string_literal: true
33

4+
require "utils/formatter"
5+
46
module DiskUsageExtension
57
extend T::Helpers
68

@@ -28,8 +30,8 @@ def file_count
2830
def abv
2931
out = +""
3032
@file_count, @disk_usage = compute_disk_usage
31-
out << "#{number_readable(@file_count)} files, " if @file_count > 1
32-
out << disk_usage_readable(@disk_usage).to_s
33+
out << "#{Formatter.number_readable(@file_count)} files, " if @file_count > 1
34+
out << Formatter.disk_usage_readable(@disk_usage).to_s
3335
out.freeze
3436
end
3537

Library/Homebrew/install.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ def ask_formulae(formulae_installer, dependants, args:)
451451

452452
puts "#{::Utils.pluralize("Formula", formulae.count)} \
453453
(#{formulae.count}): #{formulae.join(", ")}\n\n"
454-
puts "Download Size: #{disk_usage_readable(sizes.fetch(:download))}"
455-
puts "Install Size: #{disk_usage_readable(sizes.fetch(:installed))}"
454+
puts "Download Size: #{Formatter.disk_usage_readable(sizes.fetch(:download))}"
455+
puts "Install Size: #{Formatter.disk_usage_readable(sizes.fetch(:installed))}"
456456
if (net_install_size = sizes[:net]) && net_install_size != 0
457-
puts "Net Install Size: #{disk_usage_readable(net_install_size)}"
457+
puts "Net Install Size: #{Formatter.disk_usage_readable(net_install_size)}"
458458
end
459459

460460
ask_input

Library/Homebrew/system_command.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def self.run!(executable, args: [], sudo: false, sudo_as_root: false, env: {}, i
132132

133133
sig { returns(SystemCommand::Result) }
134134
def run!
135-
$stderr.puts redact_secrets(command.shelljoin.gsub('\=', "="), @secrets) if verbose? && debug?
135+
$stderr.puts Formatter.redact_secrets(command.shelljoin.gsub('\=', "="), @secrets) if verbose? && debug?
136136

137137
@output = T.let([], T.nilable(T::Array[[Symbol, String]]))
138138
@output = T.must(@output)
@@ -142,17 +142,17 @@ def run!
142142
when :stdout
143143
case @print_stdout
144144
when true
145-
$stdout << redact_secrets(line, @secrets)
145+
$stdout << Formatter.redact_secrets(line, @secrets)
146146
when :debug
147-
$stderr << redact_secrets(line, @secrets) if debug?
147+
$stderr << Formatter.redact_secrets(line, @secrets) if debug?
148148
end
149149
@output << [:stdout, line]
150150
when :stderr
151151
case @print_stderr
152152
when true
153-
$stderr << redact_secrets(line, @secrets)
153+
$stderr << Formatter.redact_secrets(line, @secrets)
154154
when :debug
155-
$stderr << redact_secrets(line, @secrets) if debug?
155+
$stderr << Formatter.redact_secrets(line, @secrets) if debug?
156156
end
157157
@output << [:stderr, line]
158158
end

Library/Homebrew/test/extend/kernel_spec.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,6 @@
5757
expect(which_editor).to eq("vemate -w")
5858
end
5959

60-
specify "#disk_usage_readable" do
61-
expect(disk_usage_readable(1)).to eq("1B")
62-
expect(disk_usage_readable(999)).to eq("999B")
63-
expect(disk_usage_readable(1000)).to eq("1KB")
64-
expect(disk_usage_readable(1025)).to eq("1KB")
65-
expect(disk_usage_readable(4_404_020)).to eq("4.4MB")
66-
expect(disk_usage_readable(4_509_715_660)).to eq("4.5GB")
67-
end
68-
69-
describe "#number_readable" do
70-
it "returns a string with thousands separators" do
71-
expect(number_readable(1)).to eq("1")
72-
expect(number_readable(1_000)).to eq("1,000")
73-
expect(number_readable(1_000_000)).to eq("1,000,000")
74-
end
75-
end
76-
7760
describe "#with_env" do
7861
it "sets environment variables within the block" do
7962
expect(ENV.fetch("PATH")).not_to eq("/bin")

0 commit comments

Comments
 (0)