-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoc_sessions
More file actions
executable file
·48 lines (39 loc) · 1.45 KB
/
oc_sessions
File metadata and controls
executable file
·48 lines (39 loc) · 1.45 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
#!/bin/bash
# Directory containing session files
SESSION_DIR="$HOME/.local/share/opencode/storage/session/"
# Accumulate output
output=""
# Determine date command
if date --version 2>/dev/null | grep -q GNU; then
date_cmd="date"
elif command -v gdate >/dev/null 2>&1; then
date_cmd="gdate"
else
echo "GNU date is not installed. Date will be incorrect." >&2
date_cmd="date"
fi
# Function to convert Unix epoch milliseconds to YYYY/MM/DD HH:MM
convert_time() {
local epoch_ms=$1
local epoch_s=$((epoch_ms / 1000))
$date_cmd -d "@$epoch_s" +"%Y/%m/%d %H:%M" 2>/dev/null || echo "Invalid time"
}
# Accumulate markdown table header
output+="| id | directory | title | time |\n"
output+="|----|-----------|-------|------|\n"
# Find all JSON files
files=()
while IFS= read -r -d '' file; do
files+=("$file")
done < <(find "$SESSION_DIR" -name "*.json" -type f -print0)
# Accumulate sorted table rows using jq
output+=$(jq -r -s --arg home "$HOME" 'map(select(.time.created)) | sort_by(.time.created)[] | "| \(.id) | \(.directory | if startswith($home) then "~" + .[$home | length:] else . end) | \(.title) | \((.time.created / 1000) | strftime("%Y/%m/%d %H:%M")) |"' "${files[@]}")
# Get terminal width
width=$(tput cols || echo 80)
# Output through glow if available
if command -v glow >/dev/null 2>&1; then
echo -e "$output" | glow -w "$width"
else
echo "glow is not installed. Install it for better formatting." >&2
echo -e "$output"
fi