A text-mode web browser with AI help, inspired by Lynx, plus TextFirst.css for text-first websites.

0 downloads

dbbasic-textbrowser/browser.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-textbrowser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-textbrowser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-textbrowser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add Gateway Architecture documentation 2693936 1 year ago History
browser.py · 34.4 KB · 954 lines
1#!/usr/bin/env python3
2"""
3DBBasic TextBrowser: A text-mode web browser with AI assistance
4"""
5
6import curses
7import requests
8from bs4 import BeautifulSoup
9from typing import Optional
10import html2text
11import sys
12import os
13from openai import OpenAI
14import re
15import json
16
17
18class Browser:
19 def __init__(self, stdscr):
20 self.stdscr = stdscr
21 self.current_url = ""
22 self.page_content = []
23 self.page_text = "" # Raw text content for AI processing
24 self.scroll_offset = 0
25 self.running = True
26 self.forms = [] # Store forms found on the page
27 self.current_soup = None # Store parsed HTML for form submission
28 self.links = [] # Store numbered links from the page
29
30 # Initialize OpenAI client if API key is available
31 api_key = os.getenv('OPENAI_API_KEY')
32 self.ai_enabled = bool(api_key)
33 if self.ai_enabled:
34 self.client = OpenAI(api_key=api_key)
35 else:
36 self.client = None
37
38 # Initialize colors
39 curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK) # Status bar / cyan
40 curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK) # Command box, links / green
41 curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) # Help bar / yellow
42 curses.init_pair(4, curses.COLOR_MAGENTA, curses.COLOR_BLACK) # Headings / magenta
43 curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLACK) # Bold text / white
44 curses.init_pair(6, curses.COLOR_BLUE, curses.COLOR_BLACK) # Secondary links / blue
45 curses.init_pair(7, curses.COLOR_RED, curses.COLOR_BLACK) # Emphasis / red
46
47 # Color name to curses color pair mapping
48 self.color_map = {
49 'red': 7,
50 'green': 2,
51 'blue': 6,
52 'yellow': 3,
53 'cyan': 1,
54 'magenta': 4,
55 'white': 5,
56 'black': 0, # Will need special handling
57 # Extended colors (map to closest)
58 'orange': 3, # Yellow
59 'purple': 4, # Magenta
60 'pink': 4, # Magenta
61 'brown': 3, # Yellow
62 'gray': 5, # White
63 'grey': 5, # White
64 }
65
66 # Hide cursor
67 curses.curs_set(0)
68
69 def fetch_page(self, url: str) -> bool:
70 """Fetch and parse a web page"""
71 try:
72 # Handle local file:// URLs or .html files
73 if url.startswith('file://'):
74 file_path = url.replace('file://', '')
75 with open(file_path, 'r') as f:
76 html_content = f.read()
77 soup = BeautifulSoup(html_content, 'html.parser')
78 elif url.endswith('.html') and not url.startswith('http'):
79 # Local file path
80 import os
81 file_path = os.path.join(os.path.dirname(__file__), url)
82 with open(file_path, 'r') as f:
83 html_content = f.read()
84 soup = BeautifulSoup(html_content, 'html.parser')
85 url = f"file://{file_path}"
86 else:
87 # Add https:// if no protocol specified
88 if not url.startswith(('http://', 'https://')):
89 url = 'https://' + url
90
91 headers = {
92 'User-Agent': 'Lynx/2.9.0dev.6 libwww-FM/2.14 SSL-MM/1.4.1'
93 }
94
95 response = requests.get(url, headers=headers, timeout=10)
96 response.raise_for_status()
97
98 # Parse HTML to text
99 soup = BeautifulSoup(response.text, 'html.parser')
100
101 # Remove script and style elements
102 for script in soup(["script", "style"]):
103 script.decompose()
104
105 # Store soup for form handling
106 self.current_soup = soup
107
108 # Extract links with numbering
109 self.links = []
110 for link in soup.find_all('a', href=True):
111 href = link.get('href')
112 if href and not href.startswith(('#', 'javascript:', 'mailto:')):
113 # Convert relative URLs to absolute
114 from urllib.parse import urljoin
115 abs_url = urljoin(url, href)
116 link_text = link.get_text(strip=True)
117 if link_text: # Only add links with visible text
118 self.links.append({
119 'url': abs_url,
120 'text': link_text[:50] # Truncate long link text
121 })
122
123 # Extract forms
124 self.forms = []
125 for idx, form in enumerate(soup.find_all('form')):
126 form_data = {
127 'index': idx,
128 'action': form.get('action', ''),
129 'method': form.get('method', 'get').upper(),
130 'fields': []
131 }
132
133 # Get all input fields
134 for input_tag in form.find_all(['input', 'textarea']):
135 input_type = input_tag.get('type', 'text')
136 if input_type not in ['hidden', 'submit', 'button']:
137 form_data['fields'].append({
138 'name': input_tag.get('name', ''),
139 'type': input_type,
140 'placeholder': input_tag.get('placeholder', ''),
141 'value': input_tag.get('value', '')
142 })
143
144 if form_data['fields']: # Only add forms with visible fields
145 self.forms.append(form_data)
146
147 # Get terminal width for wrapping
148 try:
149 height, width = self.stdscr.getmaxyx()
150 wrap_width = width - 4 # Leave some margin
151 except:
152 wrap_width = 78 # Default fallback
153
154 # Process font color tags before conversion
155 for font_tag in soup.find_all('font'):
156 color = font_tag.get('color', '').lower()
157 if color and color in self.color_map:
158 # Wrap text with special markers that we can detect later
159 text = font_tag.get_text()
160 font_tag.string = f"«{color}»{text}«/{color}»"
161
162 # Number the links in the HTML before conversion
163 link_counter = 0
164 for link in soup.find_all('a', href=True):
165 href = link.get('href')
166 if href and not href.startswith(('#', 'javascript:', 'mailto:')):
167 link_text = link.get_text(strip=True)
168 if link_text and link_counter < len(self.links):
169 # Add number before the link
170 link.string = f"[{link_counter}] {link_text}"
171 link_counter += 1
172
173 # Convert to text using html2text for better formatting
174 h = html2text.HTML2Text()
175 h.ignore_links = True # We're handling links ourselves
176 h.ignore_images = True
177 h.body_width = wrap_width # Wrap to terminal width
178 h.unicode_snob = True # Use unicode characters
179 h.mark_code = True # Mark code blocks
180 text = h.handle(str(soup))
181
182 # Add links list at the end
183 if self.links:
184 text += "\n\n" + "=" * 60 + "\n"
185 text += f"LINKS: {len(self.links)} link(s) found\n"
186 text += "=" * 60 + "\n"
187 text += "Type a number (0-{}) to follow a link\n\n".format(len(self.links) - 1)
188
189 # Show first 20 links in the list
190 for idx, link in enumerate(self.links[:20]):
191 text += f"[{idx}] {link['text']}\n"
192
193 if len(self.links) > 20:
194 text += f"\n... and {len(self.links) - 20} more links (see inline numbers)\n"
195
196 # Add form information to the display
197 if self.forms:
198 text += "\n\n" + "=" * 60 + "\n"
199 text += f"FORMS DETECTED: {len(self.forms)} form(s) found\n"
200 text += "=" * 60 + "\n"
201 for idx, form in enumerate(self.forms):
202 text += f"\n[Form {idx}] {form['method']} → {form['action'] or '(same page)'}\n"
203 for field in form['fields']:
204 placeholder = f" ({field['placeholder']})" if field['placeholder'] else ""
205 text += f" - {field['name']}: {field['type']}{placeholder}\n"
206 text += f"\nPress 'F' to fill out a form\n"
207
208 self.page_content = text.split('\n')
209 self.page_text = text # Store for AI processing
210
211 # Detect if page is too empty (likely JS-heavy)
212 content_lines = [line.strip() for line in self.page_content if line.strip()]
213 if len(content_lines) < 10:
214 self.page_content = [
215 "⚠️ JAVASCRIPT-HEAVY SITE DETECTED",
216 "",
217 f"URL: {url}",
218 "",
219 "This site appears to require JavaScript to display content.",
220 "Text browsers cannot execute JavaScript.",
221 "",
222 "Possible solutions:",
223 "",
224 "1. Ask AI for help (Ctrl-K):",
225 " - 'find a text-friendly alternative to this site'",
226 " - 'search for [topic] on a simpler site'",
227 " - 'what is this site about?'",
228 "",
229 "2. Try alternative sites:",
230 " - YouTube → Invidious instances (yewtu.be, inv.riverside.rocks)",
231 " - Twitter → Nitter instances (nitter.net)",
232 " - Reddit → old.reddit.com or teddit instances",
233 " - Instagram → bibliogram instances",
234 "",
235 "3. Use yt-dlp for YouTube:",
236 " - Command line tool to download/stream videos",
237 "",
238 "Press Ctrl-K to try a different site or ask AI for alternatives.",
239 "",
240 "=" * 60,
241 "",
242 "Raw content detected:",
243 ""
244 ] + self.page_content[:50] # Show first 50 lines for debugging
245
246 self.current_url = url
247 self.scroll_offset = 0
248
249 return True
250
251 except Exception as e:
252 self.page_content = [
253 f"Error loading page: {str(e)}",
254 "",
255 "Press Ctrl-K to enter a new URL"
256 ]
257 return False
258
259 def is_url(self, text: str) -> bool:
260 """Check if the input looks like a URL"""
261 # Check for common URL patterns
262 url_pattern = re.compile(
263 r'^(https?://)?' # Optional protocol
264 r'([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}' # Domain
265 r'(/.*)?$' # Optional path
266 )
267 return bool(url_pattern.match(text.strip()))
268
269 def process_ai_command(self, command: str):
270 """Process an AI command on the current page"""
271 if not self.ai_enabled:
272 self.page_content = [
273 "AI features are not enabled!",
274 "",
275 "To enable AI features, set your OpenAI API key:",
276 " export OPENAI_API_KEY=your-key-here",
277 "",
278 "Then restart the browser.",
279 "",
280 "Press Ctrl-K to continue browsing."
281 ]
282 self.scroll_offset = 0
283 return
284
285 # Allow AI commands even without a loaded page for navigation
286 page_context = ""
287 if self.page_text:
288 # Truncate page content if too long (OpenAI has token limits)
289 max_chars = 12000
290 truncated_content = self.page_text[:max_chars]
291 if len(self.page_text) > max_chars:
292 truncated_content += "\n\n[Content truncated...]"
293 page_context = f"Current page URL: {self.current_url}\n\nPage content:\n{truncated_content}\n\n"
294
295 # Show loading message
296 self.page_content = [
297 f"AI Processing: {command}",
298 "",
299 "Please wait..."
300 ]
301 self.scroll_offset = 0
302 self.render()
303
304 try:
305 # Define function tools for the AI
306 tools = [
307 {
308 "type": "function",
309 "function": {
310 "name": "navigate_to_url",
311 "description": "Navigate the browser to a specific URL. Use this when the user wants to visit a website or when you need to look up information that requires visiting a specific page.",
312 "parameters": {
313 "type": "object",
314 "properties": {
315 "url": {
316 "type": "string",
317 "description": "The full URL to navigate to (e.g., 'https://en.wikipedia.org/wiki/WebDAV')"
318 },
319 "reason": {
320 "type": "string",
321 "description": "Brief explanation of why navigating to this URL"
322 }
323 },
324 "required": ["url", "reason"]
325 }
326 }
327 }
328 ]
329
330 # Call OpenAI API with function calling
331 response = self.client.chat.completions.create(
332 model="gpt-5-nano",
333 messages=[
334 {
335 "role": "system",
336 "content": "You are a helpful assistant for a text-mode web browser. You can navigate to URLs using the navigate_to_url function when appropriate. Provide clear, concise responses formatted for a terminal browser."
337 },
338 {
339 "role": "user",
340 "content": f"{page_context}User request: {command}"
341 }
342 ],
343 tools=tools,
344 max_completion_tokens=2000
345 )
346
347 # Check if AI wants to call a function
348 message = response.choices[0].message
349
350 if message.tool_calls:
351 # Handle function calls
352 for tool_call in message.tool_calls:
353 if tool_call.function.name == "navigate_to_url":
354 args = json.loads(tool_call.function.arguments)
355 url = args.get("url")
356 reason = args.get("reason", "AI navigation")
357
358 # Show what we're doing
359 self.page_content = [
360 f"AI Action: Navigating to URL",
361 "",
362 f"Reason: {reason}",
363 f"URL: {url}",
364 "",
365 "Loading page..."
366 ]
367 self.scroll_offset = 0
368 self.render()
369
370 # Actually navigate
371 self.fetch_page(url)
372 return
373
374 # No function call, just display the text response
375 ai_response = message.content if message.content else "No response from AI."
376
377 # Format the response
378 result = [
379 f"AI Response to: {command}",
380 ]
381 if self.current_url:
382 result.append(f"Page: {self.current_url}")
383 result.extend([
384 "=" * 60,
385 "",
386 ])
387 result.extend(ai_response.split('\n'))
388 result.extend([
389 "",
390 "=" * 60,
391 "Press Ctrl-K to enter a new command or URL"
392 ])
393
394 self.page_content = result
395 self.scroll_offset = 0
396
397 except Exception as e:
398 self.page_content = [
399 "AI Error!",
400 "",
401 f"Error: {str(e)}",
402 "",
403 "Press Ctrl-K to try again."
404 ]
405 self.scroll_offset = 0
406
407 def show_help(self):
408 """Load and display the help page"""
409 import os
410 help_path = os.path.join(os.path.dirname(__file__), 'help.html')
411
412 if os.path.exists(help_path):
413 # Load help file like a local page
414 with open(help_path, 'r') as f:
415 help_html = f.read()
416
417 from bs4 import BeautifulSoup
418 soup = BeautifulSoup(help_html, 'html.parser')
419 self.current_soup = soup
420 self.current_url = f"file://{help_path}"
421
422 # Extract links (though help page shouldn't have many)
423 self.links = []
424 for link in soup.find_all('a', href=True):
425 href = link.get('href')
426 if href:
427 link_text = link.get_text(strip=True)
428 if link_text:
429 self.links.append({
430 'url': href,
431 'text': link_text[:50]
432 })
433
434 # Number the links
435 link_counter = 0
436 for link in soup.find_all('a', href=True):
437 href = link.get('href')
438 if href:
439 link_text = link.get_text(strip=True)
440 if link_text and link_counter < len(self.links):
441 link.string = f"[{link_counter}] {link_text}"
442 link_counter += 1
443
444 # Get terminal width
445 try:
446 height, width = self.stdscr.getmaxyx()
447 wrap_width = width - 4
448 except:
449 wrap_width = 78
450
451 # Convert to text
452 import html2text
453 h = html2text.HTML2Text()
454 h.ignore_links = True
455 h.ignore_images = True
456 h.body_width = wrap_width
457 h.unicode_snob = True
458 text = h.handle(str(soup))
459
460 self.page_content = text.split('\n')
461 self.page_text = text
462 self.forms = [] # Help page has no forms
463 self.scroll_offset = 0
464 else:
465 # Fallback inline help
466 self.page_content = [
467 "DBBasic TextBrowser Help",
468 "",
469 "Keyboard Controls:",
470 " Ctrl-K - Address/AI command box",
471 " 0-9 - Follow numbered links",
472 " G - Go to link by number",
473 " F - Fill out forms",
474 " H - Show this help",
475 " Q - Quit",
476 "",
477 "AI Commands (Ctrl-K):",
478 " summarize this page",
479 " what are the main points?",
480 " go to wikipedia for [topic]",
481 " translate to [language]",
482 "",
483 "help.html not found - reinstall browser"
484 ]
485 self.links = []
486 self.forms = []
487 self.scroll_offset = 0
488
489 def goto_link(self):
490 """Go to a link by entering its number"""
491 if not self.links:
492 self.page_content = ["No links found on this page!"]
493 self.scroll_offset = 0
494 return
495
496 height, width = self.stdscr.getmaxyx()
497
498 # Create input window
499 input_win = curses.newwin(5, width - 4, height // 2 - 2, 2)
500 input_win.box()
501 input_win.addstr(0, 2, " Go to Link ", curses.color_pair(2) | curses.A_BOLD)
502 input_win.addstr(2, 2, f"Enter link number (0-{len(self.links)-1}): ")
503 input_win.refresh()
504
505 curses.echo()
506 curses.curs_set(1)
507 try:
508 link_num_str = input_win.getstr(2, 40, 10).decode('utf-8')
509 link_num = int(link_num_str)
510
511 if 0 <= link_num < len(self.links):
512 curses.noecho()
513 curses.curs_set(0)
514 self.fetch_page(self.links[link_num]['url'])
515 return
516 except:
517 pass
518
519 curses.noecho()
520 curses.curs_set(0)
521
522 def fill_form(self):
523 """Interactive form filling"""
524 if not self.forms:
525 self.page_content = ["No forms found on this page!"]
526 self.scroll_offset = 0
527 return
528
529 # If only one form, use it; otherwise ask which one
530 if len(self.forms) == 1:
531 form_idx = 0
532 else:
533 # Show form selection
534 height, width = self.stdscr.getmaxyx()
535 select_win = curses.newwin(len(self.forms) + 4, width - 4, 2, 2)
536 select_win.box()
537 select_win.addstr(0, 2, " Select Form ", curses.color_pair(2) | curses.A_BOLD)
538
539 for idx, form in enumerate(self.forms):
540 select_win.addstr(idx + 2, 2, f"{idx}: {form['method']} → {form['action'] or '(same page)'}"[:width-8])
541
542 select_win.addstr(len(self.forms) + 2, 2, "Enter form number: ")
543 select_win.refresh()
544
545 curses.echo()
546 curses.curs_set(1)
547 try:
548 form_idx = int(select_win.getstr().decode('utf-8'))
549 if form_idx < 0 or form_idx >= len(self.forms):
550 raise ValueError()
551 except:
552 curses.noecho()
553 curses.curs_set(0)
554 return
555 curses.noecho()
556 curses.curs_set(0)
557
558 # Fill out the selected form
559 form = self.forms[form_idx]
560 form_values = {}
561
562 height, width = self.stdscr.getmaxyx()
563
564 for field in form['fields']:
565 # Create input window
566 input_win = curses.newwin(5, width - 4, height // 2 - 2, 2)
567 input_win.box()
568
569 label = f" {field['name']} "
570 if field['placeholder']:
571 label += f"({field['placeholder']}) "
572 input_win.addstr(0, 2, label[:width-8], curses.color_pair(2) | curses.A_BOLD)
573 input_win.addstr(2, 2, f"Type: {field['type']}")
574 input_win.addstr(3, 2, "Value: ")
575
576 input_win.refresh()
577
578 curses.echo()
579 curses.curs_set(1)
580 value = input_win.getstr(3, 9, width - 16).decode('utf-8')
581 curses.noecho()
582 curses.curs_set(0)
583
584 if value:
585 form_values[field['name']] = value
586
587 # Submit the form
588 self.submit_form(form, form_values)
589
590 def submit_form(self, form, values):
591 """Submit a form with the given values"""
592 try:
593 # Construct the target URL
594 action = form['action']
595 if not action:
596 action = self.current_url
597 elif not action.startswith('http'):
598 # Relative URL
599 from urllib.parse import urljoin
600 action = urljoin(self.current_url, action)
601
602 headers = {
603 'User-Agent': 'Lynx/2.9.0dev.6 libwww-FM/2.14 SSL-MM/1.4.1'
604 }
605
606 # Show loading message
607 self.page_content = ["Submitting form...", "", f"Target: {action}"]
608 self.scroll_offset = 0
609 self.render()
610
611 # Submit based on method
612 if form['method'] == 'POST':
613 response = requests.post(action, data=values, headers=headers, timeout=10)
614 else: # GET
615 response = requests.get(action, params=values, headers=headers, timeout=10)
616
617 response.raise_for_status()
618
619 # Parse the response
620 soup = BeautifulSoup(response.text, 'html.parser')
621 self.current_soup = soup
622
623 # Remove script and style elements
624 for script in soup(["script", "style"]):
625 script.decompose()
626
627 # Get terminal width
628 try:
629 height, width = self.stdscr.getmaxyx()
630 wrap_width = width - 4
631 except:
632 wrap_width = 78
633
634 # Convert to text
635 h = html2text.HTML2Text()
636 h.ignore_links = False
637 h.ignore_images = True
638 h.body_width = wrap_width
639 h.unicode_snob = True
640 h.mark_code = True
641 text = h.handle(str(soup))
642
643 self.page_content = text.split('\n')
644 self.page_text = text
645 self.current_url = response.url
646 self.scroll_offset = 0
647
648 except Exception as e:
649 self.page_content = [
650 "Form submission error!",
651 "",
652 f"Error: {str(e)}",
653 "",
654 "Press Ctrl-K to continue"
655 ]
656 self.scroll_offset = 0
657
658 def show_command_box(self) -> Optional[str]:
659 """Show the Ctrl-K command/address box"""
660 height, width = self.stdscr.getmaxyx()
661
662 # Create a window for input at the bottom
663 input_win = curses.newwin(3, width - 4, height - 4, 2)
664 input_win.box()
665 input_win.addstr(0, 2, " Address / AI Command ", curses.color_pair(2) | curses.A_BOLD)
666
667 # Enable cursor for input
668 curses.curs_set(1)
669 curses.echo()
670
671 input_win.refresh()
672 input_win.move(1, 2)
673
674 # Get user input
675 user_input = input_win.getstr(1, 2, width - 8).decode('utf-8')
676
677 # Restore cursor state
678 curses.noecho()
679 curses.curs_set(0)
680
681 return user_input.strip() if user_input else None
682
683 def render_line_with_formatting(self, y: int, line: str, width: int):
684 """Render a single line with markdown formatting and colors"""
685 if not line:
686 return
687
688 x = 0
689 line = line[:width] # Truncate to screen width
690
691 # Check for font color markers
692 if '«' in line and '»' in line:
693 # Line contains color markers, render with colors
694 import re
695 parts = re.split(r'(«[a-z]+»|«/[a-z]+»)', line)
696 current_color = None
697
698 for part in parts:
699 if part.startswith('«') and part.endswith('»'):
700 color_name = part[1:-1]
701 if color_name.startswith('/'):
702 # End color tag
703 current_color = None
704 else:
705 # Start color tag
706 current_color = color_name
707 else:
708 # Regular text, render with current color
709 if part and x < width:
710 try:
711 if current_color and current_color in self.color_map:
712 color_pair = self.color_map[current_color]
713 self.stdscr.addstr(y, x, part[:width-x], curses.color_pair(color_pair))
714 else:
715 self.stdscr.addstr(y, x, part[:width-x])
716 x += len(part)
717 except curses.error:
718 pass
719 return
720
721 # Detect heading lines (starting with #)
722 if line.startswith('#'):
723 try:
724 self.stdscr.addstr(y, x, line, curses.color_pair(4) | curses.A_BOLD)
725 except curses.error:
726 pass
727 return
728
729 # Detect links [text](url)
730 if '[' in line and '](' in line:
731 try:
732 self.stdscr.addstr(y, x, line, curses.color_pair(2))
733 except curses.error:
734 pass
735 return
736
737 # Detect bold text **text**
738 if '**' in line:
739 try:
740 self.stdscr.addstr(y, x, line, curses.color_pair(5) | curses.A_BOLD)
741 except curses.error:
742 pass
743 return
744
745 # Detect emphasis _text_
746 if line.startswith('_') or '_' in line:
747 try:
748 self.stdscr.addstr(y, x, line, curses.color_pair(7))
749 except curses.error:
750 pass
751 return
752
753 # Detect separator lines (===, ---, etc)
754 if line.strip() and all(c in '=-_*' for c in line.strip()):
755 try:
756 self.stdscr.addstr(y, x, line, curses.color_pair(3))
757 except curses.error:
758 pass
759 return
760
761 # Regular text
762 try:
763 self.stdscr.addstr(y, x, line)
764 except curses.error:
765 pass
766
767 def render(self):
768 """Render the current page"""
769 self.stdscr.clear()
770 height, width = self.stdscr.getmaxyx()
771
772 # Draw status bar at top
773 status = f" DBBasic TextBrowser | {self.current_url or 'No page loaded'} "
774 self.stdscr.addstr(0, 0, status[:width], curses.color_pair(1) | curses.A_BOLD)
775
776 # Draw help bar at bottom
777 link_hint = " | 0-9/G: Links" if self.links else ""
778 form_hint = " | F: Form" if self.forms else ""
779 help_text = f" Ctrl-K: URL/AI{link_hint}{form_hint} | H: Help | Q: Quit "
780 self.stdscr.addstr(height - 1, 0, help_text[:width], curses.color_pair(3))
781
782 # Render page content with formatting
783 content_height = height - 2 # Minus status and help bars
784 visible_lines = self.page_content[self.scroll_offset:self.scroll_offset + content_height]
785
786 for i, line in enumerate(visible_lines):
787 self.render_line_with_formatting(i + 1, line, width)
788
789 # Show scroll indicator if needed
790 if len(self.page_content) > content_height:
791 scroll_pct = int((self.scroll_offset / len(self.page_content)) * 100)
792 indicator = f" [{scroll_pct}%] "
793 self.stdscr.addstr(height - 1, width - len(indicator) - 1, indicator, curses.color_pair(2))
794
795 self.stdscr.refresh()
796
797 def handle_input(self, key: int):
798 """Handle keyboard input"""
799 height, width = self.stdscr.getmaxyx()
800 content_height = height - 2
801 max_scroll = max(0, len(self.page_content) - content_height)
802
803 # Ctrl-K: Show command box
804 if key == 11: # Ctrl-K
805 command = self.show_command_box()
806 if command:
807 # Detect if input is a URL or AI command
808 if self.is_url(command):
809 self.fetch_page(command)
810 else:
811 # It's an AI command
812 self.process_ai_command(command)
813
814 # F: Fill form
815 elif key in (ord('f'), ord('F')):
816 self.fill_form()
817
818 # H: Show help
819 elif key in (ord('h'), ord('H')):
820 self.show_help()
821
822 # G: Go to link by number
823 elif key in (ord('g'), ord('G')):
824 self.goto_link()
825
826 # Number keys 0-9: Quick link access
827 elif ord('0') <= key <= ord('9'):
828 link_num = key - ord('0')
829 if link_num < len(self.links):
830 self.fetch_page(self.links[link_num]['url'])
831
832 # Q: Quit
833 elif key in (ord('q'), ord('Q')):
834 self.running = False
835
836 # Up arrow: Scroll up
837 elif key == curses.KEY_UP:
838 self.scroll_offset = max(0, self.scroll_offset - 1)
839
840 # Down arrow: Scroll down
841 elif key == curses.KEY_DOWN:
842 self.scroll_offset = min(max_scroll, self.scroll_offset + 1)
843
844 # Page Up
845 elif key == curses.KEY_PPAGE:
846 self.scroll_offset = max(0, self.scroll_offset - content_height)
847
848 # Page Down
849 elif key == curses.KEY_NPAGE:
850 self.scroll_offset = min(max_scroll, self.scroll_offset + content_height)
851
852 # Home
853 elif key == curses.KEY_HOME:
854 self.scroll_offset = 0
855
856 # End
857 elif key == curses.KEY_END:
858 self.scroll_offset = max_scroll
859
860 def run(self):
861 """Main browser loop"""
862 # Load homepage
863 import os
864 homepage_path = os.path.join(os.path.dirname(__file__), 'homepage.html')
865
866 if os.path.exists(homepage_path):
867 # Load local homepage
868 with open(homepage_path, 'r') as f:
869 homepage_html = f.read()
870
871 from bs4 import BeautifulSoup
872 soup = BeautifulSoup(homepage_html, 'html.parser')
873 self.current_soup = soup
874 self.current_url = f"file://{homepage_path}"
875
876 # Extract links
877 self.links = []
878 for link in soup.find_all('a', href=True):
879 href = link.get('href')
880 if href:
881 link_text = link.get_text(strip=True)
882 if link_text:
883 self.links.append({
884 'url': href,
885 'text': link_text[:50]
886 })
887
888 # Number the links
889 link_counter = 0
890 for link in soup.find_all('a', href=True):
891 href = link.get('href')
892 if href:
893 link_text = link.get_text(strip=True)
894 if link_text and link_counter < len(self.links):
895 link.string = f"[{link_counter}] {link_text}"
896 link_counter += 1
897
898 # Get terminal width
899 try:
900 height, width = self.stdscr.getmaxyx()
901 wrap_width = width - 4
902 except:
903 wrap_width = 78
904
905 # Convert to text
906 import html2text
907 h = html2text.HTML2Text()
908 h.ignore_links = True
909 h.ignore_images = True
910 h.body_width = wrap_width
911 h.unicode_snob = True
912 text = h.handle(str(soup))
913
914 self.page_content = text.split('\n')
915 self.page_text = text
916 else:
917 # Fallback to welcome message
918 ai_status = "ENABLED" if self.ai_enabled else "DISABLED (set OPENAI_API_KEY to enable)"
919 self.page_content = [
920 "Welcome to DBBasic TextBrowser!",
921 "",
922 "A text-mode web browser with AI assistance.",
923 f"AI Features: {ai_status}",
924 "",
925 "Press Ctrl-K to enter a URL or AI command.",
926 "",
927 "Controls:",
928 " Ctrl-K - Open address/AI command box",
929 " 0-9/G - Follow numbered links",
930 " F - Fill forms",
931 " ↑ ↓ - Scroll up/down",
932 " PgUp/PgDn - Scroll page up/down",
933 " Home/End - Jump to top/bottom",
934 " Q - Quit",
935 ]
936
937 while self.running:
938 self.render()
939 key = self.stdscr.getch()
940 self.handle_input(key)
941
942
943def main(stdscr):
944 browser = Browser(stdscr)
945 browser.run()
946
947
948def cli():
949 """Entry point for console script."""
950 curses.wrapper(main)
951
952
953if __name__ == '__main__':
954 cli()