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
|
#!/bin/sh
# shellcheck disable=2046
SCRIPT="$(basename "$0")"
long='type:,git,sessions::,format:,verbose,help'
short='t:gs::f:vh'
if ! opts="$(getopt -o $short -l $long -n "$SCRIPT" -- "$@")"; then
exit 1
fi
eval set -- "$opts"
help() {
cat <<-EOF
$SCRIPT
Toby Vincent <tobyv13@gmail.com>
$SCRIPT
Get a timestamp for a given directory using multiple activity sources. By default, uses stat.
USAGE:
$SCRIPT [OPTION ...] <PATH> [PATH ...]
OPTIONS:
-t, --type=<file|ssh> Specify type of input
-g, --git Check last git commit for timestamp
-s, --sessions=[PATH] Check nvim sessions directory for existing session files.
(DEFAULT="$XDG_DATA_HOME/nvim/sessions")
-f, --format=<FORMAT> Format string for output. '{}' will be replaced directory
timestamp and '{1}' will be replaced directory path
-v, --verbose Increase verbosity
-h, --help Show this help
EOF
}
say() {
printf "%s: %s\n" "$SCRIPT" "$@"
}
say_verbose() {
if [ "$verbose" -gt "0" ]; then
say "$@"
fi
}
say_err() {
say "$@" >&2
}
err() {
err_dir="$1"
shift
say_err "cannot timestamp '$err_dir': $*"
exit 1
}
err_help() {
help
err "$*"
}
verbose=0
type=''
git=false
sessions=false
sessions_dir="$XDG_DATA_HOME/nvim/sessions"
histfile="${HISTFILE:-$XDG_STATE_HOME/zsh/history}"
format="{}"
while true; do
case "$1" in
-h | --help)
help
exit 0
;;
-v | --verbose)
verbose=$((verbose + 1))
shift
;;
-t | --type)
case "$2" in
s*) type="ssh" ;;
f*) type="file" ;;
d*) type="directory" ;;
*) type="$2" ;;
esac
shift 2
;;
-e | --git)
git=true
shift
;;
-s | --sessions)
sessions=true
if [ -n "$2" ]; then
sessions_dir="$2"
fi
shift 2
;;
-f | --format)
format="$2"
shift 2
;;
--)
shift
break
;;
*)
err_help "Invalid argument: $1"
;;
esac
done
if [ "$#" -eq 0 ]; then
IFS='
'
set -o noglob
set -- $(cat)
fi
while [ $# -gt 0 ]; do
ts_max="0"
case "$type" in
ssh)
if ts=$(grep -P "^: \d+:\d;ssh.* $1" "$histfile" | tail -1 | cut -d: -f2 | sed 's/^\s*//'); then
say_verbose "ssh '$1': $ts"
if [ -n "$ts" ] && [ "$ts" -gt "$ts_max" ]; then
ts_max=$ts
fi
fi
;;
file | directory)
if [ ! -d "$1" ]; then
say_err "$1" "No such file or directory"
shift
continue
fi
if ts=$(stat -c "%Y" "$1"); then
say_verbose "stat '$1': $ts"
if [ "$ts" -gt "$ts_max" ]; then
ts_max=$ts
fi
fi
if $git && ts=$(git -C "$1" --no-pager log -1 --all --format="%at" 2>/dev/null); then
say_verbose "git '$1': $ts"
if [ "$ts" -gt "$ts_max" ]; then
ts_max=$ts
fi
fi
pattern=$(printf %s\\n "$1" | sed 's|/|.{1,2}|g')
if $sessions && ts=$(fd "$pattern" "$sessions_dir" -1 --type=f --max-depth=1 -x stat -c "%Y" {} 2>/dev/null); then
say_verbose "session '$1': $ts"
if [ "$ts" -gt "$ts_max" ]; then
ts_max=$ts
fi
fi
;;
esac
printf %s\\n "$format" | sed "s|{}|$ts_max|g" | sed "s|{1}|$1|g"
shift
done
|