-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_visualizer.py
More file actions
216 lines (171 loc) · 6.8 KB
/
message_visualizer.py
File metadata and controls
216 lines (171 loc) · 6.8 KB
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
208
209
210
211
212
213
214
215
216
"""
Message Visualizer module for LOLANG communication system.
Provides formatted output for encrypted and decrypted messages.
"""
import logging
from terminal_colors import TerminalColors
from typing import Optional
logger = logging.getLogger(__name__)
class MessageVisualizer:
"""
A module for visualizing LOLANG messages and their decrypted versions.
Provides formatted output for better readability.
"""
def __init__(self, separator_width: int = 80):
"""
Initialize the message visualizer.
Args:
separator_width: Width of separator lines
"""
self.logger = logger
self.separator_width = separator_width
def visualize_message(self, role: str, encrypted_message: str,
decrypted_message: Optional[str] = None,
show_encrypted: bool = True,
show_decrypted: bool = True) -> str:
"""
Visualize a message with its encrypted and optionally decrypted forms.
Args:
role: The role of the message sender (e.g., "Server-Agent", "Client-Agent")
encrypted_message: The original LOLANG encrypted message
decrypted_message: The decrypted human-readable message
show_encrypted: Whether to show encrypted version
show_decrypted: Whether to show decrypted version
Returns:
The formatted message for display
"""
parts = []
# Format the encrypted message
if show_encrypted:
encrypted_color = TerminalColors.get_role_color(role)
formatted_encrypted = TerminalColors.colorize(
f"[ENCRYPTED] {role}: {encrypted_message}",
encrypted_color
)
parts.append(formatted_encrypted)
# If decrypted message is provided, format it
if decrypted_message and show_decrypted:
formatted_decrypted = TerminalColors.colorize(
f"[DECRYPTED] {role}: {decrypted_message}",
TerminalColors.YELLOW
)
parts.append(formatted_decrypted)
return "\n".join(parts)
def visualize_client_message(self, encrypted_message: str,
decrypted_message: Optional[str] = None,
**kwargs) -> str:
"""
Visualize a client message.
Args:
encrypted_message: The original LOLANG encrypted message
decrypted_message: The decrypted human-readable message
**kwargs: Additional arguments passed to visualize_message
Returns:
The formatted client message for display
"""
return self.visualize_message("Client-Agent", encrypted_message,
decrypted_message, **kwargs)
def visualize_server_message(self, encrypted_message: str,
decrypted_message: Optional[str] = None,
**kwargs) -> str:
"""
Visualize a server message.
Args:
encrypted_message: The original LOLANG encrypted message
decrypted_message: The decrypted human-readable message
**kwargs: Additional arguments passed to visualize_message
Returns:
The formatted server message for display
"""
return self.visualize_message("Server-Agent", encrypted_message,
decrypted_message, **kwargs)
def visualize_system_message(self, message: str) -> str:
"""
Visualize a system message.
Args:
message: The system message to display
Returns:
The formatted system message for display
"""
return TerminalColors.colorize(f"System: {message}", TerminalColors.HEADER)
def visualize_error_message(self, message: str) -> str:
"""
Visualize an error message.
Args:
message: The error message to display
Returns:
The formatted error message for display
"""
return TerminalColors.colorize(f"Error: {message}", TerminalColors.RED)
def visualize_warning_message(self, message: str) -> str:
"""
Visualize a warning message.
Args:
message: The warning message to display
Returns:
The formatted warning message for display
"""
return TerminalColors.colorize(f"Warning: {message}", TerminalColors.YELLOW)
def visualize_success_message(self, message: str) -> str:
"""
Visualize a success message.
Args:
message: The success message to display
Returns:
The formatted success message for display
"""
return TerminalColors.colorize(f"Success: {message}", TerminalColors.GREEN)
def visualize_info_message(self, message: str) -> str:
"""
Visualize an info message.
Args:
message: The info message to display
Returns:
The formatted info message for display
"""
return TerminalColors.colorize(f"Info: {message}", TerminalColors.CYAN)
def visualize_header(self, text: str) -> str:
"""
Visualize a header.
Args:
text: The header text
Returns:
The formatted header
"""
return TerminalColors.format_header(text)
def visualize_separator(self) -> str:
"""
Create a separator line.
Returns:
Formatted separator
"""
return TerminalColors.format_separator('-', self.separator_width)
def visualize_conversation(self, messages: list) -> str:
"""
Visualize a complete conversation with multiple messages.
Args:
messages: List of message dicts with keys: 'role', 'encrypted', 'decrypted' (optional)
Returns:
Formatted conversation string
"""
parts = []
parts.append(self.visualize_header("Conversation"))
parts.append(self.visualize_separator())
for msg in messages:
role = msg.get('role', 'Unknown')
encrypted = msg.get('encrypted', '')
decrypted = msg.get('decrypted')
parts.append(self.visualize_message(role, encrypted, decrypted))
parts.append(self.visualize_separator())
return "\n".join(parts)
def print_message(self, message: str, clear_screen: bool = False):
"""
Print a formatted message to console.
Args:
message: Message to print
clear_screen: Whether to clear screen before printing
"""
if clear_screen:
import os
os.system('cls' if os.name == 'nt' else 'clear')
print(message)