Пример #1
0
    def test_text(self):
        screen = get_screen()
        screen.setmaxyx(10, 20)
        ta = TextArea()
        ta.minx = 10

        lines = []
        # we need some lines longer than 10
        while not 0 < len([l for l in lines if len(l) > 10]) < 5:
            lines = get_random_lines(5, 15)

        def check():
            for index, line in enumerate(lines):
                self.assertEqual(screen.get_line(index), line)
        ta.show_text('\n'.join(lines))
        check()

        screen.setmaxyx(10, 10)
        ScreenManager.resize()
        nl = []
        for line in lines:
            nl += wrap(line, 10)
        lines = nl
        check()

        assert len(lines) < 10
        screen.setmaxyx(len(lines), 10)
        ScreenManager.resize()
        check()

        screen.setmaxyx(len(lines) - 1, 10)
        ScreenManager.resize()
        old, lines = lines, [''] * (len(lines) - 1)
        check()

        lines = old
        screen.setmaxyx(len(lines), 10)
        ScreenManager.resize()
        check()

        screen.setmaxyx(10, 9)
        ScreenManager.resize()
        lines = [''] * len(lines)
        check()
Пример #2
0
    def test_title(self):
        screen = get_screen()
        y = 10
        screen.setmaxyx(y, 20)
        ta = TextArea()
        ta.minx = 10

        lines = [chr(i) for i in range(97, 96 + y)]
        ta.show_text('\n'.join(lines))
        self.assertEqual(screen.get_line(0), lines[0])
        title = ''
        while len(title) < 10:
            title += get_random_lines(1, 20)[0].replace(' ', '')
        title = title[:10]
        ta.set_title(title)
        ta.draw()
        self.assertEqual(screen.get_line(0), title)

        title *= 3
        ta.set_title(title)
        ta.draw()
        self.assertEqual(screen.get_line(0), '')

        screen.setmaxyx(y, 30)
        ScreenManager.resize()
        self.assertEqual(screen.get_line(0), title)
Пример #3
0
def show_tags(area_id=None, text_area=None):
    """Show all tags sorted by entries number as a scrollable list."""
    screen = ScreenManager.screen
    screen.addstr(0, 0, 'loading...')
    screen.refresh()

    tags = {}

    for year, month in get_all_months(conf['data_dir']):
        m = Metadata.get(year, month)
        for tag, days in m.tags.items():
            for day in days:
                date = datetime.date(year, month, day)
                try:
                    tags[tag].append(date)
                except KeyError:
                    tags[tag] = [date]

    if not tags:
        raise WLError('No tags found')
    items = sorted(sorted(tags.items(), key=lambda i: i[0]),
                   key=lambda i: len(i[1]), reverse=True)
    tl = ['%s (%d)' % (item[0], len(item[1])) for item in items]
    sl = ScrollableList(tl, area_id=area_id)
    if text_area:
        text_area.set_title('Last entry:')
    else:
        text_area = TextArea()
        text_area.set_title('Last entry:')
    ScreenManager.draw_all()
    def tag_info(index):
        tag, dates = items[index]
        d = dates[-1]
        text_area.show_text(Metadata.get(d.year, d.month).text(d.day))
    tag_info(sl.get_current_index())
    while 1:
        try:
            kn = curses.keyname(sl.window.getch())
        except KeyboardInterrupt:
            break
        except ValueError:
            continue
        if kn in Config.general_keys['quit']:
            raise WLQuit
        if kn in Config.general_keys['quit_mode']:
            break
        if kn == 'KEY_RESIZE':
            ScreenManager.resize()
        if sl.hidden:
            continue
        elif kn in Config.tags_keys['details']:
            tag, dates = items[sl.get_current_index()]
            show_date_list(tag, dates, sl.area_id, text_area)
            ScreenManager.restore_area(sl.area_id)
            sl.reinit()
            text_area.set_title('Last entry:')
            tag_info(sl.get_current_index())
        else:
            sl.handle_keypress(kn)
            tag_info(sl.get_current_index())
    Metadata.write_all()
Пример #4
0
def show_date_list(tag, dates, area_id=None, text_area=None):
    """Show the list of entries for a tag."""
    labels = map(format_date, dates)
    sl = ScrollableList(labels, tag, area_id=area_id)
    sl.draw()
    date = dates[sl.get_current_index()]
    metadata = Metadata.get(date.year, date.month)
    if not text_area:
        text_area = TextArea()
    text_area.set_title()
    text_area.show_text(metadata.text(date.day))
    while 1:
        try:
            kn = curses.keyname(sl.window.getch())
        except KeyboardInterrupt:
            break
        if kn in Config.general_keys['quit']:
            raise WLQuit
        if kn in Config.general_keys['quit_mode']:
            break
        if kn == 'KEY_RESIZE':
            ScreenManager.resize()
        if sl.hidden:
            continue
        if kn in Config.tag_details_keys['edit']:
            date = dates[sl.get_current_index()]
            edit_date(date)
            sl.draw()
            metadata.load_day(date.day)
            text_area.show_text(metadata.text(date.day))
        elif kn in Config.tag_details_keys['edits']:
            date = dates[sl.get_current_index()]
            edits = get_edits(date)
            if edits:
                show_edits(date, edits, text_area.area_id)
                ScreenManager.restore_area(text_area.area_id)
                text_area.show_text(metadata.text(date.day))
        else:
            sl.handle_keypress(kn)
            date = dates[sl.get_current_index()]
            metadata = Metadata.get(date.year, date.month)
            text_area.show_text(metadata.text(date.day))
    Metadata.write_all()
Пример #5
0
def show_calendar():
    """Show an interactive calendar.

    Show the calendar on the left side of the screen and some metadata about
    the selected date on the right. Any entry can be edited in external editor.
    """
    today = datetime.date.today()
    year, month = today.year, today.month
    cal = Calendar(year, month, today.day, entry_exists)
    metadata = Metadata.get(year, month)
    text_area = TextArea()
    ScreenManager.draw_all()
    d = cal.get_current_date()
    text_area.show_text(metadata.text(d.day))
    keys = Config.calendar_keys
    while 1:
        try:
            kn = curses.keyname(cal.window.getch())
        except KeyboardInterrupt:
            break
        except ValueError:
            continue
        if kn in Config.general_keys['quit']:
            raise WLQuit
        if kn in Config.general_keys['quit_mode']:
            break
        if kn == 'KEY_RESIZE':
            ScreenManager.resize()
        if cal.hidden:
            continue
        if kn in keys['left']:
            moved = cal.move_left()
            if not moved:
                cal = cal.get_previous_calendar()
                cal.draw()
                metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['right']:
            moved = cal.move_right()
            if not moved:
                cal = cal.get_next_calendar()
                cal.draw()
                metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['down']:
            cal.move_down()
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['up']:
            cal.move_up()
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['edit']:
            date = cal.get_current_date()
            edit_date(date)
            metadata.load_day(date.day)
            cal.set_active(entry_exists(date))
            text_area.show_text(metadata.text(date.day))
        elif kn in keys['tags']:
            show_tags(cal.area_id, text_area)
            ScreenManager.restore_area(cal.area_id)
            cal.reinit()
            text_area.set_title()
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['edits']:
            date = cal.get_current_date()
            edits = get_edits(date)
            if edits:
                show_edits(date, edits, text_area.area_id)
                ScreenManager.restore_area(text_area.area_id)
                text_area.show_text(metadata.text(date.day))
        elif kn in keys['prev_month']:
            cal = cal.get_previous_calendar(cal.get_current_day())
            cal.draw()
            metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['next_month']:
            cal = cal.get_next_calendar(cal.get_current_day())
            cal.draw()
            metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
    Metadata.write_all()
    clean_tmp()