Split search command into separate file (#9833)

This commit is contained in:
Maximilian Downey Twiss
2024-05-22 16:36:45 +10:00
committed by GitHub
parent 2a403dccc5
commit 660211dbc8
4 changed files with 39 additions and 48 deletions

View File

@@ -101,16 +101,16 @@ class Command
puts <<~EOT
Look for package(s).
Usage: crew search [-v|--verbose] [<pattern> ...]
If <pattern> is omitted, all packages will be returned.
Both the name and description of packages will be searched.
If the package color is " + "green".lightgreen + ", it means the package is installed.
If the package color is " + "red".lightred + ", it means the architecture is not supported.
If the package color is " + "blue".lightblue + ", it means the architecture is supported but the package is not installed.
The <pattern> string can also contain regular expressions.
If `-v` or `--verbose` is present, homepage, version and license will be displayed.
If `-v` or `--verbose` is present, the homepage, version and license of found packages will be displayed.
Examples:
crew search ^lib".lightblue + " will display all packages that start with `lib`.
crew search audio".lightblue + " will display all packages with `audio` in the name.
crew search | grep -i audio".lightblue + " will display all packages with `audio` in the name or description.
crew search git -v".lightblue + " will display packages with `git` in the name along with homepage, version and license.
crew search ^lib".lightblue + " will display all packages with a name or description that starts with `lib`.
crew search audio".lightblue + " will display all packages with `audio` in the name or description.
crew search -v git".lightblue + " will display all packages with `git` in the name or description along with homepage, version and license.
EOT
when 'sysinfo'
puts <<~EOT

28
commands/search.rb Normal file
View File

@@ -0,0 +1,28 @@
require_relative '../lib/const'
require_relative '../lib/package'
require_relative '../lib/package_utils'
class Command
def self.search(regex_string, verbose)
Dir["#{CREW_PACKAGES_PATH}/*.rb"].each do |package_path|
pkg = Package.load_package(package_path)
# Create a case-insensitive regex from the passed string.
regex = Regexp.new(regex_string, true)
next unless regex.match?(File.basename(package_path, '.rb')) || regex.match?(pkg.description)
# Installed packages have green names, incompatible packages have red, and compatible but not installed have blue.
if PackageUtils.installed?(pkg.name)
print pkg.name.lightgreen
elsif !PackageUtils.compatible?(pkg)
print pkg.name.lightred
else
print pkg.name.lightblue
end
puts ": #{pkg.description}".lightblue
next unless verbose
puts pkg.homepage
puts "Version: #{pkg.version}"
puts "License: #{pkg.license}"
end
end
end