From 07b50a07e0007ac43327d31568b5a7cc26f3e0f2 Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Fri, 15 Nov 2013 17:58:47 -0500 Subject: [PATCH] Allow coloring contexts and projects independently 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 --- .../t1360-ls-project-context-highlighting.sh | 91 +++++++++++++++++++ todo.sh | 44 +++++++-- 2 files changed, 129 insertions(+), 6 deletions(-) create mode 100755 tests/t1360-ls-project-context-highlighting.sh diff --git a/tests/t1360-ls-project-context-highlighting.sh b/tests/t1360-ls-project-context-highlighting.sh new file mode 100755 index 0000000..560d01a --- /dev/null +++ b/tests/t1360-ls-project-context-highlighting.sh @@ -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 < "$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 diff --git a/todo.sh b/todo.sh index b2a2b42..6d7d549 100755 --- a/todo.sh +++ b/todo.sh @@ -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 @@ -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" @@ -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 '''