~kris/dots

srice

ref: c8d9c68937f2c331a6bac47481ed40147fe73cba srice/.local/bin/dmenu/dwrite -rwxr-xr-x 3.7 KiB
c8d9c689 — Kris Yotam shell migration: fish -> mksh, reorganize bin/, wallpaper cleanup 4 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
#!/usr/bin/env bash
set -euo pipefail

###############################################################################
# DWRITE — dmenu content opener for krisyotam.com
#
# Maintainer:   Kris Yotam <krisyotam@pm.me>
# License:      MIT
# Created:      2026-02-15
# Updated:      2026-02-22
# Description:  Select content type and file via dmenu, open in Typora.
#               TIL and Now entries open $EDITOR for markdown writing, then
#               auto-create the entry via generateMetadata.js on save.
###############################################################################

CONTENT_DIR="$HOME/content"
PROJECT_DIR="$HOME/dev/krisyotam.com"
GEN_META="$PROJECT_DIR/public/scripts/keep/generateMetadata.js"

# Content types with nerd font icons
declare -A TYPE_ICONS=(
    [blog]="󰬞"
    [diary]="󰃮"
    [essays]="󰬝"
    [fiction]="󰂺"
    [news]="󰎕"
    [notes]="󰏪"
    [ocs]="󰏫"
    [papers]="󰈙"
    [progymnasmata]="󰑴"
    [reviews]="󰓥"
    [verse]="󱗝"
    [til]="󰛄"
    [now]="󰔛"
)

CONTENT_TYPES=(blog diary essays fiction news notes ocs papers progymnasmata reviews verse til now)

# Select content type
type_menu() {
    local entries=""
    for t in "${CONTENT_TYPES[@]}"; do
        entries+="${TYPE_ICONS[$t]:-󰈙}  $t"$'\n'
    done
    echo "$entries" | dmenu -i -l 13 -p "write:" | awk '{print $2}'
}

# Select file from content type
file_menu() {
    local type="$1"
    local dir="$CONTENT_DIR/$type"
    [[ ! -d "$dir" ]] && exit 1

    ls "$dir"/*.mdx 2>/dev/null |
        xargs -I{} basename {} .mdx |
        sort |
        dmenu -i -l 20 -p "$type:"
}

# Write a TIL entry
write_til() {
    local tmpfile
    tmpfile="$(mktemp /tmp/dwrite-til-XXXXXX.md)"

    # Prompt for optional title
    local title
    title="$(echo "" | dmenu -p "TIL title (optional):" 2>/dev/null)" || true

    # Auto-generate title from date if skipped
    if [[ -z "$title" ]]; then
        title="$(date +'%B %d, %Y')"
    fi

    # Open editor
    ${EDITOR:-nano} "$tmpfile"

    # Check if user wrote anything
    if [[ ! -s "$tmpfile" ]]; then
        rm -f "$tmpfile"
        notify-send "dwrite" "TIL entry cancelled (empty file)" 2>/dev/null || true
        exit 0
    fi

    # Create entry via generateMetadata.js
    node "$GEN_META" \
        --type til \
        --title "$title" \
        --content-file "$tmpfile" \
        --status "Finished" \
        --certainty "certain" \
        --importance 9 \
        --category "til" \
        --state "active"

    rm -f "$tmpfile"
    notify-send "dwrite" "TIL entry created: $title" 2>/dev/null || true
}

# Write a Now entry
write_now() {
    local tmpfile
    tmpfile="$(mktemp /tmp/dwrite-now-XXXXXX.md)"

    # Auto-generate title from month
    local title
    title="$(date +'%B %Y')"

    # Open editor
    ${EDITOR:-nano} "$tmpfile"

    # Check if user wrote anything
    if [[ ! -s "$tmpfile" ]]; then
        rm -f "$tmpfile"
        notify-send "dwrite" "Now entry cancelled (empty file)" 2>/dev/null || true
        exit 0
    fi

    # Create entry via generateMetadata.js
    node "$GEN_META" \
        --type now \
        --title "$title" \
        --content-file "$tmpfile" \
        --status "Finished" \
        --certainty "certain" \
        --importance 9 \
        --category "now" \
        --state "active"

    rm -f "$tmpfile"
    notify-send "dwrite" "Now entry created: $title" 2>/dev/null || true
}

# Main
type="$(type_menu)"
[[ -z "$type" ]] && exit 0

case "$type" in
    til)
        write_til
        ;;
    now)
        write_now
        ;;
    *)
        file="$(file_menu "$type")"
        [[ -z "$file" ]] && exit 0
        typora "$CONTENT_DIR/$type/$file.mdx" >/dev/null 2>&1 & disown
        ;;
esac