-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpickdate
executable file
·232 lines (206 loc) · 6.21 KB
/
pickdate
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
#!/bin/bash
# rofi date picker
#
# requires rofi, cal, awk
# defaults
default_command='echo "$REPLY"'
default_format='%F'
default_cal='cal'
default_buttons=' <:<<: .:>>: >'
default_attrs='weight="bold"'
default_themestr=''
# environment variables
export PICKDATE_CMD="${PICKDATE_CMD-$default_command}"
export PICKDATE_FMT="${PICKDATE_FMT-$default_format}"
export PICKDATE_CAL="${PICKDATE_CAL-$default_cal}"
export PICKDATE_BUTTONS="${PICKDATE_BUTTONS-$default_buttons}"
export PICKDATE_SPANATTRS="${PICKDATE_SPANATTRS-$default_attrs}"
export PICKDATE_THEMESTR="${PICKDATE_THEMESTR-$default_themestr}"
# prints message to stderr
warn() {
local message="$1"
shift
printf "$message\n" "$@" >&2
}
# prints error and exits
die() {
[[ "$1" =~ ^[0-9]+$ ]] && { local retval="$1"; shift; }
warn "$@"
exit ${retval-1}
}
# parse $PICKDATE_BUTTONS
IFS=: read -a b <<< "$PICKDATE_BUTTONS"
buttons=()
(( ${#b[@]} )) && buttons=(
"${b[0]:- }" "${b[1]:- }" " " "${b[2]:- }" " " "${b[3]:- }" "${b[4]:- }"
)
# prints rofi selections from $days part of `cal` output
selections() {
local days="$*"
local w="$2"
for i in {0..6}; do
cut -c $((i*w+1))-$((i*w+w-1)) <<< "$days"
[[ -n "${buttons[i]}" ]] && echo "${buttons[i]}"
done
}
# print usage
prog="${0##*/}"
usage="Usage: $prog [options]
Picks a date with rofi. The following keybindings are available.
p/n Go to previous/next month
P/N, </> Go to previous/next year
. Jump to current month
h/j/k/l Vim-like movement (default movement bindings also work)
Options:
-r COMMAND Run COMMAND with \$REPLY set to the selected date.
-f FORMAT Control the output with a date(1) compatible FORMAT.
-m POSITION Position rofi with respect to the mouse.
-w POSITION Position rofi with respect to the focused window.
-h, --help Print this help message and exit.
Valid POSITIONs: center, north, east, southwest, etc.
"
if [[ "$*" == *--help* ]]; then
die 0 "$usage"
fi
# parse options
action=print
rofiargs=
declare -A anchors
anchors=(
[northwest]=southeast [north]=south [northeast]=southwest
[west]=east [center]=center [east]=west
[southwest]=northeast [south]=north [southeast]=northwest
[top]=northeast [bottom]=southeast
)
while getopts "r:f:m:w:h" opt; do
case $opt in
r) PICKDATE_CMD="$OPTARG" ;;
f) PICKDATE_FMT="$OPTARG" ;;
m)
if [[ "${!anchors[@]}" != *$OPTARG* ]]; then
warn "Invalid position: $OPTARG"
die "Try e.g. 'northwest' or 'center' instead"
fi
rofiargs='-m -3'
PICKDATE_THEMESTR+="window {anchor: ${anchors[$OPTARG]}; location: northwest;}"
;;
w)
if [[ "${!anchors[@]}" != *$OPTARG* ]]; then
warn "Invalid position: $OPTARG"
die "Try e.g. 'northwest' or 'center' instead"
fi
rofiargs='-m -2'
PICKDATE_THEMESTR+="window {anchor: $OPTARG; location: $OPTARG;}"
;;
h) die 0 "$usage" ;;
*) exit 1 ;;
esac
done
shift $((OPTIND-1))
# parse args
(( $# > 1 )) && die "$usage"
# parse $ROFI_INFO
export ROFI_INFO="${ROFI_INFO-$(date +'%-Y-%-m')}"
IFS=- read year month day junk <<< "$ROFI_INFO"
day="$(date +%-d)"
# adjust date if necessary
case "$ROFI_RETV:$1" in
10:*|"1:${b[0]}") # rofi kb-custom-1: go to prev month
year=$(( month-1 < 1 ? year-1 : year))
month=$((month-1 < 1 ? 12 : month-1))
ROFI_RETV=0
;;
11:*|"1:${b[4]}") # rofi kb-custom-2: go to next month
year=$(( month+1 > 12 ? year+1 : year))
month=$((month+1 > 12 ? 1 : month+1))
ROFI_RETV=0
;;
12:*|"1:${b[1]}") # rofi kb-custom-3: go to prev year
year=$(( year-1 ))
ROFI_RETV=0
;;
13:*|"1:${b[3]}") # rofi kb-custom-4: go to next year
year=$(( year+1 ))
ROFI_RETV=0
;;
14:*|"1:${b[2]}") # rofi kb-custom-5: go to current month
IFS=- read year month <<< $(date +'%-Y-%-m')
ROFI_RETV=0
;;
esac
# get days of month
cal="$($PICKDATE_CAL $month $year)"
prompt="$(awk 'NR==1 {print}' <<< "$cal")"
weekdays="$(awk 'NR==2 {print}' <<< "$cal") "
days="$(awk 'NR>2 {print}' <<< "$cal")"
cellwidth=$(( ${#weekdays} / 7 ))
# figure out how the script was called
case "$ROFI_RETV" in
"") # script called directly
# set styling
themestr='* { font: "mono 16"; }
configuration {
kb-custom-1: "p";
kb-custom-2: "n";
kb-custom-3: "P,less";
kb-custom-4: "N,greater";
kb-custom-5: "period";
kb-move-char-back: "h,Left,Control+b";
kb-move-char-forward: "l,Right,Control+f";
kb-row-up: "k,Up,Control+p";
kb-row-down: "j,Down,Control+n";
}'"
$PICKDATE_THEMESTR
window { width: $((${#weekdays}+1))ch; }
listview {
columns: 7;
lines: $(( $(wc -l <<< "$days") + (${#b[@]}?1:0) ));
scrollbar: false;
}
inputbar { children: [prompt]; }
"
# run rofi
selected="$(selections "$days" $cellwidth | cat -n | awk "/\S\s+$day\s*$/ {print \$1 - 1; exit}")"
trap 'rm -f "$_PICKDATE_TMP"' EXIT
export _PICKDATE_TMP="$(mktemp --tmpdir "$prog.XXXXXXXXXX")" || exit 1
rofi $rofiargs -show pickdate -modi "pickdate:$0" \
-selected-row "$selected" \
-theme-str "$themestr"
# do something with the output
read -r < "$_PICKDATE_TMP"
[[ -n "$REPLY" ]] && eval "$PICKDATE_CMD"
;;
0) # rofi: initial call of script
# check if called directly with rofi instead of through pickdate
if [[ -z "$1$_PICKDATE_TMP" ]]; then
warn "Invoked with 'rofi -modi'. To fix alignment issues, run 'pickdate' instead."
fi
# set rofi options
echo -en "\0prompt\x1f$prompt\n"
echo -en "\0message\x1f<span $PICKDATE_SPANATTRS>$weekdays</span>\n"
echo -en "\0no-custom\x1ftrue\n"
echo -en "\0use-hot-keys\x1ftrue\n"
# check if we're displaying current month
currentmonth=0
[[ "$year-$month" == $(date +'%-Y-%-m') ]] && currentmonth=1
# set rofi selections
selections "$days" $cellwidth | awk "
$currentmonth && /^\s*$day\s*$/ {print \"\0active\x1f\" NR-1}
/[0-9]/ {printf \$0 \"\0info\x1f$year-$month\n\"}
/[a-zA-Z]/ {printf \$0 \"\0info\x1f$year-$month\x1fnonselectable\x1ftrue\n\"}
/^\s*$/ {printf \$0 \"\0info\x1f$year-$month\x1fnonselectable\x1ftrue\n\"}
/[^0-9a-zA-Z ]/ {printf \$0 \"\0info\x1f$year-$month\n\"}
"
;;
1) # rofi: selected an entry
day="$1"
if [[ -n "$_PICKDATE_TMP" ]]; then
date -d "$year-$month-$day" +"${PICKDATE_FMT#+}" >> "$_PICKDATE_TMP"
else
date -d "$year-$month-$day" +"Selection: ${PICKDATE_FMT#+}" >&2
fi
;;
2) # rofi: selected a custom entry
die "Unknown entry selected (this is a bug): $1"
;;
esac