From d328524cd9b4f6b52a7655050e0de82cb5d9917a Mon Sep 17 00:00:00 2001 From: ackman678 Date: Mon, 15 May 2023 10:49:24 -0400 Subject: [PATCH] sbib improve and rg bool --- check-battery.sh | 10 ++++++-- cp2figs.sh | 1 - dark | 29 +++++++++++++++++++++++ dim | 2 +- dimr | 1 - f | 22 +++++++++++++----- fetch_citation | 49 +++++++++++++++++++++++++++++++++++++++ shrinkpdf.sh => pdfshrink | 0 reveal.sh | 8 +++---- s | 24 +++++++++++++++++++ sbib | 47 +++++++++++++++---------------------- sdoi.sh | 13 +++++++---- spdf.sh | 8 +++---- wm_spawn | 9 +++++++ 14 files changed, 171 insertions(+), 52 deletions(-) create mode 100755 dark create mode 100755 fetch_citation rename shrinkpdf.sh => pdfshrink (100%) create mode 100755 s create mode 100755 wm_spawn diff --git a/check-battery.sh b/check-battery.sh index 21ade4d..76806f6 100755 --- a/check-battery.sh +++ b/check-battery.sh @@ -11,9 +11,15 @@ REM=`grep -i "charge_now" $BATTERY/uevent | awk -F= '{ print $2 }'` FULL=`grep -i "charge_full_design" $BATTERY/uevent | awk -F= '{ print $2 }'` PERCENT=`echo $(( $REM * 100 / $FULL ))` -if [ $PERCENT -le "11" ]; then +echo 'battery % is '$PERCENT + +if [ $PERCENT -le "23" ]; then #/usr/bin/i3-nagbar -m "Low battery" - #echo 'low battery '$PERCENT notify-send --urgency=critical "Low battery $PERCENT%" fi +if [ $PERCENT -le "7" ]; then + echo "powering down..." + systemctl suspend +fi + diff --git a/cp2figs.sh b/cp2figs.sh index 20ed3f3..ebdcce0 100755 --- a/cp2figs.sh +++ b/cp2figs.sh @@ -1,5 +1,4 @@ #!/bin/bash -#!/bin/bash if [ "$1" == "-h" ] ; then echo " cp2figs.sh - copy and convert image to figure assets folder and put html figure string containing relative link to the asset on system clipboard diff --git a/dark b/dark new file mode 100755 index 0000000..21d09e3 --- /dev/null +++ b/dark @@ -0,0 +1,29 @@ +#!/bin/bash +#dark: control display color contrast from terminal +#usage: dark, dark 1, dark on, dark 0, dark off +#James B. Ackman 2022-05-02T22:53:41-07:00 + +set -e #exit if an error +mode=${1:-"dark"} + +if [[ $1 == "0" || $1 == "off" ]]; then + mode=light + scheme=base16-atelier-dune-light.sh + # scheme=base16-classic-light.sh + # scheme=base16-gruvbox-light-hard.sh + TaskTheme="light-256" +else + mode=dark + # scheme=base16-atelier-dune.sh + # scheme=base16-classic-dark.sh + scheme=base16-gruvbox-dark-hard.sh + TaskTheme="dark-gray-256" +fi + +#if exist base16 shell color scheme, switch to scheme +ln -sf ~/.config/base16-shell/scripts/$scheme ~/.base16_theme +sh ~/.base16_theme + +#if exist taskwarrior taskrc file, switch the color scheme +sed -i -E "s|include /usr/share/doc/task/rc/[a-z1256-]+.theme|include /usr/share/doc/task/rc/$TaskTheme.theme|" ~/.config/task/taskrc + diff --git a/dim b/dim index 2ad71b2..95cf286 100755 --- a/dim +++ b/dim @@ -7,7 +7,7 @@ #check kernel backlight driver names on your machine: `ls -l /sys/class/backlight/*` #then set following var, e.g. acpi_video0 nv_backlight or intel_backlight -backlightDriver="intel_backlight" +backlightDriver="acpi_video0" set -e #exit if an error percentValue=$1 diff --git a/dimr b/dimr index 9f70550..98d4562 100755 --- a/dimr +++ b/dimr @@ -1,3 +1,2 @@ #!/bin/bash redshift -l 38:-122 -t 5500:3500 -g 0.8 -v - diff --git a/f b/f index fbce109..9b5f579 100755 --- a/f +++ b/f @@ -4,7 +4,8 @@ if [ "$1" == "-h" ] ; then f - fuzzy preview and open text files or pdfs usage: f - f p + f p #then type to preview + f p #then type to save pdf with spdf.sh depends: fzf @@ -18,6 +19,8 @@ fi set -e #exit if an error +#todo: simplify. + if [ "$1" == "p" ]; then ls *.pdf | fzf --preview 'pdftotext -l 2 -nopgbrk -q {1} -' \ @@ -28,14 +31,21 @@ fi if [ -z "$1" ]; then #FZF_DEFAULT_COMMAND=rg -i --files --glob "!.git/*" - # fzf --delimiter : --preview 'less {1}' \ - fzf --delimiter : --preview 'bat --color=always --style=numbers --line-range=:500 {}' \ - --preview-window=up:70% --bind "enter:execute-silent(gvim {1} &)" + fzf --delimiter : --preview 'bat --color always --style=numbers --line-range=:500 {1}' \ + --preview-window=up:70% --bind "enter:execute(xdg-open {1})" +# --preview-window=up:70% --bind "enter:execute($VISUAL {1})" + + # fzf --delimiter : --preview 'bat --color=always --style=numbers --line-range=:500 {}' \ + + # --preview-window=up:70% --bind "enter:execute-silent(gvim {1} &)" else # rg $1 | fzf --delimiter : --preview 'less {1}' \ # todo: replace this with a series elif blocks mapping selective previews and downstream application bindings to a set of desired filetypes - ls *.$1 | fzf --delimiter : --preview 'bat --color=always --style=numbers --line-range=:500 {}' \ - --preview-window=up:70% --bind "enter:execute-silent(gvim {1} &)" + #use xdg-mime, filetype info for opening + ls *$1 | fzf --delimiter : --preview 'bat --color always --style=numbers --line-range=:500 {1}' \ + --preview-window=up:70% --bind "enter:execute(xdg-open {1})" + + # --preview-window=up:70% --bind "enter:execute-silent(gvim {1} &)" fi diff --git a/fetch_citation b/fetch_citation new file mode 100755 index 0000000..3b62225 --- /dev/null +++ b/fetch_citation @@ -0,0 +1,49 @@ +#!/bin/bash +if [ "$1" == "-h" ] ; then + echo " + fetch_citation - extract citation for citekey in a bibtex.bib or bibjson.json file + + usage: + fetch_citation citeKey + + depends: + grep, sed, echo + pandoc + pandoc-citeproc + wl-copy + + defaults: + Set the required default file locations (csl file, bib file) + + " + exit 0 +fi + +#Setup defaults +cslFile=${2:-$HOME/projects/learn/bibd/bibd-md.csl} +bibdFile=${3:-$HOME/projects/learn/bibd/OMEGA.bib} +# bibdFile=${2:-$HOME/projects/learn/bibd/OMEGA.json} +cd $(dirname $bibdFile) +set -e #exit if an error + +#requires one citeKey string as input +citeKey=$1 + +#method1 +#todo: use json with jq +# outCitation=$(pandoc -f latex <(echo "\cite{$citeKey}") -t plain -o - --bibliography $bibdFile --citeproc --csl $cslFile | tail -n +3) + +#method2 +#need to use a temporary bib file as input to pandoc because parsing a large bib file is too slow, but json input would be faster +#and because pandoc expects a file input for bibliography +tmpName=$(mktemp --suffix=.bib) +str3=$(grep -A 30 $citeKey, $bibdFile) +echo $str3 | sed -E "s|(.+\} ?\}).+|\1|" > $tmpName +outCitation=$(pandoc -f latex <(echo "\cite{$citeKey}") -t plain -o - --bibliography $tmpName --citeproc --csl $cslFile | tail -n +3) +rm $tmpName + +#tail -n +61 $fn | sed -E "s#(.+\}\})#\1#" +# fzf --preview="head {}" --preview-window=up:30% +# fzf --preview="file {}" --preview-window=down:1 +echo $outCitation +echo $outCitation | wl-copy diff --git a/shrinkpdf.sh b/pdfshrink similarity index 100% rename from shrinkpdf.sh rename to pdfshrink diff --git a/reveal.sh b/reveal.sh index d509ec0..899e03a 100755 --- a/reveal.sh +++ b/reveal.sh @@ -15,6 +15,9 @@ set -e appPath="$HOME/projects/dev/reveal.js" portNumber=${2:-8000} +fn=$1 #markdown document to render e.g. neuroanatomy1.md +basefn=$(basename $fn) +fullfn=$(realpath $fn) if [[ ! -d $appPath ]]; then echo "reveal.js not found" @@ -23,13 +26,10 @@ else cd $appPath fi -fn=$1 #markdown document to render e.g. neuroanatomy1.md -basefn=$(basename $fn) if [[ ! -e $basefn ]]; then - ln -s $fn $basefn + ln -s $fullfn $basefn fi - #add markdown filename into html title tag fn2=$(echo $basefn | sed -E 's/[0-9]{2,4}-//g' | sed 's/\.md//') #prep filename sed -i -E "s|().+(<\/title>)|\1$fn2\2|" index.html #swap in document title diff --git a/s b/s new file mode 100755 index 0000000..7d39b59 --- /dev/null +++ b/s @@ -0,0 +1,24 @@ +#! /usr/bin/python +# adapted from https://stackoverflow.com/questions/59052703/grep-or-ripgrep-how-to-find-only-files-that-match-multiple-patterns-not-only-o + +import sys +import subprocess +from itertools import permutations + +usage = """ +Search with multiple keywords across files within current and subdirs using ripgrep. Provides boolean AND type search. + +Usage: s keyword1 keyword2 +e.g. s cerebral cetacean sand | f +""" + +# Check that the user puts in an argument, else print the usage variable, then quit. +if len(sys.argv)< 2: + print (usage) + sys.exit(0) + +rgarg = '|'.join(('.*'.join(x) for x in permutations(sys.argv[1:]))) +cmd = ['rg', '-lUi', '--multiline-dotall', rgarg, '.'] +# print(' '.join(cmd)) +proc = subprocess.run(cmd, capture_output=True) +sys.stdout.write(proc.stdout.decode('utf-8')) diff --git a/sbib b/sbib index 396041b..78d15d1 100755 --- a/sbib +++ b/sbib @@ -12,13 +12,20 @@ if [ "$1" == "-h" ] ; then # pass a custom citation style and database sbib a citeprocStyle.csl bibdFile.json + Then press <enter> to open pdf + and press <ctrl-c> to copy citation to clipboard + + note: + # to convert a bib to json, run + # pandoc bibdFile.bib -t csljson -o bibdFile.json + + depends: fzf git grep sed tail bat or less - pandoc - pandoc-citeproc + fetch_citation, pandoc, pandoc-citeproc zathura (or other fast pdf viewer) echo wl-copy @@ -32,9 +39,9 @@ if [ "$1" == "-h" ] ; then fi #Setup defaults -cslFile=${2:-$HOME/projects/bibd/bibd-md.csl} -bibdFile=${3:-$HOME/projects/bibd/OMEGA.bib} -# bibdFile=${2:-$HOME/projects/bibd/OMEGA.json} +cslFile=${2:-$HOME/projects/learn/bibd/bibd-md.csl} +bibdFile=${3:-$HOME/projects/learn/bibd/OMEGA.bib} +# bibdFile=${2:-$HOME/projects/learn/bibd/OMEGA.json} cd $(dirname $bibdFile) set -e #exit if an error @@ -49,28 +56,9 @@ view_bib() { # str=$(git grep -E --line-number $sPattern $bibdFile | fzf --delimiter : --preview 'less {1}' --preview-window=:up:70%:+{2}-5) str=$(git grep -E --line-number $sPattern $bibdFile | fzf --delimiter : --preview 'bat --color=always --style=numbers --line-range=:500 {1}' --preview-window=:up:70%:+{2}-5) - # extract citation key from the fzf string citeKey=$(echo $str | sed -E "s|$(basename $bibdFile):[0-9]+:@[a-zA-Z]+\{(.+),|\1|") - - #method1 - #todo: use json with jq - # outCitation=$(pandoc -f latex <(echo "\cite{$citeKey}") -t plain -o - --bibliography $bibdFile --citeproc --csl $cslFile | tail -n +3) - - #method2 - #need to use a temporary bib file as input to pandoc because parsing a large bib file is too slow, but json input would be faster - #and because pandoc expects a file input for bibliography - tmpName=$(mktemp --suffix=.bib) - str3=$(grep -A 30 $citeKey $bibdFile) - echo $str3 | sed -E "s|(.+\} ?\}).+|\1|" > $tmpName - outCitation=$(pandoc -f latex <(echo "\cite{$citeKey}") -t plain -o - --bibliography $tmpName --citeproc --csl $cslFile | tail -n +3) - rm $tmpName - - #tail -n +61 $fn | sed -E "s#(.+\}\})#\1#" - # fzf --preview="head {}" --preview-window=up:30% - # fzf --preview="file {}" --preview-window=down:1 - echo $outCitation - echo $outCitation | wl-copy + fetch_citation citeKey } @@ -80,9 +68,12 @@ view_json() { # urlStr=$(jq -r '.[] | select(.id==env.citeKey).URL' $bibdFile) #actually this is the good one, opens pdfs quickly - jq -r '.[] | [.id, .title, .abstract, .keyword, .DOI, .PMID, .author[]?.family, .issued[]?[0]?[0], .["container-title"], .URL] | join(" ")' $bibdFile | fzf --preview 'echo {}' --preview-window=:up:70%:wrap --bind "enter:execute-silent(zathura {-1} &)" -} + jq -r '.[] | [.id, .title, .abstract, .keyword, .DOI, .PMID, .author[]?.family, .issued[]?[0]?[0], .["container-title"], .URL] | join(" ")' $bibdFile | fzf --preview 'echo {}' --preview-window=:up:70%:wrap \ + --bind "enter:execute-silent(zathura {-1} &)" \ + --bind "ctrl-c:execute-silent(fetch_citation {1})" +# --bind "ctrl-c:execute(notify-send {1})" +} #set pattern to the null character '\0' for search all, else search only cite keys @@ -93,7 +84,7 @@ else fi if [ "$1" == "j" ]; then - bibdFile=${3:-$HOME/projects/bibd/OMEGA.json} + bibdFile=${3:-$HOME/projects/learn/bibd/OMEGA.json} view_json else view_bib diff --git a/sdoi.sh b/sdoi.sh index de4a580..2b1c0bd 100755 --- a/sdoi.sh +++ b/sdoi.sh @@ -24,8 +24,8 @@ set -e #exit if an error doi=$1 fn=$2 styleSheet=${pubmedStyleSheet:-$HOME/bin/pubmed2bibtex.xsl} -bibdFileOut=${bibdFileOut:-$HOME/projects/bibd/OMEGA.bib} -pdfPathOut=${pdfPathOut:-$HOME/projects/bibd/papers} +bibdFileOut=${bibdFileOut:-$HOME/projects/learn/bibd/OMEGA.bib} +pdfPathOut=${pdfPathOut:-$HOME/projects/learn/bibd/papers} relPath=$(basename $pdfPathOut) #define functions @@ -45,7 +45,7 @@ import_bib() { fetchBib_pubmed() { #request pubmed xml and transform into bibtex curl -s "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=$uid&retmode=xml" > $tmpBib.xml - xsltproc --novalid $styleSheet $tmpBib.xml > $tmpBib + xsltproc --novalid $styleSheet $tmpBib.xml >> $tmpBib } fetchBib_doiDotOrg() { @@ -69,7 +69,9 @@ extract_name() { append_bibfile() { #import bibtex - #first grep for a uid (doi) in case its already in db + #replace pubmed field with pmid + sed -i -E "s|(\W*)pubmed = |\1pmid = |" $tmpBib + #grep for a uid (doi) in case its already in db if [[ -z $(rg $doi $bibdFileOut) ]]; then echo "importing $tmpBib" cat $tmpBib >> $bibdFileOut @@ -91,7 +93,7 @@ append_pdf() { clean_up() { #clean up - rm -f $tmpBib.bib $tmpBib.bib.xml + rm -f $tmpBib $tmpBib.xml exit 1 } @@ -118,5 +120,6 @@ if [ -s "$tmpBib" ]; then import_bib else echo "sorry, doi not found.." + exit 1 fi diff --git a/spdf.sh b/spdf.sh index 4168f7b..77eb4e9 100755 --- a/spdf.sh +++ b/spdf.sh @@ -1,18 +1,16 @@ #!/bin/bash if [ "$1" == "-h" ] ; then echo " - spdf - search for a doi within a pdf. If found, use sdoi.sh to query pubmed, and append bibtex entry with the pdf to your local bib database file + spdf - search for a doi within a pdf. If found, print the doi and then use sdoi.sh to query pubmed and append a bibtex entry with the pdf to your local bib database file usage: spdf.sh file.pdf depends: pdftotext - from ghostscript or poppler or texlive ? - xsltproc, xmllint - xml programs from libxml - pubmed2bibtex.xsl - xml processor stylesheet defaults: - Set the three required default file locations (xsl file, bib file, pdf directory) + See sdoi.sh " exit 0 fi @@ -37,6 +35,8 @@ fi if [ -z "$doi" ]; then echo "doi not found" exit 1 +else + echo $doi fi sdoi.sh $doi $fn diff --git a/wm_spawn b/wm_spawn new file mode 100755 index 0000000..3fbedd9 --- /dev/null +++ b/wm_spawn @@ -0,0 +1,9 @@ +#!/bin/bash +if [ -n "$TMUX" ] ; then + # tmux session running + # tmux split-window -h "nvim \"$*\"" + tmux split-window -h $EDITOR $1 +else + # alacritty -e "nvim \"$*\"" + alacritty -e bash -ic "$EDITOR $1" & +fi