File size: 5,832 Bytes
a7d7463 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | """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()
|