def show_edits(date, edits, area_id): """Show all edits of an entry as a scrollable list.""" from writelightly.screen import ScreenManager from writelightly.utils import format_time, format_size from writelightly.scrollable_list import ScrollableList import curses formatted = ['%s, created' % format_time(edits[0][0], full=True)] formatted += ['%s, %s' % (format_time(ts, full=True), format_size(size)) for ts, size in edits[1:]] sl = ScrollableList(formatted, area_id=area_id) sl.draw() while 1: kn = curses.keyname(sl.window.getch()) 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.edits_keys['open']: index = sl.get_current_index() fn = save_tmp_version(date, edits, index) edit_file(fn) sl.draw() else: sl.handle_keypress(kn)
def test_scrolling(self): lines = get_random_lines() sl = ScrollableList(lines) ScreenManager.draw_all() screen = get_screen() y, x = screen.getmaxyx() ind = sl.get_current_index for i in range(len(lines) - y): sl.scroll_down() self.assertEquals(ind(), i + 1) self.assertEquals(screen.get_line(0), lines[i + 1]) self.assertEquals(screen.get_line(-1), lines[-1]) up = min(len(lines) - y, y) sl.scroll_screen_up() self.assertEquals(screen.get_line(-1), lines[-up-1])
def test_moving(self): lines = get_random_lines() sl = ScrollableList(lines) ScreenManager.draw_all() screen = get_screen() y, x = screen.getmaxyx() ind = sl.get_current_index self.assertEquals(ind(), 0) for i in range(y): self.assertEquals(screen.get_line(i), lines[i]) sl.move_up() self.assertEquals(ind(), 0) sl.move_to_bottom() self.assertEquals(ind(), len(lines) - 1) sl.move_down() self.assertEquals(ind(), len(lines) - 1) self.assertEquals(screen.get_line(0), lines[-y]) sl.move_to_top() self.assertEquals(ind(), 0) self.assertEquals(screen.get_line(-1), lines[y - 1]) for i in range(len(lines)): sl.move_down() self.assertEquals(ind(), len(lines) - 1) self.assertEquals(screen.get_line(0), lines[-y]) self.assertEquals(screen.get_line(-1), lines[-1]) self._move_down_and_up(sl)
def test_search(self): screen = get_screen() y, x = screen.getmaxyx() lines = get_random_lines(300) lines1 = get_random_lines(3) prefix = lines[random.randint(y, 299)][:3] for i in range(3): lines.insert(random.randint(y, 299 + i), prefix + lines1[i][3:]) indices = [ind for ind, line in enumerate(lines) if prefix in line] sl = ScrollableList(lines) ScreenManager.draw_all() commands.add([ord(c) for c in prefix]) commands.add(ord('\n')) sl.handle_search() next_index = itertools.cycle(indices).next for i in range(10): index = next_index() self.assertEquals(sl.get_current_index(), index) self.assertTrue(lines[index] in map(screen.get_line, range(y))) sl.handle_keypress('n') sl.move_to_bottom() if prefix not in screen.get_line(-1): sl.handle_keypress('N') previous_index = itertools.cycle(reversed(indices)).next for i in range(10): index = previous_index() self.assertEquals(sl.get_current_index(), index) self.assertTrue(lines[index] in map(screen.get_line, range(y))) sl.handle_keypress('N')
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()
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()