Updated README.md
[lace] / lace
1 #!/usr/bin/env bash
2
3 # forked from https://gitlab.com/uoou/dotfiles/-/tree/master/stow/bin/home/drew/.local/bin/lace
4 # https://raw.githubusercontent.com/frrobert2/lace/main/lace
5 # Shows both how many days ago and date
6 # Modified to work with Debian running Sed 3.7
7 # connectto is a timeout variable in seconds 
8 # it is the time lace will wait to connect to a site before it stops the connection
9 #  default is 30 seconds you can change it what you want
10 connectto=30
11 if [ -z "$XDG_CONFIG_HOME" ];then
12         config_place="$HOME/.config/lace"
13 else
14         config_place="$XDG_CONFIG_HOME/lace"
15 fi
16 [[ ! -d "$config_place" ]] && mkdir -p $config_place
17 subsfile="$config_place/subs"
18
19 red="[31m"
20 blue="[34m"
21 nc="[m"
22 esc=$(echo -e "\033")
23 tmpfile=$(mktemp /tmp/lace.XXXXXX)
24 tmpfile2=$(mktemp /tmp/lace.XXXXXX)
25 function reldate() {
26 # stolen from https://unix.stackexchange.com/a/451216
27     local SEC_PER_MINUTE=$((60))
28     local   SEC_PER_HOUR=$((60*60))
29     local    SEC_PER_DAY=$((60*60*24))
30     local  SEC_PER_MONTH=$((60*60*24*30))
31     local   SEC_PER_YEAR=$((60*60*24*365))
32
33     local last_unix="$(date --date="@$1" +%s)"    # convert date to unix timestamp
34         local now_unix="$2"
35     local delta_s=$(( now_unix - last_unix ))
36
37     if (( delta_s <  SEC_PER_MINUTE * 2))
38     then
39         echo ""$((delta_s))" seconds ago"
40     elif (( delta_s <  SEC_PER_HOUR * 2))
41     then
42         echo ""$((delta_s / SEC_PER_MINUTE))" minutes ago"
43     elif (( delta_s <  SEC_PER_DAY * 2))
44     then
45         echo ""$((delta_s / SEC_PER_HOUR))" hours ago"
46     elif (( delta_s <  SEC_PER_MONTH * 2))
47     then
48         echo ""$((delta_s / SEC_PER_DAY))" days ago"
49     elif (( delta_s <  SEC_PER_YEAR * 2))
50     then
51         echo ""$((delta_s / SEC_PER_MONTH))" months ago"
52     else
53         echo ""$((delta_s / SEC_PER_YEAR))" years ago"
54     fi
55 }
56 #export -f reldate
57
58 function dohelp() {
59         echo -e "\nUsage:\n"
60         echo -e "\t$(basename "$0") - read subscriptions\n"
61         echo -e "\t$(basename "$0") nope - read subscriptions without pager\n"
62         echo -e "\t$(basename "$0") subs - list subscriptions\n"
63         echo -e "\t$(basename "$0") sub nickname url - add subscription, e.g.:"
64         echo -e "\t$(basename "$0") sub Drew gemini://friendo.monster/tiny.gmi\n"
65         echo -e "\t$(basename "$0") unsub nickname - remove subscription, e.g.:"
66         echo -e "\t$(basename "$0") unsub Drew\n"
67         echo -e "\t$(basename "$0") update - redownload script to location of current script (might need sudo, depending on location)\n"
68         exit
69 }
70
71 function timeline() {
72         while read f;do
73                 local user=$(awk '{print $2}' <<< "$f")
74                 local url=$(awk '{print $1}' <<< "$f")
75                 if [[ "$url" != *://* ]];then
76                         domain="$url"
77                         url="gemini://$url"
78                 else
79                                 domain=$(sed 's/gemini:\/\///' <<< "$url")
80                         fi
81                         if [[ "$url" != *.*/*.* ]] && [[ $url != */ ]];then
82                                 url="$url/"
83                         fi
84                         domain=$(sed 's/\/.*$//' <<< "$domain")
85                         fav=$(timeout $connectto openssl s_client -crlf -quiet -connect "$domain:1965" <<<"gemini://$domain/favicon.txt" 2>/dev/null | sed -e '1d')
86                         if [ ! -z "$fav" ];then
87                                 user="$fav $user"
88                         fi
89                         timeout $connectto openssl s_client -crlf -quiet -connect "$domain:1965" <<<"$url" 2>/dev/null | sed -e '1d' | sed -n '/##/,$p' | sed -e "s/^\(## .*\)$/\1\n${esc}$red$user${esc}$nc ($domain)/g" >> $tmpfile
90                         [[ ! -z $(sed -n '$p' $tmpfile) ]] && echo "" >> $tmpfile
91                 done <"$subsfile"
92                 cat $tmpfile | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sed -rn '/^## .*/p' | sed -e 's/\v/\n/g' | sed -e "s/^## \(.*\)/date -d '\1' +%s/e" | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sort -r | sed -e 's/\v/\n/g'  > $tmpfile
93
94
95         # removed segment of code       | sed "s/^\([0-9]\+\)$/{reldate \1 $now_unix;}/e" | sed "s/^\([0-9]* [a-z]* ago\)$/${esc}$blue\1${esc}$nc/g"
96
97         while read line; do
98         if [[ "$line" =~ ^[0-9]{10,}$ ]]; then
99                 reldate "$line" "$(date +%s)" >> $tmpfile2
100                 
101                 echo "$(date -d @$line)" >> $tmpfile2
102         else
103            echo "$line" >> $tmpfile2
104         fi
105         done<$tmpfile
106
107         cat $tmpfile2 | sed "s/^\([0-9]* [a-z]* ago\)$/${esc}$blue\1${esc}$nc/g" >$tmpfile
108         
109
110                 if [[ "$1" == "nope" ]];then
111                         cat $tmpfile && rm -f $tmpfile $tmpfile2
112                 else
113                         less -RisW $tmpfile && rm -f $tmpfile $tmpfile2
114                 fi
115         }
116
117         if [[ "$1" == "help" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-help" ]] || [[ "$1" == "-h" ]];then
118                 dohelp
119         elif [[ "$1" == "sub" ]];then
120                 [[ "$#" -ne 3 ]] && dohelp
121                 echo "$3 $2" >> $subsfile && echo "added $2 to subscriptions"
122 elif [[ "$1" == "unsub" ]];then
123         [[ "$#" -ne 2 ]] && dohelp
124         if grep " $2$" $subsfile 1>/dev/null;then
125                 sed -i "/ $2$/d" $subsfile &&
126                 echo "removed subscription to $2"
127         else
128                 echo "subscription \"$2\" does not exist"
129         fi
130 elif [[ "$1" == "subs" ]];then
131         awk '{print $2" ("$1")"}' $subsfile 
132 elif [[ "$1" == "update" ]];then
133         curl https://raw.githubusercontent.com/frrobert2/lace/main/lace > "${BASH_SOURCE[0]}"
134 else
135         now_unix="$(date +'%s')"
136         if [[ "$1" == "nope" ]];then
137                 timeline nope
138         else
139                 timeline
140         fi
141 fi