<commit_before># Usage: scoop search [query]
# Summary: Search available apps
# Help: Searches for apps that are available to install.
#
# If used with [query], shows app names that match the query.
# Without [query], shows all the available apps.
param($query)
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\buckets.ps1"
. "$psscriptroot\..\lib\manifest.ps1"
. "$psscriptroot\..\lib\versions.ps1"

reset_aliases

function bin_match($manifest, $query) {
    if(!$manifest.bin) { return $false }
    foreach($bin in $manifest.bin) {
        $exe, $alias, $args = $bin
        $fname = split-path $exe -leaf -ea stop

        if((strip_ext $fname) -match $query) { return $fname }
        if($alias -match $query) { return $alias }
    }
    $false
}

function search_bucket($bucket, $query) {
    $apps = apps_in_bucket (bucketdir $bucket) | foreach-object {
        @{ name = $_ }
    }

    if($query) { $apps = $apps | where-object {
        if($_.name -match $query) { return $true }
        $bin = bin_match (manifest $_.name $bucket) $query
        if($bin) {
            $_.bin = $bin; return $true;
        }
    } }
    $apps | foreach-object { $_.version = (latest_version $_.name $bucket); $_ }
}

@($null) + @(buckets) | foreach-object { # $null is main bucket
    $res = search_bucket $_ $query
    if($res) {
        $name = "$_"
        if(!$_) { $name = "main" }

        "$name bucket:"
        $res | foreach-object {
            $item = "  $($_.name) ($($_.version))"
            if($_.bin) { $item += " --> includes '$($_.bin)'" }
            $item
        }
        ""
    }
}

exit 0
<commit_msg>search ~ better handling for invalid query (fixes #634)<commit_after># Usage: scoop search [query]
# Summary: Search available apps
# Help: Searches for apps that are available to install.
#
# If used with [query], shows app names that match the query.
# Without [query], shows all the available apps.
param($query)
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\buckets.ps1"
. "$psscriptroot\..\lib\manifest.ps1"
. "$psscriptroot\..\lib\versions.ps1"

reset_aliases

function bin_match($manifest, $query) {
    if(!$manifest.bin) { return $false }
    foreach($bin in $manifest.bin) {
        $exe, $alias, $args = $bin
        $fname = split-path $exe -leaf -ea stop

        if((strip_ext $fname) -match $query) { return $fname }
        if($alias -match $query) { return $alias }
    }
    $false
}

function search_bucket($bucket, $query) {
    $apps = apps_in_bucket (bucketdir $bucket) | foreach-object {
        @{ name = $_ }
    }

    if($query) {
        try {
            $query = new-object regex $query
        } catch {
            abort "invalid regular expression: $($_.exception.innerexception.message)"
        }
        $apps = $apps | where-object {
            if($_.name -match $query) { return $true }
            $bin = bin_match (manifest $_.name $bucket) $query
            if($bin) {
                $_.bin = $bin; return $true;
            }
        }
    }
    $apps | foreach-object { $_.version = (latest_version $_.name $bucket); $_ }
}

@($null) + @(buckets) | foreach-object { # $null is main bucket
    $res = search_bucket $_ $query
    if($res) {
        $name = "$_"
        if(!$_) { $name = "main" }

        "$name bucket:"
        $res | foreach-object {
            $item = "  $($_.name) ($($_.version))"
            if($_.bin) { $item += " --> includes '$($_.bin)'" }
            $item
        }
        ""
    }
}

exit 0

{'fraction_non_alphanumeric': 0.16818700114025084, 'fraction_numerical': 0.0037058152793614596, 'mean_word_length': 2.210430009149131, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2193320', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1139, 'n_words': 292}