Skip to content

Commit 1ee783b

Browse files
committed
Added wcal
This uses ncal to provide a single month calendar with week numbers
1 parent 97eb2cb commit 1ee783b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

wcal

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#! /bin/sh
2+
# wcal (Bourne shell script) -- Shows a calendar, with week numbers, for a given month
3+
#
4+
# Usage: wcal [ month year | delta ]
5+
# A delta is a single arg with "+" or "-" prefix to adjust relative to currently month
6+
#
7+
# TO-DO:
8+
# + Replace today-highlight that's omitted because stdout is a pipe
9+
10+
case $# in
11+
0) datespec=$(date +%m)/1 ;;
12+
1) # Handle a delta
13+
current_month=$(date +%m)
14+
case $1 in
15+
+*) relative_month=$(expr $current_month + ${1#+})
16+
month=$(expr $relative_month % 12)
17+
# Have to make month 0-based when working out the year delta
18+
year=$(expr $(date +%Y) + $(expr $(expr $relative_month - 1) / 12))
19+
;;
20+
-*) relative_month=$(expr $current_month - ${1#-})
21+
if [ $relative_month -ge 1 ] ; then
22+
month=$relative_month
23+
year=$(date +%Y)
24+
else
25+
# Account for negative values or 0
26+
month=$(expr 12 + $(expr $relative_month % 12))
27+
# Have to increase negative year delta by 1, i.e. subtract 1
28+
year=$(expr $(date +%Y) + $(expr $(expr $relative_month / 12) - 1))
29+
fi
30+
;;
31+
*) echo "Usage: wcal [ month year | delta ]" >&2
32+
exit 1
33+
;;
34+
esac
35+
datespec=$month/1
36+
;;
37+
2) month=$1
38+
year=$2
39+
datespec=$month/1
40+
;;
41+
*) echo "Usage: wcal [ month year | delta ]" >&2
42+
exit 1
43+
;;
44+
esac
45+
46+
# Calculate the week number of the first (partial) week of the month and adjust
47+
# it (to be corrected later) if it's actually the last week of the previous year.
48+
if [ -n "$year" ] ; then
49+
year_prefix=$year/
50+
fi
51+
num=$(date -d "$year_prefix$datespec" +%V)
52+
if [ $num -gt 50 ] ; then
53+
pnum=$num
54+
num=0
55+
fi
56+
57+
ncal -bM $month $year |
58+
sed -e '# Insert nl header & body delimiters before the relevant lines' \
59+
-e '1i\
60+
\\:\\:\\:' -e '3i\
61+
\\:\\:' \
62+
-e '# Strip out blank lines inserted by ncal' \
63+
-e '/^ \+$/ d' |
64+
nl --number-width=3 --number-format=ln --number-separator=' ' \
65+
--starting-line-number=$num |
66+
sed -e '# Modify header to include "wk" column' \
67+
-e '3 s/^../wk/' \
68+
-e '# Strip out blank lines inserted by nl' \
69+
-e '1d' -e '4d' \
70+
-e '# Correct the first week number if necessary' \
71+
-e "s/^0 /$pnum/"

0 commit comments

Comments
 (0)