From 228c930ffd2697cfbed0aecf8f25ffcd58733cde Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 1 Jun 2020 14:58:51 -0500 Subject: Add rudimentary history support. This commit adds `history_init`, `history_append`, `history_back`, and `history_forward` functions. The history is implemented as an array, HISTORY, which is appended to with history_append when pages are visited, and a pointer, HN, which points to the current position in the array. When history_back is called, it moves the pointer back 2 and calls blastoff to the URL there. When history_forward is called, it does not move the pointer and blastsoff to the URL. Why back and forward basically have an off-by-one error is beyond me at this time. ISSUES: - if the user goes back then forward, the history is not rewritten for later URLs. for example, if the history is 1.gmi 2.gmi 3.gmi* where the * indicates the current history position, when the user goes back 2, it looks like this: 1.gmi* 2.gmi 3.gmi if the user then navigates to another page: 1.gmi 4.gmi* 3.gmi 3.gmi is still in the history, so the user can go forward from 4. This is unexpected as far as every other client that I know of goes. --- bollux | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/bollux b/bollux index 5b41cda..06e5403 100755 --- a/bollux +++ b/bollux @@ -66,6 +66,7 @@ log() { bollux() { run bollux_config run bollux_args "$@" + run history_init if [[ ! "${BOLLUX_URL:+isset}" ]]; then run prompt GO BOLLUX_URL @@ -307,7 +308,7 @@ request_url() { } handle_response() { - local url="$1" code meta + local URL="$1" code meta while read -r -d $'\r' hdr; do code="$(gawk '{print $1}' <<<"$hdr")" @@ -322,14 +323,14 @@ handle_response() { case "$code" in 1*) REDIRECTS=0 - BOLLUX_URL="$URL" + run history_append "$URL" run prompt "$meta" QUERY # shellcheck disable=2153 run blastoff "?$QUERY" ;; 2*) REDIRECTS=0 - BOLLUX_URL="$URL" + run history_append "$URL" run display "$meta" ;; 3*) @@ -337,7 +338,6 @@ handle_response() { if ((REDIRECTS > BOLLUX_MAXREDIR)); then die $((100 + code)) "Too many redirects!" fi - BOLLUX_URL="$URL" run blastoff "$meta" ;; 4*) @@ -368,7 +368,6 @@ display() { mime="$(trim "${hdr[0],,}")" for ((i = 1; i <= "${#hdr[@]}"; i++)); do h="$(trim "${hdr[$i]}")" - log d "'$h'" case "$h" in charset=*) charset="${h#charset=}" ;; esac @@ -590,10 +589,16 @@ handle_keypress() { run blastoff -u "$URL" ;; 50) # [ - back in the history - run history_back + run history_back || { + sleep 0.5 + run blastoff "$BOLLUX_URL" + } ;; 51) # ] - forward in the history - run history_forward + run history_forward || { + sleep 0.5 + run blastoff "$BOLLUX_URL" + } ;; 52) # r - re-request the current resource run blastoff "$BOLLUX_URL" @@ -643,8 +648,37 @@ download() { fi } -history_back() { log error "Not implemented."; } -history_forward() { log error "Not implemented."; } +history_init() { + declare -a HISTORY # history is kept in an array + HN=0 # position of history in the array +} + +history_append() { # history_append URL + BOLLUX_URL="$1" + HISTORY[$HN]="$BOLLUX_URL" + log d "HN=$HN HISTORY: ${HISTORY[*]}" + ((HN += 1)) +} + +history_back() { + log d "HN=$HN" + ((HN -= 2)) + if ((HN <= 0)); then + HN=0 + log e "Beginning of history." + return 1 + fi + blastoff "${HISTORY[$HN]}" +} +history_forward() { + log d "HN=$HN" + if ((HN >= ${#HISTORY[@]})); then + HN="${#HISTORY[@]}" + log e "End of history." + return 1 + fi + blastoff "${HISTORY[$HN]}" +} if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then run bollux "$@" -- cgit 1.4.1-21-gabe81