Compare commits
	
		
			43 Commits
		
	
	
		
			local
			...
			e80d9deb79
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e80d9deb79 | |||
| e1d6b2b08e | |||
| 58680a130d | |||
| 458755b020 | |||
| 31c1357fbb | |||
| c8642343e7 | |||
| e954569d5d | |||
| aeff8a3679 | |||
| e948a18a05 | |||
| bc0233962a | |||
| c8f88b2410 | |||
| a9bdca55b5 | |||
| 0dd0a81a64 | |||
| 53149ea9db | |||
| c63b3c4ee2 | |||
| d228f551ab | |||
| 3fe908738f | |||
| 8f04082d65 | |||
| 7f04d84b30 | |||
| 3150e877c7 | |||
| 26a6900555 | |||
| 76fa32da39 | |||
| 609d9712a6 | |||
| de4650d1ac | |||
| 7656da2a71 | |||
| 6c78213587 | |||
| 2a3c188f02 | |||
| 6f268c05cd | |||
| 5144465792 | |||
| 83061bd54b | |||
| b613c25f98 | |||
| 374d7e08c8 | |||
| 7df51a6980 | |||
| 4079a53c2b | |||
| 0c742eccd4 | |||
| 095a2e3810 | |||
| 498c7371b7 | |||
| 8d5223343b | |||
| f7d4a54a3d | |||
| 3db94cf627 | |||
| 50c438e656 | |||
| a72c2c73d2 | |||
| 23fdb4c3d2 | 
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
fzf-vjour
 | 
			
		||||
							
								
								
									
										91
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										91
									
								
								README.md
									
									
									
									
									
								
							@@ -1,4 +1,7 @@
 | 
			
		||||
A [fzf](https://github.com/junegunn/fzf)-based **journaling, notes, and tasks** application with CalDav support.
 | 
			
		||||
If you are interested in this, then you may also be interested in the
 | 
			
		||||
corresponding calendar application
 | 
			
		||||
[fzf-vcal](https://github.com/baumea/fzf-vcal).
 | 
			
		||||
 | 
			
		||||
Description and Use Case
 | 
			
		||||
------------------------
 | 
			
		||||
@@ -10,39 +13,44 @@ with a CalDav server, such as [Radicale](https://radicale.org/), and a synchroni
 | 
			
		||||
 | 
			
		||||
Installation
 | 
			
		||||
------------
 | 
			
		||||
Just copy the file to your preferred location, e.g., `~/.local/bin`, and make it executable.
 | 
			
		||||
 | 
			
		||||
### Manual
 | 
			
		||||
 | 
			
		||||
Run `./scripts/build.sh`, then copy `fzf-vjour` to your preferred location, e.g., `~/.local/bin`, and make it executable.
 | 
			
		||||
 | 
			
		||||
### Requirements
 | 
			
		||||
This is a POSIX script with inline `python3` elements.
 | 
			
		||||
Make sure you have [fzf](https://github.com/junegunn/fzf), [batcat](https://github.com/sharkdp/bat), [jq](https://jqlang.org/), and [yq](https://github.com/mikefarah/yq) installed.
 | 
			
		||||
For the `python3` code, we also require [icalendar](https://pypi.org/project/icalendar/).
 | 
			
		||||
This is a POSIX script with inline `awk` elements.
 | 
			
		||||
Make sure you have [fzf](https://github.com/junegunn/fzf) installed.
 | 
			
		||||
I also suggest to install [batcat](https://github.com/sharkdp/bat) for colorful previews.
 | 
			
		||||
 | 
			
		||||
### Arch Linux
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
yay -S fzf-vjour-git
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Configuration
 | 
			
		||||
--------------
 | 
			
		||||
This application is configured with a YAML file located at `$HOME/.config/fzf-vjour/config.yaml`.
 | 
			
		||||
The entry `datadir` specifies the root directory of your journal and note entries.
 | 
			
		||||
This application is configured with a file located at `$HOME/.config/fzf-vjour/config`.
 | 
			
		||||
The entry `ROOT` specifies the root directory of your journal and note entries.
 | 
			
		||||
This directory may contain several subfolders, called _collections_.
 | 
			
		||||
The entry `collections` is a list, where each item specifies a subfolder, given by `name`, and a label, given by `label` (any string free of white spaces).
 | 
			
		||||
The entry `COLLECTION_LABELS` is a `;`-delimited list, where each item specifies a subfolder and a label (see example below).
 | 
			
		||||
In the application, the user sees the collection labels instead of the collection names.
 | 
			
		||||
This is particularly useful, because some servers use randomly generated names.
 | 
			
		||||
Finally, a third entry `sync_cmd` specifies the command to be executed for synchronizing. 
 | 
			
		||||
Finally, a third entry `SYNC_CMD` specifies the command to be executed for synchronizing. 
 | 
			
		||||
 | 
			
		||||
Consider the following example:
 | 
			
		||||
```yaml
 | 
			
		||||
datadir: ~/.journal
 | 
			
		||||
sync_cmd: vdirsyncer sync journals
 | 
			
		||||
collections:
 | 
			
		||||
  - name: 12cacb18-d3e1-4ad4-a1d0-e5b209012e85
 | 
			
		||||
    label: work:💼
 | 
			
		||||
  - name: 745ae7a0-d723-4cd8-80c4-75f52f5b7d90
 | 
			
		||||
    label: priv:🏡
 | 
			
		||||
```sh
 | 
			
		||||
ROOT=~/.journal/
 | 
			
		||||
COLLECTION_LABELS="745ae7a0-d723-4cd8-80c4-75f52f5b7d90=shared 👫🏼;12cacb18-d3e1-4ad4-a1d0-e5b209012e85=work   💼;"
 | 
			
		||||
SYNC_CMD="vdirsyncer sync journals"
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Here the files are stored in
 | 
			
		||||
`~/.journal/12cacb18-d3e1-4ad4-a1d0-e5b209012e85` (work-related entries)
 | 
			
		||||
and
 | 
			
		||||
`~/.journal/745ae7a0-d723-4cd8-80c4-75f52f5b7d90` (personal collection).
 | 
			
		||||
`~/.journal/745ae7a0-d723-4cd8-80c4-75f52f5b7d90` (shared collection).
 | 
			
		||||
 | 
			
		||||
This configuration will work well with a `vdirsyncer` configuration such as 
 | 
			
		||||
```confini
 | 
			
		||||
@@ -62,6 +70,8 @@ item_types = ["VJOURNAL", "VTODO"]
 | 
			
		||||
...
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
You may also specify the location of the configuration file with the environment `CONFIGFILE`.
 | 
			
		||||
 | 
			
		||||
Usage
 | 
			
		||||
-----
 | 
			
		||||
Use the default `fzf` keys to navigate your notes, e.g., `ctrl-j` and `ctrl-k` for going down/up in the list.
 | 
			
		||||
@@ -69,13 +79,15 @@ In addition, there are the following keybindings:
 | 
			
		||||
| Key | Action |
 | 
			
		||||
| --- | ------ |
 | 
			
		||||
| `enter` | Open note/journal/task in your `$EDITOR` |
 | 
			
		||||
| `ctrl-d` | Delete the seleted entry |
 | 
			
		||||
| `ctrl-alt-d` | Delete the seleted entry |
 | 
			
		||||
| `ctrl-n` | Make a new entry |
 | 
			
		||||
| `ctrl-r` | Refresh the view |
 | 
			
		||||
| `ctrl-s` | Run the synchronization command |
 | 
			
		||||
| `ctrl-x` | Toggle task completion |
 | 
			
		||||
| `alt-up` | Increase task priority |
 | 
			
		||||
| `alt-down` | Decrease task priority |
 | 
			
		||||
| `ctrl-a` | Open attachments view |
 | 
			
		||||
| `alt-v` | View bare iCalendar file |
 | 
			
		||||
| `alt-0` | Default view: Journal, notes, and _open_ tasks |
 | 
			
		||||
| `alt-1` | Display journal entries |
 | 
			
		||||
| `alt-2` | Display notes |
 | 
			
		||||
@@ -83,6 +95,49 @@ In addition, there are the following keybindings:
 | 
			
		||||
 | 
			
		||||
You may also invoke the script with `--help` to see further command-line options. 
 | 
			
		||||
 | 
			
		||||
In the attachment view, you may use the following keys:
 | 
			
		||||
| Key | Action |
 | 
			
		||||
| --- | ------ |
 | 
			
		||||
| `enter` | Open attachment |
 | 
			
		||||
| `j` | Down |
 | 
			
		||||
| `k` | Up |
 | 
			
		||||
| `w` | Toggle line wrap |
 | 
			
		||||
| `ctrl-a` | Add attachment |
 | 
			
		||||
| `ctrl-alt-d` | Delete attachment |
 | 
			
		||||
 | 
			
		||||
Git support
 | 
			
		||||
-----------
 | 
			
		||||
You can track your entries with `git` by simply running `fzf-vjour --git-init`.
 | 
			
		||||
 | 
			
		||||
Extended configuration / Theming
 | 
			
		||||
--------------------------------
 | 
			
		||||
You may override any of the following parameters (shown with default values) in
 | 
			
		||||
the configuration file:
 | 
			
		||||
```sh
 | 
			
		||||
FLAG_OPEN=🔲
 | 
			
		||||
FLAG_COMPLETED=✅
 | 
			
		||||
FLAG_JOURNAL=📘
 | 
			
		||||
FLAG_NOTE=🗒️
 | 
			
		||||
FLAG_PRIORITY=❗
 | 
			
		||||
FLAG_ATTACHMENT=🔗
 | 
			
		||||
 | 
			
		||||
STYLE_COLLECTION="$FAINT$WHITE"
 | 
			
		||||
STYLE_DATE="$CYAN"
 | 
			
		||||
STYLE_SUMMARY="$GREEN"
 | 
			
		||||
STYLE_EXPIRED="$RED"
 | 
			
		||||
STYLE_CATEGORY="$WHITE"
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
- Inline attachments only
 | 
			
		||||
- No recurrences
 | 
			
		||||
 | 
			
		||||
License
 | 
			
		||||
-------
 | 
			
		||||
This project is licensed under the [MIT License](./LICENSE).
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										672
									
								
								fzf-vjour
									
									
									
									
									
								
							
							
						
						
									
										672
									
								
								fzf-vjour
									
									
									
									
									
								
							@@ -1,672 +0,0 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
set -u
 | 
			
		||||
 | 
			
		||||
# Read configuration
 | 
			
		||||
CONFIGFILE="$HOME/.config/fzf-vjour/config.yaml"
 | 
			
		||||
if [ ! -e "$CONFIGFILE" ]; then
 | 
			
		||||
  echo "Config file '$CONFIGFILE' not found"
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
ROOT=$(yq '.datadir' <"$CONFIGFILE")
 | 
			
		||||
ROOT=$(eval "echo $ROOT")
 | 
			
		||||
if [ ! -d "$ROOT" ]; then
 | 
			
		||||
  echo "Root directory not set or wrongly set"
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
SED_COLLECTIONNAMES_TO_LABELS=$(
 | 
			
		||||
  printf "sed "
 | 
			
		||||
  yq '.collections[] | "s|/*" + .name + "/*|:" + .label + "\ |"' <"$CONFIGFILE" |
 | 
			
		||||
    xargs printf "-e \"%s\" "
 | 
			
		||||
)
 | 
			
		||||
SED_COLLECTIONLABELS_TO_NAMES=$(
 | 
			
		||||
  printf "sed "
 | 
			
		||||
  yq '.collections[] | "s|\ *" + .label + "\ *|/" + .name + "/|"' <"$CONFIGFILE" |
 | 
			
		||||
    xargs printf "-e \"%s\" "
 | 
			
		||||
)
 | 
			
		||||
COLLECTION_NAME_MAX_LEN=$(yq '[.collections[].name | length] | max' <"$CONFIGFILE")
 | 
			
		||||
LABLES=$(yq '.collections[].label' <"$CONFIGFILE")
 | 
			
		||||
SYNC_CMD=$(yq '.sync_cmd' <"$CONFIGFILE")
 | 
			
		||||
LONGSPACE="                                                                                                                                                                                              "
 | 
			
		||||
 | 
			
		||||
if ! (yq '.collections[].label' | grep ' ') >/dev/null; then
 | 
			
		||||
  echo "We currently do not support whitespaces in the labels"
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
__vtodopriority() {
 | 
			
		||||
  python3 -c '
 | 
			
		||||
import sys
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from icalendar.cal import Todo
 | 
			
		||||
 | 
			
		||||
if not len(sys.argv) == 3:
 | 
			
		||||
    print("Pass ical file as first argument!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
increase = 1 if sys.argv[2] == "1" else -1
 | 
			
		||||
 | 
			
		||||
with open(sys.argv[1], "r") as f:
 | 
			
		||||
    try:
 | 
			
		||||
        ical = Todo.from_ical(f.read())
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"Failed to read vjournal file: {e}", file=sys.stderr)
 | 
			
		||||
        sys.exit(1)
 | 
			
		||||
 | 
			
		||||
tlist = [component for component in ical.walk("VTODO")]
 | 
			
		||||
if len(tlist) == 0:
 | 
			
		||||
    print("ical file is not a VTODO", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
t = tlist[0]
 | 
			
		||||
 | 
			
		||||
# Update ical
 | 
			
		||||
priority = t.pop("PRIORITY")
 | 
			
		||||
priority = (int(priority) if priority else 0) + increase
 | 
			
		||||
priority = 0 if priority < 0 else 9 if priority > 9 else priority
 | 
			
		||||
t["PRIORITY"] = priority
 | 
			
		||||
 | 
			
		||||
# Print
 | 
			
		||||
print(ical.to_ical().decode().replace("\r\n", "\n"))
 | 
			
		||||
' "$@"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__vtodotogglecompleted() {
 | 
			
		||||
  python3 -c '
 | 
			
		||||
import sys
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from icalendar.cal import Todo
 | 
			
		||||
from icalendar.prop import vDDDTypes
 | 
			
		||||
 | 
			
		||||
if not len(sys.argv) == 2:
 | 
			
		||||
    print("Pass ical file as first argument!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
with open(sys.argv[1], "r") as f:
 | 
			
		||||
    try:
 | 
			
		||||
        ical = Todo.from_ical(f.read())
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"Failed to read vjournal file: {e}", file=sys.stderr)
 | 
			
		||||
        sys.exit(1)
 | 
			
		||||
 | 
			
		||||
tlist = [component for component in ical.walk("VTODO")]
 | 
			
		||||
if len(tlist) == 0:
 | 
			
		||||
    print("ical file is not a VTODO", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
t = tlist[0]
 | 
			
		||||
 | 
			
		||||
# Update ical
 | 
			
		||||
if t.has_key("STATUS") and t["STATUS"] == "COMPLETED":
 | 
			
		||||
  # Mark as not completed
 | 
			
		||||
  t["STATUS"] = "NEEDS-ACTION"
 | 
			
		||||
  t["PERCENT-COMPLETE"] = 0
 | 
			
		||||
  if t.has_key("COMPLETED"): t.pop("COMPLETED")
 | 
			
		||||
else:
 | 
			
		||||
  t["STATUS"] = "COMPLETED"
 | 
			
		||||
  t["PERCENT-COMPLETE"] = 100
 | 
			
		||||
  t["COMPLETED"] = vDDDTypes(datetime.utcnow())
 | 
			
		||||
 | 
			
		||||
# Print
 | 
			
		||||
print(ical.to_ical().decode().replace("\r\n", "\n"))
 | 
			
		||||
' "$@"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__vicalnew() {
 | 
			
		||||
  python3 -c '
 | 
			
		||||
import sys
 | 
			
		||||
from datetime import date, datetime
 | 
			
		||||
from icalendar.cal import Calendar, Journal, Todo
 | 
			
		||||
from icalendar.prop import vDDDTypes
 | 
			
		||||
 | 
			
		||||
if not len(sys.argv) == 2:
 | 
			
		||||
    print("Pass UID as first argument!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
UID = sys.argv[1]
 | 
			
		||||
 | 
			
		||||
start = None
 | 
			
		||||
due = None
 | 
			
		||||
line = sys.stdin.readline().strip()
 | 
			
		||||
if line[:6] == "::: |>":
 | 
			
		||||
    start = datetime.utcnow().date()
 | 
			
		||||
    line = sys.stdin.readline().strip()
 | 
			
		||||
 | 
			
		||||
if line[:6] == "::: <|":
 | 
			
		||||
    lst = line.split(" ")
 | 
			
		||||
    due = True
 | 
			
		||||
    if len(lst) >= 3:
 | 
			
		||||
      try:
 | 
			
		||||
        duedate = datetime.strptime(lst[2], "%Y-%m-%d").date()
 | 
			
		||||
        due = duedate
 | 
			
		||||
      except Exception as e:
 | 
			
		||||
        pass
 | 
			
		||||
    line = sys.stdin.readline().strip()
 | 
			
		||||
 | 
			
		||||
if not line[:2] == "# ":
 | 
			
		||||
    print("Error: Summary line is corrupt!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
summary = line[2:]
 | 
			
		||||
 | 
			
		||||
line = sys.stdin.readline().strip()
 | 
			
		||||
if not line[:2] == "> " and not line == ">":
 | 
			
		||||
    categories = []
 | 
			
		||||
else:
 | 
			
		||||
    categories = line[2:].split(",")
 | 
			
		||||
    line = sys.stdin.readline().strip()
 | 
			
		||||
 | 
			
		||||
if not line == "":
 | 
			
		||||
    print("Error: Missing separating line!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
description = sys.stdin.read()
 | 
			
		||||
 | 
			
		||||
# Create ical
 | 
			
		||||
now = datetime.utcnow()
 | 
			
		||||
ical = Calendar()
 | 
			
		||||
if due:
 | 
			
		||||
    o = Todo()
 | 
			
		||||
 | 
			
		||||
    # The following are REQUIRED, but MUST NOT occur more than once.
 | 
			
		||||
    # dtstamp / uid /
 | 
			
		||||
    o.DTSTAMP = now
 | 
			
		||||
    o["UID"] = UID
 | 
			
		||||
 | 
			
		||||
    # The following are OPTIONAL, but MUST NOT occur more than once.
 | 
			
		||||
    # class / completed / created / description / dtstart / geo / last-mod /
 | 
			
		||||
    # location / organizer / percent / priority / recurid / seq / status /
 | 
			
		||||
    # summary / url /
 | 
			
		||||
    o["CLASS"] = "PRIVATE"
 | 
			
		||||
    o["CREATED"] = vDDDTypes(now)
 | 
			
		||||
    o["DESCRIPTION"] = description.strip()
 | 
			
		||||
    o.LAST_MODIFIED = now
 | 
			
		||||
    o["PRIORITY"] = 0
 | 
			
		||||
    o["SEQUENCE"] = 0
 | 
			
		||||
    o["STATUS"] = "NEEDS-ACTION"
 | 
			
		||||
    o["SUMMARY"] = summary
 | 
			
		||||
 | 
			
		||||
    # The following is OPTIONAL, but SHOULD NOT occur more than once.
 | 
			
		||||
    # rrule /
 | 
			
		||||
 | 
			
		||||
    # Either "due" or "duration" MAY appear in a "todoprop", but "due" and
 | 
			
		||||
    # "duration" MUST NOT occur in the same "todoprop". If "duration" appear in
 | 
			
		||||
    # a "todoprop", then "dtstart" MUST also appear in the same "todoprop".
 | 
			
		||||
    # due / duration /
 | 
			
		||||
    if isinstance(due, date):
 | 
			
		||||
      o.DUE = due
 | 
			
		||||
 | 
			
		||||
    # The following are OPTIONAL, and MAY occur more than once.
 | 
			
		||||
    # attach / attendee / categories / comment / contact / exdate / rstatus /
 | 
			
		||||
    # related / resources / rdate / x-prop / iana-prop
 | 
			
		||||
    o.categories = categories
 | 
			
		||||
 | 
			
		||||
else:
 | 
			
		||||
    o = Journal()
 | 
			
		||||
    # The following are REQUIRED, but MUST NOT occur more than once.
 | 
			
		||||
    # dtstamp / uid /
 | 
			
		||||
    o.DTSTAMP = now
 | 
			
		||||
    o["UID"] = UID
 | 
			
		||||
    
 | 
			
		||||
    # The following are OPTIONAL, but MUST NOT occur more than once.
 | 
			
		||||
    # class / created / dtstart / last-mod / organizer / recurid / seq / status
 | 
			
		||||
    # / summary / url /
 | 
			
		||||
    o["CLASS"] = "PRIVATE"
 | 
			
		||||
    o["CREATED"] = vDDDTypes(now)
 | 
			
		||||
    o.DTSTART = start
 | 
			
		||||
    o.LAST_MODIFIED = now
 | 
			
		||||
    o["SEQUENCE"] = 0
 | 
			
		||||
    o["STATUS"] = "FINAL"
 | 
			
		||||
    o["SUMMARY"] = summary
 | 
			
		||||
 | 
			
		||||
    # The following is OPTIONAL, but SHOULD NOT occur more than once.
 | 
			
		||||
    # rrule /
 | 
			
		||||
    
 | 
			
		||||
    # The following are OPTIONAL, and MAY occur more than once.
 | 
			
		||||
    # attach / attendee / categories / comment / contact / description / exdate
 | 
			
		||||
    # / related / rdate / rstatus / x-prop / iana-prop
 | 
			
		||||
    o.categories = categories
 | 
			
		||||
    o["DESCRIPTION"] = description.strip()
 | 
			
		||||
 | 
			
		||||
ical.add_component(o)
 | 
			
		||||
 | 
			
		||||
ical["PRODID"] = "fzf-vjour/basic"
 | 
			
		||||
ical["VERSION"] = "2.0"
 | 
			
		||||
 | 
			
		||||
# Print
 | 
			
		||||
print(ical.to_ical().decode().replace("\r\n", "\n"))' "$@"
 | 
			
		||||
}
 | 
			
		||||
__icalupdate() {
 | 
			
		||||
  python3 -c '
 | 
			
		||||
import sys
 | 
			
		||||
from datetime import date, datetime
 | 
			
		||||
from icalendar.cal import Calendar, Todo
 | 
			
		||||
 | 
			
		||||
if not len(sys.argv) == 2:
 | 
			
		||||
    print("Pass ical file as first argument!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
with open(sys.argv[1], "r") as f:
 | 
			
		||||
    try:
 | 
			
		||||
        ical = Calendar.from_ical(f.read())
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"Failed to read ical file: {e}", file=sys.stderr)
 | 
			
		||||
        sys.exit(1)
 | 
			
		||||
 | 
			
		||||
olist = [component for component in ical.walk(select=lambda c:c.name in ["VJOURNAL", "VTODO"])]
 | 
			
		||||
if len(olist) == 0:
 | 
			
		||||
    sys.exit(0)
 | 
			
		||||
 | 
			
		||||
o = olist[0]
 | 
			
		||||
 | 
			
		||||
line = sys.stdin.readline().strip()
 | 
			
		||||
due = None
 | 
			
		||||
if isinstance(o, Todo):
 | 
			
		||||
    if not line[:6] == "::: <|":
 | 
			
		||||
        print("Error: Due date line is corrupt!", file=sys.stderr)
 | 
			
		||||
        sys.exit(1)
 | 
			
		||||
    lst = line.split(" ")
 | 
			
		||||
    due = True
 | 
			
		||||
    if len(lst) >= 3:
 | 
			
		||||
      try:
 | 
			
		||||
        duedate = datetime.strptime(lst[2], "%Y-%m-%d").date()
 | 
			
		||||
        due = duedate
 | 
			
		||||
      except Exception as e:
 | 
			
		||||
        pass
 | 
			
		||||
    line = sys.stdin.readline().strip()
 | 
			
		||||
 | 
			
		||||
if not line[:2] == "# ":
 | 
			
		||||
    print("Error: Summary line is corrupt!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
summary = line[2:]
 | 
			
		||||
 | 
			
		||||
line = sys.stdin.readline().strip()
 | 
			
		||||
if not line[:2] == "> " and not line == ">":
 | 
			
		||||
    categories = []
 | 
			
		||||
else:
 | 
			
		||||
    categories = line[2:].split(",")
 | 
			
		||||
    line = sys.stdin.readline().strip()
 | 
			
		||||
 | 
			
		||||
if not line == "":
 | 
			
		||||
    print("Error: Missing separating line!", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
description = sys.stdin.read()
 | 
			
		||||
 | 
			
		||||
# Update ical
 | 
			
		||||
if due:
 | 
			
		||||
    if isinstance(due, date):
 | 
			
		||||
        o.DUE = due 
 | 
			
		||||
    elif "DUE" in o.keys():
 | 
			
		||||
        o.pop("DUE")
 | 
			
		||||
o["SUMMARY"] = summary
 | 
			
		||||
o.categories = categories
 | 
			
		||||
o["DESCRIPTION"] = description.strip()
 | 
			
		||||
o.LAST_MODIFIED = datetime.utcnow()
 | 
			
		||||
seq = o["SEQUENCE"] if "SEQUENCE" in o.keys() else 0
 | 
			
		||||
o["SEQUENCE"] = seq + 1
 | 
			
		||||
 | 
			
		||||
# Print
 | 
			
		||||
print(ical.to_ical().decode().replace("\r\n", "\n"))
 | 
			
		||||
' "$@"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__ical2json() {
 | 
			
		||||
  python3 -c '
 | 
			
		||||
import sys
 | 
			
		||||
import json
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from zoneinfo import ZoneInfo
 | 
			
		||||
from icalendar.cal import Calendar, Todo
 | 
			
		||||
 | 
			
		||||
input_data = sys.stdin.read()
 | 
			
		||||
try:
 | 
			
		||||
    ical = Calendar.from_ical(input_data)
 | 
			
		||||
except Exception as e:
 | 
			
		||||
    print(f"Failed to read ical file: {e}", file=sys.stderr)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
olist = [component for component in ical.walk(select=lambda c:c.name in ["VJOURNAL", "VTODO"])]
 | 
			
		||||
if len(olist) == 0:
 | 
			
		||||
    sys.exit(0)
 | 
			
		||||
 | 
			
		||||
o = olist[0]
 | 
			
		||||
 | 
			
		||||
local_tz = ZoneInfo("localtime")
 | 
			
		||||
data = {
 | 
			
		||||
    "summary": o.get("SUMMARY"),
 | 
			
		||||
    "description": o.get("DESCRIPTION") if "DESCRIPTION" in o.keys() else "",
 | 
			
		||||
    "categories": o.categories,
 | 
			
		||||
    "class": o.get("CLASS"),
 | 
			
		||||
    "created": str(o.DTSTAMP.astimezone(local_tz)) if o.DTSTAMP else "",
 | 
			
		||||
    "last_modified": str(o.LAST_MODIFIED.astimezone(local_tz)) if o.LAST_MODIFIED else "",
 | 
			
		||||
    "start": str(
 | 
			
		||||
        o.DTSTART.astimezone(local_tz)
 | 
			
		||||
        if isinstance(o.DTSTART, datetime)
 | 
			
		||||
        else o.DTSTART or ""
 | 
			
		||||
    ),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if isinstance(o, Todo):
 | 
			
		||||
    data["due"] = str(o.DUE) if o.DUE else ""
 | 
			
		||||
 | 
			
		||||
print(json.dumps(data))'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__date_not_in_future() {
 | 
			
		||||
  date_target=$(date -d "$1" +%s)
 | 
			
		||||
  date_today=$(date -d "00:00" +%s)
 | 
			
		||||
  date_delta=$((date_target - date_today))
 | 
			
		||||
  if [ "$date_delta" -le 0 ]; then
 | 
			
		||||
    echo 1
 | 
			
		||||
  fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__date_to_expression() {
 | 
			
		||||
  date_target=$(date -d "$1" +%s)
 | 
			
		||||
  date_today=$(date -d "00:00" +%s)
 | 
			
		||||
  date_delta=$(((date_target - date_today) / 86400))
 | 
			
		||||
  date_expr=$date_delta
 | 
			
		||||
  if [ "$date_delta" -eq 0 ]; then
 | 
			
		||||
    date_expr="today"
 | 
			
		||||
  elif [ "$date_delta" -eq -1 ]; then
 | 
			
		||||
    date_expr="yesterday"
 | 
			
		||||
  elif [ "$date_delta" -eq 1 ]; then
 | 
			
		||||
    date_expr="tomorrow"
 | 
			
		||||
  elif [ "$date_delta" -lt -1 ] && [ "$date_delta" -ge -7 ]; then
 | 
			
		||||
    date_expr="last $(date -d "$1" +%A)"
 | 
			
		||||
  elif [ "$date_delta" -gt 1 ] && [ "$date_delta" -le 7 ]; then
 | 
			
		||||
    date_expr="next $(date -d "$1" +%A)"
 | 
			
		||||
  else
 | 
			
		||||
    date_expr=$(date -d "$1" +%x)
 | 
			
		||||
  fi
 | 
			
		||||
  echo "$date_expr"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Process each file
 | 
			
		||||
# This function takes two arguments:
 | 
			
		||||
#
 | 
			
		||||
# @param string: Path to ics file
 | 
			
		||||
# @param string: Maximum length of filenames (for padding purposes)
 | 
			
		||||
__filepath_to_searchline() {
 | 
			
		||||
  filepath="$1"
 | 
			
		||||
  collection=$(dirname "$filepath" | sed "s|^$ROOT/*||")
 | 
			
		||||
  filename=$(basename "$filepath")
 | 
			
		||||
 | 
			
		||||
  # Color support
 | 
			
		||||
  GREEN=$(printf '\033[1;32m')
 | 
			
		||||
  RED=$(printf '\033[1;31m')
 | 
			
		||||
  WHITE=$(printf '\033[1;97m')
 | 
			
		||||
  FAINT=$(printf '\033[2m')
 | 
			
		||||
  OFF=$(printf '\033[m')
 | 
			
		||||
 | 
			
		||||
  # Parse file
 | 
			
		||||
  summary=$(grep '^SUMMARY:' "$filepath" | cut -d ':' -f 2 | sed 's/\\,/,/g')
 | 
			
		||||
  categories=$(grep '^CATEGORIES:' "$filepath" | cut -d ':' -f 2)
 | 
			
		||||
  dtstamp=$(grep '^LAST-MODIFIED:' "$filepath" | cut -d ':' -f 2)
 | 
			
		||||
  if [ -z "$dtstamp" ]; then
 | 
			
		||||
    dtstamp=$(grep '^DTSTAMP:' "$filepath" | cut -d ':' -f 2)
 | 
			
		||||
  fi
 | 
			
		||||
  dtstart=$(grep '^DTSTART' "$filepath" | grep -oE '[0-9]{8}')
 | 
			
		||||
  due=$(grep '^DUE' "$filepath" | grep -oE '[0-9]{8}')
 | 
			
		||||
  priority=$(grep '^PRIORITY:' "$filepath" | cut -d ':' -f 2)
 | 
			
		||||
  task=$(grep '^BEGIN:VTODO' "$filepath")
 | 
			
		||||
  completed=$(grep '^STATUS:COMPLETED' "$filepath")
 | 
			
		||||
 | 
			
		||||
  # Parse date
 | 
			
		||||
  if [ -n "$dtstart" ]; then
 | 
			
		||||
    emoji="📘"
 | 
			
		||||
    date_expr=$(__date_to_expression "$dtstart")
 | 
			
		||||
  else
 | 
			
		||||
    emoji="🗒️"
 | 
			
		||||
    date_expr=""
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  # Check if this is a task
 | 
			
		||||
  if [ -n "$task" ]; then
 | 
			
		||||
    emoji="🔲"
 | 
			
		||||
    if [ -n "$completed" ]; then
 | 
			
		||||
      emoji="✅"
 | 
			
		||||
    fi
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  # Check Priority
 | 
			
		||||
  if [ -n "$priority" ] && [ "$priority" -gt 0 ]; then
 | 
			
		||||
    prioritymsg="❗($priority) "
 | 
			
		||||
    priority=$((10 - priority))
 | 
			
		||||
  else
 | 
			
		||||
    prioritymsg=""
 | 
			
		||||
    priority=0
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  # Check due date
 | 
			
		||||
  if [ -n "$due" ]; then
 | 
			
		||||
    date_expr=$(__date_to_expression "$due")
 | 
			
		||||
  fi
 | 
			
		||||
  date_expr=$(printf "%12s" "$date_expr")
 | 
			
		||||
  # Color date
 | 
			
		||||
  notinfuture=$(__date_not_in_future "$due")
 | 
			
		||||
  if [ -n "$notinfuture" ] && [ -n "$due" ]; then
 | 
			
		||||
    date_expr="$RED$date_expr$OFF"
 | 
			
		||||
    summary_color="$RED"
 | 
			
		||||
  else
 | 
			
		||||
    date_expr="$WHITE$date_expr$OFF"
 | 
			
		||||
    summary_color="$GREEN"
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  # Print line
 | 
			
		||||
  echo "$priority $dtstamp $collection $date_expr $emoji $prioritymsg$summary_color$summary$OFF $FAINT$categories$OFF$LONGSPACE/$filename"
 | 
			
		||||
  #echo "$priority $dtstamp $filepathpad $date_expr $emoji $prioritymsg$summary_color$summary$OFF $FAINT$categories$OFF"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__lines() {
 | 
			
		||||
  lines=$(find "$ROOT" -type f -name '*.ics' |
 | 
			
		||||
    while IFS= read -r file; do
 | 
			
		||||
      __filepath_to_searchline "$file"
 | 
			
		||||
    done)
 | 
			
		||||
 | 
			
		||||
  # Decorate
 | 
			
		||||
  lines=$(echo "$lines" | eval "$SED_COLLECTIONNAMES_TO_LABELS")
 | 
			
		||||
 | 
			
		||||
  # Sort and cut off irreleant part
 | 
			
		||||
  lines=$(echo "$lines" | sort -g -r | cut -d ':' -f 2-)
 | 
			
		||||
 | 
			
		||||
  echo "$lines"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__filepath_from_selection() {
 | 
			
		||||
  filename=$(echo "$1" | rev | cut -d "/" -f 1 | rev)
 | 
			
		||||
  dirname=$(echo "$1" | cut -d " " -f 1 | eval "$SED_COLLECTIONLABELS_TO_NAMES" | sed "s|^|$ROOT|")
 | 
			
		||||
  echo "$dirname/$filename"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Program starts here
 | 
			
		||||
 | 
			
		||||
if [ "${1:-}" = "--help" ]; then
 | 
			
		||||
  echo "Usage: $0 [OPTION]"
 | 
			
		||||
  echo ""
 | 
			
		||||
  echo "You may specify at most one option."
 | 
			
		||||
  echo "  --help                 Show this help and exit"
 | 
			
		||||
  echo "  --tasks                Show tasks only"
 | 
			
		||||
  echo "  --no-tasks             Ignore tasks"
 | 
			
		||||
  echo "  --notes                Show notes only"
 | 
			
		||||
  echo "  --no-notes             Ignore notes"
 | 
			
		||||
  echo "  --journal              Show journal only"
 | 
			
		||||
  echo "  --no-journal           Ignore journal"
 | 
			
		||||
  echo "  --completed            Show completed tasks only"
 | 
			
		||||
  echo "  --no-completed         Ignore completed tasks"
 | 
			
		||||
  echo "  --new                  Create new entry"
 | 
			
		||||
  echo ""
 | 
			
		||||
  echo "The following options are for internal use."
 | 
			
		||||
  echo "  --reload                            Reload list"
 | 
			
		||||
  echo "  --preview <selection>               Generate preview"
 | 
			
		||||
  echo "  --delete <selection>                Delete selected entry"
 | 
			
		||||
  echo "  --decrease-priority <selection>     Decrease priority of selected task"
 | 
			
		||||
  echo "  --increase-priority <selection>     Increase priority of selected task"
 | 
			
		||||
  echo "  --toggle-completed <selection>      Toggle completion flag of task"
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
# Command line arguments to be self-contained
 | 
			
		||||
# Generate preview of file from selection
 | 
			
		||||
if [ "${1:-}" = "--preview" ]; then
 | 
			
		||||
  vjfile=$(__filepath_from_selection "$2")
 | 
			
		||||
  __ical2json <"$vjfile" | jq -r ".description" | batcat --color=always --style=numbers --language=md
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
# Delete file from selection
 | 
			
		||||
if [ "${1:-}" = "--delete" ]; then
 | 
			
		||||
  vjfile=$(__filepath_from_selection "$2")
 | 
			
		||||
  rm -i "$vjfile"
 | 
			
		||||
fi
 | 
			
		||||
# Generate new entry
 | 
			
		||||
if [ "${1:-}" = "--new" ]; then
 | 
			
		||||
  collection=$(echo "$LABLES" | fzf \
 | 
			
		||||
    --margin 20% \
 | 
			
		||||
    --prompt="Select collection> ")
 | 
			
		||||
  file=""
 | 
			
		||||
  while [ -f "$file" ] || [ -z "$file" ]; do
 | 
			
		||||
    uuid=$(uuidgen)
 | 
			
		||||
    file=$(__filepath_from_selection "$collection /$uuid.ics")
 | 
			
		||||
  done
 | 
			
		||||
  tmpmd=$(mktemp --suffix='.md')
 | 
			
		||||
  tmpsha="$tmpmd.sha"
 | 
			
		||||
  {
 | 
			
		||||
    echo "::: |> <!-- keep this line to associate the entry to _today_ -->"
 | 
			
		||||
    echo "::: <| <!-- specify the due date for to-dos, can be empty -->"
 | 
			
		||||
    echo "# <!-- write summary here -->"
 | 
			
		||||
    echo "> <!-- comma-separated list of categories -->"
 | 
			
		||||
    echo ""
 | 
			
		||||
  } >"$tmpmd"
 | 
			
		||||
  sha1sum "$tmpmd" >"$tmpsha"
 | 
			
		||||
 | 
			
		||||
  # Open in editor
 | 
			
		||||
  $EDITOR "$tmpmd" >/dev/tty
 | 
			
		||||
 | 
			
		||||
  # Update if changes are detected
 | 
			
		||||
  if ! sha1sum -c "$tmpsha" >/dev/null 2>&1; then
 | 
			
		||||
    tmpfile="$tmpmd.ics"
 | 
			
		||||
    tmpferr="$tmpmd.err"
 | 
			
		||||
    if __vicalnew "$uuid" <"$tmpmd" >"$tmpfile" 2>"$tmpferr"; then
 | 
			
		||||
      mv "$tmpfile" "$file"
 | 
			
		||||
    else
 | 
			
		||||
      rm "$tmpfile"
 | 
			
		||||
      less "$tmpferr"
 | 
			
		||||
    fi
 | 
			
		||||
    rm "$tmpferr"
 | 
			
		||||
  fi
 | 
			
		||||
  rm "$tmpmd" "$tmpsha"
 | 
			
		||||
fi
 | 
			
		||||
# Toggle completed flag
 | 
			
		||||
if [ "${1:-}" = "--toggle-completed" ]; then
 | 
			
		||||
  vtfile=$(__filepath_from_selection "$2")
 | 
			
		||||
  vtfile_tmp=$(mktemp)
 | 
			
		||||
  __vtodotogglecompleted "$vtfile" >"$vtfile_tmp" && mv "$vtfile_tmp" "$vtfile" || rm "$vtfile_tmp"
 | 
			
		||||
fi
 | 
			
		||||
# Increase priority
 | 
			
		||||
if [ "${1:-}" = "--increase-priority" ]; then
 | 
			
		||||
  vtfile=$(__filepath_from_selection "$2")
 | 
			
		||||
  vtfile_tmp=$(mktemp)
 | 
			
		||||
  __vtodopriority "$vtfile" "1" >"$vtfile_tmp" && mv "$vtfile_tmp" "$vtfile" || rm "$vtfile_tmp"
 | 
			
		||||
fi
 | 
			
		||||
# Decrease priority
 | 
			
		||||
if [ "${1:-}" = "--decrease-priority" ]; then
 | 
			
		||||
  vtfile=$(__filepath_from_selection "$2")
 | 
			
		||||
  vtfile_tmp=$(mktemp)
 | 
			
		||||
  __vtodopriority "$vtfile" "-1" >"$vtfile_tmp" && mv "$vtfile_tmp" "$vtfile" || rm "$vtfile_tmp"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--reload" ]; then
 | 
			
		||||
  __lines
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
query="${FZF_QUERY:-}"
 | 
			
		||||
if [ "${1:-}" = "--no-completed" ]; then
 | 
			
		||||
  query="!✅"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--completed" ]; then
 | 
			
		||||
  query="✅"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--tasks" ]; then
 | 
			
		||||
  query="✅ | 🔲"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--no-tasks" ]; then
 | 
			
		||||
  query="!✅ !🔲"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--notes" ]; then
 | 
			
		||||
  query="🗒️"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--no-notes" ]; then
 | 
			
		||||
  query="!🗒️"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--journal" ]; then
 | 
			
		||||
  query="📘"
 | 
			
		||||
fi
 | 
			
		||||
if [ "${1:-}" = "--no-journal" ]; then
 | 
			
		||||
  query="!📘"
 | 
			
		||||
fi
 | 
			
		||||
if [ -z "$query" ]; then
 | 
			
		||||
  query="!✅"
 | 
			
		||||
fi
 | 
			
		||||
query=$(echo "$query" | sed 's/ *$//g')
 | 
			
		||||
 | 
			
		||||
selection=$(
 | 
			
		||||
  __lines | fzf --ansi \
 | 
			
		||||
    --query="$query " \
 | 
			
		||||
    --no-sort \
 | 
			
		||||
    --no-hscroll \
 | 
			
		||||
    --ellipsis='' \
 | 
			
		||||
    --preview="$0 --preview {}" \
 | 
			
		||||
    --bind="ctrl-d:become($0 --delete {})" \
 | 
			
		||||
    --bind="ctrl-x:become($0 --toggle-completed {})" \
 | 
			
		||||
    --bind="alt-up:become($0 --increase-priority {})" \
 | 
			
		||||
    --bind="alt-down:become($0 --decrease-priority {})" \
 | 
			
		||||
    --bind="ctrl-n:become($0 --new)" \
 | 
			
		||||
    --bind="alt-0:change-query(!✅)" \
 | 
			
		||||
    --bind="alt-1:change-query(📘)" \
 | 
			
		||||
    --bind="alt-2:change-query(🗒️)" \
 | 
			
		||||
    --bind="alt-3:change-query(✅ | 🔲)" \
 | 
			
		||||
    --bind="ctrl-s:execute($SYNC_CMD)" \
 | 
			
		||||
    --bind="ctrl-r:reload-sync($0 --reload)"
 | 
			
		||||
)
 | 
			
		||||
if [ -z "$selection" ]; then
 | 
			
		||||
  return 0
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
file=$(__filepath_from_selection "$selection")
 | 
			
		||||
 | 
			
		||||
if [ ! -f "$file" ]; then
 | 
			
		||||
  echo "ERROR: File '$file' does not exist!"
 | 
			
		||||
  return 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Parse vjournal file and save as json
 | 
			
		||||
filejson=$(mktemp)
 | 
			
		||||
__ical2json <"$file" >"$filejson"
 | 
			
		||||
 | 
			
		||||
# Prepare file to be edited
 | 
			
		||||
filetmp=$(mktemp --suffix='.md')
 | 
			
		||||
filesha="$filetmp.sha"
 | 
			
		||||
if jq -e '.due' "$filejson"; then
 | 
			
		||||
  due=$(jq -r '.due' "$filejson")
 | 
			
		||||
  echo "::: <| $due" >"$filetmp"
 | 
			
		||||
fi
 | 
			
		||||
summary=$(jq -r '.summary' "$filejson")
 | 
			
		||||
categories=$(jq -r '.categories | join(",")' "$filejson")
 | 
			
		||||
{
 | 
			
		||||
  echo "# $summary"
 | 
			
		||||
  echo "> $categories"
 | 
			
		||||
  echo ""
 | 
			
		||||
  jq -r '.description' "$filejson"
 | 
			
		||||
} >>"$filetmp"
 | 
			
		||||
rm "$filejson"
 | 
			
		||||
sha1sum "$filetmp" >"$filesha"
 | 
			
		||||
 | 
			
		||||
# Open in editor
 | 
			
		||||
$EDITOR "$filetmp"
 | 
			
		||||
 | 
			
		||||
# Update only if changes are detected
 | 
			
		||||
if ! sha1sum -c "$filesha" >/dev/null 2>&1; then
 | 
			
		||||
  echo "Uh... chages detected!"
 | 
			
		||||
  vj_file_new="$filetmp.ics"
 | 
			
		||||
  __icalupdate "$file" <"$filetmp" >"$vj_file_new" && mv "$vj_file_new" "$file" || rm "$vj_file_new"
 | 
			
		||||
fi
 | 
			
		||||
rm "$filetmp" "$filesha"
 | 
			
		||||
 | 
			
		||||
exec "$0"
 | 
			
		||||
							
								
								
									
										19
									
								
								scripts/build.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										19
									
								
								scripts/build.sh
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
BOLD="\033[1m"
 | 
			
		||||
GREEN="\033[0;32m"
 | 
			
		||||
OFF="\033[m"
 | 
			
		||||
NAME="fzf-vjour"
 | 
			
		||||
SRC="./src/main.sh"
 | 
			
		||||
 | 
			
		||||
tmpdir=$(mktemp -d)
 | 
			
		||||
echo "🐔 ${GREEN}Internalize sourced files${OFF}"
 | 
			
		||||
sed -E 's|\. "([^$].+)"$|cat src/\1|e' "$SRC" >"$tmpdir/1.sh"
 | 
			
		||||
echo "🥚 ${GREEN}Internalize awk scripts${OFF}"
 | 
			
		||||
sed -E 's|@@include (.+)$|cat src/\1|e' "$tmpdir/1.sh" >"$tmpdir/2.sh"
 | 
			
		||||
echo "🐔 ${GREEN}Internalize awk libraries${OFF}"
 | 
			
		||||
sed -E 's|@include "(.+)"$|cat src/\1|e' "$tmpdir/2.sh" >"$NAME"
 | 
			
		||||
echo "🥚 ${GREEN}Make executable and cleanup${OFF}"
 | 
			
		||||
chmod +x "$NAME"
 | 
			
		||||
rm -rf "$tmpdir"
 | 
			
		||||
echo "🍳 ${GREEN}Done:${OFF} Sucessfully built ${BOLD}${GREEN}$NAME${OFF}"
 | 
			
		||||
							
								
								
									
										41
									
								
								src/awk/altertodo.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/awk/altertodo.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
# Increase/decrease priority, or toggle completed status
 | 
			
		||||
#
 | 
			
		||||
# If `delta` is specified using `-v`, then the priority value is increased by
 | 
			
		||||
# `delta.` If `delta` is unspecified (or equal to 0), then the completeness
 | 
			
		||||
# status is toggled.
 | 
			
		||||
BEGIN { 
 | 
			
		||||
  FS=":";
 | 
			
		||||
  zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1);
 | 
			
		||||
  delta = delta + 0; # cast as integer
 | 
			
		||||
}
 | 
			
		||||
/^END:VTODO/ && inside     { 
 | 
			
		||||
  # Print sequence and last-modified, if not yet printed
 | 
			
		||||
  if (!seq) print "SEQUENCE:1";
 | 
			
		||||
  if (!lm) print "LAST-MODIFIED:" zulu;
 | 
			
		||||
 | 
			
		||||
  # Print priority
 | 
			
		||||
  prio = prio ? prio + delta : 0 + delta;
 | 
			
		||||
  prio = prio < 0 ? 0 : prio;
 | 
			
		||||
  prio = prio > 9 ? 9 : prio;
 | 
			
		||||
  print "PRIORITY:" prio;
 | 
			
		||||
 | 
			
		||||
  # Print status (toggle if needed)
 | 
			
		||||
  bit_status = status == "COMPLETED" ? 1 : 0;
 | 
			
		||||
  bit_toggle = delta ? 0 : 1;
 | 
			
		||||
  percent = xor(bit_status, bit_toggle) ? 100 : 0;
 | 
			
		||||
  status = xor(bit_status, bit_toggle) ? "COMPLETED" : "NEEDS-ACTION";
 | 
			
		||||
  print "STATUS:" status
 | 
			
		||||
  print "PERCENT-COMPLETE:" percent
 | 
			
		||||
 | 
			
		||||
  # print rest
 | 
			
		||||
  inside = ""; 
 | 
			
		||||
  print $0;
 | 
			
		||||
  next
 | 
			
		||||
}
 | 
			
		||||
/^BEGIN:VTODO/                { inside = 1;    print;                       next }
 | 
			
		||||
/^SEQUENCE/ && inside         { seq = 1;       print "SEQUENCE:" $2+1;      next }
 | 
			
		||||
/^LAST-MODIFIED/ && inside    { lm = 1;        print "LAST-MODIFIED:" zulu; next }
 | 
			
		||||
/^PRIORITY:/ && inside        { prio = $2;                                  next }
 | 
			
		||||
/^STATUS/ && inside           { status = $2;                                next }
 | 
			
		||||
/^PERCENT-COMPLETE/ && inside {                                             next } # ignore, we take STATUS:COMPLETED as reference
 | 
			
		||||
{ print }
 | 
			
		||||
							
								
								
									
										35
									
								
								src/awk/attach.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/awk/attach.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
## src/awk/attach.awk
 | 
			
		||||
## Prepend attachment to iCalendar file.
 | 
			
		||||
##
 | 
			
		||||
## @assign file: Path to base64-encoded content
 | 
			
		||||
## @assign mime: Mime
 | 
			
		||||
## @assign filename: Original filename
 | 
			
		||||
 | 
			
		||||
# Functions
 | 
			
		||||
 | 
			
		||||
# Write attachment
 | 
			
		||||
#
 | 
			
		||||
# @local variables: line, aline
 | 
			
		||||
function write_attachment(    line, aline, fl) {
 | 
			
		||||
  line = "ATTACH;ENCODING=BASE64;VALUE=BINARY;FMTTYPE="mime";FILENAME="filename":"
 | 
			
		||||
  fl = 1
 | 
			
		||||
  while (getline aline <file) {
 | 
			
		||||
    line = line aline
 | 
			
		||||
    if (fl && length(line) >= 73) {
 | 
			
		||||
      print substr(line, 1, 73)
 | 
			
		||||
      line = substr(line, 74)
 | 
			
		||||
      fl = 0
 | 
			
		||||
    }
 | 
			
		||||
    while (length(line) >= 72) {
 | 
			
		||||
      print " "substr(line, 1, 72)
 | 
			
		||||
      line = substr(line, 73)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (line)
 | 
			
		||||
    print " "line
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# AWK program
 | 
			
		||||
 | 
			
		||||
/^END:(VTODO|VJOURNAL)$/ { write_attachment() }
 | 
			
		||||
{ print }
 | 
			
		||||
							
								
								
									
										8
									
								
								src/awk/attachdd.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/awk/attachdd.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
BEGIN                       { FS="[:;]" }
 | 
			
		||||
/^END:(VTODO|VJOURNAL)$/    { ins = 0; exit }
 | 
			
		||||
/^[^ ]/ && a                { a = 0 }
 | 
			
		||||
/^ / && a && p              { print substr($0, 2); }
 | 
			
		||||
/^ / && a && !p             { if (index($0, ":")) { p = 1; print substr($0, index($0, ":")+1) } }
 | 
			
		||||
/^ATTACH/ && ins            { i++; }
 | 
			
		||||
/^ATTACH/ && ins && i == id { a = 1; if (index($0, ":")) { p = 1; print substr($0, index($0, ":")+1) } }
 | 
			
		||||
/^BEGIN:(VTODO|VJOURNAL)$/  { ins = 1 }
 | 
			
		||||
							
								
								
									
										41
									
								
								src/awk/attachls.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/awk/attachls.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
# Decide if we need to read more to get all properties
 | 
			
		||||
# 
 | 
			
		||||
# @input str: strin read so far
 | 
			
		||||
# @return: 1 if we need more data, 0 otherwise
 | 
			
		||||
function cont_reading(str) {
 | 
			
		||||
  return index(str, ":") ? 0 : 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Get information about attachment
 | 
			
		||||
#
 | 
			
		||||
# @input i: Attachment index
 | 
			
		||||
# @input str: Attachment string (at least up to content separator `:`)
 | 
			
		||||
# @return: informative string
 | 
			
		||||
function att_info(i, str,    cnt, k, info) {
 | 
			
		||||
  str = substr(str, 1, index(str, ":") - 1)
 | 
			
		||||
  cnt = split(str, props)
 | 
			
		||||
  if (cnt > 1) {
 | 
			
		||||
    for (k=2; k<=cnt; k++) {
 | 
			
		||||
      pname = substr(props[k], 1, index(props[k], "=") - 1)
 | 
			
		||||
      pvalu = substr(props[k], index(props[k], "=") + 1)
 | 
			
		||||
      if (pname == "ENCODING" && pvalu = "BASE64")
 | 
			
		||||
        enc = "base64"
 | 
			
		||||
      if (pname == "FILENAME")
 | 
			
		||||
        fin = pvalu
 | 
			
		||||
      if (pname == "VALUE")
 | 
			
		||||
        val = pvalu
 | 
			
		||||
      if (pname == "FMTTYPE")
 | 
			
		||||
        type = pvalu
 | 
			
		||||
    }
 | 
			
		||||
    if (enc)
 | 
			
		||||
      info = "inline"
 | 
			
		||||
  }
 | 
			
		||||
  print i, fin, type, enc, info
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BEGIN                      { FS="[:;]"; OFS="\t" }
 | 
			
		||||
/^END:(VTODO|VJOURNAL)$/   { ins = 0; exit }
 | 
			
		||||
l && !r                    { att_info(i, l); l = "" }
 | 
			
		||||
/^ / && r                  { l = l substr($0, 2); r = cont_reading($0) }
 | 
			
		||||
/^ATTACH/ && ins           { i++; l = $0; r = cont_reading($0) }
 | 
			
		||||
/^BEGIN:(VTODO|VJOURNAL)$/ { ins = 1 }
 | 
			
		||||
							
								
								
									
										13
									
								
								src/awk/attachrm.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/awk/attachrm.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
## src/awk/attachrm.awk
 | 
			
		||||
## Remove attachment from iCalendar file.
 | 
			
		||||
##
 | 
			
		||||
## @assign id: Attachment number to remove
 | 
			
		||||
 | 
			
		||||
BEGIN                       { FS="[:;]" }
 | 
			
		||||
/^END:(VTODO|VJOURNAL)$/    { ins = 0 }
 | 
			
		||||
/^[^ ]/ && a                { a = 0 }
 | 
			
		||||
/^ / && a                   { next }
 | 
			
		||||
/^ATTACH/ && ins            { i++; }
 | 
			
		||||
/^ATTACH/ && ins && i == id { a = 1; next }
 | 
			
		||||
/^BEGIN:(VTODO|VJOURNAL)$/  { ins = 1 }
 | 
			
		||||
{ print }
 | 
			
		||||
							
								
								
									
										24
									
								
								src/awk/export.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/awk/export.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
@include "lib/awk/icalendar.awk"
 | 
			
		||||
 | 
			
		||||
BEGIN                     { FS = "[:;]"; }
 | 
			
		||||
/^BEGIN:(VJOURNAL|VTODO)/ { type = $2 }
 | 
			
		||||
/^END:/ && $2 == type     { exit }
 | 
			
		||||
/^(CATEGORIES|DESCRIPTION|SUMMARY|DUE)/ { prop = $1; c[prop] = $0; next; }
 | 
			
		||||
/^[^ ]/ && prop           { prop = ""; next; }
 | 
			
		||||
/^ / && prop              { c[prop] = c[prop] substr($0, 2); next; }
 | 
			
		||||
END {
 | 
			
		||||
  if (!type)
 | 
			
		||||
    exit
 | 
			
		||||
  # Process content lines, force CATEGORIES and SUMMARY as single-line
 | 
			
		||||
  c["CATEGORIES"]  = singleline(getcontent(c["CATEGORIES"]))
 | 
			
		||||
  c["DESCRIPTION"] = getcontent(c["DESCRIPTION"])
 | 
			
		||||
  c["SUMMARY"]     = singleline(getcontent(c["SUMMARY"]))
 | 
			
		||||
  c["DUE"]         = getcontent(c["DUE"])
 | 
			
		||||
  # Print
 | 
			
		||||
  if (c["DUE"])
 | 
			
		||||
    print "::: <| " substr(c["DUE"], 1, 4) "-" substr(c["DUE"], 5, 2) "-" substr(c["DUE"], 7, 2);
 | 
			
		||||
  print "# " c["SUMMARY"];
 | 
			
		||||
  print "> " c["CATEGORIES"];
 | 
			
		||||
  print "";
 | 
			
		||||
  print c["DESCRIPTION"];
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								src/awk/get.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/awk/get.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
@include "lib/awk/icalendar.awk"
 | 
			
		||||
 | 
			
		||||
# print content of field `field`
 | 
			
		||||
BEGIN                     { FS = ":"; regex = "^" field; }
 | 
			
		||||
/^BEGIN:(VJOURNAL|VTODO)/ { type = $2 }
 | 
			
		||||
/^END:/ && $2 == type     { exit }
 | 
			
		||||
$0 ~ field                { line = $0;                 next; }
 | 
			
		||||
/^ / && line              { line = line substr($0, 2); next; }
 | 
			
		||||
/^[^ ]/ && line           { exit }
 | 
			
		||||
END {
 | 
			
		||||
  if (!type) { exit }
 | 
			
		||||
  # Process line
 | 
			
		||||
  print getcontent(line)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										218
									
								
								src/awk/list.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										218
									
								
								src/awk/list.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,218 @@
 | 
			
		||||
# awk script to generate summary line for iCalendar VJOURNAL and VTODO entries
 | 
			
		||||
#
 | 
			
		||||
# See https://datatracker.ietf.org/doc/html/rfc5545 for the RFC 5545 that
 | 
			
		||||
# describes iCalendar, and its syntax
 | 
			
		||||
 | 
			
		||||
@include "lib/awk/icalendar.awk"
 | 
			
		||||
 | 
			
		||||
# Generate kind-of-pretty date strings.
 | 
			
		||||
#
 | 
			
		||||
# @local variables: ts, ts_y, ts_m, ts_d, delta
 | 
			
		||||
# @input date: Date in the format YYYYMMDD
 | 
			
		||||
# @input todaystamp: Today, seconds since epoch
 | 
			
		||||
# @return: string
 | 
			
		||||
function formatdate(date, todaystamp,       ts, ts_y, ts_m, ts_d, delta)
 | 
			
		||||
{
 | 
			
		||||
  ts_y = substr(date, 1, 4);
 | 
			
		||||
  ts_m = substr(date, 5, 2);
 | 
			
		||||
  ts_d = substr(date, 7);
 | 
			
		||||
  ts = mktime(ts_y " " ts_m " " ts_d " 00 00 00");
 | 
			
		||||
  delta = (ts - todaystamp) / 86400;
 | 
			
		||||
  if (delta >= 0 && delta < 1) {
 | 
			
		||||
    return "         today";
 | 
			
		||||
  }
 | 
			
		||||
  if (delta >= 1 && delta < 2) {
 | 
			
		||||
    return "      tomorrow";
 | 
			
		||||
  }
 | 
			
		||||
  if (delta >= 2 && delta < 3) {
 | 
			
		||||
    return "   in two days";
 | 
			
		||||
  }
 | 
			
		||||
  if (delta >= 3 && delta < 4) {
 | 
			
		||||
    return " in three days";
 | 
			
		||||
  }
 | 
			
		||||
  if (delta < 0 && delta >= -1) {
 | 
			
		||||
    return "     yesterday";
 | 
			
		||||
  }
 | 
			
		||||
  if (delta < -1 && delta >= -2) {
 | 
			
		||||
    return "  two days ago";
 | 
			
		||||
  }
 | 
			
		||||
  if (delta < -2 && delta >= -3) {
 | 
			
		||||
    return "three days ago";
 | 
			
		||||
  }
 | 
			
		||||
  return "    " substr(date, 1, 4) "-" substr(date, 5, 2) "-" substr(date, 7);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BEGIN {
 | 
			
		||||
  # We require the following variables to be set using -v
 | 
			
		||||
  # collection_lables: ;-delimited collection=label strings
 | 
			
		||||
  # flag_open:      symbol for open to-dos
 | 
			
		||||
  # flag_completed: symbol for completed to-dos
 | 
			
		||||
  # flag_journal:   symbol for journal entries
 | 
			
		||||
  # flag_note:      symbol for note entries
 | 
			
		||||
  # flag_priority   symbol for prior. task
 | 
			
		||||
  # flag_attachment symbol for attachment
 | 
			
		||||
  # style_collection
 | 
			
		||||
  # style_date
 | 
			
		||||
  # style_summary
 | 
			
		||||
  # style_expired
 | 
			
		||||
  # style_category
 | 
			
		||||
 | 
			
		||||
  FS = "[:;]";
 | 
			
		||||
  # Collections
 | 
			
		||||
  split(collection_labels, mapping, ";");
 | 
			
		||||
  for (map in mapping)
 | 
			
		||||
  {
 | 
			
		||||
    split(mapping[map], m, "=");
 | 
			
		||||
    collection2label[m[1]] = m[2];
 | 
			
		||||
  }
 | 
			
		||||
  # Colors
 | 
			
		||||
  OFF = "\033[m";
 | 
			
		||||
 | 
			
		||||
  # For date comparision
 | 
			
		||||
  today = strftime("%Y%m%d");
 | 
			
		||||
  todaystamp = mktime(substr(today, 1, 4) " " substr(today, 5, 2) " " substr(today, 7) " 00 00 00");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Reset variables
 | 
			
		||||
BEGINFILE {
 | 
			
		||||
  type = "";
 | 
			
		||||
  prop = "";
 | 
			
		||||
  att = "";
 | 
			
		||||
  delete c;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/^BEGIN:(VJOURNAL|VTODO)/ {
 | 
			
		||||
  type = $2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/^END:/ && $2 == type {
 | 
			
		||||
  nextfile
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/^(CATEGORIES|PRIORITY|STATUS|SUMMARY|COMPLETED|DUE|DTSTART|DURATION|CREATED|DTSTAMP|LAST-MODIFIED)/ {
 | 
			
		||||
  prop = $1;
 | 
			
		||||
  c[prop] = $0;
 | 
			
		||||
  next;
 | 
			
		||||
}
 | 
			
		||||
/^ATTACH/ {
 | 
			
		||||
  prop = ""
 | 
			
		||||
  att = 1;
 | 
			
		||||
  next;
 | 
			
		||||
}
 | 
			
		||||
/^[^ ]/ && prop {
 | 
			
		||||
  prop = "";
 | 
			
		||||
  next;
 | 
			
		||||
}
 | 
			
		||||
/^ / && prop {
 | 
			
		||||
  c[prop] = c[prop] substr($0, 2); 
 | 
			
		||||
  next; 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ENDFILE {
 | 
			
		||||
  if (!type) {
 | 
			
		||||
    exit
 | 
			
		||||
  }
 | 
			
		||||
  # Construct path, and check for validity
 | 
			
		||||
  depth = split(FILENAME, path, "/");
 | 
			
		||||
  fpath = path[depth-1] "/" path[depth]
 | 
			
		||||
  if (index(fpath, " "))
 | 
			
		||||
  {
 | 
			
		||||
    print 10,
 | 
			
		||||
          "-",
 | 
			
		||||
          type,
 | 
			
		||||
          "-",
 | 
			
		||||
          style_expired "ERROR: file '" fpath "' contains whitespaces!" OFF
 | 
			
		||||
    exit
 | 
			
		||||
  }
 | 
			
		||||
  # Collection name
 | 
			
		||||
  collection = path[depth-1]
 | 
			
		||||
  collection = collection in collection2label ? collection2label[collection] : collection;
 | 
			
		||||
 | 
			
		||||
  # Process content lines
 | 
			
		||||
  # strings
 | 
			
		||||
  cat = singleline(unescape(getcontent(c["CATEGORIES"])))
 | 
			
		||||
  sta = singleline(unescape(getcontent(c["STATUS"])))
 | 
			
		||||
  sum = singleline(unescape(getcontent(c["SUMMARY"])))
 | 
			
		||||
 | 
			
		||||
  # integers
 | 
			
		||||
  pri = unescape(getcontent(c["PRIORITY"]))
 | 
			
		||||
  pri = pri ? pri + 0 : 0
 | 
			
		||||
 | 
			
		||||
  # dates
 | 
			
		||||
  due = substr(unescape(getcontent(c["DUE"])), 1, 8)
 | 
			
		||||
  dts = substr(unescape(getcontent(c["DTSTART"])), 1, 8)
 | 
			
		||||
  
 | 
			
		||||
  # date-times
 | 
			
		||||
  com = unescape(getcontent(c["COMPLETED"]))
 | 
			
		||||
  dur = unescape(getcontent(c["DURATION"]))
 | 
			
		||||
  cre = unescape(getcontent(c["CREATED"]))
 | 
			
		||||
  stp = unescape(getcontent(c["DTSTAMP"]))
 | 
			
		||||
  lmd = unescape(getcontent(c["LAST-MODIFIED"]))
 | 
			
		||||
 | 
			
		||||
  # Priority field, primarly used for sorting
 | 
			
		||||
  psort = 0;
 | 
			
		||||
  priotext = ""
 | 
			
		||||
  if (pri > 0)
 | 
			
		||||
  {
 | 
			
		||||
    priotext = flag_priority "(" pri ") "
 | 
			
		||||
    psort = 10 - pri
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  # Last modification/creation time stamp, used for sorting
 | 
			
		||||
  # LAST-MODIFIED: Optional field for VTODO and VJOURNAL entries, date-time in
 | 
			
		||||
  #                UTC time format
 | 
			
		||||
  # DTSTAMP:       mandatory field in VTODO and VJOURNAL, date-time in UTC time
 | 
			
		||||
  #                format
 | 
			
		||||
  mod = lmd ? lmd : stp
 | 
			
		||||
 | 
			
		||||
  # Date field. For VTODO entries, we show the due date, for journal entries,
 | 
			
		||||
  # the associated date.
 | 
			
		||||
  datecolor = style_date
 | 
			
		||||
  summarycolor = style_summary
 | 
			
		||||
 | 
			
		||||
  if (type == "VTODO")
 | 
			
		||||
  {
 | 
			
		||||
    # Either DUE or DURATION may appear. If DURATION appears, then also DTSTART
 | 
			
		||||
    d = due ? due : (dur ? dts " for " dur : "");
 | 
			
		||||
    if (d && d <= today && sta != "COMPLETED")
 | 
			
		||||
    {
 | 
			
		||||
      datecolor = style_expired;
 | 
			
		||||
      summarycolor = style_expired;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    d = dts
 | 
			
		||||
  }
 | 
			
		||||
  d = d ? formatdate(d, todaystamp) : "              ";
 | 
			
		||||
 | 
			
		||||
  # flag: - "journal"   for VJOURNAL with DTSTART
 | 
			
		||||
  #       - "note"      for VJOURNAL without DTSTART
 | 
			
		||||
  #       - "completed" for VTODO with c["STATUS"] == COMPLETED
 | 
			
		||||
  #       - "open"      for VTODO with c["STATUS"] != COMPLETED
 | 
			
		||||
  if (type == "VTODO")
 | 
			
		||||
    flag = sta == "COMPLETED" ? flag_completed : flag_open;
 | 
			
		||||
  else
 | 
			
		||||
    flag = dts ? flag_journal : flag_note;
 | 
			
		||||
  
 | 
			
		||||
  # summary
 | 
			
		||||
  # c["SUMMARY"]
 | 
			
		||||
  summary = sum ? sum : " "
 | 
			
		||||
 | 
			
		||||
  # categories
 | 
			
		||||
  categories = cat ? cat : " "
 | 
			
		||||
 | 
			
		||||
  # attachments
 | 
			
		||||
  att = att ? flag_attachment " " : ""
 | 
			
		||||
 | 
			
		||||
  # filename
 | 
			
		||||
  # FILENAME
 | 
			
		||||
 | 
			
		||||
  print psort,
 | 
			
		||||
        mod,
 | 
			
		||||
        type,
 | 
			
		||||
        fpath,
 | 
			
		||||
        collection,
 | 
			
		||||
        datecolor d OFF,
 | 
			
		||||
        flag,
 | 
			
		||||
        priotext att summarycolor summary OFF,
 | 
			
		||||
        style_category categories OFF;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										58
									
								
								src/awk/new.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/awk/new.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
@include "lib/awk/icalendar.awk"
 | 
			
		||||
 | 
			
		||||
BEGIN { 
 | 
			
		||||
  FS=":"; 
 | 
			
		||||
  zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1);
 | 
			
		||||
}
 | 
			
		||||
desc                 { desc = desc "\\n" escape($0);                  next; }
 | 
			
		||||
/^::: \|>/ && !start { gsub("\"", ""); start = substr(zulu, 1, 8);    next; }
 | 
			
		||||
/^::: <\| / && !due  { gsub("\"", ""); due = substr($0, 8);           next; }
 | 
			
		||||
/^# / && !summary    { summary = escape(substr($0, 3));               next; }
 | 
			
		||||
/^> / && !categories { categories = escape_but_commas(substr($0, 3)); next; }
 | 
			
		||||
!$0 && !el           { el = 1;                                        next; }
 | 
			
		||||
!el                  { print "Unrecognized header on line "NR": " $0 > "/dev/stderr"; exit 1; }
 | 
			
		||||
                     { desc = "D" escape($0);                         next; }
 | 
			
		||||
END {
 | 
			
		||||
  # Sanitize input
 | 
			
		||||
  type = due ? "VTODO" : "VJOURNAL"
 | 
			
		||||
  if (due) {
 | 
			
		||||
    # Use command line `date` for parsing
 | 
			
		||||
    cmd = "date -d \"" due "\" +\"%Y%m%d\"";
 | 
			
		||||
    suc = cmd | getline res
 | 
			
		||||
    close(cmd)
 | 
			
		||||
    if (suc != 1)
 | 
			
		||||
      exit 1
 | 
			
		||||
    due = res ? res : ""
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  # print ical
 | 
			
		||||
  print "BEGIN:VCALENDAR";
 | 
			
		||||
  print "VERSION:2.0";
 | 
			
		||||
  print "CALSCALE:GREGORIAN";
 | 
			
		||||
  print "PRODID:-//fzf-vjour//awk//EN";
 | 
			
		||||
  print "BEGIN:" type;
 | 
			
		||||
  print "DTSTAMP:" zulu;
 | 
			
		||||
  print "UID:" uid;
 | 
			
		||||
  print "CLASS:PRIVATE";
 | 
			
		||||
  print "CREATED:" zulu;
 | 
			
		||||
  print "SEQUENCE:1";
 | 
			
		||||
  print "LAST-MODIFIED:" zulu;
 | 
			
		||||
  if (type == "VTODO")
 | 
			
		||||
  {
 | 
			
		||||
    print "STATUS:NEEDS-ACTION";
 | 
			
		||||
    print "PERCENT-COMPLETE:0";
 | 
			
		||||
    if (due)
 | 
			
		||||
      print "DUE;VALUE=DATE:" due; 
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    print "STATUS:FINAL";
 | 
			
		||||
    if (start)
 | 
			
		||||
      print "DTSTART;VALUE=DATE:" start;
 | 
			
		||||
  }
 | 
			
		||||
  if (summary)    print_fold("SUMMARY:",     summary);
 | 
			
		||||
  if (categories) print_fold("CATEGORIES:",  categories);
 | 
			
		||||
  if (desc)       print_fold("DESCRIPTION:", substr(desc, 2));
 | 
			
		||||
  print "END:" type;
 | 
			
		||||
  print "END:VCALENDAR"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								src/awk/update.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/awk/update.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
@include "lib/awk/icalendar.awk"
 | 
			
		||||
 | 
			
		||||
BEGIN { 
 | 
			
		||||
  FS=":";
 | 
			
		||||
  zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ENDFILE { 
 | 
			
		||||
  if (NR == FNR && due) {
 | 
			
		||||
    # Use command line `date` for parsing
 | 
			
		||||
    cmd = "date -d \"" due "\" +\"%Y%m%d\"";
 | 
			
		||||
    suc = cmd | getline res
 | 
			
		||||
    close(cmd)
 | 
			
		||||
    if (suc != 1)
 | 
			
		||||
      exit 1
 | 
			
		||||
    due = res ? res : ""
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
NR == FNR && desc                 { desc = desc "\\n" escape($0);                  next; }
 | 
			
		||||
NR == FNR && /^::: <\| / && !due  { gsub("\"",""); due = substr($0, 8);            next; }
 | 
			
		||||
NR == FNR && /^# / && !summary    { summary = escape(substr($0, 3));               next; }
 | 
			
		||||
NR == FNR && /^> / && !categories { categories = escape_but_commas(substr($0, 3)); next; }
 | 
			
		||||
NR == FNR && !$0 && !el           { el = 1;                                        next; }
 | 
			
		||||
NR == FNR && !el                  { print "Unrecognized header on line "NR": " $0 > "/dev/stderr"; exit 1; }
 | 
			
		||||
NR == FNR                         { desc = "D" escape($0);                         next; }
 | 
			
		||||
due && type == "VJOURNAL"         { print "Notes and journal entries do not have due dates." > "/dev/stderr"; exit 1; }
 | 
			
		||||
/^BEGIN:(VJOURNAL|VTODO)/         { type = $2; print;                              next; }
 | 
			
		||||
/^X-ALT-DESC/ && type             {                                                next; } # drop this alternative description
 | 
			
		||||
/^ / && type                      {                                                next; } # drop this folded line (the only content with folded lines will be updated)
 | 
			
		||||
/^(DUE|SUMMARY|CATEGORIES|DESCRIPTION|LAST-MODIFIED)/ && type {                    next; } # skip for now, we will write updated fields at the end
 | 
			
		||||
/^SEQUENCE/ && type               { seq = $2;                                      next; } # store sequence number and skip
 | 
			
		||||
/^END:/ && type == $2 {
 | 
			
		||||
  seq = seq ? seq + 1 : 1;
 | 
			
		||||
  print "SEQUENCE:" seq;
 | 
			
		||||
  print "LAST-MODIFIED:" zulu;
 | 
			
		||||
  if (due) print "DUE;VALUE=DATE:" due;
 | 
			
		||||
  print_fold("SUMMARY:",     summary);
 | 
			
		||||
  print_fold("CATEGORIES:",  categories);
 | 
			
		||||
  print_fold("DESCRIPTION:", substr(desc, 2));
 | 
			
		||||
  type = "";
 | 
			
		||||
}
 | 
			
		||||
{ print }
 | 
			
		||||
							
								
								
									
										143
									
								
								src/lib/awk/icalendar.awk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										143
									
								
								src/lib/awk/icalendar.awk
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,143 @@
 | 
			
		||||
# Make string single-line
 | 
			
		||||
#
 | 
			
		||||
# @input str: String
 | 
			
		||||
# @return: String without newlines
 | 
			
		||||
function singleline(str) {
 | 
			
		||||
  gsub("\\n", " ", str)
 | 
			
		||||
  return str
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Escape string to be used as content in iCalendar files.
 | 
			
		||||
#
 | 
			
		||||
# @input str: String to escape
 | 
			
		||||
# @return: Escaped string
 | 
			
		||||
function escape(str)
 | 
			
		||||
{
 | 
			
		||||
  gsub("\\\\", "\\\\", str)
 | 
			
		||||
  gsub("\\n",  "\\n",  str)
 | 
			
		||||
  gsub(";",    "\\;",  str)
 | 
			
		||||
  gsub(",",    "\\,",  str)
 | 
			
		||||
  return str
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Escape string to be used as content in iCalendar files, but don't escape
 | 
			
		||||
# commas.
 | 
			
		||||
#
 | 
			
		||||
# @input str: String to escape
 | 
			
		||||
# @return: Escaped string
 | 
			
		||||
function escape_but_commas(str)
 | 
			
		||||
{
 | 
			
		||||
  gsub("\\\\", "\\\\", str)
 | 
			
		||||
  gsub("\\n",  "\\n",  str)
 | 
			
		||||
  gsub(";",    "\\;",  str)
 | 
			
		||||
  return str
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Print property with its content and fold according to the iCalendar
 | 
			
		||||
# specification.
 | 
			
		||||
#
 | 
			
		||||
# @local variables: i, s
 | 
			
		||||
# @input nameparam: Property name with optional parameters
 | 
			
		||||
# @input content: Escaped content
 | 
			
		||||
function print_fold(nameparam, content,    i, s)
 | 
			
		||||
{
 | 
			
		||||
  i = 74 - length(nameparam)
 | 
			
		||||
  s = substr(content, 1, i)
 | 
			
		||||
  print nameparam s
 | 
			
		||||
  s = substr(content, i+1, 73)
 | 
			
		||||
  i = i + 73
 | 
			
		||||
  while (s)
 | 
			
		||||
  {
 | 
			
		||||
    print " " s
 | 
			
		||||
    s = substr(content, i+1, 73)
 | 
			
		||||
    i = i + 73
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Unescape string
 | 
			
		||||
#
 | 
			
		||||
# @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 parameter part of an iCalendar line.
 | 
			
		||||
#
 | 
			
		||||
# @input str: String
 | 
			
		||||
# @return: Parameter part
 | 
			
		||||
function getparam(str,    i) {
 | 
			
		||||
  i = index(str, ";")
 | 
			
		||||
  if (!i)
 | 
			
		||||
    return ""
 | 
			
		||||
  return substr(str, i + 1, index(str, ":") - i)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Isolate content part of an iCalendar line, and unescape.
 | 
			
		||||
#
 | 
			
		||||
# @input str: String
 | 
			
		||||
# @return: Unescaped content part
 | 
			
		||||
function getcontent(str) {
 | 
			
		||||
  return unescape(substr(str, index(str, ":") + 1))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Time-zone aware parsing of DTSTART or DTEND entries.
 | 
			
		||||
#
 | 
			
		||||
# @local variables: tz
 | 
			
		||||
# @input dt_param: iCalendar DTSTART or DTEND parameter string
 | 
			
		||||
# @input dt_content: iCalendar DTSTART or DTEND content string
 | 
			
		||||
# @return: date or date-time string that can be used in date (1)
 | 
			
		||||
function parse_dt(dt_param, dt_content,    tz, a, i, k) {
 | 
			
		||||
  if (dt_param) {
 | 
			
		||||
    split(dt_param, a, ";")
 | 
			
		||||
    for (i in a) {
 | 
			
		||||
      k = index(a[i], "=")
 | 
			
		||||
      if (substr(a[i], 1, k-1) == "TZID") {
 | 
			
		||||
        tz = "TZ=\"" substr(a[i], k + 1) "\" "
 | 
			
		||||
        break
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  # Get date/date-time
 | 
			
		||||
  return length(dt_content) == 8 ?
 | 
			
		||||
    dt dt_content :
 | 
			
		||||
    dt gensub(/^([0-9]{8})T([0-9]{2})([0-9]{2})([0-9]{2})(Z)?$/, "\\1 \\2:\\3:\\4\\5", "g", dt_content)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Map iCalendar duration specification into the format to be used in date (1).
 | 
			
		||||
#
 | 
			
		||||
# @local variables: dt, dta, i, n, a, seps
 | 
			
		||||
# @input duration: iCalendar duration string
 | 
			
		||||
# @return: relative-date/date-time specification to be used in date (1)
 | 
			
		||||
function parse_duration(duration,    dt, dta, i, n, a, seps) {
 | 
			
		||||
  n = split(duration, a, /[PTWHMSD]/, seps)
 | 
			
		||||
  for (i=2; i<=n; i++) {
 | 
			
		||||
    if(seps[i] == "W") dta["weeks"]   = a[i]
 | 
			
		||||
    if(seps[i] == "H") dta["hours"]   = a[i]
 | 
			
		||||
    if(seps[i] == "M") dta["minutes"] = a[i]
 | 
			
		||||
    if(seps[i] == "S") dta["seconds"] = a[i]
 | 
			
		||||
    if(seps[i] == "D") dta["days"]    = a[i]
 | 
			
		||||
  }
 | 
			
		||||
  dt = a[1] ? a[1] : "+"
 | 
			
		||||
  for (i in dta)
 | 
			
		||||
    dt = dt " " dta[i] " " i
 | 
			
		||||
  return dt
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										127
									
								
								src/main.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										127
									
								
								src/main.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,127 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
set -eu
 | 
			
		||||
 | 
			
		||||
# Helper functions
 | 
			
		||||
. "sh/helper.sh"
 | 
			
		||||
 | 
			
		||||
# Read theme
 | 
			
		||||
. "sh/theme.sh"
 | 
			
		||||
 | 
			
		||||
# Read configuration
 | 
			
		||||
. "sh/config.sh"
 | 
			
		||||
 | 
			
		||||
# Load awk scripts
 | 
			
		||||
. "sh/awkscripts.sh"
 | 
			
		||||
 | 
			
		||||
__lines() {
 | 
			
		||||
  find "$ROOT" -type f -name '*.ics' -print0 | xargs -0 -P 0 \
 | 
			
		||||
    awk \
 | 
			
		||||
    -v collection_labels="$COLLECTION_LABELS" \
 | 
			
		||||
    -v flag_open="$FLAG_OPEN" \
 | 
			
		||||
    -v flag_completed="$FLAG_COMPLETED" \
 | 
			
		||||
    -v flag_journal="$FLAG_JOURNAL" \
 | 
			
		||||
    -v flag_note="$FLAG_NOTE" \
 | 
			
		||||
    -v flag_priority="$FLAG_PRIORITY" \
 | 
			
		||||
    -v flag_attachment="$FLAG_ATTACHMENT" \
 | 
			
		||||
    -v style_collection="$STYLE_COLLECTION" \
 | 
			
		||||
    -v style_date="$STYLE_DATE" \
 | 
			
		||||
    -v style_summary="$STYLE_SUMMARY" \
 | 
			
		||||
    -v style_expired="$STYLE_EXPIRED" \
 | 
			
		||||
    -v style_category="$STYLE_CATEGORY" \
 | 
			
		||||
    "$AWK_LIST" |
 | 
			
		||||
    sort -g -r
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Program starts here
 | 
			
		||||
if [ "${1:-}" = "--help" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  echo "Usage: $0 [--help | --new [FILTER..] | [FILTER..] ]
 | 
			
		||||
  --help                 Show this help and exit
 | 
			
		||||
  --new                  Create new entry and do not exit
 | 
			
		||||
  --git-init             Activate git usage and exit
 | 
			
		||||
  --git <cmd>            Run git command and exit
 | 
			
		||||
 | 
			
		||||
[FILTER]
 | 
			
		||||
  You may specify any of these filters. Filters can be negated using the
 | 
			
		||||
  --no-... versions, e.g., --no-tasks. Multiple filters are applied in
 | 
			
		||||
  conjuction. By default, the filter --no-completed is used. Note that
 | 
			
		||||
  --no-completed is not the same as --open, and similarly, --no-open is not the
 | 
			
		||||
  same as --completed.
 | 
			
		||||
 | 
			
		||||
  --tasks                 Show tasks only
 | 
			
		||||
  --notes                 Show notes only
 | 
			
		||||
  --journal               Show jounral only
 | 
			
		||||
  --completed             Show completed tasks only
 | 
			
		||||
  --open                  Show open tasks only
 | 
			
		||||
  --filter <query>        Specify custom query"
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# iCalendar routines
 | 
			
		||||
. "sh/icalendar.sh"
 | 
			
		||||
 | 
			
		||||
# Command line arguments: Interal use
 | 
			
		||||
. "sh/cliinternal.sh"
 | 
			
		||||
 | 
			
		||||
# Command line arguments: Interal use
 | 
			
		||||
. "sh/cli.sh"
 | 
			
		||||
 | 
			
		||||
# Attachment handling
 | 
			
		||||
. "sh/attachment.sh"
 | 
			
		||||
 | 
			
		||||
while true; do
 | 
			
		||||
  query=$(stripws "$query")
 | 
			
		||||
  selection=$(
 | 
			
		||||
    __lines | $FZF --ansi \
 | 
			
		||||
      --query="$query " \
 | 
			
		||||
      --no-sort \
 | 
			
		||||
      --no-hscroll \
 | 
			
		||||
      --with-nth=5.. \
 | 
			
		||||
      --print-query \
 | 
			
		||||
      --accept-nth=4 \
 | 
			
		||||
      --preview="$0 --preview {4}" \
 | 
			
		||||
      --expect="ctrl-n,ctrl-alt-d,alt-v,ctrl-a" \
 | 
			
		||||
      --bind="ctrl-r:reload($0 --reload)" \
 | 
			
		||||
      --bind="ctrl-x:reload($0 --reload --toggle-completed {4})" \
 | 
			
		||||
      --bind="alt-up:reload($0 --reload --change-priority '+1' {4})" \
 | 
			
		||||
      --bind="alt-down:reload($0 --reload --change-priority '-1' {4})" \
 | 
			
		||||
      --bind="alt-0:change-query(!✅)" \
 | 
			
		||||
      --bind="alt-1:change-query(📘)" \
 | 
			
		||||
      --bind="alt-2:change-query(🗒️)" \
 | 
			
		||||
      --bind="alt-3:change-query(✅ | 🔲)" \
 | 
			
		||||
      --bind='focus:transform:[ {3} = "VTODO" ] && echo "rebind(ctrl-x)+rebind(alt-up)+rebind(alt-down)" || echo "unbind(ctrl-x)+unbind(alt-up)+unbind(alt-down)"' \
 | 
			
		||||
      --bind="ctrl-s:execute($SYNC_CMD; [ -n \"${GIT:-}\" ] && ${GIT:-} commit -am 'Synchronized'; printf 'Press <enter> to continue.'; read -r tmp)" ||
 | 
			
		||||
      true
 | 
			
		||||
  )
 | 
			
		||||
 | 
			
		||||
  # Line 1: query
 | 
			
		||||
  # Line 2: key ("" for enter)
 | 
			
		||||
  # Line 3: relative file path
 | 
			
		||||
  lines=$(echo "$selection" | wc -l)
 | 
			
		||||
  if [ "$lines" -eq 1 ]; then
 | 
			
		||||
    return 0
 | 
			
		||||
  fi
 | 
			
		||||
  query=$(echo "$selection" | head -n 1)
 | 
			
		||||
  key=$(echo "$selection" | head -n 2 | tail -n 1)
 | 
			
		||||
  fname=$(echo "$selection" | head -n 3 | tail -n 1)
 | 
			
		||||
  file="$ROOT/$fname"
 | 
			
		||||
 | 
			
		||||
  case "$key" in
 | 
			
		||||
  "ctrl-n")
 | 
			
		||||
    __new
 | 
			
		||||
    ;;
 | 
			
		||||
  "ctrl-alt-d")
 | 
			
		||||
    __delete "$file"
 | 
			
		||||
    ;;
 | 
			
		||||
  "alt-v")
 | 
			
		||||
    $EDITOR "$file"
 | 
			
		||||
    ;;
 | 
			
		||||
  "ctrl-a")
 | 
			
		||||
    __attachment_view "$file"
 | 
			
		||||
    ;;
 | 
			
		||||
  "")
 | 
			
		||||
    __edit "$file"
 | 
			
		||||
    ;;
 | 
			
		||||
  esac
 | 
			
		||||
done
 | 
			
		||||
							
								
								
									
										178
									
								
								src/sh/attachment.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										178
									
								
								src/sh/attachment.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,178 @@
 | 
			
		||||
# Add attachment to iCalendar file
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Path to iCalendar file
 | 
			
		||||
__add_attachment() {
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  sel=$(
 | 
			
		||||
    $FZF --prompt="Select attachment> " \
 | 
			
		||||
      --walker="file,hidden" \
 | 
			
		||||
      --walker-root="$HOME" \
 | 
			
		||||
      --expect="ctrl-c,ctrl-g,ctrl-q,esc"
 | 
			
		||||
  )
 | 
			
		||||
  key=$(echo "$sel" | head -1)
 | 
			
		||||
  f=$(echo "$sel" | tail -1)
 | 
			
		||||
  if [ -n "$key" ]; then
 | 
			
		||||
    f=""
 | 
			
		||||
  fi
 | 
			
		||||
  if [ -z "$f" ] || [ ! -f "$f" ]; then
 | 
			
		||||
    return
 | 
			
		||||
  fi
 | 
			
		||||
  filename=$(basename "$f")
 | 
			
		||||
  mime=$(file -b -i "$f" | cut -d ';' -f 1)
 | 
			
		||||
  if [ -z "$mime" ]; then
 | 
			
		||||
    mime="application/octet-stream"
 | 
			
		||||
  fi
 | 
			
		||||
  fenc=$(mktemp)
 | 
			
		||||
  base64 "$f" >"$fenc"
 | 
			
		||||
  filetmp=$(mktemp)
 | 
			
		||||
  awk -v file="$fenc" -v mime="$mime" -v filename="$filename" "$AWK_ATTACH" "$file" >"$filetmp"
 | 
			
		||||
  mv "$filetmp" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Added attachment" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
  rm "$fenc"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Open attachment from iCalendar file
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Attachment id
 | 
			
		||||
# @input $2: Attachment name
 | 
			
		||||
# @input $3: Attachment format
 | 
			
		||||
# @input $4: Attachment encoding
 | 
			
		||||
# @input $5: Path to iCalendar file
 | 
			
		||||
__open_attachment() {
 | 
			
		||||
  attid="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  attname="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  attfmt="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  attenc="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  if [ "$attenc" != "base64" ]; then
 | 
			
		||||
    err "Unsupported attachment encoding: $attenc. Press <enter> to continue."
 | 
			
		||||
    read -r tmp
 | 
			
		||||
    return
 | 
			
		||||
  fi
 | 
			
		||||
  if [ -n "$attname" ]; then
 | 
			
		||||
    tmpdir=$(mktemp -d)
 | 
			
		||||
    attpath="$tmpdir/$attname"
 | 
			
		||||
  elif [ -n "$attfmt" ]; then
 | 
			
		||||
    attext=$(echo "$attfmt" | cut -d "/" -f 2)
 | 
			
		||||
    attpath=$(mktemp --suffix="$attext")
 | 
			
		||||
  else
 | 
			
		||||
    attpath=$(mktemp)
 | 
			
		||||
  fi
 | 
			
		||||
  # Get file and decode
 | 
			
		||||
  awk -v id="$attid" "$AWK_ATTACHDD" "$file" | base64 -d >"$attpath"
 | 
			
		||||
  fn=$(file "$attpath")
 | 
			
		||||
  while true; do
 | 
			
		||||
    printf "Are you sure you want to open \"%s\"? (yes/no): " "$fn" >/dev/tty
 | 
			
		||||
    read -r yn
 | 
			
		||||
    case $yn in
 | 
			
		||||
    "yes")
 | 
			
		||||
      $OPEN "$attpath"
 | 
			
		||||
      printf "Press <enter> to continue." >/dev/tty
 | 
			
		||||
      read -r tmp
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    "no")
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    *)
 | 
			
		||||
      echo "Please answer \"yes\" or \"no\"." >/dev/tty
 | 
			
		||||
      ;;
 | 
			
		||||
    esac
 | 
			
		||||
  done
 | 
			
		||||
  # Clean up
 | 
			
		||||
  rm -f "$attpath"
 | 
			
		||||
  if [ -n "${tmpdir:-}" ] && [ -d "${tmpdir:-}" ]; then
 | 
			
		||||
    rm -rf "$tmpdir"
 | 
			
		||||
  fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Delete attachment from iCalendar file
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Attachment id
 | 
			
		||||
# @input $2: Path to iCalendar File
 | 
			
		||||
__del_attachment() {
 | 
			
		||||
  attid="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  while true; do
 | 
			
		||||
    printf "Are you sure you want to delete attachment \"%s\"? (yes/no): " "$attid" >/dev/tty
 | 
			
		||||
    read -r yn
 | 
			
		||||
    case $yn in
 | 
			
		||||
    "yes")
 | 
			
		||||
      filetmp=$(mktemp)
 | 
			
		||||
      awk -v id="$attid" "$AWK_ATTACHRM" "$file" >"$filetmp"
 | 
			
		||||
      mv "$filetmp" "$file"
 | 
			
		||||
      if [ -n "${GIT:-}" ]; then
 | 
			
		||||
        $GIT add "$file"
 | 
			
		||||
        $GIT commit -q -m "Deleted attachment" -- "$file"
 | 
			
		||||
      fi
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    "no")
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    *)
 | 
			
		||||
      echo "Please answer \"yes\" or \"no\"." >/dev/tty
 | 
			
		||||
      ;;
 | 
			
		||||
    esac
 | 
			
		||||
  done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Show attachment window
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Path to iCalendar file
 | 
			
		||||
__attachment_view() {
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  att=$(
 | 
			
		||||
    awk "$AWK_ATTACHLS" "$file" |
 | 
			
		||||
      $FZF \
 | 
			
		||||
        --delimiter="\t" \
 | 
			
		||||
        --accept-nth=1,2,3,4 \
 | 
			
		||||
        --with-nth="Attachment {1}: \"{2}\" {3} ({5})" \
 | 
			
		||||
        --no-sort \
 | 
			
		||||
        --tac \
 | 
			
		||||
        --margin="30%,30%" \
 | 
			
		||||
        --border=bold \
 | 
			
		||||
        --border-label="Attachment View     Keys: <enter> open, <ctrl-alt-d> delete, <ctrl-a> add" \
 | 
			
		||||
        --expect="ctrl-a" \
 | 
			
		||||
        --expect="ctrl-c,ctrl-g,ctrl-q,ctrl-d,esc,q,backspace" \
 | 
			
		||||
        --print-query \
 | 
			
		||||
        --bind="start:hide-input" \
 | 
			
		||||
        --bind="ctrl-alt-d:show-input+change-query(ctrl-alt-d)+accept" \
 | 
			
		||||
        --bind='load:transform:[ "$FZF_TOTAL_COUNT" -eq 0 ] && echo "unbind(enter)+unbind(ctrl-alt-d)"' \
 | 
			
		||||
        --bind="w:toggle-wrap" \
 | 
			
		||||
        --bind="j:down" \
 | 
			
		||||
        --bind="k:up" ||
 | 
			
		||||
      true
 | 
			
		||||
  )
 | 
			
		||||
  key=$(echo "$att" | head -2 | xargs)
 | 
			
		||||
  sel=$(echo "$att" | tail -1)
 | 
			
		||||
  attid=$(echo "$sel" | cut -f 1)
 | 
			
		||||
  attname=$(echo "$sel" | cut -f 2)
 | 
			
		||||
  attfmt=$(echo "$sel" | cut -f 3)
 | 
			
		||||
  attenc=$(echo "$sel" | cut -f 4)
 | 
			
		||||
  case "$key" in
 | 
			
		||||
  "ctrl-c" | "ctrl-g" | "ctrl-q" | "ctrl-d" | "esc" | "q" | "backspace") ;;
 | 
			
		||||
  "ctrl-alt-d")
 | 
			
		||||
    __del_attachment "$attid" "$file"
 | 
			
		||||
    ;;
 | 
			
		||||
  "ctrl-a")
 | 
			
		||||
    __add_attachment "$file"
 | 
			
		||||
    ;;
 | 
			
		||||
  *)
 | 
			
		||||
    __open_attachment "$attid" "$attname" "$attfmt" "$attenc" "$file"
 | 
			
		||||
    ;;
 | 
			
		||||
  esac
 | 
			
		||||
  #
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										69
									
								
								src/sh/awkscripts.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/sh/awkscripts.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,69 @@
 | 
			
		||||
AWK_ALTERTODO=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/altertodo.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_ALTERTODO
 | 
			
		||||
 | 
			
		||||
AWK_EXPORT=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/export.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_EXPORT
 | 
			
		||||
 | 
			
		||||
AWK_GET=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/get.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_GET
 | 
			
		||||
 | 
			
		||||
AWK_LIST=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
  @@include awk/list.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_LIST
 | 
			
		||||
 | 
			
		||||
AWK_NEW=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/new.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_NEW
 | 
			
		||||
 | 
			
		||||
AWK_UPDATE=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/update.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_UPDATE
 | 
			
		||||
 | 
			
		||||
AWK_ATTACH=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/attach.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_ATTACH
 | 
			
		||||
 | 
			
		||||
AWK_ATTACHDD=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/attachdd.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_ATTACHDD
 | 
			
		||||
 | 
			
		||||
AWK_ATTACHLS=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/attachls.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_ATTACHLS
 | 
			
		||||
 | 
			
		||||
AWK_ATTACHRM=$(
 | 
			
		||||
  cat <<'EOF'
 | 
			
		||||
@@include awk/attachrm.awk
 | 
			
		||||
EOF
 | 
			
		||||
)
 | 
			
		||||
export AWK_ATTACHRM
 | 
			
		||||
							
								
								
									
										94
									
								
								src/sh/cli.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								src/sh/cli.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
			
		||||
# Git
 | 
			
		||||
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
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Generate new entry
 | 
			
		||||
if [ "${1:-}" = "--new" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  __new
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Build query
 | 
			
		||||
while [ -n "${1:-}" ]; do
 | 
			
		||||
  case "${1:-}" in
 | 
			
		||||
  "--completed")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} $FLAG_COMPLETED"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-completed")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !$FLAG_COMPLETED"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--open")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} $FLAG_OPEN"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-open")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !$FLAG_OPEN"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--tasks")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} $FLAG_OPEN | $FLAG_COMPLETED"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-tasks")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !$FLAG_COMPLETED !$FLAG_OPEN"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--notes")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} $FLAG_NOTE"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-notes")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !$FLAG_NOTE"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--journal")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} $FLAG_JOURNAL"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-journal")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !$FLAG_JOURNAL"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--filter")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} $1"
 | 
			
		||||
    shift
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-filter")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !$1"
 | 
			
		||||
    shift
 | 
			
		||||
    ;;
 | 
			
		||||
  *)
 | 
			
		||||
    err "Unknown option \"$1\""
 | 
			
		||||
    exit 1
 | 
			
		||||
    ;;
 | 
			
		||||
  esac
 | 
			
		||||
done
 | 
			
		||||
query=${cliquery:-!$FLAG_COMPLETED}
 | 
			
		||||
export query
 | 
			
		||||
							
								
								
									
										35
									
								
								src/sh/cliinternal.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/sh/cliinternal.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
# Command-line interface for internal use only
 | 
			
		||||
 | 
			
		||||
# Generate preview of file from selection
 | 
			
		||||
if [ "${1:-}" = "--preview" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  name="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$name"
 | 
			
		||||
  awk -v field="DESCRIPTION" "$AWK_GET" "$file" |
 | 
			
		||||
    $CAT
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Reload view
 | 
			
		||||
if [ "${1:-}" = "--reload" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  case "${1:-}" in
 | 
			
		||||
  "--toggle-completed")
 | 
			
		||||
    shift
 | 
			
		||||
    fname="$1"
 | 
			
		||||
    shift
 | 
			
		||||
    __toggle_completed "$fname" >/dev/null
 | 
			
		||||
    ;;
 | 
			
		||||
  "--change-priority")
 | 
			
		||||
    shift
 | 
			
		||||
    delta=$1
 | 
			
		||||
    shift
 | 
			
		||||
    fname="$1"
 | 
			
		||||
    shift
 | 
			
		||||
    __change_priority "$delta" "$fname" >/dev/null
 | 
			
		||||
    ;;
 | 
			
		||||
  esac
 | 
			
		||||
  __lines
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
							
								
								
									
										48
									
								
								src/sh/config.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/sh/config.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
CONFIGFILE="${CONFIGFILE:-$HOME/.config/fzf-vjour/config}"
 | 
			
		||||
if [ ! -f "$CONFIGFILE" ]; then
 | 
			
		||||
  err "Configuration '$CONFIGFILE' not found."
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
# shellcheck source=/dev/null
 | 
			
		||||
. "$CONFIGFILE"
 | 
			
		||||
if [ -z "${ROOT:-}" ] || [ -z "${COLLECTION_LABELS:-}" ]; then
 | 
			
		||||
  err "Configuration is incomplete."
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
SYNC_CMD="${SYNC_CMD:-}"
 | 
			
		||||
export ROOT
 | 
			
		||||
export SYNC_CMD
 | 
			
		||||
export COLLECTION_LABELS
 | 
			
		||||
 | 
			
		||||
# Tools
 | 
			
		||||
if command -v "fzf" >/dev/null; then
 | 
			
		||||
  FZF="fzf"
 | 
			
		||||
else
 | 
			
		||||
  err "Did not find the command-line fuzzy finder fzf."
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
export FZF
 | 
			
		||||
 | 
			
		||||
if command -v "uuidgen" >/dev/null; then
 | 
			
		||||
  UUIDGEN="uuidgen"
 | 
			
		||||
else
 | 
			
		||||
  err "Did not find the uuidgen command."
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
export UUIDGEN
 | 
			
		||||
 | 
			
		||||
if command -v "bat" >/dev/null; then
 | 
			
		||||
  CAT="bat"
 | 
			
		||||
elif command -v "batcat" >/dev/null; then
 | 
			
		||||
  CAT="batcat"
 | 
			
		||||
fi
 | 
			
		||||
CAT=${CAT:+$CAT --color=always --style=numbers --language=md}
 | 
			
		||||
CAT=${CAT:-cat}
 | 
			
		||||
export CAT
 | 
			
		||||
 | 
			
		||||
if command -v "git" >/dev/null && [ -d "$ROOT/.git" ]; then
 | 
			
		||||
  GIT="git -C $ROOT"
 | 
			
		||||
  export GIT
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
export OPEN=${OPEN:-open}
 | 
			
		||||
							
								
								
									
										9
									
								
								src/sh/helper.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/sh/helper.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
# Print error message
 | 
			
		||||
err() {
 | 
			
		||||
  echo "❌ $1" >/dev/tty
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Strip whitespaces from argument
 | 
			
		||||
stripws() {
 | 
			
		||||
  echo "$@" | sed "s/^ *//" | sed "s/ *$//"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										141
									
								
								src/sh/icalendar.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										141
									
								
								src/sh/icalendar.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,141 @@
 | 
			
		||||
# Interface to modify iCalendar files
 | 
			
		||||
 | 
			
		||||
# Toggle completed status of VTODO
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Relative path to iCalendar file
 | 
			
		||||
__toggle_completed() {
 | 
			
		||||
  fname="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$fname"
 | 
			
		||||
  tmpfile=$(mktemp)
 | 
			
		||||
  awk "$AWK_ALTERTODO" "$file" >"$tmpfile"
 | 
			
		||||
  mv "$tmpfile" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Completed toggle" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Change priority of VTODO entry
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Delta, can be any integer
 | 
			
		||||
# @input $2: Relative path to iCalendar file
 | 
			
		||||
__change_priority() {
 | 
			
		||||
  delta=$1
 | 
			
		||||
  shift
 | 
			
		||||
  fname="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$fname"
 | 
			
		||||
  tmpfile=$(mktemp)
 | 
			
		||||
  awk -v delta="$delta" "$AWK_ALTERTODO" "$file" >"$tmpfile"
 | 
			
		||||
  mv "$tmpfile" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Priority changed by $delta" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Edit file
 | 
			
		||||
#
 | 
			
		||||
# @input $1: File path
 | 
			
		||||
__edit() {
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  tmpmd=$(mktemp --suffix='.md')
 | 
			
		||||
  awk "$AWK_EXPORT" "$file" >"$tmpmd"
 | 
			
		||||
  checksum=$(cksum "$tmpmd")
 | 
			
		||||
 | 
			
		||||
  # Open in editor
 | 
			
		||||
  $EDITOR "$tmpmd" >/dev/tty
 | 
			
		||||
 | 
			
		||||
  # Update only if changes are detected
 | 
			
		||||
  while [ "$checksum" != "$(cksum "$tmpmd")" ]; do
 | 
			
		||||
    tmpfile="$tmpmd.ics"
 | 
			
		||||
    if awk "$AWK_UPDATE" "$tmpmd" "$file" >"$tmpfile"; then
 | 
			
		||||
      mv "$tmpfile" "$file"
 | 
			
		||||
      if [ -n "${GIT:-}" ]; then
 | 
			
		||||
        $GIT add "$file"
 | 
			
		||||
        $GIT commit -q -m "File modified" -- "$file"
 | 
			
		||||
      fi
 | 
			
		||||
      break
 | 
			
		||||
    else
 | 
			
		||||
      rm -f "$tmpfile"
 | 
			
		||||
      err "Failed to update entry. Press <enter> to continue."
 | 
			
		||||
      read -r tmp
 | 
			
		||||
      # Re-open in editor
 | 
			
		||||
      $EDITOR "$tmpmd" >/dev/tty
 | 
			
		||||
    fi
 | 
			
		||||
  done
 | 
			
		||||
  rm "$tmpmd"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Delete file
 | 
			
		||||
#
 | 
			
		||||
# @input $1: File path
 | 
			
		||||
__delete() {
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  summary=$(awk -v field="SUMMARY" "$AWK_GET" "$file")
 | 
			
		||||
  while true; do
 | 
			
		||||
    printf "Do you want to delete the entry with the title \"%s\"? (yes/no): " "$summary" >/dev/tty
 | 
			
		||||
    read -r yn
 | 
			
		||||
    case $yn in
 | 
			
		||||
    "yes")
 | 
			
		||||
      rm -v "$file"
 | 
			
		||||
      if [ -n "${GIT:-}" ]; then
 | 
			
		||||
        $GIT add "$file"
 | 
			
		||||
        $GIT commit -q -m "File deleted" -- "$file"
 | 
			
		||||
      fi
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    "no")
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    *)
 | 
			
		||||
      echo "Please answer \"yes\" or \"no\"." >/dev/tty
 | 
			
		||||
      ;;
 | 
			
		||||
    esac
 | 
			
		||||
  done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Add file
 | 
			
		||||
__new() {
 | 
			
		||||
  collection=$(printf "%s" "$COLLECTION_LABELS" | tr ';' '\n' | $FZF --delimiter='=' --with-nth=2 --accept-nth=1)
 | 
			
		||||
  file=""
 | 
			
		||||
  while [ -f "$file" ] || [ -z "$file" ]; do
 | 
			
		||||
    uuid=$($UUIDGEN)
 | 
			
		||||
    file="$ROOT/$collection/$uuid.ics"
 | 
			
		||||
  done
 | 
			
		||||
  tmpmd=$(mktemp --suffix='.md')
 | 
			
		||||
  {
 | 
			
		||||
    echo "::: |> <!-- keep this line to associate the entry to _today_ -->"
 | 
			
		||||
    echo "::: <| <!-- specify the due date for to-dos, can be empty, a date string, or even \"next Sunday\" -->"
 | 
			
		||||
    echo "# <!-- write summary here -->"
 | 
			
		||||
    echo "> <!-- comma-separated list of categories -->"
 | 
			
		||||
    echo ""
 | 
			
		||||
  } >"$tmpmd"
 | 
			
		||||
  checksum=$(cksum "$tmpmd")
 | 
			
		||||
 | 
			
		||||
  # Open in editor
 | 
			
		||||
  $EDITOR "$tmpmd" >/dev/tty
 | 
			
		||||
 | 
			
		||||
  # Update if changes are detected
 | 
			
		||||
  while [ "$checksum" != "$(cksum "$tmpmd")" ]; do
 | 
			
		||||
    tmpfile="$tmpmd.ics"
 | 
			
		||||
    if awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile"; then
 | 
			
		||||
      mv "$tmpfile" "$file"
 | 
			
		||||
      if [ -n "${GIT:-}" ]; then
 | 
			
		||||
        $GIT add "$file"
 | 
			
		||||
        $GIT commit -q -m "File added" -- "$file"
 | 
			
		||||
      fi
 | 
			
		||||
      break
 | 
			
		||||
    else
 | 
			
		||||
      rm -f "$tmpfile"
 | 
			
		||||
      err "Failed to create new entry. Press <enter> to continue."
 | 
			
		||||
      read -r tmp
 | 
			
		||||
      # Re-open in editor
 | 
			
		||||
      $EDITOR "$tmpmd" >/dev/tty
 | 
			
		||||
    fi
 | 
			
		||||
  done
 | 
			
		||||
  rm "$tmpmd"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								src/sh/theme.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/sh/theme.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
# Colors
 | 
			
		||||
GREEN="\033[1;32m"
 | 
			
		||||
RED="\033[1;31m"
 | 
			
		||||
WHITE="\033[1;97m"
 | 
			
		||||
CYAN="\033[1;36m"
 | 
			
		||||
FAINT="\033[2m"
 | 
			
		||||
 | 
			
		||||
# Flags
 | 
			
		||||
export FLAG_OPEN="${FLAG_OPEN:-🔲}"
 | 
			
		||||
export FLAG_COMPLETED="${FLAG_COMPLETED:-✅}"
 | 
			
		||||
export FLAG_JOURNAL="${FLAG_JOURNAL:-📘}"
 | 
			
		||||
export FLAG_NOTE="${FLAG_NOTE:-🗒️}"
 | 
			
		||||
export FLAG_PRIORITY="${FLAG_PRIORITY:-❗}"
 | 
			
		||||
export FLAG_ATTACHMENT="${FLAG_ATTACHMENT:-🔗}"
 | 
			
		||||
 | 
			
		||||
# Style
 | 
			
		||||
export STYLE_COLLECTION="${STYLE_COLLECTION:-$FAINT$WHITE}"
 | 
			
		||||
export STYLE_DATE="${STYLE_DATE:-$CYAN}"
 | 
			
		||||
export STYLE_SUMMARY="${STYLE_SUMMARY:-$GREEN}"
 | 
			
		||||
export STYLE_EXPIRED="${STYLE_EXPIRED:-$RED}"
 | 
			
		||||
export STYLE_CATEGORY="${STYLE_CATEGORY:-$WHITE}"
 | 
			
		||||
		Reference in New Issue
	
	Block a user