commit 925f830e5fa4e508545d6054f80dcf701973990b
parent 32a5227dda579b535024fb2f904cfc0de73e3919
Author: Jaromil <jaromil@dyne.org>
Date:   Fri, 29 Mar 2013 14:56:53 +0100
speed optimizations in address handling
Diffstat:
4 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/src/zlibs/addressbook b/src/zlibs/addressbook
@@ -243,10 +243,10 @@ import_macosx() {
     lock ${addressbook}
     new=0; dupes=0;
     for a in ${(f)addresses}; do
-	_email=`echo $a | cut -d'|' -f1`
+	_email="${a[(ws:|:)1]}"
 	# remove from name all what is an email between brackets
 	# crop (trim) all beginning and ending whitespaces from name
-	_name=`echo $a | cut -d'|' -f2 | sed 's/<.*>//;s/^[ \t]*//;s/[ \t]*$//'`
+	_name=`print ${a[(ws:|:)2]} | sed 's/<.*>//;s/^[ \t]*//;s/[ \t]*$//'`
 	insert_address ${_email} ${_name}
 	if [ $? = 0 ]; then new=$(( $new + 1 ))
 	else dupes=$(( $dupes + 1 )); fi
@@ -356,10 +356,10 @@ EOF
     rm -f $WORKDIR/addressbook.vcf
     touch $WORKDIR/addressbook.vcf
     for a in ${(f)addresses}; do
-	_email=`echo $a | cut -d'|' -f1`
+	_email="${a[(ws:|:)1]}"
 	# remove from name all what is an email between brackets
 	# crop (trim) all beginning and ending whitespaces from name
-	_name=`echo $a | cut -d'|' -f2 | sed 's/<.*>//;s/^[ \t]*//;s/[ \t]*$//'`
+	_name=`print ${a[(ws:|:)2]} | sed 's/<.*>//;s/^[ \t]*//;s/[ \t]*$//'`
 	{ test "${_email}" != "" } && {
 	    cat <<EOF >> $WORKDIR/addressbook.vcf
 BEGIN:VCARD
@@ -392,6 +392,7 @@ edit_abook() {
     tmpread=$TMPDIR/abook.read.$datestamp.$RANDOM
     newlock $tmpread
     cp ${addressbook} $tmpread
+    func "launching SELECT email,name sqlite3 query"
     cat <<EOF | ${SQL} -column -header -batch $tmpread \
 	| grep -v '^email' > $tmp
 .width 40 100
@@ -399,14 +400,14 @@ edit_abook() {
 .separator '|'
 SELECT email, name FROM $list;
 EOF
+    func "query returned"
 
-    { test "$OS" = "MAC" } && {
-    }
     addresses=`cat $tmp`
     rm -f $tmp
 
     unlink $tmpread
 
+    func "converting database into abook format"
     cat <<EOF > $tmp
 # abook addressbook file
 
@@ -417,10 +418,10 @@ version=1.0
 EOF
     c=0
     for a in ${(f)addresses}; do
-	_email=`echo $a | cut -d'|' -f1`
+	_email="${a[(ws:|:)1]}"
 	# remove from name all what is an email between brackets
 	# crop (trim) all beginning and ending whitespaces from name
-	_name=`echo $a | cut -d'|' -f2 | sed 's/<.*>//;s/^[ \t]*//;s/[ \t]*$//'`
+	_name=`print ${a[(ws:|:)2]} | sed 's/<.*>//'`
 	{ test "${_email}" != "" } && {
 	    cat <<EOF >> $tmp
 [${c}]
@@ -432,6 +433,8 @@ EOF
 	}
     done
 
+    func "abook format ready, generating configuration"
+
     # generate abook configuration
     abookrc=$TMPDIR/abookrc.$datestamp.$RANDOM
     cat <<EOF > $abookrc
@@ -442,11 +445,15 @@ set mutt_command=jaro
 set sort_field=name
 EOF
 
+    func "ready to launch abook."
+
     abook --config $abookrc --datafile $tmp
     # remove config and backup turd
     ${=rm} ${tmp}~
     ${=rm} ${abookrc}
 
+    func "exporting abook to spruce format"
+
     tmpspruce=$TMPDIR/abook.spruce.$datestamp.$RANDOM
     newlock $tmpspruce
 
@@ -468,6 +475,8 @@ BEGIN { c=0; name=""; email=""; }
     addresses=`cat $tmpspruce`
     unlink $tmpspruce
 
+    func "done, ready to reimport the database"
+
     rm -f $tmp
 
     # move addressbook to old
@@ -479,7 +488,7 @@ CREATE TABLE $list
   email   text collate nocase unique,
   name    text collate nocase
 );
-
+PRAGMA synchronous = OFF;
 EOF
 
     newa=1; _name=""; _email=""
@@ -491,8 +500,8 @@ EOF
 	    continue }
 
 	{ test $newa -eq 1 } && {
-	    _name=`echo ${a} | cut -d: -f2`; newa=0; continue }
-	{ test $newa -eq 0 } && { _email=`echo ${a// /} | cut -d: -f2` }
+	    _name="${a[(ws/:/)2]}"; newa=0; continue }
+	{ test $newa -eq 0 } && { a=${a// /}; _email="${a[(ws/:/)2]}" }
 
     done
     func "Inserting the updated addressbook"
diff --git a/src/zlibs/email b/src/zlibs/email
@@ -76,8 +76,8 @@ fetchall() {
     res=0
     for i in `${=find} $WORKDIR/Accounts -type f | grep -v README`; do
 	account=`basename $i`
-	account_type=`print $account | cut -d. -f1`
-	account=`print $account | cut -d. -f2`
+	account_type="${account[(ws:.:)1]}"
+	account="${account[(ws:.:)2]}"
 	fetch
 	if [ $? != 0 ]; then res=1; fi
 	# returns an error if just one of the accounts did
diff --git a/src/zlibs/search b/src/zlibs/search
@@ -67,7 +67,7 @@ search() {
 	for rr in ${(f)res}; do
 	    _email=`print $rr | awk '{ print $1 }'`
 	    _name=`print $rr | awk '{ for(c=2;c<=NF;c++) printf "%s ", $c }'`
-	    results+=("$_name \t::\t <$_email>")
+	    results+=("$_name <$_email>")
 	done
 
 	{ test "${#results}" = "0" } || {
diff --git a/src/zlibs/stats b/src/zlibs/stats
@@ -40,7 +40,7 @@ stats() {
 	    grandtot=0
 	    for i in ${maildirs}; do
 		# find maildir prefixes
-		pfx=`print $i | cut -d. -f1`
+		pfx="${i[(ws:.:)1]}"
 		# is it a new prefix?
 		{ test "$pfx" = "$oldpfx" } && { continue }
 		oldpfx=$pfx
@@ -102,7 +102,7 @@ EOF
 	cp -r $MAILDIRS/${m} $TMPDIR/weekstats.db.mdir
 	for f in `${=find} $TMPDIR/weekstats.db.mdir -type f`; do
 	    timestamp=`fetchdate "%Y-%U" ${f}`
-	    mdir=`print $m|cut -d. -f1`
+	    mdir="${m[(ws:.:)1]}"
 	    cat <<EOF | ${SQL} -batch $db > $sql
 SELECT * FROM stats
 WHERE ml IS "${mdir}" AND date IS "${timestamp}";
@@ -151,7 +151,7 @@ EOF
 EOF
     prevmdir=""
     for m in ${maildirs}; do
-	mdir=`print $m|cut -d. -f1`
+	mdir="${m[(ws:.:)1]}"
 	# skip duplicates
 	if [ "$prevmdir" = "$mdir" ]; then continue
 	else prevmdir=$mdir; fi