diff options
author | Case Duckworth | 2020-06-02 21:51:24 -0500 |
---|---|---|
committer | Case Duckworth | 2020-06-02 21:51:24 -0500 |
commit | e55d43849551c65431748e7a64fc11595b7fd000 (patch) | |
tree | 21868557f689ae4ad4355e12e70f5d93676b6078 | |
parent | Reimplement extract_links in bash (diff) | |
download | bollux-e55d43849551c65431748e7a64fc11595b7fd000.tar.gz bollux-e55d43849551c65431748e7a64fc11595b7fd000.zip |
Add history file
I added a history file at $XDG_DATA_HOME/bollux/history. Its format is of the form <date %FT%T> <URL> <TITLE> 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.
-rwxr-xr-x | bollux | 29 |
1 files 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() { | |||
113 | : "${BOLLUX_LESSKEY:=/tmp/bollux-lesskey}" # where to store binds | 113 | : "${BOLLUX_LESSKEY:=/tmp/bollux-lesskey}" # where to store binds |
114 | : "${BOLLUX_PAGESRC:=/tmp/bollux-src}" # where to save the page source | 114 | : "${BOLLUX_PAGESRC:=/tmp/bollux-src}" # where to save the page source |
115 | : "${BOLLUX_URL:=}" # start url | 115 | : "${BOLLUX_URL:=}" # start url |
116 | : "${BOLLUX_DATADIR:=${XDG_DATA_DIR:-$HOME/.local/share}/bollux}" | ||
117 | BOLLUX_HISTFILE="$BOLLUX_DATADIR/history" # where to store the history | ||
116 | ## typesetting | 118 | ## typesetting |
117 | : "${T_MARGIN:=4}" # left and right margin | 119 | : "${T_MARGIN:=4}" # left and right margin |
118 | : "${T_WIDTH:=0}" # width of the viewport -- 0 = get term width | 120 | : "${T_WIDTH:=0}" # width of the viewport -- 0 = get term width |
@@ -323,15 +325,26 @@ handle_response() { | |||
323 | case "$code" in | 325 | case "$code" in |
324 | 1*) | 326 | 1*) |
325 | REDIRECTS=0 | 327 | REDIRECTS=0 |
326 | run history_append "$URL" | 328 | run history_append "$URL" "$meta" |
327 | run prompt "$meta" QUERY | 329 | run prompt "$meta" QUERY |
328 | # shellcheck disable=2153 | 330 | # shellcheck disable=2153 |
329 | run blastoff "?$QUERY" | 331 | run blastoff "?$QUERY" |
330 | ;; | 332 | ;; |
331 | 2*) | 333 | 2*) |
332 | REDIRECTS=0 | 334 | REDIRECTS=0 |
333 | run history_append "$URL" | 335 | # read ahead to find a title |
334 | run display "$meta" | 336 | while read -r; do |
337 | printf -v pretitle "%s\n" "$REPLY" | ||
338 | if [[ "$REPLY" =~ ^#[[:space:]]*(.*) ]]; then | ||
339 | title="${BASH_REMATCH[1]}" | ||
340 | break | ||
341 | fi | ||
342 | done | ||
343 | run history_append "$URL" "$title" | ||
344 | { | ||
345 | printf '%s' "$pretitle" | ||
346 | while read -r; do printf '%s\n' "$REPLY"; done | ||
347 | } | run display "$meta" | ||
335 | ;; | 348 | ;; |
336 | 3*) | 349 | 3*) |
337 | ((REDIRECTS += 1)) | 350 | ((REDIRECTS += 1)) |
@@ -653,12 +666,14 @@ download() { | |||
653 | history_init() { | 666 | history_init() { |
654 | declare -a HISTORY # history is kept in an array | 667 | declare -a HISTORY # history is kept in an array |
655 | HN=0 # position of history in the array | 668 | HN=0 # position of history in the array |
669 | run mkdir -p "${BOLLUX_HISTFILE%/*}" | ||
656 | } | 670 | } |
657 | 671 | ||
658 | history_append() { # history_append URL | 672 | history_append() { # history_append URL TITLE |
659 | BOLLUX_URL="$1" | 673 | BOLLUX_URL="$1" |
674 | # date/time, url, title (best guess) | ||
675 | run printf '%(%FT%T)T\t%s\t%s\n' -1 "$1" "$2" >>"$BOLLUX_HISTFILE" | ||
660 | HISTORY[$HN]="$BOLLUX_URL" | 676 | HISTORY[$HN]="$BOLLUX_URL" |
661 | log d "HN=$HN HISTORY: ${HISTORY[*]}" | ||
662 | ((HN += 1)) | 677 | ((HN += 1)) |
663 | } | 678 | } |
664 | 679 | ||
@@ -670,7 +685,7 @@ history_back() { | |||
670 | log e "Beginning of history." | 685 | log e "Beginning of history." |
671 | return 1 | 686 | return 1 |
672 | fi | 687 | fi |
673 | blastoff "${HISTORY[$HN]}" | 688 | run blastoff "${HISTORY[$HN]}" |
674 | } | 689 | } |
675 | history_forward() { | 690 | history_forward() { |
676 | log d "HN=$HN" | 691 | log d "HN=$HN" |
@@ -679,7 +694,7 @@ history_forward() { | |||
679 | log e "End of history." | 694 | log e "End of history." |
680 | return 1 | 695 | return 1 |
681 | fi | 696 | fi |
682 | blastoff "${HISTORY[$HN]}" | 697 | run blastoff "${HISTORY[$HN]}" |
683 | } | 698 | } |
684 | 699 | ||
685 | if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then | 700 | if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then |