aboutsummaryrefslogblamecommitdiffstats
path: root/grepjournal
blob: df02d406b80e2dfa67f901a91e9eaf167291f331 (plain) (tree)
1
2
3
4
5
6
7
8



                                                                                 

                             

                              


                      
                   




                                    
 
                                       

                                             
 




                                                              
                                                                






                                                                      
                                                                                          

                        
                                                                        
 
                               

       

           
 
                                             
                                                       
                                                             
                                                                                                  


                               
                                
                              
                               
                               
      







                                                                                 
                         

        
                     
 
#!/bin/bash

# Search ~/Notes/journal for a term and return as a markdown list sorted by date.

colourWhite="$(tput setaf 7)"
colourGreen="$(tput setaf 2)"
colourYellow="$(tput setaf 3)"
colorCyan="$(tput setaf 6)"
txBold="$(tput bold)"
txReset="$(tput sgr0)"

# Automatic cleanup
trap 'rm -f "$grepped_results"' EXIT
grepped_results=$(mktemp) || exit 1

trap 'rm -f "$output_file"' EXIT
output_file=$(mktemp) || exit 1

termsize=$(stty size| awk '{print $2}')
printf "${colourWhite}=%.0s" $(seq $termsize)
echo

# very bad way to check the params passed in...
[[ -z "$2" ]] && [[ -n "$1" ]] && searchterm="$1"
[[ -n "$2" ]] && [[ -n "$1" ]] && searchterm="$1" && flag="$2"

# some confirmatory echoing
echo "${colourWhite}Search term: ${txBold}$searchterm${txReset}"
[[ -v flag ]] && echo "Flag: $flag"

if [[ $flag != "-i" ]]; then
    flag=""
fi

# do the business, starting with using grep to get the pertinent lines
echo -e "$(grep -R $flag "$searchterm" /home/"$USER"/Notes/journal/)" > "$grepped_results"

# more confirmatory text
echo "Command: 'grep -R $flag $searchterm /home/"$USER"/Notes/journal/'"

printf "=%.0s" $(seq $termsize)
echo ""

# URL regex


# subsitute to get the right format using sed
# form: 2022-01-10 12:49: :TODO Do the MyHR content: ht
urlregex="(https?:\/\/([[:alnum:]]+\.)+([[:alnum:]]+)+.*\s?)"
re='^/home/lemon/Notes/journal/([0-9]{4})-([0-9]{2})-([0-9]{2})\.md:-\s([0-9]{2}:[0-9]{2}):\s(.*)'
while IFS=  read -r line; do
    if [[ $line =~ $re ]]; then
        year=${BASH_REMATCH[1]}
        month=${BASH_REMATCH[2]}
        day=${BASH_REMATCH[3]}
        time=${BASH_REMATCH[4]}
        note=${BASH_REMATCH[5]}
    fi
    out_line="${colourGreen}"$year"-"$month"-"$day":${txReset} ${note}"
    if [[ $out_line =~ $urlregex ]]; then
        echo ${BASH_REMATCH[1]}
    fi
    if [[ $out_line =~ $searchterm ]]; then
        o=${out_line/$searchterm/"${colourYellow}${txBold}$searchterm${txReset}"}
        echo "$o" >> "$output_file" 
    fi
done < "$grepped_results"

# output
sort < "$output_file"