From 8ae8db0798f9572b07f05b8a0120470a5417acab Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 26 May 2019 18:14:58 +0200 Subject: Move PostSelector to external file --- pyblog | 141 +---------------------------------------------------------------- 1 file changed, 1 insertion(+), 140 deletions(-) (limited to 'pyblog') diff --git a/pyblog b/pyblog index e73059bc..8813d4af 100755 --- a/pyblog +++ b/pyblog @@ -53,147 +53,8 @@ from generators import generators from cli import cli -class PostSelector: - - def __init__(self, term, posts): - self._term = term - self.posts_per_page = term.height - 2 - self.pages = [posts[i:i+self.posts_per_page] - for i in range(0, len(posts), self.posts_per_page)] - self.num_pages = len(self.pages) - self.pagepos = 0 - self.postpos = 0 - self.inserting = False # True if in the middle of inserting a post #, False otherwise - term.enter_fullscreen() - print(term.clear(), end="") - sys.stdout.flush() - self.selection = "" - self.quit = False - - self.display_page() - - def _clear_to_eol(self): - term = self._term - print(term.clear_eol, end="") - sys.stdout.flush() - - def _print_line(self, line, linenum, highlight=False): - term = self._term - width = term.width - with term.location(0, linenum): - if highlight: - print(term.reverse(line[:width]), end="") - else: - print(line[:width], end="") - self._clear_to_eol() - - def _print_post(self, page, pos, highlight=False): - if pos >= len(page): - # if position out of range, just clear the line - self._print_line("", pos + 1, highlight) - else: - date, title, path = page[pos] - line = "%3d: %s %s" % (pos, date.strftime("%m/%d/%y"), title) - self._print_line(line, pos + 1, highlight) - - def display_page(self): - term = self._term - page = self.pages[self.pagepos] - - with term.hidden_cursor(): - topline = " PAGE %d/%d POST %d" % (self.pagepos + 1, self.num_pages, self.postpos) - if self.inserting: - topline += term.blink("_") - self._print_line(topline, 0, highlight=True) - - for i in range(self.posts_per_page): - self._print_post(page, i) - # highlight selected post - self._print_post(page, self.postpos, highlight=True) - - bottomline = " Press h for help." - self._print_line(bottomline, term.height - 1, highlight=True) - - def dispatch(self, key): - term = self._term - if key in string.digits: - # insert - if self.inserting: - newpostpos = 10 * self.postpos + int(key) - if newpostpos < len(self.pages[self.pagepos]): - self.postpos = newpostpos - else: - self.postpos = int(key) - self.inserting = True - elif key.name == "KEY_DELETE": - self.postpos //= 10 - self.inserting = True - else: - self.inserting = False - if key.name == "KEY_ENTER": - self.selection = self.pages[self.pagepos][self.postpos][2] - if key in {"q", "Q"}: - self.quit = True - elif key.name == "KEY_DOWN" or key in {"n", "N"}: - if self.postpos + 1 < len(self.pages[self.pagepos]): - self.postpos += 1 - elif key.name == "KEY_UP" or key in {"p", "P"}: - if self.postpos > 0: - self.postpos -= 1 - elif key.name == "KEY_RIGHT" or key in {".", ">"}: - if self.pagepos + 1 < self.num_pages: - self.pagepos += 1 - self.postpos = 0 - elif key.name == "KEY_LEFT" or key in {",", "<"}: - if self.pagepos > 0: - self.pagepos -= 1 - self.postpos = 0 - elif key in {"h", "H"}: - print(term.clear_eol, end="") - sys.stdout.flush() - help_text_lines = [ - "Next post: n or ", - "Previous post: p or ", - "Next page: . or > or ", - "Previous page: , or < or ", - "Select post: or ", - "Select by number: type number as shown (delete or backspace to edit)", - "Get help: h", - "Quit program: q", - ] - for i in range(term.height - 1): - self._print_line(help_text_lines[i] if i < len(help_text_lines) else "", i) - bottomline = " Press any key to continue." - self._print_line(bottomline, term.height - 1, highlight=True) - - with term.raw(): - term.inkey() - - def restore(self): - term = self._term - term.exit_fullscreen() - print(term.clear(), end="") - sys.stdout.flush() - - def select(self): - term = self._term - try: - while True: - with term.raw(): - self.dispatch(term.inkey()) - if self.selection or self.quit: - break - self.display_page() - except Exception: - raise - finally: - self.restore() - - return self.selection - - def edit_existing_post(args): - selector = PostSelector(blessed.Terminal(), utils.list_posts()) + selector = utils.PostSelector(blessed.Terminal(), utils.list_posts()) selection = selector.select() if selection: print(selection) -- cgit v1.2.1