Skip to content

Commit

Permalink
Allow coloring contexts and projects independently
Browse files Browse the repository at this point in the history
Because @contexts and +projects are metadata as much as they are text,
it can be nice to highlight them independently of the coloring of the line
that contains them.  This is tough to do correctly in
a TODOTXT_FINAL_FILTER because doing it at that layer requires
re-parsing the text of the line to find the appropriate PRI_$x color to
reset to after coloring the word, or trying to reverse engineer where
the color codes inserted by todo.sh end and the user's text begins (and
vice versa).

Closes #119
  • Loading branch information
godlygeek authored and ginatrapani committed Dec 6, 2013
1 parent 745f5d5 commit 07b50a0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 6 deletions.
91 changes: 91 additions & 0 deletions tests/t1360-ls-project-context-highlighting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/bin/bash
#

test_description='highlighting projects and contexts
This test checks the highlighting (with colors) of projects and contexts.
'
. ./test-lib.sh

# Prioritized tasks with projects and contexts
cat > todo.txt <<EOF
(A) prioritized @con01 context
(B) prioritized +prj02 project
(C) prioritized context at EOL @con03
(D) prioritized project at EOL +prj04
+prj05 non-prioritized project at BOL
@con06 non-prioritized context at BOL
multiple @con_ @texts and +pro_ +jects
non-contexts: seti@home @ @* @(foo)
non-projects: lost+found + +! +(bar)
EOF

# config file specifying COLOR_PROJECT and COLOR_CONTEXT
#
TEST_TODO_LABEL_COLORS=todo-colors.cfg
cat todo.cfg > "$TEST_TODO_LABEL_COLORS"

echo "export COLOR_CONTEXT='\\\\033[1m'" >>"$TEST_TODO_LABEL_COLORS"
echo "export COLOR_PROJECT='\\\\033[2m'" >>"$TEST_TODO_LABEL_COLORS"

test_todo_session 'highlighting for contexts and projects' <<'EOF'
>>> todo.sh -d "$TEST_TODO_LABEL_COLORS" ls
1 (A) prioritized @con01 context
2 (B) prioritized +prj02 project
3 (C) prioritized context at EOL @con03
4 (D) prioritized project at EOL +prj04
5 +prj05 non-prioritized project at BOL
6 @con06 non-prioritized context at BOL
7 multiple @con_ @texts and +pro_ +jects
8 non-contexts: seti@home @ @* @(foo)
9 non-projects: lost+found + +! +(bar)
--
TODO: 9 of 9 tasks shown
EOF

test_todo_session 'suppressing highlighting for contexts and projects' <<'EOF'
>>> todo.sh -p -d "$TEST_TODO_LABEL_COLORS" ls
1 (A) prioritized @con01 context
2 (B) prioritized +prj02 project
3 (C) prioritized context at EOL @con03
4 (D) prioritized project at EOL +prj04
5 +prj05 non-prioritized project at BOL
6 @con06 non-prioritized context at BOL
7 multiple @con_ @texts and +pro_ +jects
8 non-contexts: seti@home @ @* @(foo)
9 non-projects: lost+found + +! +(bar)
--
TODO: 9 of 9 tasks shown
EOF

test_todo_session 'suppressing display of contexts' <<'EOF'
>>> todo.sh -@ -d "$TEST_TODO_LABEL_COLORS" ls
1 (A) prioritized context
2 (B) prioritized +prj02 project
3 (C) prioritized context at EOL
4 (D) prioritized project at EOL +prj04
5 +prj05 non-prioritized project at BOL
6 non-prioritized context at BOL
7 multiple and +pro_ +jects
8 non-contexts: seti@home @
9 non-projects: lost+found + +! +(bar)
--
TODO: 9 of 9 tasks shown
EOF

test_todo_session 'suppressing display of projects' <<'EOF'
>>> todo.sh -+ -d "$TEST_TODO_LABEL_COLORS" ls
1 (A) prioritized @con01 context
2 (B) prioritized project
3 (C) prioritized context at EOL @con03
4 (D) prioritized project at EOL
5 non-prioritized project at BOL
6 @con06 non-prioritized context at BOL
7 multiple @con_ @texts and
8 non-contexts: seti@home @ @* @(foo)
9 non-projects: lost+found +
--
TODO: 9 of 9 tasks shown
EOF

test_done
44 changes: 38 additions & 6 deletions todo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,10 @@ export PRI_B=$GREEN # color for B priority
export PRI_C=$LIGHT_BLUE # color for C priority
export PRI_X=$WHITE # color unless explicitly defined

# Default project and context colors.
export COLOR_PROJECT=$NONE
export COLOR_CONTEXT=$NONE

# Default highlight colors.
export COLOR_DONE=$LIGHT_GREY # color for done (but not yet archived) tasks

Expand Down Expand Up @@ -730,8 +734,13 @@ if [ $TODOTXT_PLAIN = 1 ]; then
PRI_X=$NONE
DEFAULT=$NONE
COLOR_DONE=$NONE
COLOR_PROJECT=$NONE
COLOR_CONTEXT=$NONE
fi

[[ "$HIDE_PROJECTS_SUBSTITUTION" ]] && COLOR_PROJECT="$NONE"
[[ "$HIDE_CONTEXTS_SUBSTITUTION" ]] && COLOR_CONTEXT="$NONE"

_addto() {
file="$1"
input="$2"
Expand Down Expand Up @@ -878,15 +887,38 @@ _format()
return color
}
{
clr = ""
if (match($0, /^[0-9]+ x /)) {
print highlight("COLOR_DONE") $0 highlight("DEFAULT")
clr = highlight("COLOR_DONE")
} else if (match($0, /^[0-9]+ \([A-Z]\) /)) {
clr = highlight("PRI_" substr($0, RSTART + RLENGTH - 3, 1))
print \
(clr ? clr : highlight("PRI_X")) \
(ENVIRON["HIDE_PRIORITY_SUBSTITUTION"] == "" ? $0 : substr($0, 1, RLENGTH - 4) substr($0, RSTART + RLENGTH)) \
highlight("DEFAULT")
} else { print }
clr = (clr ? clr : highlight("PRI_X"))
if (ENVIRON["HIDE_PRIORITY_SUBSTITUTION"] != "") {
$0 = substr($0, 1, RLENGTH - 4) substr($0, RSTART + RLENGTH)
}
}
end_clr = (clr ? highlight("DEFAULT") : "")
prj_beg = highlight("COLOR_PROJECT")
prj_end = (prj_beg ? (highlight("DEFAULT") clr) : "")
ctx_beg = highlight("COLOR_CONTEXT")
ctx_end = (ctx_beg ? (highlight("DEFAULT") clr) : "")
gsub(/[ \t][ \t]*/, "\n&\n")
len = split($0, words, /\n/)
printf "%s", clr
for (i = 1; i <= len; ++i) {
if (words[i] ~ /^[+].*[A-Za-z0-9_]$/) {
printf "%s", prj_beg words[i] prj_end
} else if (words[i] ~ /^[@].*[A-Za-z0-9_]$/) {
printf "%s", ctx_beg words[i] ctx_end
} else {
printf "%s", words[i]
}
}
printf "%s\n", end_clr
}
''' \
| sed '''
Expand Down

0 comments on commit 07b50a0

Please sign in to comment.