81 lines
2.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-09-06 08:29:14 +02:00
module Cask
2018-09-04 08:45:48 +01:00
class Cmd
2017-05-20 19:08:03 +02:00
class List < AbstractCommand
2020-07-09 22:58:48 +02:00
option "-1", :one, false
option "--versions", :versions, false
option "--full-name", :full_name, false
option "--json", :json, false
def self.usage
<<~EOS
`cask list`, `cask ls` [<options>] [<casks>]
-1 - Force output to be one entry per line.
This is the default when output is not to a terminal.
--versions - Show the version number for installed formulae, or only the specified
casks if <casks> are provided.
--full-name - Print casks with fully-qualified names.
--json - Print a JSON representation of <cask>. See the docs for examples of using the JSON
output: <https://docs.brew.sh/Querying-Brew>
List all installed casks.
If <casks> are provided, limit information to just those casks.
EOS
end
def self.help
"lists installed Casks or the casks provided in the arguments"
end
2017-05-20 03:30:46 +02:00
def run
self.class.list_casks(
*casks,
json: json?,
one: one?,
full_name: full_name?,
versions: versions?,
)
end
def self.list_casks(*casks, json: false, one: false, full_name: false, versions: false)
output = if casks.any?
casks.each do |cask|
raise CaskNotInstalledError, cask unless cask.installed?
end
else
Caskroom.casks
end
2020-07-09 22:58:48 +02:00
if json
2020-07-09 22:58:48 +02:00
puts JSON.generate(output.map(&:to_h))
elsif one
2020-07-09 22:58:48 +02:00
puts output.map(&:to_s)
elsif full_name
2020-07-09 22:58:48 +02:00
puts output.map(&:full_name).sort(&tap_and_name_comparison)
elsif versions
puts output.map(&method(:format_versioned))
elsif !output.empty? && casks.any?
puts output.map(&method(:list_artifacts))
2020-07-09 22:58:48 +02:00
elsif !output.empty?
puts Formatter.columns(output.map(&:to_s))
end
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.list_artifacts(cask)
2017-10-04 17:08:35 +02:00
cask.artifacts.group_by(&:class).each do |klass, artifacts|
next unless klass.respond_to?(:english_description)
2018-09-17 02:45:00 +02:00
2020-07-09 22:58:48 +02:00
return "==> #{klass.english_description}", artifacts.map(&:summarize_installed)
2016-09-24 13:52:43 +02:00
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.format_versioned(cask)
cask.to_s.concat(cask.versions.map(&:to_s).join(" ").prepend(" "))
end
end
end
2016-08-18 22:11:42 +03:00
end