diff --git a/help.sh b/help.sh deleted file mode 100644 index c299d69..0000000 --- a/help.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env bash - -helpTxt=" - usage: $(basename "${0}") -h [QUERY...] - - options: - -h, --help show help -" - -_unindent() { - [[ ${#*} -eq 0 ]] && return 1 - - local msg="${*}" - local indent=false - - _printLine() { - while IFS= read -r line; do - # line with spaces removed - noSpaces="${line/ //}" - - # skip empty lines - if [[ ${#noSpaces} -eq 0 ]]; then - echo - else - # set `indent` if not set - if [[ ${indent} == false ]]; then - indent=$( - # set `indent` to first non-empty line found - echo "${line}" | - # get number of leading spaces before first word - awk -F'[^ ]' '{print length($1)}' - ) - fi - # print formatted line - echo "${line}" | cut -c "$((indent + 1))-${#line}" - fi - done < <(echo "${msg}") # pass text to IFS loop - } - - # echo "$( - _printLine - # )" -} - -_showHelp() { - _unindent "${@}" | - tail -n +2 | - bat -pp -l help && - echo -} - -_parseHelpArgs() { - while [[ ${#*} -gt 0 ]]; do - case ${1} in - -h | --help) - _showHelp "${helpTxt}" && exit 0 - ;; - *) - shift - ;; - esac - done -} diff --git a/index.sh b/index.sh index a6945db..60c4d4d 100755 --- a/index.sh +++ b/index.sh @@ -2,12 +2,23 @@ pathname="$(dirname "${0}")" -source "${pathname}/help.sh" source "${pathname}/util.sh" source "${pathname}/previewFiles.sh" source "${pathname}/paginatedList.sh" -_parseHelpArgs "${@}" +_parseArgs() { + while [[ ${#*} -gt 0 ]]; do + case ${1} in + -h | --help) + _showHelp + exit 0 + ;; + *) + shift + ;; + esac + done +} _mainLoop() { tempfile=$(mktemp) @@ -34,4 +45,5 @@ _mainLoop() { done } +_parseArgs "${@}" _mainLoop "${@}" diff --git a/previewFiles.sh b/previewFiles.sh index ccc434c..82fbb23 100644 --- a/previewFiles.sh +++ b/previewFiles.sh @@ -31,7 +31,7 @@ _previewFiles() { [[ "${#selection}" -eq 0 ]] && break - echo "got $selection" + echo "got $selection" # match selected path to corresponding url for i in "${!pathNames[@]}"; do if [[ "${pathNames[i]}" == "${selection}" ]]; then diff --git a/util.sh b/util.sh index ffa5583..ecbe901 100644 --- a/util.sh +++ b/util.sh @@ -23,3 +23,35 @@ _ghSearchRepos() { _ghViewReadme() { gh repo view "${@}" | glow -p } + +_showHelp() { + helpText=" + usage: $(basename "${0}") -h [QUERY...] + + options: + -h, --help show help + " + + # unindenting the help message below + # ... + # so i don't have to write it ugly above + + indent=false + + while IFS="" read -r line; do + # ignore empty lines + charsOnly="${line/ //}" + if [[ ${#charsOnly} -eq 0 ]]; then + echo + else + # set indent to first non empty line + if [[ ${indent} == false ]]; then + indent=$( + echo "${line}" | awk -F'[^ ]' '{print length($1)}' # get number of leading spaces + ) + fi + # formatted + echo "${line}" | cut -c "$((indent + 1))-" + fi + done < <(echo "${helpText}") | bat -pp -l help +}