-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdx
executable file
·647 lines (564 loc) · 13.3 KB
/
tdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/bin/bash -eu
#
# Notes: Things to implement
#
# - [ ] Publish implementation
# - [ ] Utility language to expand time
if [[ -n "${TDX_DEBUG:-}" ]]; then
set -x
fi
########################################################################
# Constants
########################################################################
CUR_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
readonly CUR_DIR
readonly TDX_MDF="${TDX_MDF:-"$CUR_DIR/markdown_files/zig-out/lib/libmd_files.so"}"
export BAT_THEME="${BAT_THEME:-base16}"
readonly BAT_THEME
export SELF="$0"
if [[ -z "${ZED_TERM:-}" ]]; then
# shellcheck disable=SC2140
export FZF_DEFAULT_OPTS="--preview 'file=\$(echo {1}); [[ -f "\$file" ]] && bat --color=always --style=plain "\$file"' --preview-window 'bottom:border-sharp:wrap'"
fi
if [[ -n "${ZED_TERM:-}" ]]; then
export EDITOR="zed";
fi
FZF_DEFAULT_OPTS="--header \"TERMinal inDEX | $(pwd) \$ tdx ${*}\" ${FZF_DEFAULT_OPTS:-}"
FZF_DEFAULT_OPTS="--color header:red ${FZF_DEFAULT_OPTS:-}"
FZF_DEFAULT_OPTS="--header-first ${FZF_DEFAULT_OPTS:-}"
FZF_DEFAULT_OPTS="--layout reverse ${FZF_DEFAULT_OPTS:-}"
########################################################################
# Utilities
########################################################################
function err {
echo -e "\033[91m $* \033[0m" >&2
}
function check_dep {
if ! command -v "$1" &> /dev/null; then
err "Couldn't find $1 in \$PATH: please make sure it's installed!"
exit 1
fi
}
function check_lib {
if [[ ! -f "$1" ]]; then
err "Couldn't find $1: please make sure it's available!"
exit 1
fi
}
########################################################################
# Implementation
########################################################################
function print_usage {
echo "$(basename "${BASH_SOURCE[0]}") usage:"
echo " n, new [template=note,task,bookmark,reminder,pomodoro,*]"
echo " e, edit <path>"
echo " q, query [where clause]"
echo " j, journal"
echo " p, pomodoro"
echo " s, sql"
echo " b, board [group by] [filter]"
echo " t, tasks [collection]"
echo " w, week"
echo " r, ripgrep"
exit 1
}
function new {
local template="${1:-}"
if [[ -n "$template" ]]; then
shift
fi
local path
path="$(date +%Y/%m/%d)"
local filename
filename="$(date +%s).md"
mkdir -p "$path"
local full_path="$path/$filename"
case "$template" in
n|note)
cat <<EOF >"$full_path"
---
title: ${1:-}
collection: ${2:-}
tags:
what: note
publish: true
---
EOF
;;
t|task)
cat <<EOF >"$full_path"
---
title: ${1:-}
collection: ${2:-}
tags:
what: task
when: someday # today, tomorrow, week, month, quarter, someday
status: # blocked, active, done
deadline:
---
EOF
;;
b|bookmark)
cat <<EOF >"$full_path"
---
title: ${1:-}
url: ${2:-}
collection: ${3:-}
tags:
what: bookmark
source:
publish: true
---
EOF
;;
r|reminder)
cat <<EOF >"$full_path"
---
title: ${1:-}
when: ${2:-}
collection: ${3:-}
what: reminder
---
EOF
;;
p|pomo|pomodoro)
cat <<EOF >"$full_path"
---
title: ${1:-}
collection: ${2:-}
what: pomodoro
---
EOF
;;
m|meeting)
cat <<EOF >"$full_path"
---
title: ${1:-}
collection: ${2:-}
what: meeting
---
EOF
;;
*)
cat <<EOF >"$full_path"
---
title: ${1:-}
collection: ${2:-}
tags:
what: $template
---
EOF
esac
"$EDITOR" "$full_path"
if [[ ! -s "$full_path" ]]; then
rm "$full_path"
fi
}
function query {
local location
location=$(pwd)
local sql_query
sql_query=$(cat <<EOF
SELECT
substr(path, length('$location') + 2),
' #',
md_front_matter(path, 'collection'),
' / ',
md_front_matter(path, 'title'),
' (',
md_front_matter(path, 'what'),
') ',
md_front_matter(path, 'tags')
FROM
md_files
WHERE
path LIKE '%.md'
AND path NOT LIKE '%.git'
${1:+AND $1}
ORDER BY
mtime_s DESC,
path ASC
EOF
);
command=$(cat <<EOF
/usr/bin/env \
sqlite3 -list -separator "" :memory: \
".load ${TDX_MDF%.so}" \
"$sql_query"
EOF
);
(
export FZF_DEFAULT_COMMAND="$command";
# shellcheck disable=SC2016
fzf --bind 'ctrl-r:reload(eval "$FZF_DEFAULT_COMMAND")' \
--bind 'enter:execute(eval "$SELF e {1}")+reload(eval "$FZF_DEFAULT_COMMAND")' \
--bind 'ctrl-n:execute(eval "$SELF n")' \
--info inline-right
)
}
function edit {
local path="$1"
local parent_dir
parent_dir="$(dirname "$path")"
mkdir -p "$parent_dir"
local final_path
if [[ "$path" == *".md" ]]; then
final_path="$path"
else
final_path="${path}.md"
fi
if [[ ! -f "$final_path" ]]; then
cat <<EOF >"$final_path"
---
title:
---
EOF
fi
local rel_path
rel_path="$(realpath -s --relative-to="$(pwd)" "$final_path")"
local collection="${rel_path%.md}"
# local related
# related="$(mktemp "related-$(echo "$collection" | tr '/' '_')-XXX.md")"
#
# local query
# query="$(cat <<EOF
# SELECT
# '=== '||path||' ===',
# md_contents(path) AS file
# FROM
# md_files
# WHERE
# basename LIKE '%.md'
# AND path NOT LIKE '%.git%'
# AND md_front_matter(path, 'collection') = '$collection'
# ORDER BY
# mtime_s DESC,
# path ASC;
# EOF
# )"
#
# "$SELF" sql "-list" ".separator \n" "$query" \
# | tr -d '\r' > "$related";
# chmod -w "$related"
#
# if [[ ! -s "$related" ]]; then
# rm -f "$related"
# "$EDITOR" "$final_path"
# else
# "$EDITOR" "$final_path" "$related"
# fi
"$EDITOR" "$final_path"
if [[ ! -s "$final_path" ]]; then
rm "$final_path"
fi
# rm -f "$related"
}
function journal {
local path
local date_cmd
if [[ "${1:-}" ]]; then
date_cmd="date -d $1"
else
date_cmd="date"
fi
path="$($date_cmd +%Y/%m/%d).md"
local parent_dir
parent_dir="$(dirname "$path")"
mkdir -p "$parent_dir"
if [[ ! -f "$path" ]]; then
cat <<EOF >"$path"
---
title: $($date_cmd +%F)
---
EOF
fi
local related
related="$(mktemp "related-$($date_cmd +%F)-XXX.md")"
local location
location=$(pwd)
local query
query="$(cat <<EOF
SELECT
'=== '||path||' ===',
md_contents(path) AS file
FROM
md_files
WHERE
basename LIKE '%.md'
AND path NOT LIKE '%.git%'
AND SUBSTR(path, length('$location') + 2) LIKE '${path%.md}%'
ORDER BY
md_front_matter(path, 'what') ASC,
mtime_s DESC,
path ASC;
EOF
)"
"$SELF" sql "-list" '.separator \n' "$query"
"$EDITOR" "$path"
if [[ ! -s "$path" ]]; then
rm "$path"
fi
}
function notify {
local title="$1"
local summary="$2"
local ns
if ! which notify-send; then
return
fi
"$ns" --app-name "tdx" \
-u "normal" \
--action="action1=Focus Window" \
"$title" \
"$summary"
}
function pomodoro {
while true; do
clear
echo
echo -e " \033[32m.v,\033[0m"
echo -e " \033[31m(\033[0m' \033[31m,)\033[0m"
echo
local location
location=$(pwd)
echo -e "\e[4mRecent pomodoros:\e[0m"
local sql_query
sql_query=$(cat <<EOF
SELECT
substr(path, length('$location') + 2), ' / ',
md_front_matter(path, 'title'), ' #',
md_front_matter(path, 'collection')
FROM
md_files
WHERE
path LIKE '%.md'
AND path NOT LIKE '%.git'
AND md_front_matter(path, 'what') = 'pomodoro'
ORDER BY
mtime_s DESC,
path ASC
LIMIT 10
EOF
);
/usr/bin/env \
sqlite3 -list -separator "" :memory: \
".load ${TDX_MDF%.so}" \
"$sql_query"
echo
echo -ne "\033[4mPomodoro goal?\033[0m\n"
local purpose
read -e -r purpose
local collection
echo -ne "\033[4mAdd to collection?\033[0m\n"
read -e -r collection
local end_time
end_time="$(date -d "+25 minutes")"
local end_timestamp
end_timestamp="$(date -d "$end_time" +%s)"
echo -e "\n> Pomodoro will end at ${end_time}"
echo -n "> "
while (( $(date +%s) < end_timestamp )); do
sleep 60
echo -n "."
done
echo -e '\a'
notify "Pomodoro finished!" "${collection+$collection\n}$purpose"
$SELF new pomodoro "$purpose" "$collection"
echo -e "> Recorded.\n"
end_time="$(date -d "+5 minutes")"
end_timestamp="$(date -d "$end_time" +%s)"
echo "> Break will end at ${end_time}"
echo -n "> "
while (( $(date +%s) < end_timestamp )); do
sleep 60
echo -n "."
done
echo -e "\a\n\n"
notify "Break finished!" "$(date)"
done
}
function sql {
exec /usr/bin/env sqlite3 :memory: -cmd ".load ${TDX_MDF%.so}" "$@"
}
function board {
local group_by="${1:-when}"
local query
query=$(cat <<EOF
SELECT
DISTINCT(coalesce(md_front_matter(path, '$group_by'), 'null'))
FROM
md_files
WHERE
(
md_front_matter(path, 'status') != 'done'
OR md_front_matter(path, 'status') IS NULL
)
${2:+AND $2}
GROUP BY 1
ORDER BY COUNT(1) DESC
EOF
)
local vals
vals="$("$SELF" sql "-list" "$query")"
# shellcheck disable=SC2016
for val in $vals; do
echo -e "=== ${val} ==="
"$SELF" sql "-list" ".separator ' '" "SELECT path, md_front_matter(path, 'title'), md_front_matter(path, 'collection'), md_front_matter(path, 'tags') FROM md_files WHERE md_front_matter(path, '$group_by') = '$val' AND basename LIKE '%.md' AND path NOT LIKE '%.git%' ${2:+AND $2} ORDER BY mtime_s DESC"
echo
done |\
fzf --bind 'enter:execute(eval "$SELF e {1}")+become(eval "$SELF b $group_by")' \
--info inline-right
}
function tasks {
local collection="${1:-}"
local whens=(today week month quarter half year someday unset)
local location
location=$(pwd)
(
printf "<path> === %-20s ===\n" "active"
active_query="$(cat <<EOF
SELECT
printf('%-28s', substr(path, length('$location') + 2)),
printf('>%-10s ', md_front_matter(path, 'deadline')),
printf('#%-20s', substr(md_front_matter(path, 'collection'), length('projects/'))),
printf('@%-10s', md_front_matter(path, 'status')),
md_front_matter(path, 'title'),
md_front_matter(path, 'tags')
FROM md_files
WHERE
md_front_matter(path, 'status') = 'active'
AND basename LIKE '%.md'
AND path NOT LIKE '%.git%'
AND COALESCE(md_front_matter(path, 'status'), 'unset') != 'done'
${1:+AND coalesce(md_front_matter(path, 'collection'), 'unset') LIKE '$collection%'}
ORDER BY
mtime_s DESC
EOF
)";
"$SELF" sql "-list" ".separator ' '" "$active_query"
echo
# shellcheck disable=2016
for when in "${whens[@]}"; do
local group_query
group_query="$(cat <<EOF
SELECT
printf('%-28s', substr(path, length('$location') + 2)),
printf('>%-10s ', md_front_matter(path, 'deadline')),
printf('#%-20s', substr(md_front_matter(path, 'collection'), length('projects/'))),
printf('@%-10s', md_front_matter(path, 'status')),
md_front_matter(path, 'title'),
md_front_matter(path, 'tags')
FROM md_files
WHERE
md_front_matter(path, 'when') = '$when'
AND basename LIKE '%.md'
AND path NOT LIKE '%.git%'
AND COALESCE(md_front_matter(path, 'status'), 'unset') != 'done'
${1:+AND coalesce(md_front_matter(path, 'collection'), 'unset') LIKE '$collection%'}
ORDER BY
mtime_s DESC
EOF
)";
printf "<path> === %-20s ===\n" "$when"
"$SELF" sql "-list" ".separator ' '" "$group_query"
echo
done
) |\
fzf --bind "enter:become(eval \"$SELF e {1} && $SELF t $collection\")" \
--bind "ctrl-n:become(eval \"$SELF n t '' $collection && $SELF t $collection\")" \
--with-nth "2.." \
--info inline-right
}
function week {
local start_time end_time location
start_time=$(date -d "last sunday" +%s)
end_time=$(date -d "sunday" +%s)
location=$(pwd)
query="$(cat <<EOF
SELECT
substr(path, length('$location') + 2),
DATETIME(mtime_s, 'unixepoch'),
md_front_matter(path, 'what'),
md_front_matter(path, 'title'),
md_front_matter(path, 'tags')
FROM md_files
WHERE
mtime_s >= $start_time AND mtime_s <= $end_time
AND basename LIKE '%.md'
AND path NOT LIKE '%.git%'
${1:+AND $1}
ORDER BY
mtime_s DESC
EOF
)";
exec /usr/bin/env sqlite3 :memory: -column -cmd ".load ${TDX_MDF%.so}" "$query" |\
fzf --bind "enter:become(eval \"$SELF e {1} && $SELF w\")"
}
function ripgrep {
RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
INITIAL_QUERY="${*:-}"
# shellcheck disable=SC2016
fzf --ansi --disabled --query "$INITIAL_QUERY" \
--bind "start:reload:$RG_PREFIX {q}" \
--bind "change:reload:sleep 0.05; $RG_PREFIX {q} || true" \
--delimiter : \
--preview 'bat --style=plain {1} --highlight-line {2}' \
--bind 'enter:become($EDITOR {1} +{2})'
}
function collate {
local project="$1"
/usr/bin/env sqlite3 :memory: -cmd ".load ${TDX_MDF%.so}" \
"SELECT path, md_contents(path) FROM md_files WHERE path LIKE '%${project}%.md' AND path NOT LIKE '%.git%' ORDER BY path ASC"
/usr/bin/env sqlite3 :memory: -cmd ".load ${TDX_MDF%.so}" \
"SELECT path, md_contents(path) FROM md_files WHERE path NOT LIKE '%.git%' AND md_front_matter(path, 'collection') LIKE '${project}%' ORDER BY path DESC"
}
function main {
check_dep fzf
check_dep sqlite3
check_dep bat
check_dep rg
check_lib "$TDX_MDF"
if (( $# < 1 )); then
print_usage
fi
local basecmd="${1:-}"
shift
case "$basecmd" in
n|new)
new "$@"
;;
e|edit)
edit "$@"
;;
j|journal)
journal "$@"
;;
q|query)
query "$@"
;;
p|pomo|pomodoro)
pomodoro "$@"
;;
s|sql)
sql "$@"
;;
b|board)
board "$@"
;;
t|tasks)
tasks "$@"
;;
w|week)
week "$@"
;;
r|ripgrep)
ripgrep "$@"
;;
c|collate)
collate "$@"
;;
*)
print_usage
esac
}
main "$@"