Skip to content

Commit 77783b5

Browse files
F30buo
authored andcommitted
Refactor app table printing
Remove duplicate code for outputting tables of apps and identification of an app's state.
1 parent 99bf73b commit 77783b5

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

lib/bcu.rb

+19-31
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ module Bcu
99
def self.process(args)
1010
parse!(args)
1111

12-
outdated = find_outdated_apps
12+
outdated, state_info = find_outdated_apps
1313
return if outdated.empty?
1414

15-
print_outdated_app(outdated)
15+
puts
16+
ohai "Found outdated apps"
17+
print_app_table(outdated, state_info)
1618

1719
if options.dry_run
1820
printf "\nDo you want to upgrade %d app%s [y/N]? ", outdated.length, outdated.length > 1 ? "s" : ""
@@ -47,6 +49,7 @@ def self.process(args)
4749

4850
def self.find_outdated_apps
4951
outdated = []
52+
state_info = Hash.new("")
5053

5154
ohai "Finding outdated apps"
5255
installed = Hbc.installed_apps
@@ -59,58 +62,43 @@ def self.find_outdated_apps
5962
end
6063
end
6164

62-
table = [["No.", "Name", "Cask", "Current", "Latest", "Auto-Update", "State"]]
63-
installed.each_with_index do |app, i|
64-
row = []
65-
row << "#{i+1}/#{installed.length}"
66-
row << app[:name].to_s
67-
row << app[:token]
68-
row << app[:current].join(", ")
69-
row << app[:version]
70-
row << (app[:auto_updates] ? "Y" : "")
65+
installed.each do |app|
7166
if options.all && app[:version] == "latest"
72-
row << "forced to upgrade"
7367
outdated.push app
68+
state_info[app] = "forced to upgrade"
7469
elsif options.all && app[:auto_updates] && app[:outdated?]
75-
row << "forced to upgrade"
7670
outdated.push app
71+
state_info[app] = "forced to upgrade"
7772
elsif !options.all && app[:auto_updates]
78-
row << "ignored"
73+
state_info[app] = "ignored"
7974
elsif app[:outdated?]
80-
row << "outdated"
8175
outdated.push app
76+
state_info[app] = "outdated"
8277
elsif app[:cask].nil?
83-
row << "no cask available"
78+
state_info[app] = "no cask available"
8479
end
85-
table << row
8680
end
87-
puts Formatter.table(table)
8881

89-
outdated
82+
print_app_table(installed, state_info)
83+
84+
[outdated, state_info]
9085
end
9186

92-
def self.print_outdated_app(outdated)
87+
def self.print_app_table(apps, state_info)
9388
table = [["No.", "Name", "Cask", "Current", "Latest", "Auto-Update", "State"]]
94-
outdated.each_with_index do |app, i|
89+
90+
apps.each_with_index do |app, i|
9591
row = []
96-
row << "#{i+1}/#{outdated.length}"
92+
row << "#{i+1}/#{apps.length}"
9793
row << app[:name].to_s
9894
row << app[:token]
9995
row << app[:current].join(", ")
10096
row << app[:version]
10197
row << (app[:auto_updates] ? "Y" : "")
102-
if options.all && app[:version] == "latest"
103-
row << "forced to upgrade"
104-
elsif options.all && app[:auto_updates] && app[:outdated?]
105-
row << "forced to upgrade"
106-
elsif app[:outdated?]
107-
row << "outdated"
108-
end
98+
row << state_info[app]
10999
table << row
110100
end
111101

112-
puts
113-
ohai "Found outdated apps"
114102
puts Formatter.table(table)
115103
end
116104
end

0 commit comments

Comments
 (0)