From e55d43849551c65431748e7a64fc11595b7fd000 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Tue, 2 Jun 2020 21:51:24 -0500 Subject: Add history file I added a history file at $XDG_DATA_HOME/bollux/history. Its format is of the form the date is generated with the printf trick. I should probably be doing version checking or at least say we require bash 4+. The code to get the title of the page is pretty gross. Basically I have to run the content of the pipeline through like, two times? A read loop. Check out ll. 333-348 to see how weird it is. --- bollux | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/bollux b/bollux index 7ed4ae1..68eade9 100755 --- a/bollux +++ b/bollux @@ -113,6 +113,8 @@ bollux_config() { : "${BOLLUX_LESSKEY:=/tmp/bollux-lesskey}" # where to store binds : "${BOLLUX_PAGESRC:=/tmp/bollux-src}" # where to save the page source : "${BOLLUX_URL:=}" # start url + : "${BOLLUX_DATADIR:=${XDG_DATA_DIR:-$HOME/.local/share}/bollux}" + BOLLUX_HISTFILE="$BOLLUX_DATADIR/history" # where to store the history ## typesetting : "${T_MARGIN:=4}" # left and right margin : "${T_WIDTH:=0}" # width of the viewport -- 0 = get term width @@ -323,15 +325,26 @@ handle_response() { case "$code" in 1*) REDIRECTS=0 - run history_append "$URL" + run history_append "$URL" "$meta" run prompt "$meta" QUERY # shellcheck disable=2153 run blastoff "?$QUERY" ;; 2*) REDIRECTS=0 - run history_append "$URL" - run display "$meta" + # read ahead to find a title + while read -r; do + printf -v pretitle "%s\n" "$REPLY" + if [[ "$REPLY" =~ ^#[[:space:]]*(.*) ]]; then + title="${BASH_REMATCH[1]}" + break + fi + done + run history_append "$URL" "$title" + { + printf '%s' "$pretitle" + while read -r; do printf '%s\n' "$REPLY"; done + } | run display "$meta" ;; 3*) ((REDIRECTS += 1)) @@ -653,12 +666,14 @@ download() { history_init() { declare -a HISTORY # history is kept in an array HN=0 # position of history in the array + run mkdir -p "${BOLLUX_HISTFILE%/*}" } -history_append() { # history_append URL +history_append() { # history_append URL TITLE BOLLUX_URL="$1" + # date/time, url, title (best guess) + run printf '%(%FT%T)T\t%s\t%s\n' -1 "$1" "$2" >>"$BOLLUX_HISTFILE" HISTORY[$HN]="$BOLLUX_URL" - log d "HN=$HN HISTORY: ${HISTORY[*]}" ((HN += 1)) } @@ -670,7 +685,7 @@ history_back() { log e "Beginning of history." return 1 fi - blastoff "${HISTORY[$HN]}" + run blastoff "${HISTORY[$HN]}" } history_forward() { log d "HN=$HN" @@ -679,7 +694,7 @@ history_forward() { log e "End of history." return 1 fi - blastoff "${HISTORY[$HN]}" + run blastoff "${HISTORY[$HN]}" } if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then -- cgit 1.4.1-21-gabe81