Compare commits

...

7 Commits

7 changed files with 256 additions and 123 deletions

View File

@@ -91,6 +91,16 @@ In addition, there are the following keybindings:
You may also invoke the script with `--help` to see further command-line options. You may also invoke the script with `--help` to see further command-line options.
Limitations
-----------
Here is a list of some currently present limitations.
- Timezone agnostic: Timezone specifications are ignored.
- Time agnostic: We use the date portion only of date-time specifications.
- No alarms or notifications
- No attachments
- No recurrences
License License
------- -------
This project is licensed under the [MIT License](./LICENSE). This project is licensed under the [MIT License](./LICENSE).

View File

@@ -1,16 +1,28 @@
# unescape # unescape
# Isolate and unescape the content part of an iCalendar line. # Isolate and unescape the content part of an iCalendar line.
# #
# @local variables: tmp # @local variables: i, c, c2, res
# @input str: String # @input str: String
# @return: Unescaped string # @return: Unescaped string
function unescape(str) { function unescape(str, i, c, c2, res) {
gsub("\\\\n", "\n", str) for(i=1; i<=length(str);i++) {
gsub("\\\\N", "\n", str) c = substr(str, i, 1)
gsub("\\\\,", ",", str) if (c != "\\") {
gsub("\\\\;", ";", str) res = res c
gsub("\\\\\\\\", "\\", str) continue
return str }
i++
c2 = substr(str, i, 1)
if (c2 == "n" || c2 == "N") {
res = res "\n"
continue
}
# Alternatively, c2 is "\\" or "," or ";". In each case, append res with
# c2. If the strings has been escaped correctly, then the character c2
# cannot be anything else. To be fail-safe, simply append res with c2.
res = res c2
}
return res
} }
# getcontent # getcontent

View File

@@ -1,16 +1,28 @@
# unescape # unescape
# Isolate and unescape the content part of an iCalendar line. # Isolate and unescape the content part of an iCalendar line.
# #
# @local variables: tmp # @local variables: i, c, c2, res
# @input str: String # @input str: String
# @return: Unescaped string # @return: Unescaped string
function unescape(str) { function unescape(str, i, c, c2, res) {
gsub("\\\\n", "\n", str) for(i=1; i<=length(str);i++) {
gsub("\\\\N", "\n", str) c = substr(str, i, 1)
gsub("\\\\,", ",", str) if (c != "\\") {
gsub("\\\\;", ";", str) res = res c
gsub("\\\\\\\\", "\\", str) continue
return str }
i++
c2 = substr(str, i, 1)
if (c2 == "n" || c2 == "N") {
res = res "\n"
continue
}
# Alternatively, c2 is "\\" or "," or ";". In each case, append res with
# c2. If the strings has been escaped correctly, then the character c2
# cannot be anything else. To be fail-safe, simply append res with c2.
res = res c2
}
return res
} }
# getcontent # getcontent

View File

@@ -3,22 +3,41 @@
# See https://datatracker.ietf.org/doc/html/rfc5545 for the RFC 5545 that # See https://datatracker.ietf.org/doc/html/rfc5545 for the RFC 5545 that
# describes iCalendar, and its syntax # describes iCalendar, and its syntax
# unescape # Make string single-line
# Isolate and unescape the content part of an iCalendar line.
# #
# @local variables: tmp
# @input str: String # @input str: String
# @return: Unescaped string # @return: String without newlines
function unescape(str) { function singleline(str) {
gsub("\\\\n", "\n", str) gsub("\\n", " ", str)
gsub("\\\\N", "\n", str)
gsub("\\\\,", ",", str)
gsub("\\\\;", ";", str)
gsub("\\\\\\\\", "\\", str)
return str return str
} }
# getcontent # Isolate and unescape the content part of an iCalendar line.
#
# @local variables: i, c, c2, res
# @input str: String
# @return: Unescaped string
function unescape(str, i, c, c2, res) {
for(i=1; i<=length(str);i++) {
c = substr(str, i, 1)
if (c != "\\") {
res = res c
continue
}
i++
c2 = substr(str, i, 1)
if (c2 == "n" || c2 == "N") {
res = res "\n"
continue
}
# Alternatively, c2 is "\\" or "," or ";". In each case, append res with
# c2. If the strings has been escaped correctly, then the character c2
# cannot be anything else. To be fail-safe, simply append res with c2.
res = res c2
}
return res
}
# Isolate content part of an iCalendar line, and unescape. # Isolate content part of an iCalendar line, and unescape.
# #
# @input str: String # @input str: String
@@ -27,17 +46,6 @@ function getcontent(str) {
return unescape(substr(str, index(str, ":") + 1)) return unescape(substr(str, index(str, ":") + 1))
} }
function storetext_line(content_line, c, prop)
{
c[prop] = getcontent(content_line, prop);
gsub("\\\\n", " ", c[prop]);
gsub("\\\\N", " ", c[prop]);
gsub("\\\\,", ",", c[prop]);
gsub("\\\\;", ";", c[prop]);
gsub("\\\\\\\\", "\\", c[prop]);
#gsub(" ", "_", c[prop]);
}
# formatdate # formatdate
# Generate kind-of-pretty date strings. # Generate kind-of-pretty date strings.
# #
@@ -109,7 +117,6 @@ BEGINFILE {
type = ""; type = "";
prop = ""; prop = "";
delete c; delete c;
} }
/^BEGIN:(VJOURNAL|VTODO)/ { /^BEGIN:(VJOURNAL|VTODO)/ {
@@ -155,10 +162,10 @@ ENDFILE {
# Process content lines # Process content lines
# strings # strings
cat = unescape(getcontent(c["CATEGORIES"])) cat = singleline(unescape(getcontent(c["CATEGORIES"])))
des = unescape(getcontent(c["DESCRIPTION"])) des = singleline(unescape(getcontent(c["DESCRIPTION"])))
sta = unescape(getcontent(c["STATUS"])) sta = singleline(unescape(getcontent(c["STATUS"])))
sum = unescape(getcontent(c["SUMMARY"])) sum = singleline(unescape(getcontent(c["SUMMARY"])))
# integers # integers
pri = unescape(getcontent(c["PRIORITY"])) pri = unescape(getcontent(c["PRIORITY"]))
@@ -177,6 +184,7 @@ ENDFILE {
# Priority field, primarly used for sorting # Priority field, primarly used for sorting
psort = 0; psort = 0;
priotext = ""
if (pri > 0) if (pri > 0)
{ {
priotext = "❗(" pri ") " priotext = "❗(" pri ") "
@@ -198,15 +206,14 @@ ENDFILE {
if (type == "VTODO") if (type == "VTODO")
{ {
# Either DUE or DURATION may appear. If DURATION appears, then also DTSTART # Either DUE or DURATION may appear. If DURATION appears, then also DTSTART
d = due ? due : d = due ? due : (dur ? dts " for " dur : "");
(dur ? dts " for " dur : "");
if (d && d <= today && sta != "COMPLETED") if (d && d <= today && sta != "COMPLETED")
{ {
datecolor = RED; datecolor = RED;
summarycolor = RED; summarycolor = RED;
} }
} else { } else {
d = c["DTSTART"]; d = dts
} }
d = d ? formatdate(d, todaystamp) : " "; d = d ? formatdate(d, todaystamp) : " ";

View File

@@ -4,7 +4,7 @@
# @return: Escaped string # @return: Escaped string
function escape(str) function escape(str)
{ {
gsub("\\\\", "\\", str) gsub("\\\\", "\\\\", str)
gsub(";", "\\;", str) gsub(";", "\\;", str)
gsub(",", "\\,", str) gsub(",", "\\,", str)
return str return str
@@ -16,7 +16,7 @@ function escape(str)
# @return: Escaped string # @return: Escaped string
function escape_categories(str) function escape_categories(str)
{ {
gsub("\\\\", "\\", str) gsub("\\\\", "\\\\", str)
gsub(";", "\\;", str) gsub(";", "\\;", str)
return str return str
} }
@@ -47,7 +47,7 @@ BEGIN {
type = "VJOURNAL"; type = "VJOURNAL";
zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1); zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1);
} }
desc { desc = desc "\\n" $0; next; } desc { desc = desc "\\n" escape($0); next; }
{ {
if (substr($0, 1, 6) == "::: |>") if (substr($0, 1, 6) == "::: |>")
{ {
@@ -60,12 +60,12 @@ desc { desc = desc "\\n" $0; next; }
due = substr($0, 8); due = substr($0, 8);
getline; getline;
} }
summary = substr($0, 1, 2) != "# " ? "" : substr($0, 3); summary = substr($0, 1, 2) != "# " ? "" : escape(substr($0, 3));
getline; getline;
categories = substr($0, 1, 1) != ">" ? "" : substr($0, 3); categories = substr($0, 1, 1) != ">" ? "" : escape(substr($0, 3));
getline; # This line should be empty getline; # This line should be empty
getline; # First line of description getline; # First line of description
desc = $0; desc = escape($0);
next; next;
} }
END { END {
@@ -76,9 +76,6 @@ END {
cmd | getline res cmd | getline res
due = res ? res : "" due = res ? res : ""
} }
summary = escape(summary);
desc = escape(desc);
categories = escape_categories(categories);
# print ical # print ical
print "BEGIN:VCALENDAR"; print "BEGIN:VCALENDAR";

View File

@@ -4,7 +4,7 @@
# @return: Escaped string # @return: Escaped string
function escape(str) function escape(str)
{ {
gsub("\\\\", "\\", str) gsub("\\\\", "\\\\", str)
gsub(";", "\\;", str) gsub(";", "\\;", str)
gsub(",", "\\,", str) gsub(",", "\\,", str)
return str return str
@@ -16,7 +16,7 @@ function escape(str)
# @return: Escaped string # @return: Escaped string
function escape_categories(str) function escape_categories(str)
{ {
gsub("\\\\", "\\", str) gsub("\\\\", "\\\\", str)
gsub(";", "\\;", str) gsub(";", "\\;", str)
return str return str
} }
@@ -57,25 +57,22 @@ ENDFILE {
cmd | getline res cmd | getline res
due = res ? res : "" due = res ? res : ""
} }
summary = escape(summary);
desc = escape(desc);
categories = escape_categories(categories);
} }
} }
NR == FNR && desc { desc = desc "\\n" $0; next; } NR == FNR && desc { desc = desc "\\n" escape($0); next; }
NR == FNR { NR == FNR {
if (substr($0, 1, 6) == "::: <|") if (substr($0, 1, 6) == "::: <|")
{ {
due = substr($0, 8); due = substr($0, 8);
getline; getline;
} }
summary = substr($0, 1, 2) != "# " ? "" : substr($0, 3); summary = substr($0, 1, 2) != "# " ? "" : escape(substr($0, 3));
getline; getline;
categories = substr($0, 1, 1) != ">" ? "" : substr($0, 3); categories = substr($0, 1, 1) != ">" ? "" : escape_categories(substr($0, 3));
getline; # This line should be empty getline; # This line should be empty
getline; # First line of description getline; # First line of description
desc = $0; desc = escape($0);
next; next;
} }

View File

@@ -110,34 +110,64 @@ __lines() {
# Program starts here # Program starts here
if [ "${1:-}" = "--help" ]; then if [ "${1:-}" = "--help" ]; then
echo "Usage: $0 [OPTION]" shift
echo "" echo "Usage: $0 [--help | --new [FILTER..] | [FILTER..] ]
echo "You may specify at most one option." --help Show this help and exit
echo " --help Show this help and exit" --new Create new entry and do not exit
echo " --tasks Show tasks only" --git-init Activate git usage and exit
echo " --no-tasks Ignore tasks" --git <cmd> Run git command and exit
echo " --notes Show notes only"
echo " --no-notes Ignore notes" [FILTER]
echo " --journal Show journal only" You may specify any of these filters. Filters can be negated using the
echo " --no-journal Ignore journal" --no-... versions, e.g., --no-tasks. Multiple filters are applied in
echo " --completed Show completed tasks only" conjuction. By default, the filter --no-completed is used. Note that
echo " --no-completed Ignore completed tasks" --no-completed is not the same as --open, and similarly, --no-open is not the
echo " --new Create new entry" same as --completed.
echo ""
echo "The following options are for internal use." --tasks Show tasks only
echo " --reload Reload list" --notes Show notes only
echo " --preview <selection> Generate preview" --journal Show jounral only
echo " --delete <selection> Delete selected entry" --completed Show completed tasks only
echo " --decrease-priority <selection> Decrease priority of selected task" --open Show open tasks only
echo " --increase-priority <selection> Increase priority of selected task" --filter <query> Specify custom query"
echo " --toggle-completed <selection> Toggle completion flag of task" exit
fi
# Git
if command -v "git" >/dev/null && [ -d "$ROOT/.git" ]; then
GIT="git -C $ROOT"
fi
if [ "${1:-}" = "--git-init" ]; then
shift
if [ -n "${GIT:-}" ]; then
err "Git already enabled"
return 1
fi
if ! command -v "git" >/dev/null; then
err "Git not installed"
return 1
fi
git -C "$ROOT" init
git -C "$ROOT" add -A
git -C "$ROOT" commit -m 'Initial commit: Start git tracking'
exit
fi
if [ "${1:-}" = "--git" ]; then
shift
if [ -z "${GIT:-}" ]; then
err "Git not supported, run \`$0 --git-init\` first"
return 1
fi
$GIT "$@"
exit exit
fi fi
# Command line arguments to be self-contained # Command line arguments to be self-contained
# Generate preview of file from selection # Generate preview of file from selection
if [ "${1:-}" = "--preview" ]; then if [ "${1:-}" = "--preview" ]; then
name=$(echo "$2" | cut -d ' ' -f 3) shift
name=$(echo "$1" | cut -d ' ' -f 3)
shift
file="$ROOT/$name" file="$ROOT/$name"
awk -v field="DESCRIPTION" "$AWK_GET" "$file" | awk -v field="DESCRIPTION" "$AWK_GET" "$file" |
$CAT $CAT
@@ -145,7 +175,9 @@ if [ "${1:-}" = "--preview" ]; then
fi fi
# Delete file from selection # Delete file from selection
if [ "${1:-}" = "--delete" ]; then if [ "${1:-}" = "--delete" ]; then
name=$(echo "$2" | cut -d ' ' -f 3) shift
name=$(echo "$1" | cut -d ' ' -f 3)
shift
file="$ROOT/$name" file="$ROOT/$name"
summary=$(awk -v field="SUMMARY" "$AWK_GET" "$file") summary=$(awk -v field="SUMMARY" "$AWK_GET" "$file")
while true; do while true; do
@@ -154,6 +186,10 @@ if [ "${1:-}" = "--delete" ]; then
case $yn in case $yn in
"yes") "yes")
rm -v "$file" rm -v "$file"
if [ -n "$GIT" ]; then
$GIT add "$file"
$GIT commit -q -m "File deleted" -- "$file"
fi
break break
;; ;;
"no") "no")
@@ -167,6 +203,7 @@ if [ "${1:-}" = "--delete" ]; then
fi fi
# Generate new entry # Generate new entry
if [ "${1:-}" = "--new" ]; then if [ "${1:-}" = "--new" ]; then
shift
collection=$(echo "$COLLECTION_LABELS" | tr ';' '\n' | $FZF --delimiter='=' --with-nth=2 --accept-nth=1) collection=$(echo "$COLLECTION_LABELS" | tr ';' '\n' | $FZF --delimiter='=' --with-nth=2 --accept-nth=1)
file="" file=""
while [ -f "$file" ] || [ -z "$file" ]; do while [ -f "$file" ] || [ -z "$file" ]; do
@@ -191,65 +228,122 @@ if [ "${1:-}" = "--new" ]; then
tmpfile="$tmpmd.ics" tmpfile="$tmpmd.ics"
awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile" awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile"
mv "$tmpfile" "$file" mv "$tmpfile" "$file"
if [ -n "$GIT" ]; then
$GIT add "$file"
$GIT commit -q -m "File added" -- "$file"
fi
fi fi
rm "$tmpmd" rm "$tmpmd"
fi fi
# Toggle completed flag # Toggle completed flag
if [ "${1:-}" = "--toggle-completed" ]; then if [ "${1:-}" = "--toggle-completed" ]; then
name=$(echo "$2" | cut -d ' ' -f 3) shift
name=$(echo "$1" | cut -d ' ' -f 3)
shift
file="$ROOT/$name" file="$ROOT/$name"
tmpfile=$(mktemp) tmpfile=$(mktemp)
awk "$AWK_ALTERTODO" "$file" >"$tmpfile" awk "$AWK_ALTERTODO" "$file" >"$tmpfile"
mv "$tmpfile" "$file" mv "$tmpfile" "$file"
if [ -n "$GIT" ]; then
$GIT add "$file"
$GIT commit -q -m "Completed toggle" -- "$file"
fi
fi fi
# Increase priority # Increase priority
if [ "${1:-}" = "--increase-priority" ]; then if [ "${1:-}" = "--increase-priority" ]; then
name=$(echo "$2" | cut -d ' ' -f 3) shift
name=$(echo "$1" | cut -d ' ' -f 3)
shift
file="$ROOT/$name" file="$ROOT/$name"
tmpfile=$(mktemp) tmpfile=$(mktemp)
awk -v delta="1" "$AWK_ALTERTODO" "$file" >"$tmpfile" awk -v delta="1" "$AWK_ALTERTODO" "$file" >"$tmpfile"
mv "$tmpfile" "$file" mv "$tmpfile" "$file"
if [ -n "$GIT" ]; then
$GIT add "$file"
$GIT commit -q -m "Priority increased" -- "$file"
fi
fi fi
# Decrease priority # Decrease priority
if [ "${1:-}" = "--decrease-priority" ]; then if [ "${1:-}" = "--decrease-priority" ]; then
name=$(echo "$2" | cut -d ' ' -f 3) shift
name=$(echo "$1" | cut -d ' ' -f 3)
shift
file="$ROOT/$name" file="$ROOT/$name"
tmpfile=$(mktemp) tmpfile=$(mktemp)
awk -v delta="-1" "$AWK_ALTERTODO" "$file" >"$tmpfile" awk -v delta="-1" "$AWK_ALTERTODO" "$file" >"$tmpfile"
mv "$tmpfile" "$file" mv "$tmpfile" "$file"
if [ -n "$GIT" ]; then
$GIT add "$file"
$GIT commit -q -m "Priority decreased" -- "$file"
fi fi
fi
# Reload view
if [ "${1:-}" = "--reload" ]; then if [ "${1:-}" = "--reload" ]; then
shift
__lines __lines
exit exit
fi fi
query="${FZF_QUERY:-}" while [ -n "${1:-}" ]; do
if [ "${1:-}" = "--no-completed" ]; then case "${1:-}" in
query="!✅" "--completed")
fi shift
if [ "${1:-}" = "--completed" ]; then cliquery="${cliquery:-}"
query="✅" ;;
fi "--no-completed")
if [ "${1:-}" = "--tasks" ]; then shift
query="✅ | 🔲" cliquery="${cliquery:-} !✅"
fi ;;
if [ "${1:-}" = "--no-tasks" ]; then "--open")
query="!✅ !🔲" shift
fi cliquery="${cliquery:-} 🔲"
if [ "${1:-}" = "--notes" ]; then ;;
query="🗒️" "--no-open")
fi shift
if [ "${1:-}" = "--no-notes" ]; then cliquery="${cliquery:-} !🔲"
query="!🗒️" ;;
fi "--tasks")
if [ "${1:-}" = "--journal" ]; then shift
query="📘" cliquery="${cliquery:-} ✅ | 🔲"
fi ;;
if [ "${1:-}" = "--no-journal" ]; then "--no-tasks")
query="!📘" shift
fi cliquery="${cliquery:-} !✅ !🔲"
query=${query:-!✅} ;;
query=$(echo "$query" | xargs) "--notes")
shift
cliquery="${cliquery:-} 🗒️"
;;
"--no-notes")
shift
cliquery="${cliquery:-} !🗒️"
;;
"--journal")
shift
cliquery="${cliquery:-} 📘"
;;
"--no-journal")
shift
cliquery="${cliquery:-} !📘"
;;
"--filter")
shift
cliquery="${cliquery:-} $1"
shift
;;
"--no-filter")
shift
cliquery="${cliquery:-} !$1"
shift
;;
*)
err "Unknown option \"$1\""
exit 1
;;
esac
done
query=${cliquery:-${FZF_QUERY:-!✅}}
query=$(echo "$query" | sed "s/^ *//" | sed "s/ *$//")
selection=$( selection=$(
__lines | $FZF --ansi \ __lines | $FZF --ansi \
@@ -296,6 +390,10 @@ if [ "$checksum" != "$(cksum "$filetmp")" ]; then
file_new="$filetmp.ics" file_new="$filetmp.ics"
awk "$AWK_UPDATE" "$filetmp" "$file" >"$file_new" awk "$AWK_UPDATE" "$filetmp" "$file" >"$file_new"
mv "$file_new" "$file" mv "$file_new" "$file"
if [ -n "$GIT" ]; then
$GIT add "$file"
$GIT commit -q -m "File modified" -- "$file"
fi
fi fi
rm "$filetmp" rm "$filetmp"