Skip to content

Commit

Permalink
naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
admin authored and admin committed Jan 1, 2024
1 parent d5fe391 commit b8abcac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
14 changes: 7 additions & 7 deletions index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

selfdir="$(dirname "${0}")"

source "${selfdir}/paginatedList.sh"
source "${selfdir}/ui.sh"
source "${selfdir}/util.sh"

_mainLoop() {
tempfile=$(mktemp)
outfile=$(mktemp)
q=$(_getInput "${@}")

[[ ${#q} -gt 0 ]] &&
declare -a searchResults="($(_ghSearchRepos "${q}"))"
declare -a searchResults="($(_searchRepos "${q}"))"

[[ ${#searchResults[@]} -eq 0 ]] &&
echo "no results" &&
_mainLoop

while true; do
_paginatedList "${searchResults[@]}" -o "${tempfile}" &&
selection=$(cat "${tempfile}")
_multiplePages "${searchResults[@]}" -o "${outfile}" &&
selection=$(cat "${outfile}")

if [[ "${#selection}" -gt 0 ]]; then
_ghViewReadme "${selection}" && # view README.md
_previewFiles "${selection}" || # preview files in repo
_viewReadme "${selection}" && # view README.md
_viewFileTree "${selection}" || # preview files in repo
_mainLoop # handle empty repos
else
_mainLoop
Expand Down
34 changes: 16 additions & 18 deletions paginatedList.sh → ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# https://docs.github.com/en/rest/repos/contents
# https://docs.github.com/en/rest/git/trees

# single page
_scrollableList() {
trap "tput rmcup; exit 1" SIGINT

_singlePage() {
declare -a items=()
outfile=""
max=0
Expand All @@ -22,8 +19,10 @@ _scrollableList() {
pos=${max}
tput cup ${max} 0
}

_printItem() { echo -ne "${items[pos]}\r"; }
_printItemBold() { echo -ne "\x1b[1m${items[pos]}\x1b[0m\r"; }

_parseArgs() {
while [[ ${#*} -gt 0 ]]; do
case ${1} in
Expand Down Expand Up @@ -53,10 +52,10 @@ _scrollableList() {
max=${maxLines}

# render list
trap "tput rmcup; exit 1" SIGINT
tput smcup
for ((i = 0; i < max; i += 1)); do
echo "${items[i]}"
# skipping last item on first render, needs `echo -n`
echo "${items[i]}" # skipping last item on first render, needs `echo -n`
done
_mvTop
_printItemBold
Expand Down Expand Up @@ -106,8 +105,7 @@ _scrollableList() {
done
}

# multiple pages
_paginatedList() {
_multiplePages() {
declare -a items=()
declare -a pages=()
max=$(tput lines) # decrease by 1?
Expand All @@ -123,7 +121,7 @@ _paginatedList() {
shift
;;
*)
items+=($(echo "${1}" | tr ' ' '\\')) # escape spaces
items+=($( echo "${1}" | tr ' ' '\\')) # escape spaces
shift
;;
esac
Expand All @@ -133,42 +131,42 @@ _paginatedList() {
_parseArgs "${@}"

while true; do
if [[ ${#items[@]} -eq 0 ]]; then
[[ ${#items[@]} -eq 0 ]] &&
break
else

pages[pageCount]="${items[*]:0:${max}}"
pageCount=$((pageCount + 1))
items=(${items[@]:${max}})
fi
done

for i in "${!pages[@]}"; do
declare -a currentPage=(${pages[i]})
_scrollableList "${currentPage[@]}" -o "${outfile}" &&

_singlePage "${currentPage[@]}" -o "${outfile}" &&
selection="$(cat "${outfile}")"

[[ ${#selection} -gt 0 ]] &&
break
done
}

# view filetree
_previewFiles() {
_viewFileTree() {
outfile=$(mktemp)
repoName="${*}"
branchName=$(gh api "repos/${repoName}" | jq -r '.default_branch')
treeJSON=$(gh api "repos/${repoName}/git/trees/${branchName}?recursive=true") # TODO: dirs
urlJSON=$(echo "${treeJSON}" | jq '.tree[]? | if .type == "blob" then .url else empty end' 2>/dev/null)
pathJSON=$(echo "${treeJSON}" | jq '.tree[]? | if .type == "blob" then .path else empty end' 2>/dev/null)
outfile=$(mktemp)
declare -a pathNames="(${pathJSON})"
declare -a urlNames="(${urlJSON})"
declare -a pathNames="(${pathJSON})"

# repo is empty
[[ ${#pathNames[@]} -eq 0 ]] &&
return 1

while true; do
selection=""
_paginatedList "${pathNames[@]}" -o "${outfile}" &&
_multiplePages "${pathNames[@]}" -o "${outfile}" &&
selection=$(cat "${outfile}" | tr '\\' ' ') # un escape spaces

[[ "${#selection}" -eq 0 ]] && break
Expand Down
4 changes: 2 additions & 2 deletions util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _getInput() {
echo "${q}"
}

_ghSearchRepos() {
_searchRepos() {
gh search repos \
--sort "stars" \
--limit 100 \
Expand All @@ -64,6 +64,6 @@ _ghSearchRepos() {
"${@}"
}

_ghViewReadme() {
_viewReadme() {
gh repo view "${@}" | glow -p
}

0 comments on commit b8abcac

Please sign in to comment.