Skip to content

Commit 2df0801

Browse files
committed
Improve and refactor "sbase print FILE"
1 parent 92e49fe commit 2df0801

File tree

3 files changed

+85
-37
lines changed

3 files changed

+85
-37
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from rich.console import Console
2+
from rich.markdown import Markdown
3+
from rich.syntax import Syntax
4+
5+
6+
def process_syntax(code, lang, theme, line_numbers, code_width, word_wrap):
7+
syntax = Syntax(
8+
code,
9+
lang,
10+
theme=theme,
11+
line_numbers=line_numbers,
12+
code_width=code_width,
13+
word_wrap=word_wrap,
14+
)
15+
return syntax
16+
17+
18+
def display_markdown(code):
19+
try:
20+
markdown = Markdown(code)
21+
console = Console()
22+
console.print(markdown) # noqa
23+
return True # Success
24+
except Exception:
25+
return False # Failure
26+
27+
28+
def display_code(code):
29+
try:
30+
console = Console()
31+
console.print(code) # noqa
32+
return True # Success
33+
except Exception:
34+
return False # Failure
35+
36+
37+
def fix_emoji_spacing(code):
38+
try:
39+
# Fix the display width of certain emojis that take up two spaces
40+
double_width_emojis = ["🗺️", "🖼️", "🗄️", "⏺️", "♻️", "🗂️", "🖥️"]
41+
for emoji in double_width_emojis:
42+
if emoji in code:
43+
code = code.replace(emoji, emoji + " ")
44+
except Exception:
45+
pass
46+
return code

seleniumbase/console_scripts/run.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -760,14 +760,6 @@ def main():
760760
show_convert_usage()
761761
elif command == "print":
762762
if len(command_args) >= 1:
763-
if sys.version_info[0] == 2:
764-
c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
765-
cr = colorama.Style.RESET_ALL
766-
msg = '"sbase print" does NOT support Python 2! '
767-
msg += 'Try using the Unix "cat" command instead!'
768-
message = "\n" + c5 + msg + cr + "\n"
769-
print("")
770-
raise Exception(message)
771763
from seleniumbase.console_scripts import sb_print
772764

773765
sb_print.main()

seleniumbase/console_scripts/sb_print.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ def main():
114114
if help_me:
115115
invalid_run_command(invalid_cmd)
116116

117-
with open(file_to_print, "r", encoding="utf-8") as f:
118-
all_code = f.read()
117+
all_code = None
118+
if sys.version_info[0] >= 3:
119+
with open(file_to_print, "r", encoding="utf-8") as f:
120+
all_code = f.read()
121+
else:
122+
with open(file_to_print, "r") as f:
123+
all_code = f.read()
119124
all_code = all_code.replace("\t", " ")
120125
code_lines = all_code.split("\n")
121126

122127
console_width = None # width of console output when running script
123128
used_width = None # code_width and few spaces on right for padding
124-
magic_console = None
125-
magic_syntax = None
129+
magic_syntax = None # the syntax generated by rich.syntax.Syntax()
126130
try:
127131
console_width = os.popen("stty size", "r").read().split()[1]
128132
if console_width:
@@ -135,8 +139,7 @@ def main():
135139
use_rich = True
136140

137141
if use_rich:
138-
from rich.console import Console
139-
from rich.syntax import Syntax
142+
from seleniumbase.console_scripts import rich_helper
140143

141144
the_code = "\n".join(code_lines)
142145
code_width = 1
@@ -566,22 +569,16 @@ def main():
566569
if console_width and (code_width + extra_r_spaces < console_width):
567570
used_width = code_width + extra_r_spaces
568571

569-
try:
570-
if "🗺️" in the_code:
571-
# Fix width of an emoji
572-
the_code = the_code.replace("🗺️", "🗺️ ")
573-
except Exception:
574-
pass
572+
the_code = rich_helper.fix_emoji_spacing(the_code)
575573

576-
magic_syntax = Syntax(
574+
magic_syntax = rich_helper.process_syntax(
577575
the_code,
578576
code_lang,
579577
theme="monokai",
580578
line_numbers=line_numbers,
581579
code_width=used_width,
582580
word_wrap=word_wrap,
583581
)
584-
magic_console = Console()
585582
# ----------------------------------------
586583
dash_length = 62 # May change
587584
if used_width and used_width + w < console_width:
@@ -591,22 +588,35 @@ def main():
591588
dashes = "-" * dash_length
592589
print(dashes)
593590
print_success = False
594-
if use_rich and code_lang == "markdown":
595-
try:
596-
from rich.markdown import Markdown
597-
598-
markdown = Markdown(all_code)
599-
markdown_console = Console()
600-
markdown_console.print(markdown) # noqa
601-
print_success = True
602-
except Exception:
603-
pass
591+
if use_rich and code_lang == "markdown" and not line_numbers:
592+
all_code = rich_helper.fix_emoji_spacing(all_code)
593+
all_code = all_code.replace("<br />", "\n")
594+
all_code = all_code.replace(
595+
'<p align="center">', '\n')
596+
all_code = all_code.replace(
597+
'<p align="left">', '\n')
598+
all_code = all_code.replace(
599+
'<p align="right">', '\n')
600+
all_code = all_code.replace("<p>", "\n")
601+
all_code = all_code.replace("</p>", "\n")
602+
if "<b>*" not in all_code and "*<b>" not in all_code:
603+
if "</b>*" not in all_code and "*</b>" not in all_code:
604+
all_code = all_code.replace("<b>", "**")
605+
all_code = all_code.replace("</b>", "**")
606+
if "<code>`" not in all_code and "`<code>" not in all_code:
607+
if "</code>`" not in all_code and "`</code>" not in all_code:
608+
all_code = all_code.replace("<code>", "``")
609+
all_code = all_code.replace("</code>", "``")
610+
# Display ALL <h> tags as an <h1> because the font size is fixed
611+
all_code = all_code.replace("\n<h1>", "\n# ").replace("</h1>", "")
612+
all_code = all_code.replace("\n<h2>", "\n# ").replace("</h2>", "")
613+
all_code = all_code.replace("\n<h3>", "\n# ").replace("</h3>", "")
614+
all_code = all_code.replace("\n<h4>", "\n# ").replace("</h4>", "")
615+
print_success = rich_helper.display_markdown(all_code)
616+
if all_code.endswith("\n"):
617+
print() # Because "rich" skips the last line if new-line
604618
elif use_rich and magic_syntax:
605-
try:
606-
magic_console.print(magic_syntax) # noqa
607-
print_success = True
608-
except Exception:
609-
pass
619+
print_success = rich_helper.display_code(magic_syntax)
610620
if not use_rich or not magic_syntax or not print_success:
611621
for line in code_lines:
612622
print(line)

0 commit comments

Comments
 (0)