Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # aliased as : paragraph='/path/to/this/file.sh $$'
- # it recieves the pid of current terminal as
- # first argument $1 to use for zoom function
- # defined bellow
- #
- pyScr="$(dirname $(realpath "$0") )/rParagraph.py"
- green="\\\033[1;32m"
- colors[0]="\\\033[1;31m" #red
- colors[1]="\\\033[1;34m" #blue
- colors[2]="\\\033[1;33m" #yellow
- file=$(python "$pyScr" get |cut -d: -f2)
- fid=$(python "$pyScr" get |cut -d: -f1)
- lastP=$(python "$pyScr" getp "$fid" )
- lastP=$(( lastP +1 ))
- if [[ $2 == "-" ]] ; then lastP=$(( lastP -1 )) ; fi
- if [[ $2 == "--" ]] ; then lastP=$(( lastP -2 )) ; fi
- if (( $lastP <=0 )) ; then lastP=1 ; fi
- do_not_encr=0
- speak=0
- i=0
- while read -r w ; do
- Hwords[$i]=$w
- i=$((i+1))
- done <<< "$(python "$pyScr" getw "$fid")"
- function _printhelp () {
- printf '\033[1;31m %-23s\033[1;0m\t%s\n' "$1" "$2"
- }
- function help_f(){
- printf '\033[01;0m'
- echo "
- read a text file paragraph by paragraph
- "
- _printhelp "h" "print this help"
- _printhelp "a [filename]" "add file to database"
- _printhelp "c" "display current file"
- _printhelp "favs" "display indeces of favorite paragraphs"
- _printhelp "" "of current selected file"
- _printhelp "f [number]" "go to fav paragraph"
- _printhelp "af [number]" "add paragraph to favs"
- _printhelp "o" "xdg-open current file"
- _printhelp "[number]" "jump to paragraph"
- _printhelp "s" "select current file"
- _printhelp "ss [pattern]" "search for pattern"
- _printhelp "w [word/sentence]" "add words to be colored"
- _printhelp "" "first one is red, 2nd is blue"
- _printhelp "" "and the rest is yellow"
- _printhelp "words" "print colored words for current file"
- _printhelp "rw" "remove a colored word"
- exit
- }
- function display_center(){
- line="$1"
- columns="$(tput cols)"
- printf "%*s\n" $(( (${#line} + columns) / 2)) "$line"
- }
- function rmWord(){
- w=$(
- python "$pyScr" getw "$fid" |
- rofi -i -dmenu -p "select word to delete" -width -50
- )
- [[ ! -z "$w" ]] && python "$pyScr" rmw "$fid" "$w"
- echo "($fid,$w) deleted"
- exit
- }
- function set_file(){
- f=$(
- python "$pyScr" all |
- awk -F: '
- function basename(file) {
- sub(".*/", "", file)
- return file
- }
- {
- printf("%s:%s\n",$1,basename($2))
- }
- '|
- rofi -i -dmenu -p "$3" -width -100 -columns 1
- )
- [[ -z "$f" ]] && exit
- fid=${f%:*}
- python "$pyScr" "set" "$fid"
- exit
- }
- function readIT(){
- local lastP=$1
- local count=$2
- local file=$3
- local pattern="$4"
- awk -v par="$lastP" -v c="$count" -v pat="$pattern" '
- BEGIN{
- p=0;
- str="";
- }
- NF == 0 {
- p = p +1 ;
- }
- length(pat) && index($0, pat){
- msg = msg "pattern found in paragraph " (p+1) "\n"
- }
- {
- if ( $0 ~ /^ [A-Z]/ ) {
- p = p +1 ;
- }
- if ( p == par ) {
- str = str " " $0 "\n"
- }
- }
- END {
- if ( c == -1 ) {
- print p
- }else {
- if (length(pat)){
- printf msg
- }
- else {
- print str
- }
- }
- }
- ' "$file"
- }
- function addFav(){
- if [[ -z "$1" ]]
- then local lastP=$((lastP-1))
- else local lastP=$1
- fi
- python "$pyScr" "addf" "$fid" "$lastP"
- echo "($fid,$lastP) added"
- exit
- }
- function zoom(){
- local ff="/tmp/paragrap-$1"
- oldzoom=$(cat "$ff" 2>/dev/null || echo 0)
- (( $oldzoom == 5 )) && return
- newzoom=0
- for ii in {1..5} ; do
- xdotool key ctrl+shift+KP_Add
- newzoom=$((newzoom+1))
- done
- echo "$newzoom" > "$ff"
- }
- zoom "$1"
- case "$2" in
- h) help_f ;;
- a)
- fid=$( python "$pyScr" add "$PWD/$3" )
- python "$pyScr" "set" "$fid"
- exit
- ;;
- c)
- echo "$file"
- exit
- ;;
- favs)
- python "$pyScr" "getfs" "$fid"
- exit
- ;;
- f)
- n='^[0-9]*$'
- [[ -z $3 ]] && exit
- ! [[ $3 =~ $n ]] && exit
- lastP=$(python "$pyScr" "getf" "$fid" "$3" )
- do_not_encr=1
- ;;
- af) addFav "$3" ;;
- o) xdg-open "$file" & disown
- exit
- ;;
- s) set_file ;;
- ss)
- readIT 0 0 "$file" "$3"
- exit
- ;;
- rw) rmWord ;;
- read) speak=1;;
- words)
- python "$pyScr" getw "$fid"
- exit
- ;;
- w)
- python "$pyScr" "addw" "$fid" "$3"
- exit
- ;;
- *)
- n='^[0-9]*$'
- if ! [[ -z "$2" ]] && [[ $2 =~ $n ]]
- then lastP=$2
- fi
- esac
- [[ -z "$lastP" ]] && lastP=1
- TOTAL=$(readIT "$lastP" -1 "$file")
- TEXT=$(readIT "$lastP" 0 "$file" )
- (( $speak == 1 )) && TEXTTOSPEAK=$TEXT
- if ! [[ -z "${Hwords[@]}" ]] ; then
- for (( i = 0;i<${#Hwords[@]};i++ )) ; do
- if (( $i < 2 ))
- then
- Cwords[$i]="${colors[$i]}${Hwords[$i]}$green"
- else
- Cwords[$i]="${colors[2]}${Hwords[$i]}$green"
- fi
- TEXT=$(
- echo "$TEXT" |
- sed "s@${Hwords[$i]}@${Cwords[$i]}@g"
- )
- done
- fi
- tput reset
- printf '\033[1;34m'
- display_center "> $(basename "$file") <"
- printf '\033[1;32m'
- printf "$TEXT" | fmt -w 85 -s
- echo
- display_center "$lastP/$TOTAL"
- if (( $do_not_encr == 0 )) ; then
- python "$pyScr" setp "$fid" "$lastP"
- fi
- (( $speak == 1 )) && {
- echo "$TEXTTOSPEAK" | espeak -ven-us+f5 -a10 >/dev/null 2>&1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement