-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwt
More file actions
executable file
·979 lines (854 loc) · 33.5 KB
/
wt
File metadata and controls
executable file
·979 lines (854 loc) · 33.5 KB
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#!/bin/bash
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# wt — WorkTracker CLI
# Colorful, structured command-line interface for WorkTracker
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
set -euo pipefail
# ── Paths ────────────────────────────────────────────────────────────────────
BASE="$HOME/WorkTracker"
DAEMON="$BASE/daemon"
VENV="$DAEMON/.venv/bin/python"
LOG_DIR="$BASE/logs"
SUMMARIES="$BASE/summaries"
LABEL_COL="com.peab.worktracker.collector"
LABEL_AGG_D="com.peab.worktracker.aggregator.daily"
LABEL_AGG_W="com.peab.worktracker.aggregator.weekly"
LABEL_AGG_M="com.peab.worktracker.aggregator.monthly"
PLIST_COL="$HOME/Library/LaunchAgents/$LABEL_COL.plist"
# ── Colors ───────────────────────────────────────────────────────────────────
RST='\033[0m'
BOLD='\033[1m'
DIM='\033[2m'
RED='\033[38;5;203m'
GREEN='\033[38;5;114m'
YELLOW='\033[38;5;221m'
BLUE='\033[38;5;118m'
CYAN='\033[38;5;154m'
MAGENTA='\033[38;5;176m'
ORANGE='\033[38;5;215m'
WHITE='\033[38;5;252m'
GRAY='\033[38;5;240m'
GRAY_L='\033[38;5;245m'
BG_GREEN='\033[48;5;22m'
BG_RED='\033[48;5;52m'
BG_BLUE='\033[48;5;22m'
BG_YELLOW='\033[48;5;58m'
# ── Symbols ──────────────────────────────────────────────────────────────────
SYM_OK="●"
SYM_WARN="◆"
SYM_ERR="○"
SYM_ARROW="▸"
SYM_CHECK="✓"
SYM_CROSS="✗"
SYM_LINE="─"
SYM_DOUBLE="━"
# ── Helpers ──────────────────────────────────────────────────────────────────
line() {
local char="${1:-$SYM_LINE}"
local color="${2:-$GRAY}"
local cols=$(tput cols 2>/dev/null || echo 80)
printf "${color}"
printf '%*s' "$cols" '' | tr ' ' "$char"
printf "${RST}\n"
}
header() {
local title="$1"
echo ""
line "$SYM_DOUBLE" "$CYAN"
printf " ${CYAN}${BOLD}%s${RST}" "$title"
printf "${GRAY_L} %s${RST}\n" "$(date '+%Y-%m-%d %H:%M:%S')"
line "$SYM_DOUBLE" "$CYAN"
}
section() {
echo ""
printf " ${CYAN}${BOLD}%s${RST}\n" "$1"
printf " ${GRAY}%s${RST}\n" "$(printf '%*s' ${#1} '' | tr ' ' "$SYM_LINE")"
}
badge_ok() { printf "${BG_GREEN}${GREEN}${BOLD} %s ${RST}" "$1"; }
badge_warn() { printf "${BG_YELLOW}${YELLOW}${BOLD} %s ${RST}" "$1"; }
badge_err() { printf "${BG_RED}${RED}${BOLD} %s ${RST}" "$1"; }
badge_info() { printf "${BG_BLUE}${BLUE}${BOLD} %s ${RST}" "$1"; }
msg_ok() { printf " ${GREEN}${SYM_CHECK}${RST} %b\n" "$1"; }
msg_err() { printf " ${RED}${SYM_CROSS}${RST} %b\n" "$1"; }
msg_info() { printf " ${BLUE}${SYM_ARROW}${RST} %b\n" "$1"; }
msg_warn() { printf " ${YELLOW}${SYM_WARN}${RST} %b\n" "$1"; }
# Format ps etime (e.g. "25:08", "1:05:08", "2-03:12:45") → human readable
fmt_uptime() {
local raw="$1"
[[ -z "$raw" ]] && return
local days=0 hours=0 mins=0 secs=0
if [[ "$raw" == *-* ]]; then
# dd-hh:mm:ss
days="${raw%%-*}"
raw="${raw#*-}"
fi
IFS=':' read -ra parts <<< "$raw"
local n=${#parts[@]}
if (( n == 3 )); then
hours=$((10#${parts[0]}))
mins=$((10#${parts[1]}))
secs=$((10#${parts[2]}))
elif (( n == 2 )); then
mins=$((10#${parts[0]}))
secs=$((10#${parts[1]}))
fi
local result=""
if (( days > 0 )); then
result="${days}d ${hours}h ${mins}m"
elif (( hours > 0 )); then
result="${hours}h ${mins}m"
elif (( mins > 0 )); then
result="${mins}m ${secs}s"
else
result="${secs}s"
fi
echo "$result"
}
# Service status check
svc_status() {
local label="$1"
local info
info=$(launchctl list "$label" 2>/dev/null) || { echo "unloaded|0|0|"; return; }
local pid
pid=$(echo "$info" | grep '"PID"' | awk '{print $NF}' | tr -d '";')
local exit_code
exit_code=$(echo "$info" | grep '"LastExitStatus"' | awk '{print $NF}' | tr -d '";')
if [[ -n "$pid" && "$pid" =~ ^[0-9]+$ ]]; then
local raw_uptime
raw_uptime=$(ps -p "$pid" -o etime= 2>/dev/null | xargs)
local uptime
uptime=$(fmt_uptime "$raw_uptime")
echo "running|$pid|${exit_code:-0}|${uptime}"
else
echo "loaded|0|${exit_code:-0}|"
fi
}
# Formatted service line
print_svc() {
local name="$1"
local label="$2"
local schedule="${3:-}"
local result
result=$(svc_status "$label")
local state pid exit_code uptime
IFS='|' read -r state pid exit_code uptime <<< "$result"
printf " "
case "$state" in
running)
printf "${GREEN}${SYM_OK}${RST} "
printf "${WHITE}${BOLD}%-16s${RST}" "$name"
printf "${GREEN}running${RST} "
printf "${GRAY_L}PID %-8s${RST}" "$pid"
if [[ -n "$uptime" ]]; then
printf "${GRAY}up ${WHITE}%s${RST}" "$uptime"
fi
;;
loaded)
printf "${BLUE}${SYM_WARN}${RST} "
printf "${WHITE}${BOLD}%-16s${RST}" "$name"
printf "${BLUE}ready${RST} "
if [[ -n "$schedule" ]]; then
printf "${GRAY_L}%-14s${RST}" "$schedule"
fi
if [[ "$exit_code" != "0" ]]; then
printf "${YELLOW}exit=${exit_code}${RST}"
else
printf "${GREEN}exit=0${RST}"
fi
;;
unloaded)
printf "${RED}${SYM_ERR}${RST} "
printf "${WHITE}%-16s${RST}" "$name"
printf "${RED}not loaded${RST}"
;;
esac
echo ""
}
fmt_dur() {
local sec=$1
if (( sec < 60 )); then
echo "${sec}s"
elif (( sec < 3600 )); then
echo "$(( sec / 60 ))m"
else
printf "%dh %02dm" $((sec / 3600)) $(( (sec % 3600) / 60 ))
fi
}
latest_report() {
local type="$1"
local dir="$SUMMARIES/$type"
if [[ -d "$dir" ]]; then
local latest
latest=$(ls -t "$dir"/*.md 2>/dev/null | head -1)
if [[ -n "$latest" ]]; then
local name size
name=$(basename "$latest")
size=$(stat -f%z "$latest" 2>/dev/null || echo 0)
echo "$name|$size"
return
fi
fi
echo "|0"
}
human_size() {
local bytes="${1:-0}"
awk -v b="$bytes" 'BEGIN {
if (b < 1024) printf "%dB", b
else if (b < 1048576) printf "%dK", b/1024
else if (b < 1073741824) printf "%.1fM", b/1048576
else printf "%.1fG", b/1073741824
}'
}
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Commands
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
cmd_status() {
header "WORKTRACKER STATUS"
section "Services"
print_svc "Collector" "$LABEL_COL"
print_svc "Agg Daily" "$LABEL_AGG_D" "daily 22:00"
print_svc "Agg Weekly" "$LABEL_AGG_W" "Sun 22:30"
print_svc "Agg Monthly" "$LABEL_AGG_M" "1st of month"
local today
today=$(date '+%Y-%m-%d')
section "Data ($today)"
local snap_file="$BASE/data/snapshots/${today}.jsonl"
local sess_file="$BASE/data/sessions/${today}.json"
if [[ -f "$snap_file" ]]; then
local snap_count snap_bytes snap_size
snap_count=$(wc -l < "$snap_file" | tr -d ' ')
snap_bytes=$(stat -f%z "$snap_file" 2>/dev/null || echo 0)
snap_size=$(human_size "$snap_bytes")
printf " ${GRAY_L}Snapshots:${RST} ${WHITE}${BOLD}%s${RST} ${GRAY}(%s)${RST}\n" "$snap_count" "$snap_size"
else
printf " ${GRAY_L}Snapshots:${RST} ${GRAY}none${RST}\n"
fi
if [[ -f "$sess_file" ]]; then
local sess_count sess_bytes sess_size
sess_count=$("$VENV" -c "import json; print(len(json.load(open('$sess_file'))))" 2>/dev/null || echo "0")
sess_bytes=$(stat -f%z "$sess_file" 2>/dev/null || echo 0)
sess_size=$(human_size "$sess_bytes")
printf " ${GRAY_L}Sessions:${RST} ${WHITE}${BOLD}%s${RST} ${GRAY}(%s)${RST}\n" "$sess_count" "$sess_size"
else
printf " ${GRAY_L}Sessions:${RST} ${GRAY}none${RST}\n"
fi
local scr_dir_today="$BASE/data/screenshots/${today}"
if [[ -d "$scr_dir_today" ]]; then
local scr_count scr_size_today
scr_count=$(find "$scr_dir_today" -maxdepth 1 -name '*.png' 2>/dev/null | wc -l | tr -d ' ')
scr_size_today=$(du -sh "$scr_dir_today" 2>/dev/null | awk '{print $1}')
printf " ${GRAY_L}Screenshots:${RST} ${WHITE}${BOLD}%s${RST} ${GRAY}(%s)${RST}\n" "$scr_count" "$scr_size_today"
else
printf " ${GRAY_L}Screenshots:${RST} ${GRAY}none${RST}\n"
fi
local data_size
data_size=$(du -sh "$BASE/data" 2>/dev/null | awk '{print $1}')
local log_size
log_size=$(du -sh "$LOG_DIR" 2>/dev/null | awk '{print $1}')
printf " ${GRAY_L}Data:${RST} ${WHITE}%s${RST} " "$data_size"
printf "${GRAY_L}Logs:${RST} ${WHITE}%s${RST}\n" "$log_size"
local snap_total sess_total scr_total
snap_total=$(du -sh "$BASE/data/snapshots" 2>/dev/null | awk '{print $1}')
sess_total=$(du -sh "$BASE/data/sessions" 2>/dev/null | awk '{print $1}')
scr_total=$(du -sh "$BASE/data/screenshots" 2>/dev/null | awk '{print $1}')
printf " ${GRAY_L}Totals:${RST} ${GRAY_L}snap${RST} ${WHITE}%s${RST} ${GRAY_L}sess${RST} ${WHITE}%s${RST} ${GRAY_L}scr${RST} ${WHITE}%s${RST}\n" \
"${snap_total:-0}" "${sess_total:-0}" "${scr_total:-0}"
section "Reports"
for type in daily weekly monthly; do
local result name size
result=$(latest_report "$type")
IFS='|' read -r name size <<< "$result"
local type_label
type_label=$(echo "$type" | awk '{print toupper(substr($0,1,1)) substr($0,2)}')
printf " ${GRAY_L}%-10s${RST}" "${type_label}:"
if [[ -n "$name" ]]; then
local kb
kb=$(echo "scale=1; $size / 1024" | bc 2>/dev/null || echo "?")
printf "${WHITE}%s${RST} ${GRAY}(%s KB)${RST}\n" "$name" "$kb"
else
printf "${GRAY}—${RST}\n"
fi
done
section "Recent Logs"
for logname in collector aggregator; do
local logfile="$LOG_DIR/${logname}.log"
if [[ -f "$logfile" ]]; then
printf " ${GRAY_L}${logname}:${RST}\n"
tail -2 "$logfile" 2>/dev/null | while IFS= read -r logline; do
if echo "$logline" | grep -q '\[ERROR\]'; then
printf " ${RED}%s${RST}\n" "$logline"
elif echo "$logline" | grep -q '\[WARNING\]'; then
printf " ${YELLOW}%s${RST}\n" "$logline"
else
printf " ${GRAY}%s${RST}\n" "$logline"
fi
done
fi
done
echo ""
}
cmd_restart() {
header "COLLECTOR RESTART"
msg_info "Stopping collector..."
launchctl stop "$LABEL_COL" 2>/dev/null || true
launchctl unload "$PLIST_COL" 2>/dev/null || true
sleep 1
msg_info "Starting collector..."
launchctl load "$PLIST_COL" 2>/dev/null
launchctl start "$LABEL_COL"
sleep 1
local result
result=$(svc_status "$LABEL_COL")
local state pid
IFS='|' read -r state pid _ <<< "$result"
if [[ "$state" == "running" ]]; then
msg_ok "Collector running — PID ${BOLD}${pid}${RST}"
elif [[ "$state" == "loaded" ]]; then
msg_warn "Collector loaded but no PID"
msg_info "Check: ${DIM}$LOG_DIR/collector-stderr.log${RST}"
else
msg_err "Collector failed to start"
msg_info "Check: ${DIM}$LOG_DIR/collector-stderr.log${RST}"
echo ""
printf " ${GRAY_L}Recent errors:${RST}\n"
tail -3 "$LOG_DIR/collector-stderr.log" 2>/dev/null | while IFS= read -r l; do
printf " ${RED}%s${RST}\n" "$l"
done
exit 1
fi
echo ""
}
cmd_agg() {
local mode="$1"
local mode_upper
mode_upper=$(echo "$mode" | tr '[:lower:]' '[:upper:]')
case "$mode" in
daily) local label_name="Daily";;
weekly) local label_name="Weekly";;
monthly) local label_name="Monthly";;
esac
header "AGGREGATOR $mode_upper"
msg_info "Running ${label_name} aggregation..."
echo ""
local start_time
start_time=$(perl -MTime::HiRes=time -e 'printf "%.3f", time')
# For daily mode, use the live-progress UI (the aggregator writes phase
# updates on stderr and final marker lines on stdout). Weekly/monthly
# keep the old log-line-streaming behaviour for now.
local exit_code=0
if [[ "$mode" == "daily" ]]; then
local agg_out
if agg_out=$("$VENV" "$DAEMON/aggregator.py" --mode "$mode" --progress); then
exit_code=0
else
exit_code=$?
fi
# Echo any remaining stdout lines (report paths, suggestions, etc.) —
# skip [SUMMARY] and empty lines.
echo "$agg_out" | while IFS= read -r line; do
[[ -z "$line" ]] && continue
[[ "$line" == "[SUMMARY]"* ]] && continue
if [[ "$line" == "✓"* ]]; then
printf " ${GREEN}${BOLD}%s${RST}\n" "$line"
else
printf " ${GRAY_L}%s${RST}\n" "$line"
fi
done
else
"$VENV" "$DAEMON/aggregator.py" --mode "$mode" 2>&1 | while IFS= read -r agg_line; do
if echo "$agg_line" | grep -q '^✓'; then
printf " ${GREEN}${BOLD}%s${RST}\n" "$agg_line"
elif echo "$agg_line" | grep -q '\[ERROR\]'; then
printf " ${RED}%s${RST}\n" "$agg_line"
elif echo "$agg_line" | grep -q '\[WARNING\]'; then
printf " ${YELLOW}%s${RST}\n" "$agg_line"
elif echo "$agg_line" | grep -q '\[INFO\]'; then
printf " ${GRAY_L}%s${RST}\n" "$agg_line"
else
printf " ${GRAY}%s${RST}\n" "$agg_line"
fi
done
exit_code=${PIPESTATUS[0]}
fi
local end_time
end_time=$(perl -MTime::HiRes=time -e 'printf "%.3f", time')
local elapsed_ms
elapsed_ms=$(perl -e "printf '%d', ($end_time - $start_time) * 1000")
echo ""
if [[ "$exit_code" -eq 0 ]]; then
msg_ok "${label_name} aggregation complete $(badge_ok "${elapsed_ms}ms")"
local result name size
result=$(latest_report "$mode")
IFS='|' read -r name size <<< "$result"
if [[ -n "$name" ]]; then
local kb
kb=$(echo "scale=1; $size / 1024" | bc 2>/dev/null || echo "?")
msg_info "Report: ${WHITE}${BOLD}$name${RST} ${GRAY}(${kb} KB)${RST}"
fi
# Review learned patterns (daily only, interactive terminal)
if [[ "$mode" == "daily" && -t 0 ]]; then
"$VENV" "$DAEMON/review_patterns.py"
fi
else
msg_err "${label_name} aggregation failed $(badge_err "exit=$exit_code")"
msg_info "Check: ${DIM}$LOG_DIR/aggregator.log${RST}"
fi
echo ""
}
cmd_dashboard() {
exec "$VENV" "$DAEMON/dashboard.py"
}
cmd_rhythm() {
"$VENV" "$DAEMON/rhythm_heatmap.py" "${2:-1}"
}
cmd_web() {
header "WEB DASHBOARD"
msg_info "Starting on ${CYAN}${BOLD}http://127.0.0.1:7880${RST}"
msg_info "Press ${WHITE}${BOLD}Ctrl+C${RST} to stop"
line "$SYM_LINE" "$GRAY"
echo ""
"$VENV" "$DAEMON/web_dashboard.py" &
local _srv_pid=$!
# Wait for server to be ready, then open browser
for _ in 1 2 3 4 5 6; do
sleep 0.5
if curl -s --max-time 1 http://127.0.0.1:7880 >/dev/null 2>&1; then
open http://127.0.0.1:7880
break
fi
done
trap 'kill "$_srv_pid" 2>/dev/null; exit 0' INT
wait "$_srv_pid"
}
cmd_menubar() {
header "MENUBAR WIDGET"
# Ensure web dashboard is running on port 7880
if ! curl -s --max-time 1 http://127.0.0.1:7880 >/dev/null 2>&1; then
msg_info "Web dashboard not running — starting in background..."
"$VENV" "$DAEMON/web_dashboard.py" &
_web_pid=$!
# Wait briefly for it to come up
for _ in 1 2 3 4 5; do
sleep 0.5
if curl -s --max-time 1 http://127.0.0.1:7880 >/dev/null 2>&1; then
break
fi
done
if curl -s --max-time 1 http://127.0.0.1:7880 >/dev/null 2>&1; then
msg_ok "Web dashboard started (PID ${_web_pid})"
else
msg_warn "Web dashboard may not be ready yet"
fi
else
msg_ok "Web dashboard already running on port 7880"
fi
msg_info "Starting menubar widget..."
msg_info "Press ${WHITE}${BOLD}Ctrl+C${RST} to stop"
line "$SYM_LINE" "$GRAY"
echo ""
trap 'echo ""; msg_info "Menubar widget stopped"; [[ -n "${_web_pid:-}" ]] && kill "$_web_pid" 2>/dev/null; exit 0' INT
"$VENV" "$DAEMON/menubar.py"
}
cmd_tail() {
header "LOG TAIL"
msg_info "Following collector logs... ${GRAY}(Ctrl+C to stop)${RST}"
line "$SYM_LINE" "$GRAY"
echo ""
tail -f "$LOG_DIR/collector.log" "$LOG_DIR/collector-stderr.log" 2>/dev/null
}
cmd_help() {
printf '\033[?25l' # hide cursor
printf '\033[2J\033[H' # clear screen
# ── Matrix decode effect for WORK:TRACKER ──
local title="WORK:TRACKER"
local len=${#title}
local glyphs='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#$%&*!?=+<>{}[]~^'
local -a matrix_colors=(
'\033[38;5;22m' # dark green
'\033[38;5;28m' # medium green
'\033[38;5;34m' # green
'\033[38;5;40m' # bright green
'\033[38;5;46m' # neon green
'\033[38;5;82m' # light green
'\033[38;5;118m' # yellow-green
)
local n_colors=${#matrix_colors[@]}
local -a resolved=()
for (( i=0; i<len; i++ )); do resolved[$i]=0; done
local iterations=12
printf "\n "
# print initial random chars
for (( i=0; i<len; i++ )); do
local rc="${matrix_colors[$(( RANDOM % n_colors ))]}"
local rg="${glyphs:$(( RANDOM % ${#glyphs} )):1}"
printf "${rc}${BOLD}%s${RST}" "$rg"
done
# decode loop: each iteration resolves 1-2 more characters
local resolved_count=0
for (( iter=0; iter<iterations && resolved_count<len; iter++ )); do
sleep 0.08
# resolve 1-2 random unresolved positions
local to_resolve=$(( (iter > 6) ? 2 : 1 ))
for (( t=0; t<to_resolve && resolved_count<len; t++ )); do
# pick random unresolved position
while true; do
local pos=$(( RANDOM % len ))
if [[ ${resolved[$pos]} -eq 0 ]]; then
resolved[$pos]=1
(( resolved_count++ ))
break
fi
done
done
# redraw entire title
tput cup 1 2
for (( i=0; i<len; i++ )); do
local ch="${title:$i:1}"
if [[ ${resolved[$i]} -eq 1 ]]; then
if [[ "$ch" == ":" ]]; then
printf "${RED}${BOLD}%s${RST}" "$ch"
else
printf "${CYAN}${BOLD}%s${RST}" "$ch"
fi
else
local rc="${matrix_colors[$(( RANDOM % n_colors ))]}"
local rg="${glyphs:$(( RANDOM % ${#glyphs} )):1}"
printf "${rc}${BOLD}%s${RST}" "$rg"
fi
done
done
# final clean render
tput cup 1 2
for (( i=0; i<len; i++ )); do
local ch="${title:$i:1}"
if [[ "$ch" == ":" ]]; then
printf "${RED}${BOLD}%s${RST}" "$ch"
else
printf "${CYAN}${BOLD}%s${RST}" "$ch"
fi
done
printf " ${GRAY}for mac${RST} \033[38;5;196m${RST}"
sleep 0.15
echo ""
printf " ${GRAY_L}made in Austria with ${RED}<3${GRAY_L} by ${CYAN}${BOLD}peab.at${RST}\n"
echo ""
printf " ${WHITE}${BOLD}Usage:${RST} ${CYAN}wt${RST} ${WHITE}<command>${RST}\n"
echo ""
# Helper: print one help row with fixed column widths so aliases line up.
# Widths: cmd=10, desc=42, alias=right-aligned column.
help_row() {
local cmd="$1" desc="$2" alias="$3"
printf " ${WHITE}${BOLD}%-10s${RST} ${GRAY_L}%-42s${RST} ${ORANGE}%s${RST}\n" \
"$cmd" "$desc" "$alias"
}
printf " ${CYAN}${BOLD}Overview${RST}\n"
help_row "status" "Show services, data & reports" "wts"
help_row "tail" "Follow collector logs live" "wtl"
echo ""
printf " ${CYAN}${BOLD}Collector${RST}\n"
help_row "restart" "Restart collector daemon" "wtr"
help_row "start" "Start collector" ""
help_row "stop" "Stop collector" ""
echo ""
printf " ${CYAN}${BOLD}Aggregator${RST}\n"
help_row "daily" "Run daily aggregation" "wtd"
help_row "weekly" "Run weekly aggregation" "wtw"
help_row "monthly" "Run monthly aggregation" "wtm"
help_row "all" "Run all aggregations" "wtx"
help_row "past" "Backfill missing daily/weekly reports" "wtp"
help_row "reprocess" "Re-run all days with current patterns" "wtrp"
echo ""
printf " ${CYAN}${BOLD}Dashboard${RST}\n"
help_row "dash" "Open terminal dashboard" "wtdash"
help_row "rhythm" "Weekly activity heatmap" "wtrh"
help_row "web" "Start web dashboard (port 7880)" "wtweb"
help_row "menubar" "Start macOS menubar widget" "wtmb"
help_row "docs" "Open documentation in browser" "wtdocs"
echo ""
printf " ${CYAN}${BOLD}Maintenance${RST}\n"
help_row "compress" "Shrink screenshots PNG → JPEG q75" "wtcmp"
echo ""
printf " ${GRAY_L}wtrl${RST} ${GRAY}= reload shell (activate aliases)${RST}\n"
echo ""
# ── Blinking colon (cyan/red) + Rainbow Apple ──
trap 'printf "\033[?25h\033[0m"; stty echo 2>/dev/null; exit 0' INT
stty -echo 2>/dev/null
local colon_col=$(( 2 + 4 )) # indent + "WORK" = position of ":"
local apple_col=$(( 2 + len + 2 + 8 )) # after "for mac "
local colon_colors=("${RED}" "${CYAN}")
local rainbow=('\033[38;5;196m' '\033[38;5;208m' '\033[38;5;226m' '\033[38;5;46m' '\033[38;5;33m' '\033[38;5;93m')
local ci=0
local ai=0
while true; do
local cc="${colon_colors[$ci]}"
local ac="${rainbow[$ai]}"
tput cup 1 $colon_col; printf "${cc}${BOLD}:${RST}"
tput cup 1 $apple_col; printf "${ac}${RST}"
ci=$(( (ci + 1) % 2 ))
ai=$(( (ai + 1) % ${#rainbow[@]} ))
if read -t 1 -n 1 -s 2>/dev/null; then
break
fi
done
stty echo 2>/dev/null
printf '\033[?25h\033[0m'
echo ""
}
cmd_start() {
header "COLLECTOR START"
msg_info "Loading and starting collector..."
launchctl load "$PLIST_COL" 2>/dev/null || true
launchctl start "$LABEL_COL"
sleep 1
local result
result=$(svc_status "$LABEL_COL")
local state pid
IFS='|' read -r state pid _ <<< "$result"
if [[ "$state" == "running" ]]; then
msg_ok "Collector running — PID ${BOLD}${pid}${RST}"
else
msg_warn "Collector loaded, waiting to start"
fi
echo ""
}
cmd_stop() {
header "COLLECTOR STOP"
msg_info "Stopping collector..."
launchctl stop "$LABEL_COL" 2>/dev/null || true
launchctl unload "$PLIST_COL" 2>/dev/null || true
msg_ok "Collector stopped"
echo ""
}
cmd_docs() {
local docs_file="$BASE/docs/index.html"
if [[ -f "$docs_file" ]]; then
msg_info "Opening documentation..."
open "$docs_file"
else
msg_err "Documentation not found: ${WHITE}$docs_file${RST}"
fi
}
cmd_reprocess() {
header "REPROCESS SESSIONS"
local snap_dir="$BASE/data/snapshots"
local dates=()
# Collect all dates with snapshot files
for f in "$snap_dir"/*.jsonl; do
[[ -f "$f" ]] || continue
local d
d=$(basename "$f" .jsonl)
dates+=("$d")
done
if [[ ${#dates[@]} -eq 0 ]]; then
msg_err "No snapshot data found"
echo ""
return
fi
# Sort dates
IFS=$'\n' dates=($(sort <<<"${dates[*]}")); unset IFS
msg_info "Reprocessing ${WHITE}${BOLD}${#dates[@]}${RST} days with current patterns..."
echo ""
local ok=0
local fail=0
local total_inherited=0
local total_topics=0
local total_sessions=0
for d in "${dates[@]}"; do
# Aggregator writes live progress on stderr (inherits terminal).
# stdout is captured for the [SUMMARY] line.
local agg_out
if agg_out=$("$VENV" "$DAEMON/aggregator.py" --mode daily --date "$d" --progress --tag "$d"); then
local summary sess inh topics
summary=$(echo "$agg_out" | grep '^\[SUMMARY\]' | head -1 || true)
sess=$(echo "$summary" | grep -oE 'sessions=[0-9]+' | cut -d= -f2 || true)
inh=$(echo "$summary" | grep -oE 'inherited=[0-9]+' | cut -d= -f2 || true)
topics=$(echo "$summary" | grep -oE 'topics=[0-9]+' | cut -d= -f2 || true)
total_sessions=$((total_sessions + ${sess:-0}))
total_inherited=$((total_inherited + ${inh:-0}))
total_topics=$((total_topics + ${topics:-0}))
((ok++))
else
printf " ${GRAY_L}%s${RST} ${RED}${SYM_CROSS}${RST} aggregation failed\n" "$d"
((fail++))
fi
done
echo ""
msg_ok "Reprocessed: ${WHITE}${BOLD}${ok}${RST} OK, ${fail} failed"
msg_info "Total: ${WHITE}${BOLD}${total_sessions}${RST} sessions, ${WHITE}${BOLD}${total_inherited}${RST} inherited, ${WHITE}${BOLD}${total_topics}${RST} topics"
echo ""
}
cmd_backfill() {
header "BACKFILL MISSING REPORTS"
local snap_dir="$BASE/data/snapshots"
local daily_dir="$SUMMARIES/daily"
local weekly_dir="$SUMMARIES/weekly"
local today
today=$(date '+%Y-%m-%d')
# ── Collect snapshot dates (exclude today — today's report runs at 22:00) ──
local snap_dates=()
for f in "$snap_dir"/*.jsonl; do
[[ -f "$f" ]] || continue
local d
d=$(basename "$f" .jsonl)
[[ "$d" == "$today" ]] && continue
snap_dates+=("$d")
done
IFS=$'\n' snap_dates=($(sort <<<"${snap_dates[*]}")); unset IFS
if [[ ${#snap_dates[@]} -eq 0 ]]; then
msg_info "Keine vergangenen Snapshots gefunden"
echo ""
return
fi
# ── Check missing daily reports ──
section "Tagesberichte"
local missing_daily=()
for d in "${snap_dates[@]}"; do
if [[ ! -f "$daily_dir/${d}.md" ]]; then
missing_daily+=("$d")
fi
done
if [[ ${#missing_daily[@]} -eq 0 ]]; then
msg_ok "Alle ${WHITE}${BOLD}${#snap_dates[@]}${RST} Tagesberichte vorhanden"
else
msg_warn "${WHITE}${BOLD}${#missing_daily[@]}${RST} fehlende Tagesberichte gefunden"
echo ""
local ok=0
local fail=0
local total_sessions=0
local total_inherited=0
local total_topics=0
for d in "${missing_daily[@]}"; do
local agg_out
if agg_out=$("$VENV" "$DAEMON/aggregator.py" --mode daily --date "$d" --progress --tag "$d"); then
local summary sess inh topics
summary=$(echo "$agg_out" | grep '^\[SUMMARY\]' | head -1 || true)
sess=$(echo "$summary" | grep -oE 'sessions=[0-9]+' | cut -d= -f2 || true)
inh=$(echo "$summary" | grep -oE 'inherited=[0-9]+' | cut -d= -f2 || true)
topics=$(echo "$summary" | grep -oE 'topics=[0-9]+' | cut -d= -f2 || true)
total_sessions=$((total_sessions + ${sess:-0}))
total_inherited=$((total_inherited + ${inh:-0}))
total_topics=$((total_topics + ${topics:-0}))
((ok++))
else
printf " ${GRAY_L}%s${RST} ${RED}${SYM_CROSS} fehlgeschlagen${RST}\n" "$d"
((fail++))
fi
done
echo ""
if [[ $fail -eq 0 ]]; then
msg_ok "Alle ${WHITE}${BOLD}${ok}${RST} fehlenden Tagesberichte nachgeholt"
else
msg_warn "${WHITE}${BOLD}${ok}${RST} OK, ${RED}${fail}${RST} fehlgeschlagen"
fi
msg_info "Total: ${WHITE}${BOLD}${total_sessions}${RST} sessions, ${WHITE}${BOLD}${total_inherited}${RST} inherited, ${WHITE}${BOLD}${total_topics}${RST} topics"
fi
# ── Check missing weekly reports ──
section "Wochenberichte"
# Collect unique ISO weeks from snapshot dates (exclude current incomplete week)
local current_week
current_week=$(date "+%G-W%V")
local all_weeks
all_weeks=$(for d in "${snap_dates[@]}"; do
date -j -f "%Y-%m-%d" "$d" "+%G-W%V" 2>/dev/null
done | sort -u | grep -v "^${current_week}$" || true)
local missing_weekly=()
local total_weeks=0
for wk in $all_weeks; do
((total_weeks++))
if [[ ! -f "$weekly_dir/${wk}.md" ]]; then
missing_weekly+=("$wk")
fi
done
if [[ ${#missing_weekly[@]} -eq 0 ]]; then
msg_ok "Alle ${WHITE}${BOLD}${total_weeks}${RST} Wochenberichte vorhanden"
else
msg_warn "${WHITE}${BOLD}${#missing_weekly[@]}${RST} fehlende Wochenberichte gefunden"
echo ""
local ok=0
local fail=0
for wk in "${missing_weekly[@]}"; do
# Extract a date from that week (Monday) to pass to aggregator
local iso_year="${wk%%-W*}"
local iso_wnum="${wk##*W}"
# Get Monday of that ISO week
local monday
monday=$("$VENV" -c "from datetime import datetime; d=datetime.fromisocalendar($iso_year,$((10#$iso_wnum)),1); print(d.strftime('%Y-%m-%d'))")
printf " ${GRAY_L}%s${RST} ${GRAY}(%s)${RST} " "$wk" "$monday"
if "$VENV" "$DAEMON/aggregator.py" --mode weekly --date "$monday" >/dev/null 2>&1; then
printf "${GREEN}${SYM_CHECK} generiert${RST}\n"
((ok++))
else
printf "${RED}${SYM_CROSS} fehlgeschlagen${RST}\n"
((fail++))
fi
done
echo ""
if [[ $fail -eq 0 ]]; then
msg_ok "Alle ${WHITE}${BOLD}${ok}${RST} fehlenden Wochenberichte nachgeholt"
else
msg_warn "${WHITE}${BOLD}${ok}${RST} OK, ${RED}${fail}${RST} fehlgeschlagen"
fi
fi
echo ""
}
cmd_compress() {
header "SCREENSHOT COMPRESSION"
local script="$DAEMON/compress_screenshots.sh"
if [[ ! -x "$script" ]]; then
msg_err "Skript fehlt oder nicht ausführbar: ${DIM}$script${RST}"
exit 1
fi
msg_info "PNG → JPEG q75 (macOS sips)"
echo ""
local start_time
start_time=$(perl -MTime::HiRes=time -e 'printf "%.3f", time')
local exit_code=0
"$script" "$@" || exit_code=$?
local end_time elapsed_ms
end_time=$(perl -MTime::HiRes=time -e 'printf "%.3f", time')
elapsed_ms=$(perl -e "printf '%d', ($end_time - $start_time) * 1000")
echo ""
if [[ "$exit_code" -eq 0 ]]; then
msg_ok "Kompression fertig $(badge_ok "${elapsed_ms}ms")"
else
msg_err "Kompression mit Fehlern beendet (exit $exit_code)"
exit "$exit_code"
fi
echo ""
}
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Main
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Timing (ms precision via perl, always available on macOS)
_wt_t0=$(perl -MTime::HiRes=time -e 'printf "%.3f", time')
_wt_timed=true
case "${1:-help}" in
s|status) cmd_status;;
r|restart) cmd_restart;;
start) cmd_start;;
stop) cmd_stop;;
d|daily) cmd_agg daily;;
w|weekly) cmd_agg weekly;;
m|monthly) cmd_agg monthly;;
dash) _wt_timed=false; cmd_dashboard;;
rhythm|rh) cmd_rhythm;;
web) _wt_timed=false; cmd_web;;
menubar|mb) _wt_timed=false; cmd_menubar;;
past|p) cmd_backfill;;
reprocess|rp) cmd_reprocess;;
compress|cmp) cmd_compress "${@:2}";;
docs|docu|documentation) cmd_docs;;
tail|log) _wt_timed=false; cmd_tail;;
h|help|-h|--help) _wt_timed=false; cmd_help;;
*)
_wt_timed=false
printf "${RED}Unknown command: ${WHITE}${BOLD}%s${RST}\n" "$1"
printf "${GRAY_L}Run ${WHITE}wt help${GRAY_L} for usage info${RST}\n"
exit 1
;;
esac
if [[ "$_wt_timed" == "true" ]]; then
_wt_t1=$(perl -MTime::HiRes=time -e 'printf "%.3f", time')
_wt_elapsed=$(perl -e "printf '%.3f', $_wt_t1 - $_wt_t0")
printf " ${GRAY}${DIM}done in ${_wt_elapsed}s${RST}\n"
fi