Skip to content

Commit 4fec6fa

Browse files
author
Panos
committed
Markdown reader
1 parent e93087e commit 4fec6fa

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ The project follows a clean, modular architecture:
175175

176176
## 🔧 Dependencies
177177

178+
### Dependencies
178179
- **requests** (≥2.28.0) - HTTP requests to GitHub
179180
- **beautifulsoup4** (≥4.11.0) - HTML parsing
180181
- **lxml** (≥4.9.0) - Fast XML/HTML parser backend
182+
- **rich** (≥13.0.0) - Beautiful terminal rendering with colors and formatting
181183

182184
## 🎯 Usage Examples
183185

github_trending/display.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import shutil
88
from typing import List, Dict
99
import re
10+
from rich.console import Console
11+
from rich.markdown import Markdown
1012

1113

1214
class DisplayManager:
@@ -48,6 +50,7 @@ class DisplayManager:
4850

4951
def __init__(self):
5052
self.terminal_width = self._get_terminal_width()
53+
self.console = Console(width=min(self.terminal_width - 4, 120))
5154

5255
def _get_terminal_width(self) -> int:
5356
"""Get the current terminal width."""
@@ -151,7 +154,7 @@ def show_repository_header(self, repo: Dict[str, str]):
151154
print(f"📝 Description: {description}")
152155

153156
def show_readme(self, readme_content: str):
154-
"""Display README content with proper formatting."""
157+
"""Display README content with beautiful Rich markdown rendering."""
155158
print("\n" + "-"*80)
156159
print("📖 README")
157160
print("-"*80)
@@ -160,8 +163,61 @@ def show_readme(self, readme_content: str):
160163
print("No README content available.")
161164
return
162165

166+
self._render_with_rich(readme_content)
167+
168+
def _render_with_rich(self, content: str):
169+
"""Render markdown content using Rich with beautiful formatting and pagination."""
170+
try:
171+
# Create Rich Markdown object
172+
markdown = Markdown(content)
173+
174+
# Render to string to get the formatted content
175+
with self.console.capture() as capture:
176+
self.console.print(markdown)
177+
178+
# Get the rendered content and split into lines
179+
rendered_content = capture.get()
180+
lines = rendered_content.split('\n')
181+
182+
# Paginate the beautifully rendered content
183+
self._paginate_rich_content(lines)
184+
185+
except Exception as e:
186+
print(f"⚠️ Error rendering with Rich: {e}")
187+
self._render_fallback(content)
188+
189+
def _paginate_rich_content(self, lines: list):
190+
"""Display Rich-rendered content with pagination."""
191+
lines_per_page = 30
192+
current_line = 0
193+
194+
while current_line < len(lines):
195+
# Display current page
196+
end_line = min(current_line + lines_per_page, len(lines))
197+
198+
for i in range(current_line, end_line):
199+
print(lines[i])
200+
201+
current_line = end_line
202+
203+
# Check if there are more lines
204+
if current_line < len(lines):
205+
remaining_lines = len(lines) - current_line
206+
print(f"\n--- More content available ({remaining_lines} lines remaining) ---")
207+
208+
try:
209+
user_input = input("📖 Press Enter to continue, 'q' to stop reading: ").strip().lower()
210+
if user_input == 'q':
211+
print("📖 README reading stopped.")
212+
break
213+
except KeyboardInterrupt:
214+
print("\n📖 README reading interrupted.")
215+
break
216+
217+
def _render_fallback(self, content: str):
218+
"""Fallback rendering for plain text display."""
163219
# Split content into lines for better display
164-
lines = readme_content.split('\n')
220+
lines = content.split('\n')
165221

166222
# Display with pagination for very long READMEs
167223
if len(lines) > 50:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
requests>=2.28.0
22
beautifulsoup4>=4.11.0
33
lxml>=4.9.0
4+
rich>=13.0.0

0 commit comments

Comments
 (0)