| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  | # does the quickest output of brew list possible for no named arguments. | 
					
						
							|  |  |  | # HOMEBREW_CELLAR, HOMEBREW_PREFIX are set by brew.sh | 
					
						
							|  |  |  | # shellcheck disable=SC2154 | 
					
						
							|  |  |  | homebrew-list() { | 
					
						
							|  |  |  |   case "$1" in | 
					
						
							|  |  |  |     # check we actually have list and not e.g. listsomething | 
					
						
							| 
									
										
										
										
											2024-09-03 00:03:18 +01:00
										 |  |  |     list | ls) ;; | 
					
						
							|  |  |  |     list* | ls*) return 1 ;; | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |     *) ;; | 
					
						
							|  |  |  |   esac | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-02 23:31:36 +01:00
										 |  |  |   local ls_env=() | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |   local ls_args=() | 
					
						
							| 
									
										
										
										
											2024-09-02 23:31:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   local tty | 
					
						
							|  |  |  |   if [[ -t 1 ]] | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     tty=1 | 
					
						
							|  |  |  |     ls_args+=("-Cq") | 
					
						
							|  |  |  |     source "${HOMEBREW_LIBRARY}/Homebrew/utils/helpers.sh" | 
					
						
							|  |  |  |     ls_env+=("COLUMNS=$(columns)") | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |   local formula="" | 
					
						
							|  |  |  |   local cask="" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-03 00:03:18 +01:00
										 |  |  |   # `OPTIND` is used internally by `getopts` to track parsing position | 
					
						
							|  |  |  |   local OPTIND=2 # skip $1 (and localise OPTIND to this function) | 
					
						
							|  |  |  |   while getopts ":1lrt-:" arg | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |   do | 
					
						
							|  |  |  |     case "${arg}" in | 
					
						
							|  |  |  |       # check for flags passed to ls | 
					
						
							| 
									
										
										
										
											2024-09-03 00:03:18 +01:00
										 |  |  |       1 | l | r | t) ls_args+=("-${arg}") ;; | 
					
						
							|  |  |  |       -) | 
					
						
							|  |  |  |         local parsed_index=$((OPTIND - 1)) # Parse full arg to reject e.g. -r-formula | 
					
						
							|  |  |  |         case "${!parsed_index}" in | 
					
						
							|  |  |  |           --formula | --formulae) formula=1 ;; | 
					
						
							|  |  |  |           --cask | --casks) cask=1 ;; | 
					
						
							|  |  |  |           *) return 1 ;; | 
					
						
							|  |  |  |         esac | 
					
						
							|  |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |       # reject all other flags | 
					
						
							| 
									
										
										
										
											2024-09-03 00:03:18 +01:00
										 |  |  |       *) return 1 ;; | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |     esac | 
					
						
							|  |  |  |   done | 
					
						
							| 
									
										
										
										
											2024-09-03 00:03:18 +01:00
										 |  |  |   # If we haven't reached the end of the arg list, we have named args. | 
					
						
							|  |  |  |   if ((OPTIND - 1 != $#)) | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     return 1 | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if [[ -z "${cask}" && -d "${HOMEBREW_CELLAR}" ]] | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     if [[ -n "${tty}" && -z "${formula}" ]] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |       ohai "Formulae" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     local formula_output | 
					
						
							| 
									
										
										
										
											2024-09-02 23:31:36 +01:00
										 |  |  |     formula_output="$(/usr/bin/env "${ls_env[@]}" ls "${ls_args[@]}" "${HOMEBREW_CELLAR}")" || exit 1 | 
					
						
							| 
									
										
										
										
											2024-09-01 10:36:22 +08:00
										 |  |  |     if [[ -n "${formula_output}" ]] | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |     then | 
					
						
							|  |  |  |       echo "${formula_output}" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if [[ -n "${tty}" && -z "${formula}" ]] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |       echo | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [[ -z "${formula}" && -d "${HOMEBREW_CASKROOM}" ]] | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     if [[ -n "${tty}" && -z "${cask}" ]] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |       ohai "Casks" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     local cask_output | 
					
						
							| 
									
										
										
										
											2024-09-02 23:31:36 +01:00
										 |  |  |     cask_output="$(/usr/bin/env "${ls_env[@]}" ls "${ls_args[@]}" "${HOMEBREW_CASKROOM}")" || exit 1 | 
					
						
							| 
									
										
										
										
											2024-09-01 10:36:22 +08:00
										 |  |  |     if [[ -n "${cask_output}" ]] | 
					
						
							| 
									
										
										
										
											2024-05-17 11:59:40 +09:00
										 |  |  |     then | 
					
						
							|  |  |  |       echo "${cask_output}" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0 | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } |