From 902c996b3b1b885fb3c4dd6ed1b3d9e68f34ce74 Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 8 Apr 2020 16:43:07 -0700 Subject: [PATCH] Improve output formatting. --- hosts | 100 +++++++++++++++++++++++++++++++++++++-------- test/disabled.bats | 4 +- test/enabled.bats | 10 ++--- test/hosts.bats | 8 ++-- test/list.bats | 41 ++++++++++--------- test/search.bats | 26 ++++++------ test/show.bats | 64 +++++++---------------------- 7 files changed, 143 insertions(+), 110 deletions(-) diff --git a/hosts b/hosts index e515e1d..d4aa807 100755 --- a/hosts +++ b/hosts @@ -475,6 +475,68 @@ _piped_input() { ! _interactive_input } +# _print_entries() +# +# Usage: +# _print_entries +_print_entries() { + local _input="${1:-}" + [[ -n "${_input}" ]] || return 0 + + local _newline=$'\n' + + if [[ -n "${2:-}" ]] + then + _input+="${_newline}${2:-}" + fi + + local _disabled=0 + local _max_length=0 + + while IFS=$'\t ' read -r -a _parts + do + if [[ "${_parts[0]}" =~ disabled ]] + then + _parts=("${_parts[@]:1}") + fi + + if [[ "${_max_length}" -lt "${#_parts[0]}" ]] + then + _max_length="${#_parts[0]}" + fi + done <<< "${_input}" + + _max_divided="$((${_max_length} / 8))" + + while IFS=$'\t ' read -r -a _parts + do + if [[ "${_parts[0]}" =~ disabled ]] + then + _parts=("${_parts[@]:1}") + printf "disabled:\n" + fi + + _current_divided=$((${#_parts[0]} / 8)) + _tab_count=$((${_max_divided} - ${_current_divided} + 1)) + _tabs="$(printf "%*s" ${_tab_count} | tr " " '\t')" + + if [[ -n "${_parts[2]:-}" ]] + then + printf "%s%s %s\t%s\\n" \ + "${_parts[0]}" \ + "${_tabs}" \ + "${_parts[1]}" \ + "$(printf "%s" "${_parts[*]:2}" | tr '\r\n' ' ')" + else + printf "%s%s %s\\n" \ + "${_parts[0]}" \ + "${_tabs}" \ + "${_parts[1]}" + fi + + done <<< "${_input}" +} + # _verify_write_permissions # # Print a helpful error message when the specified operation can't be @@ -1152,21 +1214,26 @@ list() { then if [[ "${1}" == "disabled" ]] then - printf "%s\\n" "${_disabled_records}" + _print_entries "${_disabled_records}" elif [[ "${1}" == "enabled" ]] then - grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}" + _print_entries "$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")" else show "${1}" fi else # NOTE: use separate expressions since using a | for the or results in # inconsistent behavior. - grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}" + local _enabled_records + _enabled_records="$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")" + _print_entries "${_enabled_records:-}" if [[ -n "${_disabled_records:-}" ]] then - printf "\\nDisabled:\\n%s\\n" "${_disabled_records}" + [[ -n "${_enabled_records:-}" ]] && printf "\\n" + printf "Disabled:\\n" + printf "%s\\n" "---------" + _print_entries "${_disabled_records}" fi fi } @@ -1349,23 +1416,24 @@ show() { then # Run `sed` before `grep` to avoid conflict that supresses `sed` output. local _disabled_records - _disabled_records=$( - sed -n "s/^\\#\\(disabled: .*${1}.*\\)$/\\1/p" "${HOSTS_PATH}" - ) + _disabled_records="$( + sed -n "s/^\\#disabled: \\(.*${1}.*\\)$/\\1/p" "${HOSTS_PATH}" + )" local _enabled_records - _enabled_records=$( - grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}" - ) + _enabled_records="$( + grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}" || true + )" + + _print_entries "${_enabled_records}" + - # Output disabled records secondly for better organization. - if [[ -n "${_enabled_records}" ]] - then - printf "%s\\n" "${_enabled_records}" - fi if [[ -n "${_disabled_records}" ]] then - printf "%s\\n" "${_disabled_records}" + [[ -n "${_enabled_records}" ]] && printf "\\n" + printf "Disabled:\\n" + printf "%s\\n" "---------" + _print_entries "${_disabled_records}" fi else help show diff --git a/test/disabled.bats b/test/disabled.bats index 5831cf9..34702b9 100644 --- a/test/disabled.bats +++ b/test/disabled.bats @@ -29,8 +29,8 @@ load test_helper run "${_HOSTS}" disabled printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.com" ]] - [[ "${lines[1]}" == "127.0.0.1 example.com" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]] + [[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]] [[ "${lines[2]}" == "" ]] } diff --git a/test/enabled.bats b/test/enabled.bats index 21cfcd6..211b2fa 100644 --- a/test/enabled.bats +++ b/test/enabled.bats @@ -29,11 +29,11 @@ load test_helper run "${_HOSTS}" enabled printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "127.0.0.1 localhost" ]] - [[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]] - [[ "${lines[2]}" == "::1 localhost" ]] - [[ "${lines[3]}" == "fe80::1%lo0 localhost" ]] - [[ "${lines[4]}" == "127.0.0.2 example.com" ]] + [[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]] + [[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]] + [[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]] + [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]] + [[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]] } # help ######################################################################## diff --git a/test/hosts.bats b/test/hosts.bats index e1434d3..5b48d55 100644 --- a/test/hosts.bats +++ b/test/hosts.bats @@ -10,9 +10,9 @@ load test_helper @test "\`hosts\` with no arguments prints enabled rules." { run "${_HOSTS}" [[ "${#lines[@]}" -eq 4 ]] - [[ "${lines[0]}" == "127.0.0.1 localhost" ]] - [[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]] - [[ "${lines[2]}" == "::1 localhost" ]] - [[ "${lines[3]}" == "fe80::1%lo0 localhost" ]] + [[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]] + [[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]] + [[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]] + [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]] [[ "${lines[4]}" == "" ]] } diff --git a/test/list.bats b/test/list.bats index cfdd799..7695a7d 100644 --- a/test/list.bats +++ b/test/list.bats @@ -30,15 +30,16 @@ load test_helper printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" _expected="\ -127.0.0.1 localhost -255.255.255.255 broadcasthost -::1 localhost -fe80::1%lo0 localhost -127.0.0.2 example.com +127.0.0.1 localhost +255.255.255.255 broadcasthost +::1 localhost +fe80::1%lo0 localhost +127.0.0.2 example.com Disabled: -0.0.0.0 example.com -0.0.0.0 example.net" +--------- +0.0.0.0 example.com +0.0.0.0 example.net" _compare "'${_expected}'" "'${output}'" [[ "${output}" == "${_expected}" ]] } @@ -70,11 +71,11 @@ Disabled: run "${_HOSTS}" list enabled printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "127.0.0.1 localhost" ]] - [[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]] - [[ "${lines[2]}" == "::1 localhost" ]] - [[ "${lines[3]}" == "fe80::1%lo0 localhost" ]] - [[ "${lines[4]}" == "127.0.0.2 example.com" ]] + [[ "${lines[0]}" =~ 127\.0\.0\.1[[:space:]]+localhost ]] + [[ "${lines[1]}" =~ 255\.255\.255\.255[[:space:]]+broadcasthost ]] + [[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]] + [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]] + [[ "${lines[4]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]] } # `hosts list disabled` ####################################################### @@ -104,8 +105,8 @@ Disabled: run "${_HOSTS}" list disabled printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.com" ]] - [[ "${lines[1]}" == "127.0.0.1 example.com" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]] + [[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]] [[ "${lines[2]}" == "" ]] } @@ -134,8 +135,8 @@ Disabled: run "${_HOSTS}" list example.com printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.com" ]] - [[ "${lines[1]}" == "127.0.0.1 example.com" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]] + [[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]] [[ "${lines[2]}" == "" ]] } @@ -149,7 +150,7 @@ Disabled: run "${_HOSTS}" list "Comment" printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]] [[ "${lines[2]}" == "" ]] } @@ -166,9 +167,9 @@ Disabled: run "${_HOSTS}" list "Comment" printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] - [[ "${lines[1]}" == "disabled: 127.0.0.1 example.biz # Example Comment" ]] - [[ "${lines[2]}" == "" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]] + [[ "${lines[3]}" =~ 127\.0\.0\.1[[:space:]]+example\.biz[[:space:]]+\#\ Example\ Comment ]] + [[ "${lines[4]}" == "" ]] } # help ######################################################################## diff --git a/test/search.bats b/test/search.bats index 71ab206..360e6d1 100644 --- a/test/search.bats +++ b/test/search.bats @@ -66,11 +66,11 @@ Description: run "${_HOSTS}" search enabled printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "127.0.0.1 localhost" ]] - [[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]] - [[ "${lines[2]}" == "::1 localhost" ]] - [[ "${lines[3]}" == "fe80::1%lo0 localhost" ]] - [[ "${lines[4]}" == "127.0.0.2 example.com" ]] + [[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]] + [[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]] + [[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]] + [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]] + [[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]] } # `hosts search disabled` ####################################################### @@ -100,8 +100,8 @@ Description: run "${_HOSTS}" search disabled printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.com" ]] - [[ "${lines[1]}" == "127.0.0.1 example.com" ]] + [[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]] + [[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]] [[ "${lines[2]}" == "" ]] } @@ -130,8 +130,8 @@ Description: run "${_HOSTS}" search example.com printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.com" ]] - [[ "${lines[1]}" == "127.0.0.1 example.com" ]] + [[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]] + [[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]] [[ "${lines[2]}" == "" ]] } @@ -145,7 +145,7 @@ Description: run "${_HOSTS}" search "Comment" printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] + [[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.net[[:space:]]+\#\ Example\ Comment ]] [[ "${lines[2]}" == "" ]] } @@ -161,9 +161,9 @@ Description: run "${_HOSTS}" search "Comment" printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] - [[ "${lines[1]}" == "disabled: 127.0.0.1 example.biz # Example Comment" ]] - [[ "${lines[2]}" == "" ]] + [[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.net[[:space:]]+\#\ Example\ Comment ]] + [[ "${lines[3]}" =~ 127.0.0.1[[:space:]]+example.biz[[:space:]]+\#\ Example\ Comment ]] + [[ "${lines[4]}" == "" ]] } # help ######################################################################## diff --git a/test/show.bats b/test/show.bats index 017a782..5809762 100644 --- a/test/show.bats +++ b/test/show.bats @@ -21,7 +21,7 @@ load test_helper # `hosts show ` ######################################################### -@test "\`show \` exits with status 0." { +@test "\`show \` exits with status 0 and shows all matches." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -33,27 +33,14 @@ load test_helper printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ ${status} -eq 0 ]] -} -@test "\`show \` shows all matches." { - { - run "${_HOSTS}" add 0.0.0.0 example.com - run "${_HOSTS}" add 0.0.0.0 example.net - run "${_HOSTS}" add 127.0.0.2 example.com - run "${_HOSTS}" disable example.com - } - - run "${_HOSTS}" show 0.0.0.0 - printf "\${status}: %s\\n" "${status}" - printf "\${output}: '%s'\\n" "${output}" - - [[ "${lines[0]}" == "0.0.0.0 example.net" ]] - [[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.net ]] + [[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]] } # `hosts show ` ##################################################### -@test "\`show \` exits with status 0." { +@test "\`show \` exits with status 0 and shows all matches." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -65,27 +52,14 @@ load test_helper printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ ${status} -eq 0 ]] -} -@test "\`show \` shows all matches." { - { - run "${_HOSTS}" add 0.0.0.0 example.com - run "${_HOSTS}" add 0.0.0.0 example.net - run "${_HOSTS}" add 127.0.0.2 example.com - run "${_HOSTS}" disable 0.0.0.0 - } - - run "${_HOSTS}" show example.com - printf "\${status}: %s\\n" "${status}" - printf "\${output}: '%s'\\n" "${output}" - - [[ "${lines[0]}" == "127.0.0.2 example.com" ]] - [[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]] + [[ "${lines[0]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]] + [[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]] } # `hosts show ` ################################################ -@test "\`show \` exits with status 0." { +@test "\`show \` exits with status 0 and shows matching records." { { run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.net @@ -96,20 +70,9 @@ load test_helper printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ ${status} -eq 0 ]] -} -@test "\`show \` prints list of matching records." { - { - run "${_HOSTS}" add 0.0.0.0 example.com - run "${_HOSTS}" add 0.0.0.0 example.net - run "${_HOSTS}" add 127.0.0.1 example.com - } - - run "${_HOSTS}" show example.com - printf "\${status}: %s\\n" "${status}" - printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.com" ]] - [[ "${lines[1]}" == "127.0.0.1 example.com" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]] + [[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example.com ]] [[ "${lines[2]}" == "" ]] } @@ -123,7 +86,8 @@ load test_helper run "${_HOSTS}" show "Comment" printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] + printf "\${lines[0]}: '%s'\\n" "${lines[0]}" + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]] [[ "${lines[2]}" == "" ]] } @@ -139,9 +103,9 @@ load test_helper run "${_HOSTS}" show "Comment" printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" - [[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]] - [[ "${lines[1]}" == "disabled: 127.0.0.1 example.biz # Example Comment" ]] - [[ "${lines[2]}" == "" ]] + [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]] + [[ "${lines[3]}" =~ 127\.0\.0\.1[[:space:]]+example\.biz[[:space:]]+\#\ Example\ Comment ]] + [[ "${lines[4]}" == "" ]] } # help ########################################################################