Skip to content

Commit

Permalink
view nested directories in filetree
Browse files Browse the repository at this point in the history
  • Loading branch information
ixc7 committed Jan 2, 2024
1 parent 4d4051e commit 1c06dbe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 50 deletions.
9 changes: 6 additions & 3 deletions index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ _mainLoop() {
_multiplePages "${searchResults[@]}" -o "${outfile}" &&
selection=$(cat "${outfile}")

branchName=$(gh api "repos/${selection}" | jq -r '.default_branch')
treeJSON=$(gh api "repos/${selection}/git/trees/${branchName}")

if [[ "${#selection}" -gt 0 ]]; then
_viewReadme "${selection}" && # view README.md
_viewFileTree "${selection}" || # preview files in repo
_mainLoop # handle empty repos
_viewReadme "${selection}" && # view README.md
_viewFileTree "${treeJSON}" || # preview files in repo
_mainLoop # handle empty repos
else
_mainLoop
fi
Expand Down
69 changes: 22 additions & 47 deletions ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
_singlePage() {
trap "tput rmcup" RETURN

declare -a items=()
outfile=""
max=0
pos=0

declare -a items=()

_mvUp() { echo -ne "\x1b[1A\r"; }
_mvDown() { echo -ne "\x1b[1B\r"; }
_mvTop() {
Expand Down Expand Up @@ -43,8 +44,7 @@ _singlePage() {

_parseArgs "${@}"

[[ ${#items[@]} -eq 0 ]] &&
return 1
[[ ${#items[@]} -eq 0 ]] && return 1

# limit range to screen height
max="$((${#items[@]} - 1))"
Expand Down Expand Up @@ -138,8 +138,7 @@ _multiplePages() {
_parseArgs "${@}"

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

pages[pageCount]="${items[*]:0:${max}}"
pageCount=$((pageCount + 1))
Expand Down Expand Up @@ -176,54 +175,29 @@ _multiplePages() {
_viewFileTree() {
outfile=$(mktemp)

repoName="${*}"
branchName=$(gh api "repos/${repoName}" | jq -r '.default_branch')

# treeJSON=$(gh api "repos/${repoName}/git/trees/${branchName}?recursive=true") # TODO: multi level directory tree
treeJSON=$(gh api "repos/${repoName}/git/trees/${branchName}")

# debug
# TEMPJSON_RECURSIVE=$(gh api "repos/${repoName}/git/trees/${branchName}?recursive=true")
# TEMPJSON_FLAT=$(gh api "repos/${repoName}/git/trees/${branchName}")
# echo "${TEMPJSON_RECURSIVE}" | jq -r . > "TEMPJSON_RECURSIVE.json"
# echo "${TEMPJSON_FLAT}" | jq -r . > "TEMPJSON_FLAT.json"

# 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
# )

urlJSON=$(
# repoName="${*}"
# branchName=$(gh api "repos/${repoName}" | jq -r '.default_branch')
# treeJSON=$(gh api "repos/${repoName}/git/trees/${branchName}") # ?recursive=true
treeJSON="${*}"

urlNamesJSON=$(
echo "${treeJSON}" |
jq '
.tree[]? | .url
' 2>/dev/null
jq '.tree[]? | .url'
)
pathJSON=$(
declare -a urlNames="(${urlNamesJSON})"

pathNamesJSON=$(
echo "${treeJSON}" |
jq '
.tree[]? | .path
' 2>/dev/null
jq '.tree[]? | .path'
)

declare -a urlNames="(${urlJSON})"
declare -a pathNames="(${pathJSON})"
declare -a pathNames="(${pathNamesJSON})"

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

while true; do
echo "" >${outfile}
selection=""
echo "" >${outfile}

_multiplePages "${pathNames[@]}" -o "${outfile}" &&
selection=$(cat "${outfile}" | tr '\\' ' ') # un escape spaces
Expand All @@ -239,10 +213,11 @@ _viewFileTree() {
# decode file contents and open in editor
gh api "${fileUrl}" |
jq -r '.content? // .tree?' |
base64 -d >"${tempBuffer}" ||
echo "'${selection}' is a directory." >${tempBuffer}
base64 -d >"${tempBuffer}" &&
${EDITOR} "${tempBuffer}" ||
_viewFileTree "$(gh api ${fileUrl})"
# || echo "'${selection}' is a directory." >${tempBuffer}

${EDITOR} "${tempBuffer}"
fi
done
done
Expand Down

0 comments on commit 1c06dbe

Please sign in to comment.