aboutsummaryrefslogtreecommitdiff
path: root/functions/mark.fish
blob: 3afc12fb4a931599dec10ce19a9299f559a505e9 (plain)
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
# Copyright (c) 2017-2019 Florian Fischer. All rights reserved.
# Use of this source code is governed by a MIT license found in the LICENSE file.

function __mark_find_store --argument store
	if test -n "$store" -a -f "$store"
		echo $store
		return
	end

	if set -q MARK_STORE
		echo $MARK_STORE
		return
	end

	for dir in $XDG_DATA_DIRS
		if test -f $dir/mark/marks
			echo $dir/mark/marks
			return
		end
	end

	set xdg_data_home $XDG_DATA_HOME
	if test -z $xdg_data_home
		set xdg_data_home ~/.local/share
	end
	if test -f $xdg_data_home/mark/marks
		echo $xdg_data_home/mark/marks
		return
	end

	if test -f ~/.marks
		echo ~/.marks
		return
	end

	echo "Creating new mark file at $xdg_data_home/mark/marks" >&2
	mkdir -p $xdg_data_home/mark
	__mark_new_year_block (date +%Y) > $xdg_data_home/mark/marks
	echo $xdg_data_home/mark/marks
end

function __mark_new_year_block --argument year
	echo "$year":
	echo "01: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "02: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "03: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "04: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "05: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "06: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "07: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "08: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "09: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "10: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "11: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
	echo "12: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
end

function __mark_years_in_store
	grep -o "....:" (__mark_find_store) | sed "s/.//5"
end

function __mark_print_year --argument year
	grep --color=never -A12 $year (__mark_find_store)
end

function __mark_print_month --argument year month
	__mark_print_year $year | sed -n (math $month +1)"p"
end

function __mark_print_date --argument date
	set date (string split "-" $date)
	__mark_print_month $date[1] $date[2] | cut -d' ' -f(math $date[3] + 1)
end

function __mark_print_dates --argument date before after
	if test -n "$before" -a "$before" -gt 0
		for b in (seq 1 $before | tac)
			set cur_date (date -I -d "$date - $b days")
			__mark_print_date $cur_date
		end
	end

	__mark_print_date $date

	if test -n "$after" -a "$after" -gt 0
		for a in (seq 1 $after)
			set cur_date (date -I -d "$date + $a days")
			__mark_print_date $cur_date
		end
	end
end

function __mark_append_mark --argument mark date
	set date_a (string split "-" $date)
	set year $date_a[1]
	set month $date_a[2]
	set day $date_a[3]

	for y in (__mark_years_in_store)
		if test "$y" != $year
			__mark_print_year $y
		else
			# set first mark
			if test (__mark_print_date $date) = "?"
				__mark_print_year $year | sed (math $month + 1)"s/ [^[:space:]]*/ $mark/"$day
			# append to marks
			else
				__mark_print_year $year | sed (math $month + 1)"s/ [^[:space:]]*/&$mark/"$day
			end
		end
		echo
	end
end

function __mark_count --argument mark
	if test -z "$mark"
		echo "No mark to count" >&2
		return 1
	end

	grep -v '[1-9][0-9][0-9][0-9]:' (__mark_find_store) | sed -r 's/^.{3}//' | grep -o $mark | wc -l
	return 0
end

function __mark_print_usage
	echo "Usage:	mark [cmd | <mark>] date [cmd args]"
	echo "Cmds:"
	echo "	print [before] [after]: print marks in range [date-before, date-after]"
	echo "	edit: open the mark store file in an editor or using xdg-open"
	echo "	count <mark>: count the occurrences of <mark> in the mark store"
end

function mark --argument cmd date
	if test -z "$cmd"
		echo "No cmd specified"
		__mark_print_usage
		return 1
	end

	if test -n "$argv[2]"
		if not date -d "$argv[2]" > /dev/null 2>&1
			echo $argv[2] " is not a valid date" >&2
		end
		set date "$argv[2]"
	else
		set date (date -I)
	end

	switch "$cmd"
		case "print"
			__mark_print_dates $date $argv[3] $argv[4]
		case "--help" "-h"
			__mark_print_usage
		case "count"
			__mark_count $argv[2]
		case "edit" "e"
			set store (__mark_find_store)
			if test ! -z "$VISUAL"
				eval $VISUAL $store
			else if test ! -z "EDITOR"
				eval $EDITOR $store
			else if type -q "xdg-open"
				xdg-open $store
			else
				echo "Don't know how to open marks."
				echo "Please set VISUAL or EDITOR in your environment."
			end
		case "*"
			set store (__mark_find_store)

			cp $store $store.bak

			__mark_append_mark $cmd $date > $store.new

			mv $store.new $store
	end
	return $status
end