list.sh: support column output

This commit is contained in:
Bo Anderson 2024-09-02 23:31:36 +01:00
parent fafa61852d
commit e4896f3440
No known key found for this signature in database
2 changed files with 31 additions and 9 deletions

View File

@ -9,7 +9,18 @@ homebrew-list() {
*) ;;
esac
local ls_env=()
local ls_args=()
local tty
if [[ -t 1 ]]
then
tty=1
ls_args+=("-Cq")
source "${HOMEBREW_LIBRARY}/Homebrew/utils/helpers.sh"
ls_env+=("COLUMNS=$(columns)")
fi
local formula=""
local cask=""
@ -31,13 +42,6 @@ homebrew-list() {
esac
done
local tty
if [[ -t 1 ]]
then
tty=1
source "${HOMEBREW_LIBRARY}/Homebrew/utils/helpers.sh"
fi
if [[ -z "${cask}" && -d "${HOMEBREW_CELLAR}" ]]
then
if [[ -n "${tty}" && -z "${formula}" ]]
@ -46,7 +50,7 @@ homebrew-list() {
fi
local formula_output
formula_output="$(ls "${ls_args[@]}" "${HOMEBREW_CELLAR}")" || exit 1
formula_output="$(/usr/bin/env "${ls_env[@]}" ls "${ls_args[@]}" "${HOMEBREW_CELLAR}")" || exit 1
if [[ -n "${formula_output}" ]]
then
echo "${formula_output}"
@ -66,7 +70,7 @@ homebrew-list() {
fi
local cask_output
cask_output="$(ls "${ls_args[@]}" "${HOMEBREW_CASKROOM}")" || exit 1
cask_output="$(/usr/bin/env "${ls_env[@]}" ls "${ls_args[@]}" "${HOMEBREW_CASKROOM}")" || exit 1
if [[ -n "${cask_output}" ]]
then
echo "${cask_output}"

View File

@ -86,3 +86,21 @@ numeric() {
IFS=".rc" read -r -a version_array <<<"${1}"
printf "%01d%02d%02d%03d" "${version_array[@]}" 2>/dev/null
}
columns() {
if [[ -n "${COLUMNS}" ]]
then
echo "${COLUMNS}"
return
fi
local columns
read -r _ columns < <(stty size 2>/dev/null)
if [[ -z "${columns}" ]] && tput cols >/dev/null 2>&1
then
columns="$(tput cols)"
fi
echo "${columns:-80}"
}