|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Append text to docs/iavro_forgiving_typing_chatlog.md with a timestamped divider. |
| 5 | +# Sources: stdin (default), --from-file <path>, or --from-clipboard (macOS pbpaste / xclip/xsel). |
| 6 | +# Optional: --commit to auto git-commit the change. |
| 7 | + |
| 8 | +FILE="docs/iavro_forgiving_typing_chatlog.md" |
| 9 | +SOURCE="stdin" |
| 10 | +INPUT_PATH="" |
| 11 | +DO_COMMIT=0 |
| 12 | +TITLE="iAvro Forgiving Typing Project — Chat Log" |
| 13 | +FORCE_APPEND=0 |
| 14 | +LABEL="" |
| 15 | + |
| 16 | +usage() { |
| 17 | + cat <<EOF |
| 18 | +Usage: $0 [--from-file <path> | --from-clipboard] [--label <text>] [--commit] [--force] |
| 19 | +
|
| 20 | +Examples: |
| 21 | + # Append from clipboard (copy chat text first) |
| 22 | + $0 --from-clipboard --label "Codex CLI" --commit |
| 23 | +
|
| 24 | + # Append from a file |
| 25 | + $0 --from-file tmp/chat_append.txt --label "CGE export" |
| 26 | +
|
| 27 | + # Append from stdin |
| 28 | + echo "My note" | $0 --label "Manual note" |
| 29 | +EOF |
| 30 | +} |
| 31 | + |
| 32 | +while [[ $# -gt 0 ]]; do |
| 33 | + case "$1" in |
| 34 | + --from-file) |
| 35 | + SOURCE="file"; INPUT_PATH="${2:-}"; shift 2;; |
| 36 | + --from-clipboard|-c) |
| 37 | + SOURCE="clipboard"; shift;; |
| 38 | + --commit) |
| 39 | + DO_COMMIT=1; shift;; |
| 40 | + --force) |
| 41 | + FORCE_APPEND=1; shift;; |
| 42 | + --label) |
| 43 | + LABEL="${2:-}"; shift 2;; |
| 44 | + --help|-h) |
| 45 | + usage; exit 0;; |
| 46 | + *) |
| 47 | + echo "Unknown argument: $1" >&2; usage; exit 2;; |
| 48 | + esac |
| 49 | +done |
| 50 | + |
| 51 | +# Resolve input |
| 52 | +CONTENT="" |
| 53 | +if [[ "$SOURCE" == "file" ]]; then |
| 54 | + if [[ -z "$INPUT_PATH" || ! -f "$INPUT_PATH" ]]; then |
| 55 | + echo "Error: --from-file requires a valid file path" >&2 |
| 56 | + exit 2 |
| 57 | + fi |
| 58 | + CONTENT=$(cat "$INPUT_PATH") |
| 59 | +elif [[ "$SOURCE" == "clipboard" ]]; then |
| 60 | + if command -v pbpaste >/dev/null 2>&1; then |
| 61 | + CONTENT=$(pbpaste) |
| 62 | + elif command -v xclip >/dev/null 2>&1; then |
| 63 | + CONTENT=$(xclip -o -selection clipboard) |
| 64 | + elif command -v xsel >/dev/null 2>&1; then |
| 65 | + CONTENT=$(xsel --clipboard --output) |
| 66 | + else |
| 67 | + echo "Error: no clipboard tool found (pbpaste/xclip/xsel)" >&2 |
| 68 | + exit 2 |
| 69 | + fi |
| 70 | +else |
| 71 | + # stdin |
| 72 | + if [ -t 0 ]; then |
| 73 | + echo "Reading from stdin... (press Ctrl-D to finish)" >&2 |
| 74 | + fi |
| 75 | + CONTENT=$(cat || true) |
| 76 | +fi |
| 77 | + |
| 78 | +if [[ -z "$CONTENT" ]]; then |
| 79 | + echo "Nothing to append (empty content)." >&2 |
| 80 | + exit 0 |
| 81 | +fi |
| 82 | + |
| 83 | +mkdir -p "docs" |
| 84 | +if [[ ! -f "$FILE" ]]; then |
| 85 | + cat > "$FILE" <<EOF |
| 86 | +# $TITLE |
| 87 | +
|
| 88 | +This file contains the entire discussion history between the user and the assistant, |
| 89 | +covering project planning, design, regression test phrases, and clarifications. |
| 90 | +
|
| 91 | +--- |
| 92 | +
|
| 93 | +## Conversation |
| 94 | +
|
| 95 | +EOF |
| 96 | +fi |
| 97 | + |
| 98 | +STAMP="$(date +"%Y-%m-%d %H:%M:%S %Z")" |
| 99 | + |
| 100 | +# Normalize line endings and compute content hash for idempotency |
| 101 | +CONTENT_NORM=$(printf '%s' "$CONTENT" | tr -d '\r') |
| 102 | +# Normalize label |
| 103 | +LABEL_NORM=$(printf '%s' "$LABEL" | tr -d '\r') |
| 104 | +if command -v shasum >/dev/null 2>&1; then |
| 105 | + CID=$(printf '%s' "$CONTENT_NORM" | shasum -a 256 | awk '{print $1}') |
| 106 | +elif command -v openssl >/dev/null 2>&1; then |
| 107 | + CID=$(printf '%s' "$CONTENT_NORM" | openssl dgst -sha256 -r | awk '{print $1}') |
| 108 | +else |
| 109 | + CID=$(printf '%s' "$CONTENT_NORM" | wc -c | tr -d '[:space:]') # fallback: length |
| 110 | +fi |
| 111 | + |
| 112 | +# Skip if this exact content was already appended (idempotent) |
| 113 | +if [[ $FORCE_APPEND -eq 0 ]] && grep -q "^\[CID:$CID\]$" "$FILE" 2>/dev/null; then |
| 114 | + echo "This content (CID:$CID) is already present in $FILE; skipping." |
| 115 | + exit 0 |
| 116 | +fi |
| 117 | +{ |
| 118 | + echo "\n---\n\nAppended on $STAMP\n[CID:$CID]${LABEL_NORM:+ [LABEL:$LABEL_NORM]}\n\n$CONTENT_NORM\n" |
| 119 | +} >> "$FILE" |
| 120 | + |
| 121 | +echo "Appended chat content to $FILE" |
| 122 | + |
| 123 | +if [[ $DO_COMMIT -eq 1 ]]; then |
| 124 | + if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then |
| 125 | + git add "$FILE" |
| 126 | + git commit -m "docs(chatlog): append conversation at $STAMP" |
| 127 | + echo "Committed chatlog append." |
| 128 | + else |
| 129 | + echo "Not a git repo; skipping commit." >&2 |
| 130 | + fi |
| 131 | +fi |
0 commit comments