Skip to content

Commit 8abf42b

Browse files
author
fz
committed
Merge branch 'main' into feature/beta-2.0-release
2 parents 2a20ebe + d860ced commit 8abf42b

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

common/util.py

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Utility functions."""
22

3+
from argparse import ArgumentTypeError
34
import os
5+
import re
46
import shutil
5-
from argparse import ArgumentTypeError
6-
from typing import List, Tuple
7+
from typing import Any, List, Tuple
78

89
from lldb import (
910
SBAddress,
@@ -57,6 +58,74 @@ def get_frame_range(frame: SBFrame, target: SBTarget) -> Tuple[str, str]:
5758

5859
return start_address, end_address
5960

61+
def clear_page() -> None:
62+
"""
63+
Used to clear the previously printed breakpoint information before
64+
printing the next information.
65+
"""
66+
num_lines = shutil.get_terminal_size().lines
67+
for _ in range(num_lines):
68+
print()
69+
print("\033[0;0H") # Ansi escape code: Set cursor to 0,0 position
70+
print("\033[J") # Ansi escape code: Clear contents from cursor to end of screen
71+
72+
73+
def print_line_with_string(
74+
string: str,
75+
char: GLYPHS = GLYPHS.HORIZONTAL_LINE,
76+
line_color: TERM_COLORS = TERM_COLORS.GREY,
77+
string_color: TERM_COLORS = TERM_COLORS.BLUE,
78+
align: ALIGN = ALIGN.RIGHT,
79+
) -> None:
80+
"""Print a line with the provided @string padded with @char"""
81+
width = shutil.get_terminal_size().columns
82+
if align == ALIGN.RIGHT:
83+
l_pad = (width - len(string) - 6) * char.value
84+
r_pad = 4 * char.value
85+
86+
elif align == ALIGN.CENTRE:
87+
l_pad = (width - len(string)) * char.value
88+
r_pad = 4 * char.value
89+
90+
elif align == ALIGN.LEFT:
91+
l_pad = 4 * char.value
92+
r_pad = (width - len(string) - 6) * char.value
93+
94+
output_line(
95+
f"{line_color.value}{l_pad}{TERM_COLORS.ENDC.value} "
96+
+ f"{string_color.value}{string}{TERM_COLORS.ENDC.value} {line_color.value}{r_pad}{TERM_COLORS.ENDC.value}"
97+
)
98+
99+
100+
def print_line(
101+
char: GLYPHS = GLYPHS.HORIZONTAL_LINE, color: TERM_COLORS = TERM_COLORS.GREY
102+
) -> None:
103+
"""Print a line of @char"""
104+
output_line(
105+
f"{color.value}{shutil.get_terminal_size().columns*char.value}{TERM_COLORS.ENDC.value}"
106+
)
107+
108+
109+
def print_message(msg_type: MSG_TYPE, message: str) -> None:
110+
"""Format and print a @message"""
111+
info_color = TERM_COLORS.BLUE
112+
success_color = TERM_COLORS.GREEN
113+
error_color = TERM_COLORS.GREEN
114+
115+
if msg_type == MSG_TYPE.INFO:
116+
output_line(f"{info_color.value}[+]{TERM_COLORS.ENDC.value} {message}")
117+
elif msg_type == MSG_TYPE.SUCCESS:
118+
output_line(f"{success_color.value}[+]{TERM_COLORS.ENDC.value} {message}")
119+
elif msg_type == MSG_TYPE.ERROR:
120+
output_line(f"{error_color.value}[+]{TERM_COLORS.ENDC.value} {message}")
121+
122+
123+
def print_instruction(line: str, color: TERM_COLORS = TERM_COLORS.ENDC) -> None:
124+
"""Format and print a line of disassembly returned from LLDB (SBFrame.disassembly)"""
125+
loc_0x = line.find("0x")
126+
start_idx = loc_0x if loc_0x >= 0 else 0
127+
output_line(f"{color.value}{line[start_idx:]}{TERM_COLORS.ENDC.value}")
128+
60129

61130
def get_registers(frame: SBFrame, frame_type: str) -> List[SBValue]:
62131
"""

0 commit comments

Comments
 (0)