def resize(self, y=None, x=None): if not y: y = ScreenManager.get_coords(self.area_id)[0] if not x: x = ScreenManager.get_coords(self.area_id)[1] if self.search_mode: y -= 1 self.window.resize(y, x) self._calc_lines() new_ysize = self.window.getmaxyx()[0] - self.offset dif = new_ysize - self.ysize self.ysize = new_ysize bottom_lines = self.last - self.bottom down = dif if bottom_lines >= dif else bottom_lines if down < dif: top_lines = self.top up = dif - down if top_lines >= dif - down else top_lines self.top -= up self.bottom = self.top + self.ysize - 1 if self.bottom > self.last: self.bottom = self.last self.ibottom = self.lines[self.bottom][0] if self.current > self.ibottom: self.current = self.ibottom
def __init__(self, year, month, init_day=None, is_active=lambda date: False, *args, **kwargs): """Initialize calendar by year and month. init_day: day of the month that is initially selected is_active: function that takes a datetime.date instance and returns True if that date is "active" """ super(Calendar, self).__init__(*args, **kwargs) self.window = curses.newwin(*ScreenManager.get_coords(self.area_id)) self.window.keypad(1) self.year, self.month = year, month self.selected = () last = lastday(year, month) if not init_day: init_day = datetime.date.today().day if init_day > last: init_day = last self.init_day = init_day self.is_active = is_active first = datetime.date(self.year, self.month, 1) x, y = 0, 1 week = [None] * first.weekday() x += 3 * first.weekday() last = lastday(first) day = first.day data = [] while day <= last: date = datetime.date(self.year, self.month, day) week.append((x, y, is_active(date), day)) if len(week) == 7: data.append(week) week = [] y += 1 x = 0 else: x += 3 day += 1 if week: data.append(week + [None] * (7 - len(week))) self.data = data self.miny = len(self.data) + 1