@@ -32,15 +32,20 @@ function Calendar.new(data)
32
32
return Calendar
33
33
end
34
34
35
+ local width = 36
36
+ local height = 10
37
+ local x_offset = 1 -- one border cell
38
+ local y_offset = 2 -- one border cell and one padding cell
39
+
35
40
function Calendar .open ()
36
41
local opts = {
37
42
relative = ' editor' ,
38
- width = 36 ,
39
- height = Calendar .clearable and 11 or 10 ,
43
+ width = width ,
44
+ height = Calendar .clearable and height + 1 or height ,
40
45
style = ' minimal' ,
41
46
border = ' single' ,
42
- row = vim .o .lines / 2 - 4 ,
43
- col = vim .o .columns / 2 - 20 ,
47
+ row = vim .o .lines / 2 - ( y_offset + height ) / 2 ,
48
+ col = vim .o .columns / 2 - ( x_offset + width ) / 2 ,
44
49
}
45
50
46
51
Calendar .buf = vim .api .nvim_create_buf (false , true )
@@ -85,7 +90,7 @@ function Calendar.open()
85
90
if Calendar .date then
86
91
search_day = Calendar .date :format (' %d' )
87
92
end
88
- vim .fn .cursor ({ 2 , 0 } )
93
+ vim .fn .cursor (2 , 0 )
89
94
vim .fn .search (search_day , ' W' )
90
95
return Promise .new (function (resolve )
91
96
Calendar .callback = resolve
94
99
95
100
function Calendar .render ()
96
101
vim .api .nvim_buf_set_option (Calendar .buf , ' modifiable' , true )
102
+
103
+ local cal_rows = { {}, {}, {}, {}, {}, {} } -- the calendar rows
97
104
local start_from_sunday = config .calendar_week_start_day == 0
105
+ local weekday_row = { ' Mon' , ' Tue' , ' Wed' , ' Thu' , ' Fri' , ' Sat' , ' Sun' }
98
106
99
- local first_row = { ' Mon' , ' Tue' , ' Wed' , ' Thu' , ' Fri' , ' Sat' , ' Sun' }
100
107
if start_from_sunday then
101
- first_row = { ' Sun' , ' Mon' , ' Tue' , ' Wed' , ' Thu' , ' Fri' , ' Sat' }
108
+ weekday_row = { ' Sun' , ' Mon' , ' Tue' , ' Wed' , ' Thu' , ' Fri' , ' Sat' }
102
109
end
103
- local content = { {}, {}, {}, {}, {}, {} }
110
+
111
+ -- construct title (Month YYYY)
112
+ local title = Calendar .month :format (' %B %Y' )
113
+ title = string.rep (' ' , math.floor ((width - title :len ()) / 2 )) .. title
114
+
115
+ -- insert whitespace before first day of month
104
116
local start_weekday = Calendar .month :get_isoweekday ()
105
117
if start_from_sunday then
106
118
start_weekday = Calendar .month :get_weekday ()
107
119
end
108
120
while start_weekday > 1 do
109
- table.insert (content [1 ], ' ' )
121
+ table.insert (cal_rows [1 ], ' ' )
110
122
start_weekday = start_weekday - 1
111
123
end
112
- local today = Date . today ()
113
- local is_today_month = today : is_same ( Calendar . month , ' month ' )
124
+
125
+ -- insert dates into cal_rows
114
126
local dates = Calendar .month :get_range_until (Calendar .month :end_of (' month' ))
115
- local month = Calendar .month :format (' %B %Y' )
116
- month = string.rep (' ' , math.floor ((36 - month :len ()) / 2 )) .. month
117
- local start_row = 1
127
+ local current_row = 1
118
128
for _ , date in ipairs (dates ) do
119
- table.insert (content [ start_row ], date :format (' %d' ))
120
- if # content [ start_row ] % 7 == 0 then
121
- start_row = start_row + 1
129
+ table.insert (cal_rows [ current_row ], date :format (' %d' ))
130
+ if # cal_rows [ current_row ] % 7 == 0 then
131
+ current_row = current_row + 1
122
132
end
123
133
end
124
- local value = vim .tbl_map (function (item )
134
+
135
+ -- add spacing between the calendar cells
136
+ local content = vim .tbl_map (function (item )
125
137
return ' ' .. table.concat (item , ' ' ) .. ' '
126
- end , content )
127
- first_row = ' ' .. table.concat (first_row , ' ' )
128
- table.insert (value , 1 , first_row )
129
- table.insert (value , 1 , month )
130
- table.insert (value , ' [<] - prev month [>] - next month' )
131
- table.insert (value , ' [.] - today [Enter] - select day' )
138
+ end , cal_rows )
139
+ weekday_row = ' ' .. table.concat (weekday_row , ' ' )
140
+
141
+ -- put it all together
142
+ table.insert (content , 1 , weekday_row )
143
+ table.insert (content , 1 , title )
144
+ -- TODO: redundant, since it's static data
145
+ table.insert (content , ' [<] - prev month [>] - next month' )
146
+ table.insert (content , ' [.] - today [Enter] - select day' )
132
147
if Calendar .clearable then
133
- table.insert (value , ' [r] Clear date' )
148
+ table.insert (content , ' [r] Clear date' )
134
149
end
135
150
136
- vim .api .nvim_buf_set_lines (Calendar .buf , 0 , - 1 , true , value )
151
+ vim .api .nvim_buf_set_lines (Calendar .buf , 0 , - 1 , true , content )
137
152
vim .api .nvim_buf_clear_namespace (Calendar .buf , Calendar .namespace , 0 , - 1 )
138
153
if Calendar .clearable then
139
- vim .api .nvim_buf_add_highlight (Calendar .buf , Calendar .namespace , ' Comment' , # value - 3 , 0 , - 1 )
154
+ vim .api .nvim_buf_add_highlight (Calendar .buf , Calendar .namespace , ' Comment' , # content - 3 , 0 , - 1 )
140
155
end
141
- vim .api .nvim_buf_add_highlight (Calendar .buf , Calendar .namespace , ' Comment' , # value - 2 , 0 , - 1 )
142
- vim .api .nvim_buf_add_highlight (Calendar .buf , Calendar .namespace , ' Comment' , # value - 1 , 0 , - 1 )
156
+ vim .api .nvim_buf_add_highlight (Calendar .buf , Calendar .namespace , ' Comment' , # content - 2 , 0 , - 1 )
157
+ vim .api .nvim_buf_add_highlight (Calendar .buf , Calendar .namespace , ' Comment' , # content - 1 , 0 , - 1 )
158
+
159
+ -- highlight the cell of the current day
160
+ local today = Date .today ()
161
+ local is_today_month = today :is_same (Calendar .month , ' month' )
143
162
if is_today_month then
144
163
local day_formatted = today :format (' %d' )
145
- for i , line in ipairs (value ) do
164
+ for i , line in ipairs (content ) do
146
165
local from , to = line :find (' %s' .. day_formatted .. ' %s' )
147
166
if from and to then
148
167
vim .api .nvim_buf_add_highlight (Calendar .buf , Calendar .namespace , ' OrgCalendarToday' , i - 1 , from - 1 , to )
@@ -154,14 +173,14 @@ function Calendar.render()
154
173
end
155
174
156
175
function Calendar .forward ()
157
- Calendar .month = Calendar .month :add ({ month = 1 })
176
+ Calendar .month = Calendar .month :add ({ month = vim . v . count1 })
158
177
Calendar .render ()
159
- vim .fn .cursor ({ 2 , 0 } )
178
+ vim .fn .cursor (2 , 0 )
160
179
vim .fn .search (' 01' )
161
180
end
162
181
163
182
function Calendar .backward ()
164
- Calendar .month = Calendar .month :subtract ({ month = 1 })
183
+ Calendar .month = Calendar .month :subtract ({ month = vim . v . count1 })
165
184
Calendar .render ()
166
185
vim .fn .cursor (' $' , 0 )
167
186
vim .fn .search ([[ \d\d]] , ' b' )
@@ -173,7 +192,7 @@ function Calendar.cursor_right()
173
192
local curr_line = vim .fn .getline (' .' )
174
193
local offset = curr_line :sub (col + 1 , # curr_line ):find (' %d%d' )
175
194
if offset ~= nil then
176
- vim .fn .cursor ({ line , col + offset } )
195
+ vim .fn .cursor (line , col + offset )
177
196
end
178
197
end
179
198
end
0 commit comments