Skip to content

Commit

Permalink
cleanup help util and flag parse
Browse files Browse the repository at this point in the history
  • Loading branch information
ixc7 committed Dec 31, 2023
1 parent 65cd8ba commit b3e006f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 66 deletions.
63 changes: 0 additions & 63 deletions help.sh

This file was deleted.

16 changes: 14 additions & 2 deletions index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -34,4 +45,5 @@ _mainLoop() {
done
}

_parseArgs "${@}"
_mainLoop "${@}"
2 changes: 1 addition & 1 deletion previewFiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b3e006f

Please sign in to comment.