~kris/dots

srice

ref: 2dc7c2f7e39f562e3603676ef50c2df0a4723c19 srice/.local/bin/history -rwxr-xr-x 8.6 KiB
2dc7c2f7 — Kris Yotam update nvim plan9 theme, add hosts file, misc updates 6 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
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
#!/bin/sh
# history - unified history viewer (encrypted with SQLCipher)
# Usage: history -m <mode> [options]

set -e

DB_PATH="$HOME/.local/share/history/history.db"
KEY_FILE="$HOME/.local/share/history/.key"
DEFAULT_LIMIT=100

# Load encryption key
load_key() {
    if [ ! -f "$KEY_FILE" ]; then
        echo "Encryption key not found. Run 'history-init' first." >&2
        exit 1
    fi
    cat "$KEY_FILE"
}

KEY=$(load_key)

# SQLCipher wrapper for queries
sql_query() {
    sqlcipher "$DB_PATH" "PRAGMA key = '$KEY'; $1"
}

sql_json() {
    sqlcipher -json "$DB_PATH" "PRAGMA key = '$KEY'; $1"
}

sql_table() {
    sqlcipher -header -column "$DB_PATH" "PRAGMA key = '$KEY'; $1"
}

sql_raw() {
    sqlcipher -separator '|' "$DB_PATH" "PRAGMA key = '$KEY'; $1"
}

# Colors
C_RESET='\033[0m'
C_DIM='\033[2m'
C_CYAN='\033[36m'
C_GREEN='\033[32m'
C_YELLOW='\033[33m'
C_MAGENTA='\033[35m'

usage() {
    cat <<EOF
history - unified history viewer (encrypted)

USAGE:
    history -m <mode> [OPTIONS]

MODES (-m):
    t, terminal     Terminal command history
    c, clipboard    Clipboard history
    s, search       Browser search history

OPTIONS:
    -o <format>     Output format: t (table), j (json), p (plain) [default: p]
    -n <count>      Number of entries to show [default: 100, 0 for unlimited]
    -d <date>       Filter by date (YYYY.MM.DD)
    -q <query>      Search within entries
    -b <browser>    Filter by browser (search mode only)
    --from <date>   Start date (YYYY.MM.DD)
    --to <date>     End date (YYYY.MM.DD)
    --all           Show all entries (no limit)
    --stats         Show statistics instead of entries
    -h, --help      Show this help

EXAMPLES:
    history -m t                     # Last 100 terminal commands
    history -m t -n 50               # Last 50 terminal commands
    history -m s -b librewolf        # Searches from Librewolf
    history -m c -d 2026.01.23       # Clipboard entries from specific day
    history -m t -q "git"            # Terminal commands containing "git"
    history -m s --from 2026.01.01 --to 2026.01.15
    history -m t --stats             # Terminal statistics
EOF
    exit 0
}

# Check if database exists
if [ ! -f "$DB_PATH" ]; then
    echo "Database not found. Run 'history-init' first." >&2
    exit 1
fi

# Parse arguments
MODE=""
OUTPUT="p"
LIMIT=$DEFAULT_LIMIT
DATE=""
QUERY=""
BROWSER=""
FROM_DATE=""
TO_DATE=""
SHOW_STATS=0

while [ $# -gt 0 ]; do
    case "$1" in
        -m)
            MODE="$2"
            shift 2
            ;;
        -o)
            OUTPUT="$2"
            shift 2
            ;;
        -n)
            LIMIT="$2"
            shift 2
            ;;
        -d)
            DATE="$2"
            shift 2
            ;;
        -q)
            QUERY="$2"
            shift 2
            ;;
        -b)
            BROWSER="$2"
            shift 2
            ;;
        --from)
            FROM_DATE="$2"
            shift 2
            ;;
        --to)
            TO_DATE="$2"
            shift 2
            ;;
        --all)
            LIMIT=0
            shift
            ;;
        --stats)
            SHOW_STATS=1
            shift
            ;;
        -h|--help)
            usage
            ;;
        *)
            echo "Unknown option: $1" >&2
            usage
            ;;
    esac
done

# Validate mode
case "$MODE" in
    t|terminal) MODE="terminal" ;;
    c|clipboard) MODE="clipboard" ;;
    s|search) MODE="search" ;;
    "")
        echo "Error: Mode (-m) is required" >&2
        usage
        ;;
    *)
        echo "Error: Invalid mode '$MODE'" >&2
        usage
        ;;
esac

# Convert date format YYYY.MM.DD to YYYY-MM-DD
convert_date() {
    echo "$1" | tr '.' '-'
}

# Build WHERE clause
build_where() {
    local conditions=""

    if [ -n "$DATE" ]; then
        DATE=$(convert_date "$DATE")
        conditions="date(timestamp) = '$DATE'"
    fi

    if [ -n "$FROM_DATE" ]; then
        FROM_DATE=$(convert_date "$FROM_DATE")
        [ -n "$conditions" ] && conditions="$conditions AND "
        conditions="${conditions}date(timestamp) >= '$FROM_DATE'"
    fi

    if [ -n "$TO_DATE" ]; then
        TO_DATE=$(convert_date "$TO_DATE")
        [ -n "$conditions" ] && conditions="$conditions AND "
        conditions="${conditions}date(timestamp) <= '$TO_DATE'"
    fi

    if [ -n "$QUERY" ]; then
        [ -n "$conditions" ] && conditions="$conditions AND "
        case "$MODE" in
            terminal)
                conditions="${conditions}command LIKE '%$QUERY%'"
                ;;
            clipboard)
                conditions="${conditions}content LIKE '%$QUERY%'"
                ;;
            search)
                conditions="${conditions}query LIKE '%$QUERY%'"
                ;;
        esac
    fi

    if [ -n "$BROWSER" ] && [ "$MODE" = "search" ]; then
        [ -n "$conditions" ] && conditions="$conditions AND "
        conditions="${conditions}browser = '$BROWSER'"
    fi

    [ -n "$conditions" ] && echo "WHERE $conditions" || echo ""
}

# Build LIMIT clause
build_limit() {
    if [ "$LIMIT" -gt 0 ] 2>/dev/null; then
        echo "LIMIT $LIMIT"
    fi
}

# Show statistics
show_stats() {
    case "$MODE" in
        terminal)
            echo "Terminal History Statistics"
            echo "============================"
            sql_query "SELECT 'Total commands: ' || COUNT(*) FROM terminal_history;"
            sql_query "SELECT 'Unique commands: ' || COUNT(DISTINCT command) FROM terminal_history;"
            sql_query "SELECT 'First entry: ' || MIN(timestamp) FROM terminal_history;"
            sql_query "SELECT 'Last entry: ' || MAX(timestamp) FROM terminal_history;"
            echo ""
            echo "Top 10 commands:"
            sql_query "SELECT '  ' || COUNT(*) || 'x  ' || command FROM terminal_history GROUP BY command ORDER BY COUNT(*) DESC LIMIT 10;"
            ;;
        clipboard)
            echo "Clipboard History Statistics"
            echo "============================="
            sql_query "SELECT 'Total entries: ' || COUNT(*) FROM clipboard_history;"
            sql_query "SELECT 'First entry: ' || MIN(timestamp) FROM clipboard_history;"
            sql_query "SELECT 'Last entry: ' || MAX(timestamp) FROM clipboard_history;"
            ;;
        search)
            echo "Search History Statistics"
            echo "========================="
            sql_query "SELECT 'Total searches: ' || COUNT(*) FROM search_history;"
            sql_query "SELECT 'First entry: ' || MIN(timestamp) FROM search_history;"
            sql_query "SELECT 'Last entry: ' || MAX(timestamp) FROM search_history;"
            echo ""
            echo "By browser:"
            sql_query "SELECT '  ' || browser || ': ' || COUNT(*) FROM search_history GROUP BY browser ORDER BY COUNT(*) DESC;"
            echo ""
            echo "Top 10 searches:"
            sql_query "SELECT '  ' || COUNT(*) || 'x  ' || query FROM search_history GROUP BY query ORDER BY COUNT(*) DESC LIMIT 10;"
            ;;
    esac
}

# Format output
format_output() {
    local table="$1"
    local where="$2"
    local limit="$3"

    case "$MODE" in
        terminal)
            FIELDS="timestamp, command, exit_code, pwd"
            ;;
        clipboard)
            FIELDS="timestamp, substr(content, 1, 100) as content"
            ;;
        search)
            FIELDS="timestamp, browser, query"
            ;;
    esac

    SQL="SELECT $FIELDS FROM ${table}_history $where ORDER BY timestamp DESC $limit"

    case "$OUTPUT" in
        j|json)
            sql_json "$SQL"
            ;;
        t|table)
            sql_table "$SQL"
            ;;
        p|plain|*)
            case "$MODE" in
                terminal)
                    sql_raw "$SQL" | while IFS='|' read -r ts cmd code pwd; do
                        printf "${C_DIM}%s${C_RESET}  ${C_GREEN}%s${C_RESET}" "$ts" "$cmd"
                        [ -n "$code" ] && [ "$code" != "0" ] && printf "  ${C_YELLOW}[%s]${C_RESET}" "$code"
                        printf "\n"
                    done
                    ;;
                clipboard)
                    sql_raw "$SQL" | while IFS='|' read -r ts content; do
                        printf "${C_DIM}%s${C_RESET}  %s\n" "$ts" "$content"
                    done
                    ;;
                search)
                    sql_raw "$SQL" | while IFS='|' read -r ts browser query; do
                        printf "${C_DIM}%s${C_RESET}  ${C_MAGENTA}[%s]${C_RESET}  %s\n" "$ts" "$browser" "$query"
                    done
                    ;;
            esac
            ;;
    esac
}

# Main
if [ "$SHOW_STATS" -eq 1 ]; then
    show_stats
else
    WHERE=$(build_where)
    LIMIT_CLAUSE=$(build_limit)
    format_output "$MODE" "$WHERE" "$LIMIT_CLAUSE"
fi