From e1f569b01b686879b5b97b473c83cf5c98e0505d Mon Sep 17 00:00:00 2001 From: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> Date: Thu, 17 Feb 2022 22:57:18 +0530 Subject: [PATCH] feat(scoop-cat): Use `bat` to pretty-print JSON (#4742) * feat(scoop-cat): Use `bat` to pretty-print JSON * update changelog * hide filename and line numbers * Add `cat_style` config option * use underscores * Revert "use underscores" This reverts commit 46524fc94702b5ac8837d572aaf9d2f7030eed90. * Apply suggestions from code review Co-authored-by: Hsiao-nan Cheung Co-authored-by: Hsiao-nan Cheung --- CHANGELOG.md | 1 + lib/install.ps1 | 15 ++++++++++----- libexec/scoop-cat.ps1 | 15 ++++++++++----- libexec/scoop-config.ps1 | 6 ++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 646ae9749c..a2b68188ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - **scoop-list:** Allow list manipulation ([#4718](https://github.com/ScoopInstaller/Scoop/issues/4718)) - **scoop-list:** Show last-updated time [#4723](https://github.com/ScoopInstaller/Scoop/issues/4723)) - **scoop-cache:** Handle multiple apps and show detailed information ([#4738](https://github.com/ScoopInstaller/Scoop/pull/4738)) +- **scoop-cat:** Use `bat` to pretty-print JSON ([#4742](https://github.com/ScoopInstaller/Scoop/pull/4742)) ### Bug Fixes diff --git a/lib/install.ps1 b/lib/install.ps1 index af9f54b0b2..02f2470a91 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -1,5 +1,5 @@ -. "$psscriptroot/autoupdate.ps1" -. "$psscriptroot/buckets.ps1" +. "$PSScriptRoot/autoupdate.ps1" +. "$PSScriptRoot/buckets.ps1" function nightly_version($date, $quiet = $false) { $date_str = $date.tostring("yyyyMMdd") @@ -34,9 +34,14 @@ function install_app($app, $architecture, $global, $suggested, $use_cache = $tru return } - if ((get_config 'manifest-review' $false) -and ($MyInvocation.ScriptName -notlike '*scoop-update*')) { - Write-Output "Manifest: $app.json" - Write-Output $manifest | ConvertToPrettyJson + if ((get_config 'manifest_review' $false) -and ($MyInvocation.ScriptName -notlike '*scoop-update*')) { + Write-Host "Manifest: $app.json" + $style = get_config cat_style + if ($style) { + $manifest | ConvertToPrettyJson | bat --no-paging --style $style --language json + } else { + $manifest | ConvertToPrettyJson + } $answer = Read-Host -Prompt "Continue installation? [Y/n]" if (($answer -eq 'n') -or ($answer -eq 'N')) { return diff --git a/libexec/scoop-cat.ps1 b/libexec/scoop-cat.ps1 index f3a6b3e533..43bf7e95dd 100644 --- a/libexec/scoop-cat.ps1 +++ b/libexec/scoop-cat.ps1 @@ -1,11 +1,11 @@ # Usage: scoop cat -# Summary: Show content of specified manifest. +# Summary: Show content of specified manifest. If available, `bat` will be used to pretty-print the JSON. param($app) -. "$psscriptroot\..\lib\manifest.ps1" -. "$psscriptroot\..\lib\install.ps1" -. "$psscriptroot\..\lib\help.ps1" +. "$PSScriptRoot\..\lib\manifest.ps1" +. "$PSScriptRoot\..\lib\install.ps1" +. "$PSScriptRoot\..\lib\help.ps1" reset_aliases @@ -15,7 +15,12 @@ $app, $bucket, $null = parse_app $app $app, $manifest, $bucket, $url = Find-Manifest $app $bucket if ($manifest) { - $manifest | ConvertToPrettyJson | Write-Host + $style = get_config cat_style + if ($style) { + $manifest | ConvertToPrettyJson | bat --no-paging --style $style --language json + } else { + $manifest | ConvertToPrettyJson + } } else { abort "Couldn't find manifest for '$app'$(if($url) { " at the URL $url" })." } diff --git a/libexec/scoop-config.ps1 b/libexec/scoop-config.ps1 index 005eccff91..b66ff2cc48 100644 --- a/libexec/scoop-config.ps1 +++ b/libexec/scoop-config.ps1 @@ -81,6 +81,12 @@ # API key used for uploading/scanning files using virustotal. # See: 'https://support.virustotal.com/hc/en-us/articles/115002088769-Please-give-me-an-API-key' # +# cat_style: +# When set to a non-empty string, Scoop will use 'bat' to display the manifest for +# the `scoop cat` command and while doing manifest review. This requires 'bat' to be +# installed (run `scoop install bat` to install it), otherwise errors will be thrown. +# The accepted values are the same as ones passed to the --style flag of 'bat'. +# # ignore_running_processes: $true|$false # When set to $false (default), Scoop would stop its procedure immediately if it detects # any target app process is running. Procedure here refers to reset/uninstall/update.