~kris/dots

srice

ref: e9b48d06a8541f3eda5c4db90382ab3c77183afb srice/.local/bin/dmenu/dlog -rwxr-xr-x 9.0 KiB
e9b48d06 — Kris Yotam xprofile: systemd-aware pipewire start + blueman-applet; sb-internet: tolerate missing /proc/net/wireless 2 months ago
                                                                                
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
#!/usr/bin/env bash
###############################################################################
# dlog — Reading log & completed content manager via dmenu
#
# Writes to krisyotam.com media.db (SQLite)
# Tables: reading_log, reading_books, reading_audiobooks, reading_blogs,
#         reading_essays, reading_papers, reading_verse, short_stories
#
# Maintainer:   Kris Yotam <krisyotam@pm.me>
# License:      MIT
# Created:      2026-02-18
###############################################################################

DB="$HOME/dev/krisyotam.com/public/data/media.db"

# ── Helpers ────────────────────────────────────────────────────

ask()    { printf '' | dmenu -p "$1"; }
pick()   { dmenu -i -l "${2:-10}" -p "$1"; }
esc()    { printf '%s' "$1" | sed "s/'/''/g"; }
today()  { date +%Y-%m-%d; }

# Duration: >99m displays as Xh Ym
fmt_dur() {
    local m="$1"
    if [ "$m" -gt 99 ] 2>/dev/null; then
        printf '%dh %dm' "$((m / 60))" "$((m % 60))"
    else
        printf '%dm' "$m"
    fi
}

sql() { sqlite3 "$DB" "$1"; }

# ── Log Entry (reading_log) ───────────────────────────────────

log_entry() {
    local dt title author type minutes

    dt=$(echo "$(today)" | pick "󰃭  Date:")
    [ -z "$dt" ] && return

    title=$(ask "󰗴  Title:")
    [ -z "$title" ] && return

    author=$(ask "󰏪  Author:")
    [ -z "$author" ] && return

    type=$(printf '%s\n' \
        "󰂽  Book" \
        "󰋋  Audiobook" \
        "󰖟  Blog Post" \
        "󰏫  Short Story" \
        "󰎈  Verse" \
        "󰧮  Essay" \
        "󰈙  Paper" \
    | pick "󰏗  Type:" 7)
    [ -z "$type" ] && return
    type=$(echo "$type" | sed 's/^[^ ]* *//')

    # Offer "+ Audiobook" if primary type isn't already Audiobook
    if [ "$type" != "Audiobook" ]; then
        local also
        also=$(printf '%s\n' "󰜺  No" "󰋋  + Audiobook" \
            | pick "󰋋  Also listening?" 2)
        [[ -n "$also" && "$also" == *Audiobook* ]] && type="$type + Audiobook"
    fi

    minutes=$(printf '%s\n' 5 10 15 20 30 45 60 90 120 180 \
        | pick "󱑂  Minutes:" 10)
    [ -z "$minutes" ] && return

    local dur
    dur=$(fmt_dur "$minutes")

    local ok
    ok=$(printf '%s\n' "󰄬  Confirm" "󰜺  Cancel" \
        | pick "$title · $author · $type · $dur" 2)
    [[ -z "$ok" || "$ok" == *Cancel* ]] && return

    sql "INSERT INTO reading_log (date, title, author, type, minutes)
         VALUES ('$(esc "$dt")', '$(esc "$title")', '$(esc "$author")',
                 '$(esc "$type")', $minutes);"

    notify-send "dlog" "󰎚  Logged: $title ($dur)"
}

# ── Completed: Books ──────────────────────────────────────────

add_book() {
    local title subtitle author cover link

    title=$(ask "󰂽  Title:")
    [ -z "$title" ] && return
    subtitle=$(ask "󰂽  Subtitle:")
    author=$(ask "󰏪  Author:")
    [ -z "$author" ] && return
    cover=$(ask "󰋩  Cover URL:")
    link=$(ask "󰌹  Link:")

    sql "INSERT INTO reading_books (title, subtitle, author, cover, link)
         VALUES ('$(esc "$title")', '$(esc "$subtitle")', '$(esc "$author")',
                 '$(esc "$cover")', '$(esc "$link")');"

    notify-send "dlog" "󰂽  Added: $title"
}

# ── Completed: Audiobooks ─────────────────────────────────────

add_audiobook() {
    local title subtitle author cover link

    title=$(ask "󰋋  Title:")
    [ -z "$title" ] && return
    subtitle=$(ask "󰋋  Subtitle:")
    author=$(ask "󰏪  Author:")
    [ -z "$author" ] && return
    cover=$(ask "󰋩  Cover URL:")
    link=$(ask "󰌹  Link:")

    sql "INSERT INTO reading_audiobooks (title, subtitle, author, cover, link)
         VALUES ('$(esc "$title")', '$(esc "$subtitle")', '$(esc "$author")',
                 '$(esc "$cover")', '$(esc "$link")');"

    notify-send "dlog" "󰋋  Added: $title"
}

# ── Completed: Blog Posts ─────────────────────────────────────

add_blog() {
    local title author src arc year

    title=$(ask "󰖟  Title:")
    [ -z "$title" ] && return
    author=$(ask "󰏪  Author:")
    src=$(ask "󰌹  Source URL:")
    arc=$(ask "󰀼  Archive URL:")
    year=$(ask "󰃭  Year:")

    sql "INSERT INTO reading_blogs (title, author, source_link, archive_link, publication_year)
         VALUES ('$(esc "$title")', '$(esc "$author")', '$(esc "$src")',
                 '$(esc "$arc")', ${year:-NULL});"

    notify-send "dlog" "󰖟  Added: $title"
}

# ── Completed: Short Stories ──────────────────────────────────

add_short_story() {
    local title author year

    title=$(ask "󰏫  Title:")
    [ -z "$title" ] && return
    author=$(ask "󰏪  Author:")
    [ -z "$author" ] && return
    year=$(ask "󰃭  Year:")

    sql "INSERT INTO short_stories (title, author, publication_year)
         VALUES ('$(esc "$title")', '$(esc "$author")', ${year:-NULL});"

    notify-send "dlog" "󰏫  Added: $title"
}

# ── Completed: Verse ──────────────────────────────────────────

add_verse() {
    local title author vtype src year

    title=$(ask "󰎈  Title:")
    [ -z "$title" ] && return
    author=$(ask "󰏪  Author:")
    [ -z "$author" ] && return

    vtype=$(printf '%s\n' \
        "lyric poem" "narrative poem" "ballad" "sonnet" "epigram" \
        "haiku" "ode" "elegy" "free verse" "epic" "limerick" "villanelle" \
    | pick "󰎈  Form:" 12)

    src=$(ask "󰌹  Source URL:")
    year=$(ask "󰃭  Year:")

    sql "INSERT INTO reading_verse (title, author, verse_type, source_link, publication_year)
         VALUES ('$(esc "$title")', '$(esc "$author")', '$(esc "$vtype")',
                 '$(esc "$src")', ${year:-NULL});"

    notify-send "dlog" "󰎈  Added: $title"
}

# ── Completed: Essays ─────────────────────────────────────────

add_essay() {
    local title author src arc year

    title=$(ask "󰧮  Title:")
    [ -z "$title" ] && return
    author=$(ask "󰏪  Author:")
    [ -z "$author" ] && return
    src=$(ask "󰌹  Source URL:")
    arc=$(ask "󰀼  Archive URL:")
    year=$(ask "󰃭  Year:")

    sql "INSERT INTO reading_essays (title, author, source_link, archive_link, publication_year)
         VALUES ('$(esc "$title")', '$(esc "$author")', '$(esc "$src")',
                 '$(esc "$arc")', ${year:-NULL});"

    notify-send "dlog" "󰧮  Added: $title"
}

# ── Completed: Papers ─────────────────────────────────────────

add_paper() {
    local title src arc year
    local authors=()

    title=$(ask "󰈙  Title:")
    [ -z "$title" ] && return

    # Multi-author loop
    while true; do
        local a
        a=$(printf '%s\n' "󰄬  Done adding authors" \
            | pick "󰏪  Author $((${#authors[@]} + 1)):" 1)
        [[ -z "$a" || "$a" == *Done* ]] && break
        authors+=("$a")
    done
    [ ${#authors[@]} -eq 0 ] && return

    # Build JSON array for author field
    local json='['
    for i in "${!authors[@]}"; do
        [ "$i" -gt 0 ] && json+=','
        json+="\"$(esc "${authors[$i]}")\""
    done
    json+=']'

    src=$(ask "󰌹  Source URL:")
    arc=$(ask "󰀼  Archive URL:")
    year=$(ask "󰃭  Year:")

    sql "INSERT INTO reading_papers (title, author, source_link, archive_link, publication_year)
         VALUES ('$(esc "$title")', '$(esc "$json")', '$(esc "$src")',
                 '$(esc "$arc")', ${year:-NULL});"

    notify-send "dlog" "󰈙  Added: $title (${#authors[@]} authors)"
}

# ── Completed Submenu ─────────────────────────────────────────

completed_menu() {
    local choice
    choice=$(printf '%s\n' \
        "󰂽  Books" \
        "󰋋  Audiobooks" \
        "󰖟  Blog Posts" \
        "󰏫  Short Stories" \
        "󰎈  Verse" \
        "󰧮  Essays" \
        "󰈙  Papers" \
    | pick "󰄲  Completed" 7)

    [ -z "$choice" ] && return

    case "$choice" in
        *Books*)      add_book ;;
        *Audiobooks*) add_audiobook ;;
        *Blog*)       add_blog ;;
        *Short*)      add_short_story ;;
        *Verse*)      add_verse ;;
        *Essays*)     add_essay ;;
        *Papers*)     add_paper ;;
    esac
}

# ── Main ──────────────────────────────────────────────────────

main=$(printf '%s\n' \
    "󰎚  Log" \
    "󰄲  Completed" \
| pick "󰎚  dlog" 2)

[ -z "$main" ] && exit 0

case "$main" in
    *Log*)       log_entry ;;
    *Completed*) completed_menu ;;
esac