| """Terminal Layout Builder""" |
|
|
| from typing import Optional, List, Dict |
| from dataclasses import dataclass |
|
|
|
|
| @dataclass |
| class BoxStyle: |
| """Box drawing characters""" |
|
|
| TOP_LEFT = "┌" |
| TOP_RIGHT = "┐" |
| BOTTOM_LEFT = "└" |
| BOTTOM_RIGHT = "┘" |
| HORIZONTAL = "─" |
| VERTICAL = "│" |
|
|
|
|
| class TerminalBox: |
| """Draw boxes in terminal""" |
|
|
| def __init__( |
| self, |
| width: int = 60, |
| height: int = 10, |
| title: Optional[str] = None, |
| style: Optional[BoxStyle] = None, |
| ): |
| self.width = width |
| self.height = height |
| self.title = title |
| self.style = style or BoxStyle() |
| self.content: List[str] = [] |
|
|
| def set_content(self, lines: List[str]): |
| """Set box content""" |
| self.content = lines[: self.height - 2] |
|
|
| def render(self) -> str: |
| """Render the box""" |
| result = [] |
|
|
| top_line = ( |
| f"{self.style.TOP_LEFT}" |
| + f"{self.style.HORIZONTAL * (self.width - 2)}" |
| + f"{self.style.TOP_RIGHT}" |
| ) |
| result.append(top_line) |
|
|
| if self.title: |
| title_line = f"{self.style.VERTICAL} {self.title.center(self.width - 4)} {self.style.VERTICAL}" |
| result.append(title_line) |
| result.append( |
| f"{self.style.VERTICAL} {'─' * (self.width - 4)} {self.style.VERTICAL}" |
| ) |
|
|
| for line in self.content: |
| padding = self.width - len(line) - 4 |
| result.append( |
| f"{self.style.VERTICAL} {line}{' ' * max(0, padding)} {self.style.VERTICAL}" |
| ) |
|
|
| for _ in range(max(0, self.height - len(self.content) - (3 if self.title else 2))): |
| result.append( |
| f"{self.style.VERTICAL}{' ' * (self.width - 2)}{self.style.VERTICAL}" |
| ) |
|
|
| bottom_line = ( |
| f"{self.style.BOTTOM_LEFT}" |
| + f"{self.style.HORIZONTAL * (self.width - 2)}" |
| + f"{self.style.BOTTOM_RIGHT}" |
| ) |
| result.append(bottom_line) |
|
|
| return "\n".join(result) |
|
|
|
|
| class ChatboxLayout: |
| """Layout for chat interface""" |
|
|
| def __init__(self, width: int = 80): |
| self.width = width |
| self.max_history = 50 |
| self.conversation: List[Dict[str, str]] = [] |
|
|
| def add_user_message(self, message: str): |
| """Add user message to conversation""" |
| self.conversation.append({"role": "user", "content": message}) |
| if len(self.conversation) > self.max_history: |
| self.conversation = self.conversation[-self.max_history :] |
|
|
| def add_agent_message(self, message: str): |
| """Add agent message to conversation""" |
| self.conversation.append({"role": "agent", "content": message}) |
| if len(self.conversation) > self.max_history: |
| self.conversation = self.conversation[-self.max_history :] |
|
|
| def render(self) -> str: |
| """Render the chat layout""" |
| lines = [] |
|
|
| lines.append("=" * self.width) |
| lines.append(" Burme-Coder-Max Chat ".center(self.width, "=")) |
| lines.append("=" * self.width) |
|
|
| for msg in self.conversation[-10:]: |
| role = msg["role"].upper() |
| content = msg["content"] |
|
|
| if msg["role"] == "user": |
| lines.append(f"\n[{role}]:") |
| else: |
| lines.append(f"\n[{role}]:") |
| if len(content) > self.width - 4: |
| content = content[: self.width - 7] + "..." |
|
|
| lines.append(content) |
|
|
| lines.append("=" * self.width) |
|
|
| return "\n".join(lines) |
|
|
|
|
| class TableLayout: |
| """Table layout for displaying data""" |
|
|
| def __init__(self, columns: List[str], widths: Optional[List[int]] = None): |
| self.columns = columns |
| self.total_width = widths |
| self.rows: List[List[str]] = [] |
|
|
| if widths: |
| self.widths = widths |
| else: |
| avail_width = 80 |
| col_width = avail_width // len(columns) |
| self.widths = [col_width] * len(columns) |
|
|
| def add_row(self, values: List[str]): |
| """Add a row of data""" |
| self.rows.append(values) |
|
|
| def render(self) -> str: |
| """Render the table""" |
| lines = [] |
|
|
| header_line = "│".join( |
| f" {col.ljust(w)} " for col, w in zip(self.columns, self.widths) |
| ) |
| separator = "┼".join("─" * (w + 2) for w in self.widths) |
|
|
| lines.append(header_line) |
| lines.append(separator) |
|
|
| for row in self.rows: |
| row_line = "│".join( |
| f" {val.ljust(w)} " for val, w in zip(row, self.widths) |
| ) |
| lines.append(row_line) |
|
|
| return "\n".join(lines) |
|
|
|
|
| class Panel: |
| """Simple panel for grouping content""" |
|
|
| def __init__( |
| self, |
| content: str, |
| title: Optional[str] = None, |
| width: int = 60, |
| border: str = "─", |
| ): |
| self.content = content |
| self.title = title |
| self.width = width |
| self.border = border |
|
|
| def render(self) -> str: |
| """Render the panel""" |
| lines = [self.border * self.width] |
|
|
| if self.title: |
| title_str = f" {self.title} " |
| padding = self.width - len(title_str) |
| lines.append(title_str + self.border * padding) |
| lines.append(self.border * self.width) |
|
|
| for line in self.content.split("\n"): |
| padding = self.width - len(line) - 2 |
| lines.append(f" {line}{' ' * max(0, padding)} ") |
|
|
| lines.append(self.border * self.width) |
|
|
| return "\n".join(lines) |
|
|
|
|
| def divider(char: str = "─", width: int = 80) -> str: |
| """Create a horizontal divider""" |
| return char * width |
|
|
|
|
| def spacer(lines: int = 1): |
| """Print blank lines""" |
| for _ in range(lines): |
| print() |
|
|