Advertisement
mosaid

readparagraph.sh

Nov 7th, 2019
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.94 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # aliased as : paragraph='/path/to/this/file.sh $$'
  4. # it recieves the pid of current terminal as
  5. # first argument $1 to use for zoom function
  6. # defined bellow
  7. #
  8.  
  9. pyScr="$(dirname $(realpath "$0") )/rParagraph.py"
  10.  
  11. green="\\\033[1;32m"
  12.  
  13. colors[0]="\\\033[1;31m"    #red
  14. colors[1]="\\\033[1;34m"    #blue
  15. colors[2]="\\\033[1;33m"    #yellow
  16.  
  17.  
  18. file=$(python "$pyScr" get |cut -d: -f2)
  19. fid=$(python "$pyScr" get |cut -d: -f1)
  20. lastP=$(python "$pyScr" getp "$fid" )
  21.  
  22. lastP=$(( lastP +1 ))
  23. if [[ $2 == "-" ]] ; then lastP=$(( lastP -1 )) ; fi
  24. if [[ $2 == "--" ]] ; then lastP=$(( lastP -2 )) ; fi
  25. if (( $lastP <=0 )) ; then lastP=1 ; fi
  26.  
  27. do_not_encr=0
  28. speak=0
  29.  
  30. i=0
  31. while read -r w ; do
  32.     Hwords[$i]=$w
  33.     i=$((i+1))
  34. done <<< "$(python "$pyScr" getw "$fid")"
  35.  
  36.  
  37. function _printhelp () {
  38.     printf '\033[1;31m  %-23s\033[1;0m\t%s\n' "$1" "$2"
  39. }
  40.  
  41. function help_f(){
  42.     printf '\033[01;0m'
  43.     echo "
  44.        read a text file paragraph by paragraph
  45.    "
  46.     _printhelp "h"                     "print this help"
  47.     _printhelp "a [filename]"          "add file to database"
  48.     _printhelp "c"                     "display current file"
  49.     _printhelp "favs"                  "display indeces of favorite paragraphs"
  50.     _printhelp ""                      "of current selected file"
  51.     _printhelp "f [number]"            "go to fav paragraph"
  52.     _printhelp "af [number]"           "add paragraph to favs"
  53.     _printhelp "o"                     "xdg-open current file"
  54.     _printhelp "[number]"              "jump to paragraph"
  55.     _printhelp "s"                     "select current file"
  56.     _printhelp "ss [pattern]"          "search for pattern"
  57.     _printhelp "w [word/sentence]"     "add words to be colored"
  58.     _printhelp ""                      "first one is red, 2nd is blue"
  59.     _printhelp ""                      "and the rest is yellow"
  60.     _printhelp "words"                 "print colored words for current file"
  61.     _printhelp "rw"                    "remove a colored word"
  62.     exit
  63. }
  64.  
  65. function display_center(){
  66.     line="$1"
  67.     columns="$(tput cols)"
  68.     printf "%*s\n" $(( (${#line} + columns) / 2)) "$line"
  69. }
  70.  
  71. function rmWord(){
  72.     w=$(
  73.         python "$pyScr" getw "$fid" |
  74.         rofi -i -dmenu -p "select word to delete" -width -50
  75.     )
  76.     [[ ! -z "$w" ]] && python "$pyScr" rmw "$fid" "$w"
  77.     echo "($fid,$w) deleted"
  78.     exit
  79. }
  80.  
  81. function set_file(){
  82.     f=$(
  83.         python "$pyScr" all |
  84.         awk -F: '
  85.            function basename(file) {
  86.                sub(".*/", "", file)
  87.                return file
  88.            }
  89.            {
  90.                printf("%s:%s\n",$1,basename($2))
  91.            }
  92.        '|
  93.         rofi -i -dmenu -p "$3" -width -100 -columns 1
  94.     )
  95.     [[ -z "$f" ]] && exit
  96.     fid=${f%:*}
  97.     python "$pyScr" "set" "$fid"
  98.     exit
  99. }
  100.  
  101. function readIT(){
  102.     local lastP=$1
  103.     local count=$2
  104.     local file=$3
  105.     local pattern="$4"
  106.     awk -v par="$lastP" -v c="$count" -v pat="$pattern" '
  107.        BEGIN{
  108.            p=0;
  109.            str="";
  110.        }
  111.        NF == 0 {
  112.            p = p +1 ;
  113.        }
  114.        length(pat) && index($0, pat){
  115.        msg = msg "pattern found in paragraph " (p+1) "\n"
  116.        }
  117.        {
  118.            if ( $0 ~ /^    [A-Z]/ ) {
  119.                p = p +1 ;
  120.            }
  121.            if ( p == par ) {
  122.                str = str " " $0 "\n"
  123.            }
  124.        }
  125.        END {
  126.            if ( c == -1 ) {
  127.                print p
  128.            }else {
  129.                if (length(pat)){
  130.                    printf msg
  131.                }
  132.                else {
  133.                    print str
  134.                }
  135.            }
  136.        }
  137.    ' "$file"
  138. }
  139. function addFav(){
  140.     if [[ -z "$1" ]]
  141.         then local lastP=$((lastP-1))
  142.         else local lastP=$1
  143.     fi
  144.     python "$pyScr" "addf" "$fid" "$lastP"
  145.     echo "($fid,$lastP) added"
  146.     exit
  147. }
  148.  
  149. function zoom(){
  150.     local ff="/tmp/paragrap-$1"
  151.     oldzoom=$(cat "$ff" 2>/dev/null || echo 0)
  152.     (( $oldzoom == 5 )) && return
  153.     newzoom=0
  154.     for ii in {1..5} ; do
  155.         xdotool key ctrl+shift+KP_Add
  156.         newzoom=$((newzoom+1))
  157.     done
  158.     echo "$newzoom" > "$ff"
  159. }
  160.  
  161. zoom "$1"
  162.  
  163. case "$2" in
  164.     h)  help_f ;;
  165.     a)
  166.         fid=$( python "$pyScr" add "$PWD/$3" )
  167.         python "$pyScr" "set" "$fid"
  168.         exit
  169.         ;;
  170.     c)
  171.         echo "$file"
  172.         exit
  173.         ;;
  174.     favs)
  175.         python "$pyScr" "getfs" "$fid"
  176.         exit
  177.         ;;
  178.     f)
  179.        n='^[0-9]*$'
  180.        [[ -z $3 ]] && exit
  181.        ! [[ $3 =~ $n ]] && exit
  182.        lastP=$(python "$pyScr" "getf" "$fid" "$3" )
  183.        do_not_encr=1
  184.        ;;
  185.    af) addFav "$3" ;;
  186.     o) xdg-open "$file" & disown
  187.        exit
  188.        ;;
  189.     s) set_file ;;
  190.    ss)
  191.        readIT 0 0 "$file" "$3"
  192.        exit
  193.        ;;
  194.    rw) rmWord ;;
  195.    read) speak=1;;
  196.     words)
  197.         python "$pyScr" getw "$fid"
  198.         exit
  199.         ;;
  200.     w)
  201.         python "$pyScr" "addw" "$fid" "$3"
  202.         exit
  203.         ;;
  204.     *)
  205.         n='^[0-9]*$'
  206.         if ! [[ -z "$2" ]] && [[ $2 =~ $n ]]
  207.             then lastP=$2
  208.         fi
  209. esac
  210.  
  211.  
  212. [[ -z "$lastP" ]] && lastP=1
  213.  
  214. TOTAL=$(readIT "$lastP" -1 "$file")
  215. TEXT=$(readIT "$lastP"  0 "$file" )
  216. (( $speak == 1 )) && TEXTTOSPEAK=$TEXT
  217.  
  218. if ! [[ -z "${Hwords[@]}" ]] ; then
  219. for (( i = 0;i<${#Hwords[@]};i++ )) ; do
  220.     if (( $i < 2 ))
  221.         then
  222.             Cwords[$i]="${colors[$i]}${Hwords[$i]}$green"
  223.         else
  224.             Cwords[$i]="${colors[2]}${Hwords[$i]}$green"
  225.     fi
  226.     TEXT=$(
  227.         echo "$TEXT" |
  228.         sed "s@${Hwords[$i]}@${Cwords[$i]}@g"
  229.     )
  230. done
  231. fi
  232.  
  233. tput reset
  234. printf '\033[1;34m'
  235. display_center "> $(basename "$file") <"
  236. printf '\033[1;32m'
  237. printf "$TEXT" | fmt -w 85 -s
  238. echo
  239. display_center "$lastP/$TOTAL"
  240.  
  241.  
  242. if (( $do_not_encr == 0 )) ; then
  243.     python "$pyScr" setp "$fid" "$lastP"
  244. fi
  245.  
  246. (( $speak == 1 )) && {
  247.     echo "$TEXTTOSPEAK" | espeak -ven-us+f5 -a10 >/dev/null 2>&1
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement