diff --git a/App.py b/App.py index 7d1ecf5..f73aa1d 100644 --- a/App.py +++ b/App.py @@ -103,6 +103,8 @@ class RightPane(urwid.ListBox): self.body = walker self.isList = True self.articlePosition = 0 + self.article = None + self.chunkNumber = 0 def fill(self, articles): items = [urwid.AttrMap(urwid.Columns([(2, urwid.Text("N")), urwid.Text(article["title"])]), @@ -116,6 +118,13 @@ class RightPane(urwid.ListBox): def keypress(self, size, key): if key in ("j", "down"): + if not self.isList: + if self.chunkNumber >= len(self.article.chunks) - 1: + pass + else: + self.chunkNumber += 1 + walker = urwid.SimpleListWalker([urwid.Text(self.article.chunks[self.chunkNumber])]) + self.body = walker item_size = len(self.body) focus_widget, idx = self.get_focus() if idx < item_size - 1: @@ -123,6 +132,13 @@ class RightPane(urwid.ListBox): self.set_focus(idx) return elif key in ("k", "up"): + if not self.isList: + if self.chunkNumber == 0: + pass + else: + self.chunkNumber -= 1 + walker = urwid.SimpleListWalker([urwid.Text(self.article.chunks[self.chunkNumber])]) + self.body = walker focus_widget, idx = self.get_focus() if idx > 0: idx = idx - 1 @@ -131,14 +147,14 @@ class RightPane(urwid.ListBox): elif key in ("l", "right"): if self.isList is True: self.isList = False + self.chunkNumber = 0 focus_widget, idx = self.get_focus() self.articlePosition = idx articleId = focus_widget.attr_map[None] - article = Article(self.getArticle(articleId)) - walker = urwid.SimpleListWalker([urwid.Text(""), urwid.Text(article.text), urwid.Text("")]) + self.article = Article(self.getArticle(articleId)) + walker = urwid.SimpleListWalker([urwid.Text(self.article.chunks[self.chunkNumber])]) self.body = walker - self.set_focus(1) - tui.rightBox.set_title(article.title) + tui.rightBox.set_title(self.article.title) return elif key in ("h", "left"): if self.isList is False: diff --git a/Render.py b/Render.py index 0657cc5..6ccb8ca 100644 --- a/Render.py +++ b/Render.py @@ -1,7 +1,29 @@ from inscriptis import get_text +import os class Article: def __init__(self, articleObj): self.text = get_text(articleObj["summary"]["content"]) self.title = articleObj["title"] + terminal_width, terminal_height = os.get_terminal_size() + terminal_width -= 60 + start_of_chunk = 0 + end_of_chunk = 0 + rows_passed = 0 + self.chunks = [] + i = 0 + column_position = 0 + for s in self.text: + i += 1 + column_position += 1 + if column_position > terminal_width or s == "\n": + rows_passed += 1 + column_position = 0 + if rows_passed > terminal_height - 2: + end_of_chunk = i + self.chunks.append(self.text[start_of_chunk:end_of_chunk]) + start_of_chunk = end_of_chunk + rows_passed = 0 + if end_of_chunk <= i: + self.chunks.append(self.text[start_of_chunk:i])