Modularized Listing Sed

Still more to do tomorrow.
This commit is contained in:
David A. Harding
2009-03-12 01:02:10 -04:00
parent 6bc374c5f2
commit d508ed9dee

131
todo.sh
View File

@@ -339,6 +339,61 @@ fi
# === HEAVY LIFTING === # === HEAVY LIFTING ===
shopt -s extglob shopt -s extglob
_list-zero_padding() {
local LINES=$( wc -l $FILE_TO_NUMBER | sed 's/ .*//' )
local i=0
for i in $( seq 1 6 )
do
if [ $(( $LINES / 10 ** $i )) -gt 0 ]
then
:
else
break
fi
done
echo $i
}
_list-number_lines() {
local FILE_TO_NUMBER="$1"
local PADDING=$( _list-zero_padding "$FILE_TO_NUMBER" )
sed = "$FILE_TO_NUMBER" \
| sed "N; s/^/ /; s/ *\(.\{$PADDING,\}\)\n/\1 /" \
| sed '''
s/^ /00000/;
s/^ /0000/;
s/^ /000/;
s/^ /00/;
s/^ /0/;
'''
}
_list-sort_alphabetically() {
sort -f -k2
}
_list-add_priority_color() {
local FILE_TO_NUMBER="$1"
local PADDING=$( _list-zero_padding "$FILE_TO_NUMBER" )
sed '/^[0-9]\{'$PADDING'\} x /! {
s/\(.*(A).*\)/'$PRI_A'\1 '$DEFAULT'/g;
s/\(.*(B).*\)/'$PRI_B'\1 '$DEFAULT'/g;
s/\(.*(C).*\)/'$PRI_C'\1 '$DEFAULT'/g;
s/\(.*([D-Z]).*\)/'$PRI_X'\1 '$DEFAULT'/g;
}'
}
_list-hide_priority_projects_contexts() {
sed 's/'${HIDE_PRIORITY_SUBSTITUTION:-^}'//g' \
| sed 's/'${HIDE_PROJECTS_SUBSTITUTION:-^}'//g' \
| sed 's/'${HIDE_CONTEXTS_SUBSTITUTION:-^}'//g'
}
# == HANDLE ACTION == # == HANDLE ACTION ==
action=$( printf "%s\n" "$1" | tr 'A-Z' 'a-z' ) action=$( printf "%s\n" "$1" | tr 'A-Z' 'a-z' )
@@ -508,35 +563,19 @@ case $action in
item=$2 item=$2
if [ -z "$item" ]; then if [ -z "$item" ]; then
echo -e "$( \ echo -e "$( \
sed = "$TODO_FILE" \ _list-number_lines "$TODO_FILE" \
| sed 'N; s/^/ /; s/ *\(.\{2,\}\)\n/\1 /' \ | _list-sort_alphabetically \
| sed 's/^ /0/' \ | _list-add_priority_color "$TODO_FILE" \
| sort -f -k2 \ | _list-hide_priority_projects_contexts \
| sed '/^[0-9][0-9] x /! {
s/\(.*(A).*\)/'$PRI_A'\1 '$DEFAULT'/g;
s/\(.*(B).*\)/'$PRI_B'\1 '$DEFAULT'/g;
s/\(.*(C).*\)/'$PRI_C'\1 '$DEFAULT'/g;
s/\(.*([D-Z]).*\)/'$PRI_X'\1 '$DEFAULT'/g;
}' \
| sed 's/'${HIDE_PRIORITY_SUBSTITUTION:-^}'//g' \
| sed 's/'${HIDE_PROJECTS_SUBSTITUTION:-^}'//g' \
| sed 's/'${HIDE_CONTEXTS_SUBSTITUTION:-^}'//g' \
)" )"
echo "--" echo "--"
NUMTASKS=$(wc -l "$TODO_FILE" | sed 's/^[[:space:]]*\([0-9]*\).*/\1/') NUMTASKS=$(wc -l "$TODO_FILE" | sed 's/^[[:space:]]*\([0-9]*\).*/\1/')
echo "TODO: $NUMTASKS tasks in $TODO_FILE." echo "TODO: $NUMTASKS tasks in $TODO_FILE."
else else
command=$( command=$(
sed = "$TODO_FILE" \ _list-number_lines "$TODO_FILE" \
| sed 'N; s/^/ /; s/ *\(.\{2,\}\)\n/\1 /' \ | _list-sort_alphabetically \
| sed 's/^ /0/' \ | _list-add_priority_color "$TODO_FILE" \
| sort -f -k2 \
| sed '/^[0-9][0-9] x /! {
s/\(.*(A).*\)/'$PRI_A'\1 '$DEFAULT'/g;
s/\(.*(B).*\)/'$PRI_B'\1 '$DEFAULT'/g;
s/\(.*(C).*\)/'$PRI_C'\1 '$DEFAULT'/g;
s/\(.*([D-Z]).*\)/'$PRI_X'\1 '$DEFAULT'/g;
}' \
| grep -i $item | grep -i $item
) )
shift shift
@@ -547,10 +586,8 @@ case $action in
done done
command=$( \ command=$( \
echo "$command" \ echo "$command" \
| sort -f -k2 \ | _list-sort_alphabetically \
| sed 's/'${HIDE_PRIORITY_SUBSTITUTION:-^}'//g' \ | _list-hide_priority_projects_contexts \
| sed 's/'${HIDE_PROJECTS_SUBSTITUTION:-^}'//g' \
| sed 's/'${HIDE_CONTEXTS_SUBSTITUTION:-^}'//g' \
) )
echo -e "$command" echo -e "$command"
fi fi
@@ -561,9 +598,18 @@ case $action in
cat "$TODO_FILE" "$DONE_FILE" > "$TMP_FILE" cat "$TODO_FILE" "$DONE_FILE" > "$TMP_FILE"
if [ -z "$item" ]; then if [ -z "$item" ]; then
echo -e "`sed = "$TMP_FILE" | sed 'N; s/^/ /; s/ *\(.\{3,\}\)\n/\1 /' | sed 's/^ /00/' | sed 's/^ /0/' | sort -f -k2 | sed '/^[0-9][0-9][0-9] x /!s/\(.*(A).*\)/'$PRI_A'\1'$DEFAULT'/g' | sed '/^[0-9][0-9][0-9] x /!s/\(.*(B).*\)/'$PRI_B'\1'$DEFAULT'/g' | sed '/^[0-9][0-9][0-9] x /!s/\(.*(C).*\)/'$PRI_C'\1'$DEFAULT'/g' | sed '/^[0-9][0-9][0-9] x /!s/\(.*([A-Z]).*\)/'$PRI_X'\1'$DEFAULT'/'`" echo -e "$(
_list-number_lines "$TMP_FILE" \
| _list-sort_alphabetically \
| _list-add_priority_color "$TMP_FILE" \
)"
else else
command=`sed = "$TMP_FILE" | sed 'N; s/^/ /; s/ *\(.\{3,\}\)\n/\1 /' | sed 's/^ /00/' | sed 's/^ /0/' | sort -f -k2 | sed '/^[0-9][0-9][0-9] x /!s/\(.*(A).*\)/'$PRI_A'\1'$DEFAULT'/g' | sed '/^[0-9][0-9][0-9] x /!s/\(.*(B).*\)/'$PRI_B'\1'$DEFAULT'/g' | sed '/^[0-9][0-9][0-9] x /!s/\(.*(C).*\)/'$PRI_C'\1'$DEFAULT'/g' | sed '/^[0-9][0-9][0-9] x /!s/\(.*([A-Z]).*\)/'$PRI_X'\1'$DEFAULT'/' | grep -i $item ` command=$(
_list-number_lines "$TMP_FILE" \
| _list-sort_alphabetically \
| _list-add_priority_color "$TMP_FILE" \
| grep -i $item
)
shift shift
shift shift
for i in $* for i in $*
@@ -586,14 +632,23 @@ case $action in
fi fi
if [ -f "$src" ]; then if [ -f "$src" ]; then
if [ -z "$item" ]; then if [ -z "$item" ]; then
echo -e "`sed = "$src" | sed 'N; s/^/ /; s/ *\(.\{2,\}\)\n/\1 /' | sed 's/^ /0/' | sort -f -k2 | sed '/^[0-9][0-9] x /!s/\(.*(A).*\)/'$PRI_A'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(B).*\)/'$PRI_B'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(C).*\)/'$PRI_C'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*([A-Z]).*\)/'$PRI_X'\1'$DEFAULT'/'`" echo -e "$(
_list-number_lines "$src" \
| _list-sort_alphabetically \
| _list-add_priority_color "$src"
)"
if [ $TODOTXT_VERBOSE = 1 ]; then if [ $TODOTXT_VERBOSE = 1 ]; then
echo "--" echo "--"
NUMTASKS=$( sed '/./!d' "$src" | wc -l | sed 's/^[[:space:]]*\([0-9]*\).*/\1/') NUMTASKS=$( sed '/./!d' "$src" | wc -l | sed 's/^[[:space:]]*\([0-9]*\).*/\1/')
echo "TODO: $NUMTASKS lines in $src." echo "TODO: $NUMTASKS lines in $src."
fi fi
else else
command=`sed = "$src" | sed 'N; s/^/ /; s/ *\(.\{2,\}\)\n/\1 /' | sed 's/^ /0/' | sort -f -k2 | sed '/^[0-9][0-9] x /!s/\(.*(A).*\)/'$PRI_A'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(B).*\)/'$PRI_B'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(C).*\)/'$PRI_C'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*([A-Z]).*\)/'$PRI_X'\1'$DEFAULT'/' | grep -i $item ` command=$(
_list-number_lines "$src" \
| _list-sort_alphabetically \
| _list-add_priority_color "$src" \
| grep -i $item
)
shift shift
shift shift
for i in $* for i in $*
@@ -623,7 +678,12 @@ case $action in
note: PRIORITY must a single letter from A to Z." note: PRIORITY must a single letter from A to Z."
if [ -z "$pri" ]; then if [ -z "$pri" ]; then
echo -e "`sed = "$TODO_FILE" | sed 'N; s/^/ /; s/ *\(.\{2,\}\)\n/\1 /' | sed 's/^ /0/' | sort -f -k2 | sed 's/^ /0/' | sort -f -k2 | sed '/^[0-9][0-9] x /!s/\(.*(A).*\)/'$PRI_A'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(B).*\)/'$PRI_B'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(C).*\)/'$PRI_C'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*([A-Z]).*\)/'$PRI_X'\1'$DEFAULT'/'`" | grep \([A-Z]\) echo -e "$(
_list-number_lines "$TODO_FILE" \
| _list-sort_alphabetically \
| _list-add_priority_color "$TODO_FILE" \
| grep \([A-Z]\)
)"
if [ $TODOTXT_VERBOSE = 1 ]; then if [ $TODOTXT_VERBOSE = 1 ]; then
echo "--" echo "--"
NUMTASKS=$(grep \([A-Z]\) "$TODO_FILE" | wc -l | sed 's/^[[:space:]]*\([0-9]*\).*/\1/') NUMTASKS=$(grep \([A-Z]\) "$TODO_FILE" | wc -l | sed 's/^[[:space:]]*\([0-9]*\).*/\1/')
@@ -632,7 +692,12 @@ note: PRIORITY must a single letter from A to Z."
else else
[[ "$pri" = +([A-Z]) ]] || die "$errmsg" [[ "$pri" = +([A-Z]) ]] || die "$errmsg"
echo -e "`sed = "$TODO_FILE" | sed 'N; s/^/ /; s/ *\(.\{2,\}\)\n/\1 /' | sed 's/^ /0/' | sort -f -k2 | sed 's/^ /0/' | sort -f -k2 | sed '/^[0-9][0-9] x /!s/\(.*(A).*\)/'$PRI_A'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(B).*\)/'$PRI_B'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*(C).*\)/'$PRI_C'\1'$DEFAULT'/g' | sed '/^[0-9][0-9] x /!s/\(.*([A-Z]).*\)/'$PRI_X'\1'$DEFAULT'/'`" | grep \($pri\) echo -e "$(
_list-number_lines "$TODO_FILE" \
| _list-sort_alphabetically \
| _list-add_priority_color "$TODO_FILE" \
| grep \($pri\)
)"
if [ $TODOTXT_VERBOSE = 1 ]; then if [ $TODOTXT_VERBOSE = 1 ]; then
echo "--" echo "--"
NUMTASKS=$(grep \($pri\) "$TODO_FILE" | wc -l | sed 's/^[[:space:]]*\([0-9]*\).*/\1/') NUMTASKS=$(grep \($pri\) "$TODO_FILE" | wc -l | sed 's/^[[:space:]]*\([0-9]*\).*/\1/')